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>
31
#include <drizzled/message/schema.pb.h>
32
#include <drizzled/message/table.pb.h>
33
#include <drizzled/message/schema.h>
34
#include <drizzled/message/table.h>
33
35
#include <google/protobuf/io/zero_copy_stream.h>
34
36
#include <google/protobuf/io/zero_copy_stream_impl.h>
35
37
#include <google/protobuf/message.h>
37
39
#include <drizzled/table_proto.h>
38
40
#include <drizzled/charset.h>
40
#include "drizzled/function/time/typecast.h"
42
#include <drizzled/function/time/typecast.h>
42
44
using namespace std;
44
46
namespace drizzled {
46
static int fill_table_proto(message::Table &table_proto,
47
List<CreateField> &create_fields,
48
HA_CREATE_INFO *create_info,
48
bool fill_table_proto(identifier::Table::const_reference identifier,
49
message::Table &table_proto,
50
List<CreateField> &create_fields,
51
HA_CREATE_INFO *create_info,
52
55
CreateField *field_arg;
53
List_iterator<CreateField> it(create_fields);
56
List<CreateField>::iterator it(create_fields.begin());
54
57
message::Table::TableOptions *table_options= table_proto.mutable_options();
56
59
if (create_fields.elements > MAX_FIELDS)
58
61
my_error(ER_TOO_MANY_FIELDS, MYF(0), ER(ER_TOO_MANY_FIELDS));
62
65
assert(strcmp(table_proto.engine().name().c_str(),
63
66
create_info->db_type->getName().c_str())==0);
68
message::schema::shared_ptr schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
70
if (schema_message and not message::is_replicated(*schema_message))
72
message::set_is_replicated(table_proto, false);
65
75
int field_number= 0;
66
76
bool use_existing_fields= table_proto.field_size() > 0;
67
77
while ((field_arg= it++))
82
94
if (field_arg->flags & NOT_NULL_FLAG)
84
message::Table::Field::FieldConstraints *constraints;
96
attribute->mutable_constraints()->set_is_notnull(true);
86
constraints= attribute->mutable_constraints();
87
constraints->set_is_nullable(false);
99
if (field_arg->flags & UNSIGNED_FLAG and
100
(field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
102
field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
103
attribute->mutable_constraints()->set_is_unsigned(true);
90
106
attribute->set_name(field_arg->field_name);
93
assert((!(field_arg->flags & NOT_NULL_FLAG)) == attribute->constraints().is_nullable());
109
assert(((field_arg->flags & NOT_NULL_FLAG)) == attribute->constraints().is_notnull());
94
110
assert(strcmp(attribute->name().c_str(), field_arg->field_name)==0);
99
115
if (field_arg->sql_type == DRIZZLE_TYPE_NULL)
101
117
my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), table_proto.name().c_str(), -1);
121
if (field_arg->flags & UNSIGNED_FLAG and
122
(field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
124
message::Table::Field::FieldConstraints *constraints= attribute->mutable_constraints();
126
field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
127
constraints->set_is_unsigned(true);
105
130
attribute->set_type(message::internalFieldTypeToFieldProtoType(field_arg->sql_type));
107
132
switch (attribute->type()) {
108
default: /* Only deal with types that need extra information */
133
case message::Table::Field::BIGINT:
134
case message::Table::Field::INTEGER:
135
case message::Table::Field::DATE:
136
case message::Table::Field::DATETIME:
137
case message::Table::Field::UUID:
138
case message::Table::Field::TIME:
139
case message::Table::Field::BOOLEAN:
110
141
case message::Table::Field::DOUBLE:
217
case message::Table::Field::EPOCH:
219
if (field_arg->sql_type == DRIZZLE_TYPE_MICROTIME)
220
attribute->mutable_time_options()->set_microseconds(true);
186
226
assert (!use_existing_fields || parser_type == attribute->type());
204
244
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
205
245
field_arg->comment.str,"COLUMN COMMENT",
206
246
(uint32_t) COLUMN_COMMENT_MAXLEN);
210
250
if (! use_existing_fields)
236
276
field_options->set_update_expression("CURRENT_TIMESTAMP");
239
if (field_arg->def == NULL && attribute->constraints().is_nullable())
279
if (field_arg->def == NULL && not attribute->constraints().is_notnull())
241
281
message::Table::Field::FieldOptions *field_options;
242
282
field_options= attribute->mutable_options();
265
305
< default_value->length()))
267
307
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)
311
if (field::isDateTime(field_arg->sql_type))
277
if (field_arg->def->get_date(<ime, TIME_FUZZY_DATE))
315
if (field_arg->def->get_date(ltime, TIME_FUZZY_DATE))
279
317
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR),
280
318
default_value->c_str());
284
322
/* We now do the casting down to the appropriate type.
352
390
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
353
391
table_options->comment().c_str(),"Table COMMENT",
354
392
(uint32_t) TABLE_COMMENT_MAXLEN);
359
397
if (create_info->default_table_charset)
361
table_options->set_collation_id(
362
create_info->default_table_charset->number);
399
table_options->set_collation_id(create_info->default_table_charset->number);
363
400
table_options->set_collation(create_info->default_table_charset->name);
449
486
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
450
487
key_info[i].comment.str,"Index COMMENT",
451
488
(uint32_t) TABLE_COMMENT_MAXLEN);
455
492
idx->set_comment(key_info[i].comment.str);
497
534
if (not table_proto.IsInitialized())
499
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table_proto.InitializationErrorString().c_str());
536
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
537
table_proto.name().c_str(),
538
table_proto.InitializationErrorString().c_str());
516
556
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
517
table_proto.InitializationErrorString().empty() ? "": table_proto.InitializationErrorString().c_str());
557
table_proto.name().c_str(),
558
table_proto.InitializationErrorString().c_str());
545
586
bool rea_create_table(Session *session,
546
const TableIdentifier &identifier,
587
const identifier::Table &identifier,
547
588
message::Table &table_proto,
548
589
HA_CREATE_INFO *create_info,
549
590
List<CreateField> &create_fields,
550
591
uint32_t keys, KeyInfo *key_info)
552
593
assert(table_proto.has_name());
553
if (fill_table_proto(table_proto, create_fields, create_info,
595
if (fill_table_proto(identifier,
596
table_proto, create_fields, create_info,
557
602
assert(table_proto.name() == identifier.getTableName());
559
if (plugin::StorageEngine::createTable(*session,
604
if (not plugin::StorageEngine::createTable(*session,