12
12
You should have received a copy of the GNU General Public License
13
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 */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
16
#include "config.h"
17
17
#include <drizzled/error.h>
35
35
#include <google/protobuf/message.h>
37
37
#include <drizzled/table_proto.h>
38
#include <drizzled/charset.h>
40
#include "drizzled/function/time/typecast.h"
42
39
using namespace std;
44
41
namespace drizzled {
46
43
static int fill_table_proto(message::Table &table_proto,
44
const std::string &table_name,
47
45
List<CreateField> &create_fields,
48
46
HA_CREATE_INFO *create_info,
62
60
assert(strcmp(table_proto.engine().name().c_str(),
63
61
create_info->db_type->getName().c_str())==0);
63
assert(table_proto.name() == table_name);
65
65
int field_number= 0;
66
66
bool use_existing_fields= table_proto.field_size() > 0;
67
67
while ((field_arg= it++))
97
97
message::Table::Field::FieldType parser_type= attribute->type();
99
if (field_arg->sql_type == DRIZZLE_TYPE_NULL)
101
my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), table_proto.name().c_str(), -1);
105
99
attribute->set_type(message::internalFieldTypeToFieldProtoType(field_arg->sql_type));
107
101
switch (attribute->type()) {
226
220
message::Table::Field::FieldOptions *field_options;
227
221
field_options= attribute->mutable_options();
228
field_options->set_default_expression("CURRENT_TIMESTAMP");
222
field_options->set_default_value("NOW()");
231
225
if (field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
234
228
message::Table::Field::FieldOptions *field_options;
235
229
field_options= attribute->mutable_options();
236
field_options->set_update_expression("CURRENT_TIMESTAMP");
230
field_options->set_update_value("NOW()");
239
233
if (field_arg->def == NULL && attribute->constraints().is_nullable())
271
if (field_arg->sql_type == DRIZZLE_TYPE_DATE
272
|| field_arg->sql_type == DRIZZLE_TYPE_DATETIME
273
|| field_arg->sql_type == DRIZZLE_TYPE_TIMESTAMP)
277
if (field_arg->def->get_date(<ime, TIME_FUZZY_DATE))
279
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR),
280
default_value->c_str());
284
/* We now do the casting down to the appropriate type.
286
Yes, this implicit casting is balls.
287
It was previously done on reading the proto back in,
288
but we really shouldn't store the bogus things in the proto,
289
and instead do the casting behaviour here.
291
the timestamp errors are taken care of elsewhere.
294
if (field_arg->sql_type == DRIZZLE_TYPE_DATETIME)
296
Item *typecast= new Item_datetime_typecast(field_arg->def);
297
typecast->quick_fix_field();
298
typecast->val_str(default_value);
300
else if (field_arg->sql_type == DRIZZLE_TYPE_DATE)
302
Item *typecast= new Item_date_typecast(field_arg->def);
303
typecast->quick_fix_field();
304
typecast->val_str(default_value);
308
265
if ((field_arg->sql_type==DRIZZLE_TYPE_VARCHAR
309
266
&& field_arg->charset==&my_charset_bin)
310
267
|| (field_arg->sql_type==DRIZZLE_TYPE_BLOB
335
292
if (create_info->table_options & HA_OPTION_PACK_RECORD)
336
293
table_options->set_pack_record(true);
338
if (table_options->has_comment() && table_options->comment().length() == 0)
339
table_options->clear_comment();
341
295
if (table_options->has_comment())
343
297
uint32_t tmp_len;
363
317
table_options->set_collation(create_info->default_table_charset->name);
366
if (create_info->used_fields & HA_CREATE_USED_AUTO)
367
table_options->set_has_user_set_auto_increment_value(true);
369
table_options->set_has_user_set_auto_increment_value(false);
371
320
if (create_info->auto_increment_value)
372
321
table_options->set_auto_increment_value(create_info->auto_increment_value);
455
404
idx->set_comment(key_info[i].comment.str);
457
static const uint64_t unknown_index_flag= (HA_NOSAME | HA_PACK_KEY |
462
HA_KEY_HAS_PART_KEY_SEG |
465
if (key_info[i].flags & ~unknown_index_flag)
406
if (key_info[i].flags &
407
~(HA_NOSAME | HA_PACK_KEY | HA_USES_BLOCK_SIZE |
408
HA_BINARY_PACK_KEY | HA_VAR_LENGTH_PART | HA_NULL_PART_KEY |
409
HA_KEY_HAS_PART_KEY_SEG | HA_GENERATED_KEY | HA_USES_COMMENT))
466
410
abort(); // Invalid (unknown) index flag.
468
412
for(unsigned int j=0; j< key_info[i].key_parts; j++)
470
414
message::Table::Index::IndexPart *idxpart;
471
const int fieldnr= key_info[i].key_part[j].fieldnr;
474
416
idxpart= idx->add_index_part();
476
idxpart->set_fieldnr(fieldnr);
478
if (table_proto.field(fieldnr).type() == message::Table::Field::VARCHAR
479
|| table_proto.field(fieldnr).type() == message::Table::Field::BLOB)
481
uint32_t collation_id;
483
if (table_proto.field(fieldnr).string_options().has_collation_id())
484
collation_id= table_proto.field(fieldnr).string_options().collation_id();
486
collation_id= table_proto.options().collation_id();
488
const CHARSET_INFO *cs= get_charset(collation_id);
490
mbmaxlen= cs->mbmaxlen;
493
idxpart->set_compare_length(key_info[i].key_part[j].length / mbmaxlen);
418
idxpart->set_fieldnr(key_info[i].key_part[j].fieldnr);
420
idxpart->set_compare_length(key_info[i].key_part[j].length);
545
472
bool rea_create_table(Session *session,
546
const TableIdentifier &identifier,
473
TableIdentifier &identifier,
547
474
message::Table &table_proto,
548
475
HA_CREATE_INFO *create_info,
549
476
List<CreateField> &create_fields,
550
477
uint32_t keys, KeyInfo *key_info)
552
assert(table_proto.has_name());
553
if (fill_table_proto(table_proto, create_fields, create_info,
479
if (fill_table_proto(table_proto, identifier.getTableName(), create_fields, create_info,
557
assert(table_proto.name() == identifier.getTableName());
559
483
if (plugin::StorageEngine::createTable(*session,