drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
table_proto.cc
1/* Copyright (C) 2000-2006 MySQL AB
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include <config.h>
17#include <drizzled/error.h>
18#include <drizzled/session.h>
19#include <drizzled/sql_table.h>
21
22#include <drizzled/plugin/storage_engine.h>
23
24#include <drizzled/internal/my_sys.h>
25#include <drizzled/typelib.h>
26#include <drizzled/util/test.h>
27#include <drizzled/key_part_info.h>
28#include <drizzled/key_info.h>
29
30/* For proto */
31#include <string>
32#include <fstream>
33#include <fcntl.h>
34#include <drizzled/message/schema.h>
35#include <drizzled/message/table.h>
36#include <google/protobuf/io/zero_copy_stream.h>
37#include <google/protobuf/io/zero_copy_stream_impl.h>
38#include <google/protobuf/message.h>
39
40#include <drizzled/table_proto.h>
41#include <drizzled/charset.h>
42#include <drizzled/create_field.h>
43#include <drizzled/function/time/typecast.h>
44
45using namespace std;
46
47namespace drizzled {
48
49static
50bool fill_table_proto(const identifier::Table& identifier,
51 message::Table &table_proto,
52 List<CreateField> &create_fields,
53 HA_CREATE_INFO *create_info,
54 uint32_t keys,
55 KeyInfo *key_info)
56{
57 CreateField *field_arg;
58 List<CreateField>::iterator it(create_fields.begin());
59 message::Table::TableOptions *table_options= table_proto.mutable_options();
60
61 if (create_fields.size() > MAX_FIELDS)
62 {
63 my_error(ER_TOO_MANY_FIELDS, MYF(0), ER(ER_TOO_MANY_FIELDS));
64 return true;
65 }
66
67 assert(strcmp(table_proto.engine().name().c_str(),
68 create_info->db_type->getName().c_str())==0);
69
70 message::schema::shared_ptr schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
71
72 if (schema_message and not message::is_replicated(*schema_message))
73 {
74 message::set_is_replicated(table_proto, false);
75 }
76
77 int field_number= 0;
78 bool use_existing_fields= table_proto.field_size() > 0;
79 while ((field_arg= it++))
80 {
81 message::Table::Field *attribute;
82
83 /* some (one) code path for CREATE TABLE fills the proto
84 out more than the others, so we already have partially
85 filled out Field messages */
86
87 if (use_existing_fields)
88 {
89 attribute= table_proto.mutable_field(field_number++);
90 }
91 else
92 {
93 /* Other code paths still have to fill out the proto */
94 attribute= table_proto.add_field();
95
96 if (field_arg->flags & NOT_NULL_FLAG)
97 {
98 attribute->mutable_constraints()->set_is_notnull(true);
99 }
100
101 if (field_arg->flags & UNSIGNED_FLAG and
102 (field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
103 {
104 field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
105 attribute->mutable_constraints()->set_is_unsigned(true);
106 }
107
108 attribute->set_name(field_arg->field_name);
109 }
110
111 assert(((field_arg->flags & NOT_NULL_FLAG)) == attribute->constraints().is_notnull());
112 assert(strcmp(attribute->name().c_str(), field_arg->field_name)==0);
113
114
115 message::Table::Field::FieldType parser_type= attribute->type();
116
117 if (field_arg->sql_type == DRIZZLE_TYPE_NULL)
118 {
119 my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), table_proto.name().c_str(), -1);
120 return true;
121 }
122
123 if (field_arg->flags & UNSIGNED_FLAG and
124 (field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
125 {
126 message::Table::Field::FieldConstraints *constraints= attribute->mutable_constraints();
127
128 field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
129 constraints->set_is_unsigned(true);
130 }
131
132 attribute->set_type(message::internalFieldTypeToFieldProtoType(field_arg->sql_type));
133
134 switch (attribute->type()) {
135 case message::Table::Field::BIGINT:
136 case message::Table::Field::INTEGER:
137 case message::Table::Field::DATE:
138 case message::Table::Field::DATETIME:
139 case message::Table::Field::UUID:
140 case message::Table::Field::IPV6:
141 case message::Table::Field::TIME:
142 case message::Table::Field::BOOLEAN:
143 break;
144 case message::Table::Field::DOUBLE:
145 {
146 /*
147 * For DOUBLE, we only add a specific scale and precision iff
148 * the fixed decimal point has been specified...
149 */
150 if (field_arg->decimals != NOT_FIXED_DEC)
151 {
152 message::Table::Field::NumericFieldOptions *numeric_field_options;
153
154 numeric_field_options= attribute->mutable_numeric_options();
155
156 numeric_field_options->set_precision(field_arg->length);
157 numeric_field_options->set_scale(field_arg->decimals);
158 }
159 }
160 break;
161 case message::Table::Field::VARCHAR:
162 {
163 message::Table::Field::StringFieldOptions *string_field_options;
164
165 string_field_options= attribute->mutable_string_options();
166
167 if (! use_existing_fields || string_field_options->length()==0)
168 string_field_options->set_length(field_arg->length
169 / field_arg->charset->mbmaxlen);
170 else
171 assert((uint32_t)string_field_options->length() == (uint32_t)(field_arg->length / field_arg->charset->mbmaxlen));
172
173 if (! string_field_options->has_collation())
174 {
175 string_field_options->set_collation_id(field_arg->charset->number);
176 string_field_options->set_collation(field_arg->charset->name);
177 }
178 break;
179 }
180 case message::Table::Field::DECIMAL:
181 {
182 message::Table::Field::NumericFieldOptions *numeric_field_options;
183
184 numeric_field_options= attribute->mutable_numeric_options();
185 /* This is magic, I hate magic numbers -Brian */
186 numeric_field_options->set_precision(field_arg->length + ( field_arg->decimals ? -2 : -1));
187 numeric_field_options->set_scale(field_arg->decimals);
188 break;
189 }
190 case message::Table::Field::ENUM:
191 {
192 message::Table::Field::EnumerationValues *enumeration_options;
193
194 assert(field_arg->interval);
195
196 enumeration_options= attribute->mutable_enumeration_values();
197
198 for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
199 {
200 const char *src= field_arg->interval->type_names[pos];
201
202 enumeration_options->add_field_value(src);
203 }
204 enumeration_options->set_collation_id(field_arg->charset->number);
205 enumeration_options->set_collation(field_arg->charset->name);
206 break;
207 }
208
209 case message::Table::Field::BLOB:
210 {
211 message::Table::Field::StringFieldOptions *string_field_options;
212
213 string_field_options= attribute->mutable_string_options();
214 string_field_options->set_collation_id(field_arg->charset->number);
215 string_field_options->set_collation(field_arg->charset->name);
216 }
217
218 break;
219
220 case message::Table::Field::EPOCH:
221 {
222 if (field_arg->sql_type == DRIZZLE_TYPE_MICROTIME)
223 attribute->mutable_time_options()->set_microseconds(true);
224 }
225
226 break;
227 }
228
229 assert (!use_existing_fields || parser_type == attribute->type());
230
231#ifdef NOTDONE
232 field_constraints= attribute->mutable_constraints();
233 constraints->set_is_nullable(field_arg->def->null_value);
234#endif
235
236 if (not field_arg->comment.empty())
237 {
238 uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
239 field_arg->comment.begin(),
240 field_arg->comment.end(),
241 COLUMN_COMMENT_MAXLEN);
242
243 if (tmp_len < field_arg->comment.size())
244 {
245 my_error(ER_WRONG_STRING_LENGTH, MYF(0), field_arg->comment.data(), "COLUMN COMMENT", (uint32_t) COLUMN_COMMENT_MAXLEN);
246 return true;
247 }
248
249 if (not use_existing_fields)
250 attribute->set_comment(field_arg->comment.data());
251
252 assert(strcmp(attribute->comment().c_str(), field_arg->comment.data())==0);
253 }
254
255 if (field_arg->unireg_check == Field::NEXT_NUMBER)
256 {
257 message::Table::Field::NumericFieldOptions *field_options;
258 field_options= attribute->mutable_numeric_options();
259 field_options->set_is_autoincrement(true);
260 }
261
262 if (field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
263 || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
264 {
265 message::Table::Field::FieldOptions *field_options;
266 field_options= attribute->mutable_options();
267 field_options->set_default_expression("CURRENT_TIMESTAMP");
268 }
269
270 if (field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
271 || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
272 {
273 message::Table::Field::FieldOptions *field_options;
274 field_options= attribute->mutable_options();
275 field_options->set_update_expression("CURRENT_TIMESTAMP");
276 }
277
278 if (field_arg->def == NULL && not attribute->constraints().is_notnull())
279 {
280 message::Table::Field::FieldOptions *field_options;
281 field_options= attribute->mutable_options();
282
283 field_options->set_default_null(true);
284 }
285 if (field_arg->def)
286 {
287 message::Table::Field::FieldOptions *field_options;
288 field_options= attribute->mutable_options();
289
290 if (field_arg->def->is_null())
291 {
292 field_options->set_default_null(true);
293 }
294 else
295 {
296 String d;
297 String *default_value= field_arg->def->val_str(&d);
298
299 assert(default_value);
300
301 if ((field_arg->sql_type==DRIZZLE_TYPE_VARCHAR
302 || field_arg->sql_type==DRIZZLE_TYPE_BLOB)
303 && ((field_arg->length / field_arg->charset->mbmaxlen)
304 < default_value->length()))
305 {
306 my_error(ER_INVALID_DEFAULT, MYF(0), field_arg->field_name);
307 return true;
308 }
309
310 if (field::isDateTime(field_arg->sql_type))
311 {
312 type::Time ltime;
313
314 if (field_arg->def->get_date(ltime, TIME_FUZZY_DATE))
315 {
316 my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR),
317 default_value->c_str());
318 return true;
319 }
320
321 /* We now do the casting down to the appropriate type.
322
323 Yes, this implicit casting is balls.
324 It was previously done on reading the proto back in,
325 but we really shouldn't store the bogus things in the proto,
326 and instead do the casting behaviour here.
327
328 the timestamp errors are taken care of elsewhere.
329 */
330
331 if (field_arg->sql_type == DRIZZLE_TYPE_DATETIME)
332 {
333 Item *typecast= new Item_datetime_typecast(field_arg->def);
334 typecast->quick_fix_field();
335 typecast->val_str(default_value);
336 }
337 else if (field_arg->sql_type == DRIZZLE_TYPE_DATE)
338 {
339 Item *typecast= new Item_date_typecast(field_arg->def);
340 typecast->quick_fix_field();
341 typecast->val_str(default_value);
342 }
343 }
344
345 if ((field_arg->sql_type == DRIZZLE_TYPE_VARCHAR && field_arg->charset == &my_charset_bin)
346 || (field_arg->sql_type == DRIZZLE_TYPE_BLOB && field_arg->charset == &my_charset_bin))
347 {
348 field_options->set_default_bin_value(string(default_value->c_ptr(), default_value->length()));
349 }
350 else
351 {
352 field_options->set_default_value(default_value->c_ptr());
353 }
354 }
355 }
356
357 assert(field_arg->unireg_check == Field::NONE
358 || field_arg->unireg_check == Field::NEXT_NUMBER
359 || field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
360 || field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
361 || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD);
362
363 }
364
365 assert(! use_existing_fields || (field_number == table_proto.field_size()));
366
367 if (create_info->table_options & HA_OPTION_PACK_RECORD)
368 table_options->set_pack_record(true);
369
370 if (table_options->has_comment() && table_options->comment().length() == 0)
371 table_options->clear_comment();
372
373 if (table_options->has_comment())
374 {
375 uint32_t tmp_len;
376 tmp_len= system_charset_info->cset->charpos(system_charset_info,
377 table_options->comment().c_str(),
378 table_options->comment().c_str() +
379 table_options->comment().length(),
380 TABLE_COMMENT_MAXLEN);
381
382 if (tmp_len < table_options->comment().length())
383 {
384 my_error(ER_WRONG_STRING_LENGTH, MYF(0),
385 table_options->comment().c_str(),"Table COMMENT",
386 (uint32_t) TABLE_COMMENT_MAXLEN);
387 return true;
388 }
389 }
390
391 if (create_info->default_table_charset)
392 {
393 table_options->set_collation_id(create_info->default_table_charset->number);
394 table_options->set_collation(create_info->default_table_charset->name);
395 }
396
397 if (create_info->used_fields & HA_CREATE_USED_AUTO)
398 table_options->set_has_user_set_auto_increment_value(true);
399 else
400 table_options->set_has_user_set_auto_increment_value(false);
401
402 if (create_info->auto_increment_value)
403 table_options->set_auto_increment_value(create_info->auto_increment_value);
404
405 for (uint32_t i= 0; i < keys; i++)
406 {
407 message::Table::Index *idx= table_proto.add_indexes();
408
409 assert(test(key_info[i].flags & HA_USES_COMMENT) == (key_info[i].comment.size() > 0));
410
411 idx->set_name(key_info[i].name);
412 idx->set_key_length(key_info[i].key_length);
413 idx->set_is_primary(is_primary_key(key_info[i].name));
414
415 if (key_info[i].algorithm == message::Table::Index::UNKNOWN_INDEX)
416 idx->set_type(create_info->db_type->default_index_type());
417 else
418 idx->set_type(key_info[i].algorithm);
419
420 if (key_info[i].flags & HA_NOSAME)
421 idx->set_is_unique(true);
422 else
423 idx->set_is_unique(false);
424
425 message::Table::Index::Options *index_options= idx->mutable_options();
426
427 if (key_info[i].flags & HA_USES_BLOCK_SIZE)
428 index_options->set_key_block_size(key_info[i].block_size);
429
430 if (key_info[i].flags & HA_PACK_KEY)
431 index_options->set_pack_key(true);
432
433 if (key_info[i].flags & HA_BINARY_PACK_KEY)
434 index_options->set_binary_pack_key(true);
435
436 if (key_info[i].flags & HA_VAR_LENGTH_PART)
437 index_options->set_var_length_key(true);
438
439 if (key_info[i].flags & HA_NULL_PART_KEY)
440 index_options->set_null_part_key(true);
441
442 if (key_info[i].flags & HA_KEY_HAS_PART_KEY_SEG)
443 index_options->set_has_partial_segments(true);
444
445 if (key_info[i].flags & HA_GENERATED_KEY)
446 index_options->set_auto_generated_key(true);
447
448 if (key_info[i].flags & HA_USES_COMMENT)
449 {
450 uint32_t tmp_len;
451 tmp_len= system_charset_info->cset->charpos(system_charset_info,
452 key_info[i].comment.begin(),
453 key_info[i].comment.end(),
454 TABLE_COMMENT_MAXLEN);
455
456 if (tmp_len < key_info[i].comment.size())
457 {
458 my_error(ER_WRONG_STRING_LENGTH, MYF(0),
459 key_info[i].comment.data(), "Index COMMENT",
460 (uint32_t) TABLE_COMMENT_MAXLEN);
461 return true;
462 }
463
464 idx->set_comment(key_info[i].comment.data());
465 }
466 static const uint64_t unknown_index_flag= (HA_NOSAME | HA_PACK_KEY |
467 HA_USES_BLOCK_SIZE |
468 HA_BINARY_PACK_KEY |
469 HA_VAR_LENGTH_PART |
470 HA_NULL_PART_KEY |
471 HA_KEY_HAS_PART_KEY_SEG |
472 HA_GENERATED_KEY |
473 HA_USES_COMMENT);
474 if (key_info[i].flags & ~unknown_index_flag)
475 abort(); // Invalid (unknown) index flag.
476
477 for(unsigned int j=0; j< key_info[i].key_parts; j++)
478 {
479 message::Table::Index::IndexPart *idxpart;
480 const int fieldnr= key_info[i].key_part[j].fieldnr;
481 int mbmaxlen= 1;
482
483 idxpart= idx->add_index_part();
484
485 idxpart->set_fieldnr(fieldnr);
486
487 if (table_proto.field(fieldnr).type() == message::Table::Field::VARCHAR
488 || table_proto.field(fieldnr).type() == message::Table::Field::BLOB)
489 {
490 uint32_t collation_id;
491
492 if (table_proto.field(fieldnr).string_options().has_collation_id())
493 collation_id= table_proto.field(fieldnr).string_options().collation_id();
494 else
495 collation_id= table_proto.options().collation_id();
496
497 const charset_info_st *cs= get_charset(collation_id);
498
499 mbmaxlen= cs->mbmaxlen;
500 }
501
502 idxpart->set_compare_length(key_info[i].key_part[j].length / mbmaxlen);
503 }
504 }
505
506 if (not table_proto.IsInitialized())
507 {
508 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
509 table_proto.name().c_str(),
510 table_proto.InitializationErrorString().c_str());
511
512 return true;
513 }
514
515 /*
516 Here we test to see if we can validate the Table Message before we continue.
517 We do this by serializing the protobuffer.
518 */
519 {
520 string tmp_string;
521
522 try {
523 table_proto.SerializeToString(&tmp_string);
524 }
525
526 catch (...)
527 {
528 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
529 table_proto.name().c_str(),
530 table_proto.InitializationErrorString().c_str());
531
532 return true;
533 }
534 }
535
536 return false;
537}
538
539/*
540 Create a table definition proto file and the tables
541
542 SYNOPSIS
543 rea_create_table()
544 session Thread handler
545 path Name of file (including database, without .frm)
546 db Data base name
547 table_name Table name
548 create_info create info parameters
549 create_fields Fields to create
550 keys number of keys to create
551 key_info Keys to create
552
553 RETURN
554 0 ok
555 1 error
556*/
557
558bool rea_create_table(Session *session,
559 const identifier::Table &identifier,
560 message::Table &table_proto,
561 HA_CREATE_INFO *create_info,
562 List<CreateField> &create_fields,
563 uint32_t keys, KeyInfo *key_info)
564{
565 assert(table_proto.has_name());
566
567 if (fill_table_proto(identifier,
568 table_proto, create_fields, create_info,
569 keys, key_info))
570 {
571 return false;
572 }
573
574 assert(table_proto.name() == identifier.getTableName());
575
576 if (not plugin::StorageEngine::createTable(*session,
577 identifier,
578 table_proto))
579 {
580 return false;
581 }
582
583 return true;
584
585} /* rea_create_table */
586
587} /* namespace drizzled */
588
static bool createTable(Session &session, const identifier::Table &identifier, message::Table &table_message)
TODO: Rename this file - func.h is stupid.
STL namespace.