13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
17
17
#include <drizzled/error.h>
18
18
#include <drizzled/session.h>
19
19
#include <drizzled/unireg.h>
20
#include "drizzled/sql_table.h"
21
#include "drizzled/global_charset_info.h"
22
#include "drizzled/message/statement_transform.h"
24
#include "drizzled/internal/my_sys.h"
20
#include <drizzled/sql_table.h>
21
#include <drizzled/global_charset_info.h>
22
#include <drizzled/message/statement_transform.h>
24
#include <drizzled/plugin/storage_engine.h>
26
#include <drizzled/internal/my_sys.h>
27
#include <drizzled/typelib.h>
28
#include <drizzled/util/test.h>
31
#include <drizzled/message/schema.pb.h>
32
#include <drizzled/message/table.pb.h>
34
#include <drizzled/message/schema.h>
35
#include <drizzled/message/table.h>
33
36
#include <google/protobuf/io/zero_copy_stream.h>
34
37
#include <google/protobuf/io/zero_copy_stream_impl.h>
35
38
#include <google/protobuf/message.h>
37
40
#include <drizzled/table_proto.h>
38
41
#include <drizzled/charset.h>
40
#include "drizzled/function/time/typecast.h"
43
#include <drizzled/function/time/typecast.h>
42
45
using namespace std;
44
47
namespace drizzled {
46
static int fill_table_proto(message::Table &table_proto,
47
List<CreateField> &create_fields,
48
HA_CREATE_INFO *create_info,
50
bool fill_table_proto(const identifier::Table& identifier,
51
message::Table &table_proto,
52
List<CreateField> &create_fields,
53
HA_CREATE_INFO *create_info,
52
57
CreateField *field_arg;
53
List_iterator<CreateField> it(create_fields);
58
List<CreateField>::iterator it(create_fields.begin());
54
59
message::Table::TableOptions *table_options= table_proto.mutable_options();
56
if (create_fields.elements > MAX_FIELDS)
61
if (create_fields.size() > MAX_FIELDS)
58
63
my_error(ER_TOO_MANY_FIELDS, MYF(0), ER(ER_TOO_MANY_FIELDS));
62
67
assert(strcmp(table_proto.engine().name().c_str(),
63
68
create_info->db_type->getName().c_str())==0);
70
message::schema::shared_ptr schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
72
if (schema_message and not message::is_replicated(*schema_message))
74
message::set_is_replicated(table_proto, false);
65
77
int field_number= 0;
66
78
bool use_existing_fields= table_proto.field_size() > 0;
67
79
while ((field_arg= it++))
82
96
if (field_arg->flags & NOT_NULL_FLAG)
84
message::Table::Field::FieldConstraints *constraints;
98
attribute->mutable_constraints()->set_is_notnull(true);
86
constraints= attribute->mutable_constraints();
87
constraints->set_is_nullable(false);
101
if (field_arg->flags & UNSIGNED_FLAG and
102
(field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
104
field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
105
attribute->mutable_constraints()->set_is_unsigned(true);
90
108
attribute->set_name(field_arg->field_name);
93
assert((!(field_arg->flags & NOT_NULL_FLAG)) == attribute->constraints().is_nullable());
111
assert(((field_arg->flags & NOT_NULL_FLAG)) == attribute->constraints().is_notnull());
94
112
assert(strcmp(attribute->name().c_str(), field_arg->field_name)==0);
99
117
if (field_arg->sql_type == DRIZZLE_TYPE_NULL)
101
119
my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), table_proto.name().c_str(), -1);
123
if (field_arg->flags & UNSIGNED_FLAG and
124
(field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
126
message::Table::Field::FieldConstraints *constraints= attribute->mutable_constraints();
128
field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
129
constraints->set_is_unsigned(true);
105
132
attribute->set_type(message::internalFieldTypeToFieldProtoType(field_arg->sql_type));
107
134
switch (attribute->type()) {
108
default: /* Only deal with types that need extra information */
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::TIME:
141
case message::Table::Field::BOOLEAN:
110
143
case message::Table::Field::DOUBLE:
219
case message::Table::Field::EPOCH:
221
if (field_arg->sql_type == DRIZZLE_TYPE_MICROTIME)
222
attribute->mutable_time_options()->set_microseconds(true);
186
228
assert (!use_existing_fields || parser_type == attribute->type());
204
246
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
205
247
field_arg->comment.str,"COLUMN COMMENT",
206
248
(uint32_t) COLUMN_COMMENT_MAXLEN);
210
252
if (! use_existing_fields)
236
278
field_options->set_update_expression("CURRENT_TIMESTAMP");
239
if (field_arg->def == NULL && attribute->constraints().is_nullable())
281
if (field_arg->def == NULL && not attribute->constraints().is_notnull())
241
283
message::Table::Field::FieldOptions *field_options;
242
284
field_options= attribute->mutable_options();
265
307
< default_value->length()))
267
309
my_error(ER_INVALID_DEFAULT, MYF(0), field_arg->field_name);
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)
313
if (field::isDateTime(field_arg->sql_type))
277
if (field_arg->def->get_date(<ime, TIME_FUZZY_DATE))
317
if (field_arg->def->get_date(ltime, TIME_FUZZY_DATE))
279
319
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR),
280
320
default_value->c_str());
284
324
/* We now do the casting down to the appropriate type.
352
392
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
353
393
table_options->comment().c_str(),"Table COMMENT",
354
394
(uint32_t) TABLE_COMMENT_MAXLEN);
359
399
if (create_info->default_table_charset)
361
table_options->set_collation_id(
362
create_info->default_table_charset->number);
401
table_options->set_collation_id(create_info->default_table_charset->number);
363
402
table_options->set_collation(create_info->default_table_charset->name);
449
488
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
450
489
key_info[i].comment.str,"Index COMMENT",
451
490
(uint32_t) TABLE_COMMENT_MAXLEN);
455
494
idx->set_comment(key_info[i].comment.str);
497
536
if (not table_proto.IsInitialized())
499
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table_proto.InitializationErrorString().c_str());
538
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
539
table_proto.name().c_str(),
540
table_proto.InitializationErrorString().c_str());
516
558
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
517
table_proto.InitializationErrorString().empty() ? "": table_proto.InitializationErrorString().c_str());
559
table_proto.name().c_str(),
560
table_proto.InitializationErrorString().c_str());
545
588
bool rea_create_table(Session *session,
546
const TableIdentifier &identifier,
589
const identifier::Table &identifier,
547
590
message::Table &table_proto,
548
591
HA_CREATE_INFO *create_info,
549
592
List<CreateField> &create_fields,
550
593
uint32_t keys, KeyInfo *key_info)
552
595
assert(table_proto.has_name());
553
if (fill_table_proto(table_proto, create_fields, create_info,
597
if (fill_table_proto(identifier,
598
table_proto, create_fields, create_info,
557
604
assert(table_proto.name() == identifier.getTableName());
559
if (plugin::StorageEngine::createTable(*session,
606
if (not plugin::StorageEngine::createTable(*session,