13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include <drizzled/server_includes.h>
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"
31
24
#include <drizzled/message/schema.pb.h>
32
25
#include <drizzled/message/table.pb.h>
33
26
#include <google/protobuf/io/zero_copy_stream.h>
34
27
#include <google/protobuf/io/zero_copy_stream_impl.h>
36
29
#include <drizzled/table_proto.h>
38
30
using namespace std;
42
static int fill_table_proto(message::Table &table_proto,
43
const std::string &table_name,
44
List<CreateField> &create_fields,
45
HA_CREATE_INFO *create_info,
31
using namespace drizzled;
33
int fill_table_proto(message::Table *table_proto,
34
const char *table_name,
35
List<CreateField> &create_fields,
36
HA_CREATE_INFO *create_info,
49
40
CreateField *field_arg;
50
41
List_iterator<CreateField> it(create_fields);
51
message::Table::TableOptions *table_options= table_proto.mutable_options();
42
message::Table::TableOptions *table_options= table_proto->mutable_options();
53
44
if (create_fields.elements > MAX_FIELDS)
59
assert(strcmp(table_proto.engine().name().c_str(),
50
assert(strcmp(table_proto->engine().name().c_str(),
60
51
create_info->db_type->getName().c_str())==0);
62
assert(table_proto.name() == table_name);
53
assert(strcmp(table_proto->name().c_str(),table_name)==0);
65
bool use_existing_fields= table_proto.field_size() > 0;
66
55
while ((field_arg= it++))
68
57
message::Table::Field *attribute;
70
/* some (one) code path for CREATE TABLE fills the proto
71
out more than the others, so we already have partially
72
filled out Field messages */
74
if (use_existing_fields)
75
attribute= table_proto.mutable_field(field_number++);
59
attribute= table_proto->add_field();
60
attribute->set_name(field_arg->field_name);
62
attribute->set_pack_flag(field_arg->pack_flag); /* TODO: MUST DIE */
64
if(f_maybe_null(field_arg->pack_flag))
78
/* Other code paths still have to fill out the proto */
79
attribute= table_proto.add_field();
81
if (field_arg->flags & NOT_NULL_FLAG)
83
message::Table::Field::FieldConstraints *constraints;
85
constraints= attribute->mutable_constraints();
86
constraints->set_is_nullable(false);
89
attribute->set_name(field_arg->field_name);
66
message::Table::Field::FieldConstraints *constraints;
68
constraints= attribute->mutable_constraints();
69
constraints->set_is_nullable(true);
92
assert((!(field_arg->flags & NOT_NULL_FLAG)) == attribute->constraints().is_nullable());
93
assert(strcmp(attribute->name().c_str(), field_arg->field_name)==0);
96
message::Table::Field::FieldType parser_type= attribute->type();
98
attribute->set_type(message::internalFieldTypeToFieldProtoType(field_arg->sql_type));
100
switch (attribute->type()) {
101
default: /* Only deal with types that need extra information */
103
case message::Table::Field::DOUBLE:
106
* For DOUBLE, we only add a specific scale and precision iff
107
* the fixed decimal point has been specified...
109
if (field_arg->decimals != NOT_FIXED_DEC)
111
message::Table::Field::NumericFieldOptions *numeric_field_options;
113
numeric_field_options= attribute->mutable_numeric_options();
115
numeric_field_options->set_precision(field_arg->length);
116
numeric_field_options->set_scale(field_arg->decimals);
120
case message::Table::Field::VARCHAR:
72
switch (field_arg->sql_type) {
73
case DRIZZLE_TYPE_TINY:
74
attribute->set_type(message::Table::Field::TINYINT);
76
case DRIZZLE_TYPE_LONG:
77
attribute->set_type(message::Table::Field::INTEGER);
79
case DRIZZLE_TYPE_DOUBLE:
80
attribute->set_type(message::Table::Field::DOUBLE);
82
case DRIZZLE_TYPE_NULL :
83
assert(1); /* Not a user definable type */
84
case DRIZZLE_TYPE_TIMESTAMP:
85
attribute->set_type(message::Table::Field::TIMESTAMP);
87
case DRIZZLE_TYPE_LONGLONG:
88
attribute->set_type(message::Table::Field::BIGINT);
90
case DRIZZLE_TYPE_DATETIME:
91
attribute->set_type(message::Table::Field::DATETIME);
93
case DRIZZLE_TYPE_DATE:
94
attribute->set_type(message::Table::Field::DATE);
96
case DRIZZLE_TYPE_VARCHAR:
122
98
message::Table::Field::StringFieldOptions *string_field_options;
124
100
string_field_options= attribute->mutable_string_options();
126
if (! use_existing_fields || string_field_options->length()==0)
127
string_field_options->set_length(field_arg->length
128
/ field_arg->charset->mbmaxlen);
130
assert((uint32_t)string_field_options->length() == (uint32_t)(field_arg->length / field_arg->charset->mbmaxlen));
132
if (! string_field_options->has_collation())
134
string_field_options->set_collation_id(field_arg->charset->number);
135
string_field_options->set_collation(field_arg->charset->name);
101
attribute->set_type(message::Table::Field::VARCHAR);
102
string_field_options->set_length(field_arg->length
103
/ field_arg->charset->mbmaxlen);
104
string_field_options->set_collation_id(field_arg->charset->number);
105
string_field_options->set_collation(field_arg->charset->name);
139
case message::Table::Field::DECIMAL:
109
case DRIZZLE_TYPE_NEWDECIMAL:
141
111
message::Table::Field::NumericFieldOptions *numeric_field_options;
113
attribute->set_type(message::Table::Field::DECIMAL);
143
114
numeric_field_options= attribute->mutable_numeric_options();
144
115
/* This is magic, I hate magic numbers -Brian */
145
116
numeric_field_options->set_precision(field_arg->length + ( field_arg->decimals ? -2 : -1));
146
117
numeric_field_options->set_scale(field_arg->decimals);
149
case message::Table::Field::ENUM:
120
case DRIZZLE_TYPE_ENUM:
151
message::Table::Field::EnumerationValues *enumeration_options;
122
message::Table::Field::SetFieldOptions *set_field_options;
153
124
assert(field_arg->interval);
155
enumeration_options= attribute->mutable_enumeration_values();
126
attribute->set_type(message::Table::Field::ENUM);
127
set_field_options= attribute->mutable_set_options();
157
129
for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
159
131
const char *src= field_arg->interval->type_names[pos];
161
enumeration_options->add_field_value(src);
133
set_field_options->add_field_value(src);
163
enumeration_options->set_collation_id(field_arg->charset->number);
164
enumeration_options->set_collation(field_arg->charset->name);
135
set_field_options->set_count_elements(set_field_options->field_value_size());
136
set_field_options->set_collation_id(field_arg->charset->number);
137
set_field_options->set_collation(field_arg->charset->name);
167
case message::Table::Field::BLOB:
140
case DRIZZLE_TYPE_BLOB:
142
attribute->set_type(message::Table::Field::BLOB);
169
144
message::Table::Field::StringFieldOptions *string_field_options;
171
146
string_field_options= attribute->mutable_string_options();
380
387
message::Table::Index::IndexOptions *index_options= idx->mutable_options();
382
if (key_info[i].flags & HA_USES_BLOCK_SIZE)
389
if(key_info[i].flags & HA_USES_BLOCK_SIZE)
383
390
index_options->set_key_block_size(key_info[i].block_size);
385
if (key_info[i].flags & HA_PACK_KEY)
392
if(key_info[i].flags & HA_PACK_KEY)
386
393
index_options->set_pack_key(true);
388
if (key_info[i].flags & HA_BINARY_PACK_KEY)
395
if(key_info[i].flags & HA_BINARY_PACK_KEY)
389
396
index_options->set_binary_pack_key(true);
391
if (key_info[i].flags & HA_VAR_LENGTH_PART)
398
if(key_info[i].flags & HA_VAR_LENGTH_PART)
392
399
index_options->set_var_length_key(true);
394
if (key_info[i].flags & HA_NULL_PART_KEY)
401
if(key_info[i].flags & HA_NULL_PART_KEY)
395
402
index_options->set_null_part_key(true);
397
if (key_info[i].flags & HA_KEY_HAS_PART_KEY_SEG)
404
if(key_info[i].flags & HA_KEY_HAS_PART_KEY_SEG)
398
405
index_options->set_has_partial_segments(true);
400
if (key_info[i].flags & HA_GENERATED_KEY)
407
if(key_info[i].flags & HA_GENERATED_KEY)
401
408
index_options->set_auto_generated_key(true);
403
410
if (key_info[i].flags & HA_USES_COMMENT)
450
int rename_table_proto_file(const char *from, const char* to)
452
string from_path(from);
454
string file_ext = ".dfe";
456
from_path.append(file_ext);
457
to_path.append(file_ext);
459
return my_rename(from_path.c_str(),to_path.c_str(),MYF(MY_WME));
462
int delete_table_proto_file(const char *file_name)
464
string new_path(file_name);
465
string file_ext = ".dfe";
467
new_path.append(file_ext);
468
return my_delete(new_path.c_str(), MYF(0));
471
int drizzle_write_proto_file(const std::string file_name,
472
message::Table *table_proto)
474
int fd= open(file_name.c_str(), O_RDWR|O_CREAT|O_TRUNC, my_umask);
479
google::protobuf::io::ZeroCopyOutputStream* output=
480
new google::protobuf::io::FileOutputStream(fd);
482
if (table_proto->SerializeToZeroCopyStream(output) == false)
447
495
Create a table definition proto file and the tables
456
504
create_fields Fields to create
457
505
keys number of keys to create
458
506
key_info Keys to create
508
is_like is true for mysql_create_like_schema_frm
465
bool rea_create_table(Session *session,
466
TableIdentifier &identifier,
467
message::Table &table_proto,
468
HA_CREATE_INFO *create_info,
469
List<CreateField> &create_fields,
470
uint32_t keys, KEY *key_info)
515
int rea_create_table(Session *session, const char *path,
516
const char *db, const char *table_name,
517
message::Table *table_proto,
518
HA_CREATE_INFO *create_info,
519
List<CreateField> &create_fields,
520
uint32_t keys, KEY *key_info)
472
if (fill_table_proto(table_proto, identifier.getTableName(), create_fields, create_info,
476
if (plugin::StorageEngine::createTable(*session,
522
/* Proto will blow up unless we give a name */
525
if (fill_table_proto(table_proto, table_name, create_fields, create_info,
529
string new_path(path);
530
string file_ext = ".dfe";
532
new_path.append(file_ext);
536
StorageEngine* engine= ha_resolve_by_name(session,
537
table_proto->engine().name());
538
if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
539
err= drizzle_write_proto_file(new_path, table_proto);
544
my_error(ER_BAD_DB_ERROR,MYF(0),db);
546
my_error(ER_CANT_CREATE_TABLE,MYF(0),table_name,err);
551
if (ha_create_table(session, path, db, table_name,
552
create_info,0, table_proto))
557
if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
558
delete_table_proto_file(path);
485
561
} /* rea_create_table */
487
} /* namespace drizzled */