~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
16
#include <config.h>
549 by Monty Taylor
Took gettext.h out of header files.
17
#include <drizzled/error.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
18
#include <drizzled/session.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
19
#include <drizzled/sql_table.h>
20
#include <drizzled/message/statement_transform.h>
1241.9.28 by Monty Taylor
Removed global_charset_info.h from server_includes.h
21
2148.7.12 by Brian Aker
Merge in header fixes.
22
#include <drizzled/plugin/storage_engine.h>
23
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
24
#include <drizzled/internal/my_sys.h>
25
#include <drizzled/typelib.h>
2239.1.9 by Olaf van der Spek
Refactor includes
26
#include <drizzled/util/test.h>
1 by brian
clean slate
27
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
28
/* For proto */
29
#include <string>
30
#include <fstream>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
31
#include <fcntl.h>
2187.7.6 by Brian Aker
This fixes the message such that the table inherits the no replication
32
#include <drizzled/message/schema.h>
33
#include <drizzled/message/table.h>
869.1.2 by Stewart Smith
move to using ZeroCopyInputStream with a file descriptor from open(2) for FRM parsing so we can get errno.
34
#include <google/protobuf/io/zero_copy_stream.h>
35
#include <google/protobuf/io/zero_copy_stream_impl.h>
1608.1.2 by Brian Aker
Merge enum test
36
#include <google/protobuf/message.h>
1095.3.1 by Stewart Smith
make writing the proto file sep to just creating the table. this allows the future removal of copy_table_proto_file. Add table_proto.h header for table proto manipulation stuff.
37
38
#include <drizzled/table_proto.h>
1638.8.1 by Stewart Smith
make the compare_length() of index parts of VARCHAR and BLOB (i.e. those with charsets) be a length in characters instead of bytes. This makes the logic converting a table proto back into a CREATE TABLE statement remotely sane. This does mean that if we increase the number of bytes utf8_general_ci uses, engines may have issues with their on disk formats (if they're not too smart)
39
#include <drizzled/charset.h>
2318.8.3 by Olaf van der Spek
CreateField includes
40
#include <drizzled/create_field.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
41
#include <drizzled/function/time/typecast.h>
1668.7.1 by Stewart Smith
default values for DATE columns were being stored in the original string that the user entered in CREATE TABLE while what actually happens is that this string is typecast to DATE (e.g. if it had a time as well the time vanishes silently). This patch fixes what's stored in the table proto. This is visible when you use statement_transform for SHOW CREATE TABLE
42
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
43
using namespace std;
1130.3.9 by Monty Taylor
Wrapped table_proto_write.cc code in namespace drizzled.
44
45
namespace drizzled {
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
46
2224.4.3 by Brian Aker
Merge in fill_table_proto() fixes.
47
static
2246.4.10 by Olaf van der Spek
Remove const_reference and reference from identifier::Table
48
bool fill_table_proto(const identifier::Table& identifier,
2187.7.6 by Brian Aker
This fixes the message such that the table inherits the no replication
49
                      message::Table &table_proto,
2064.2.2 by Brian Aker
Formattting, etc.
50
                      List<CreateField> &create_fields,
51
                      HA_CREATE_INFO *create_info,
52
                      uint32_t keys,
53
                      KeyInfo *key_info)
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
54
{
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
55
  CreateField *field_arg;
2183.2.10 by Olaf van der Spek
Use List::begin()
56
  List<CreateField>::iterator it(create_fields.begin());
1320.1.1 by Brian Aker
Light cleanup for references.
57
  message::Table::TableOptions *table_options= table_proto.mutable_options();
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
58
2183.2.17 by Olaf van der Spek
Use List::size()
59
  if (create_fields.size() > MAX_FIELDS)
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
60
  {
61
    my_error(ER_TOO_MANY_FIELDS, MYF(0), ER(ER_TOO_MANY_FIELDS));
2064.2.2 by Brian Aker
Formattting, etc.
62
    return true;
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
63
  }
64
1320.1.1 by Brian Aker
Light cleanup for references.
65
  assert(strcmp(table_proto.engine().name().c_str(),
1067.1.1 by Stewart Smith
Beginning of create table proto in parser: pass which Engine is being used for CREATE TABLE in table proto.
66
		create_info->db_type->getName().c_str())==0);
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
67
2187.7.6 by Brian Aker
This fixes the message such that the table inherits the no replication
68
  message::schema::shared_ptr schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
69
70
  if (schema_message and not message::is_replicated(*schema_message))
71
  {
72
    message::set_is_replicated(table_proto, false);
73
  }
74
1215.1.1 by stewart at flamingspork
[patch 01/17] horrible hack to have one lonely codepath produce Fields in the table proto
75
  int field_number= 0;
1320.1.1 by Brian Aker
Light cleanup for references.
76
  bool use_existing_fields= table_proto.field_size() > 0;
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
77
  while ((field_arg= it++))
78
  {
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
79
    message::Table::Field *attribute;
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
80
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
81
    /* some (one) code path for CREATE TABLE fills the proto
82
       out more than the others, so we already have partially
83
       filled out Field messages */
84
1215.1.1 by stewart at flamingspork
[patch 01/17] horrible hack to have one lonely codepath produce Fields in the table proto
85
    if (use_existing_fields)
2008.2.3 by Brian Aker
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
86
    {
1320.1.1 by Brian Aker
Light cleanup for references.
87
      attribute= table_proto.mutable_field(field_number++);
2008.2.3 by Brian Aker
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
88
    }
1215.1.1 by stewart at flamingspork
[patch 01/17] horrible hack to have one lonely codepath produce Fields in the table proto
89
    else
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
90
    {
91
      /* Other code paths still have to fill out the proto */
1320.1.1 by Brian Aker
Light cleanup for references.
92
      attribute= table_proto.add_field();
1215.1.1 by stewart at flamingspork
[patch 01/17] horrible hack to have one lonely codepath produce Fields in the table proto
93
1372.1.3 by Brian Aker
Refactor for table message.
94
      if (field_arg->flags & NOT_NULL_FLAG)
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
95
      {
2064.2.1 by Brian Aker
Fixes naming conventions and issues around notnull being "true" by default.
96
        attribute->mutable_constraints()->set_is_notnull(true);
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
97
      }
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
98
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
99
      if (field_arg->flags & UNSIGNED_FLAG and 
100
          (field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
101
      {
102
        field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
103
        attribute->mutable_constraints()->set_is_unsigned(true);
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
104
      }
1215.1.3 by stewart at flamingspork
[patch 03/17] set field name in parser, assert that its correct when filling out the rest of the table proto
105
106
      attribute->set_name(field_arg->field_name);
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
107
    }
108
2064.2.1 by Brian Aker
Fixes naming conventions and issues around notnull being "true" by default.
109
    assert(((field_arg->flags & NOT_NULL_FLAG)) == attribute->constraints().is_notnull());
1215.1.3 by stewart at flamingspork
[patch 03/17] set field name in parser, assert that its correct when filling out the rest of the table proto
110
    assert(strcmp(attribute->name().c_str(), field_arg->field_name)==0);
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
111
1215.1.5 by stewart at flamingspork
[patch 05/17] Set Field type in proto in parser.
112
113
    message::Table::Field::FieldType parser_type= attribute->type();
114
1919.2.2 by Stewart Smith
throw an error on trying to create a table with a column of type NULL. Yes, the error message here isn't ideal...
115
    if (field_arg->sql_type == DRIZZLE_TYPE_NULL)
116
    {
117
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), table_proto.name().c_str(), -1);
2064.2.2 by Brian Aker
Formattting, etc.
118
      return true;
1919.2.2 by Stewart Smith
throw an error on trying to create a table with a column of type NULL. Yes, the error message here isn't ideal...
119
    }
120
2008.2.3 by Brian Aker
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
121
    if (field_arg->flags & UNSIGNED_FLAG and 
122
       (field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
123
    {
124
      message::Table::Field::FieldConstraints *constraints= attribute->mutable_constraints();
125
126
      field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
127
      constraints->set_is_unsigned(true);
128
    }
129
1273.10.2 by Stewart Smith
de-duplicate internal field type to field proto field type code. Consolidate that in replication_services and table_proto_write and now just have it in statement_transform.
130
    attribute->set_type(message::internalFieldTypeToFieldProtoType(field_arg->sql_type));
1273.2.17 by Stewart Smith
move conversion of enum_field_types to Field protobuf message type out into function so it can be reused in tmp table code.
131
132
    switch (attribute->type()) {
2057.2.8 by Brian Aker
Remove default from switch for field types.
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:
2398.1.1 by Muhammad Umair
merge lp:~mumair/drizzle/drizzle-IPv6Address
138
    case message::Table::Field::IPV6:
2057.2.8 by Brian Aker
Remove default from switch for field types.
139
    case message::Table::Field::TIME:
140
    case message::Table::Field::BOOLEAN:
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
141
      break;
1273.2.17 by Stewart Smith
move conversion of enum_field_types to Field protobuf message type out into function so it can be reused in tmp table code.
142
    case message::Table::Field::DOUBLE:
1119.9.8 by Jay Pipes
Fixes incorrect non-setup of optional scale and precision arguments for DOUBLE in proto.
143
      {
1273.2.17 by Stewart Smith
move conversion of enum_field_types to Field protobuf message type out into function so it can be reused in tmp table code.
144
        /*
1119.9.8 by Jay Pipes
Fixes incorrect non-setup of optional scale and precision arguments for DOUBLE in proto.
145
         * For DOUBLE, we only add a specific scale and precision iff
146
         * the fixed decimal point has been specified...
147
         */
148
        if (field_arg->decimals != NOT_FIXED_DEC)
149
        {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
150
          message::Table::Field::NumericFieldOptions *numeric_field_options;
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
151
1119.9.8 by Jay Pipes
Fixes incorrect non-setup of optional scale and precision arguments for DOUBLE in proto.
152
          numeric_field_options= attribute->mutable_numeric_options();
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
153
154
          numeric_field_options->set_precision(field_arg->length);
1119.9.8 by Jay Pipes
Fixes incorrect non-setup of optional scale and precision arguments for DOUBLE in proto.
155
          numeric_field_options->set_scale(field_arg->decimals);
156
        }
157
      }
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
158
      break;
1273.2.17 by Stewart Smith
move conversion of enum_field_types to Field protobuf message type out into function so it can be reused in tmp table code.
159
    case message::Table::Field::VARCHAR:
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
160
      {
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
161
        message::Table::Field::StringFieldOptions *string_field_options;
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
162
163
        string_field_options= attribute->mutable_string_options();
1273.2.17 by Stewart Smith
move conversion of enum_field_types to Field protobuf message type out into function so it can be reused in tmp table code.
164
1215.1.14 by stewart at flamingspork
[patch 14/17] add length of CHAR column to proto in parser
165
        if (! use_existing_fields || string_field_options->length()==0)
166
          string_field_options->set_length(field_arg->length
167
                                           / field_arg->charset->mbmaxlen);
168
        else
169
          assert((uint32_t)string_field_options->length() == (uint32_t)(field_arg->length / field_arg->charset->mbmaxlen));
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
170
1215.1.17 by stewart at flamingspork
[patch 17/17] set varbinary charset in parser
171
        if (! string_field_options->has_collation())
172
        {
173
          string_field_options->set_collation_id(field_arg->charset->number);
174
          string_field_options->set_collation(field_arg->charset->name);
175
        }
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
176
        break;
177
      }
1273.2.17 by Stewart Smith
move conversion of enum_field_types to Field protobuf message type out into function so it can be reused in tmp table code.
178
    case message::Table::Field::DECIMAL:
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
179
      {
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
180
        message::Table::Field::NumericFieldOptions *numeric_field_options;
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
181
182
        numeric_field_options= attribute->mutable_numeric_options();
183
        /* This is magic, I hate magic numbers -Brian */
184
        numeric_field_options->set_precision(field_arg->length + ( field_arg->decimals ? -2 : -1));
185
        numeric_field_options->set_scale(field_arg->decimals);
186
        break;
187
      }
1273.2.17 by Stewart Smith
move conversion of enum_field_types to Field protobuf message type out into function so it can be reused in tmp table code.
188
    case message::Table::Field::ENUM:
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
189
      {
1396 by Brian Aker
Simple rename.
190
        message::Table::Field::EnumerationValues *enumeration_options;
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
191
192
        assert(field_arg->interval);
193
1396 by Brian Aker
Simple rename.
194
        enumeration_options= attribute->mutable_enumeration_values();
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
195
196
        for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
197
        {
198
          const char *src= field_arg->interval->type_names[pos];
199
1396 by Brian Aker
Simple rename.
200
          enumeration_options->add_field_value(src);
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
201
        }
1396 by Brian Aker
Simple rename.
202
	enumeration_options->set_collation_id(field_arg->charset->number);
203
        enumeration_options->set_collation(field_arg->charset->name);
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
204
        break;
205
      }
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
206
1273.2.17 by Stewart Smith
move conversion of enum_field_types to Field protobuf message type out into function so it can be reused in tmp table code.
207
    case message::Table::Field::BLOB:
896.3.6 by Stewart Smith
Read Fields out of proto instead of FRM.
208
      {
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
209
        message::Table::Field::StringFieldOptions *string_field_options;
896.3.6 by Stewart Smith
Read Fields out of proto instead of FRM.
210
211
        string_field_options= attribute->mutable_string_options();
212
        string_field_options->set_collation_id(field_arg->charset->number);
213
        string_field_options->set_collation(field_arg->charset->name);
214
      }
215
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
216
      break;
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
217
218
    case message::Table::Field::EPOCH:
219
      {
220
        if (field_arg->sql_type == DRIZZLE_TYPE_MICROTIME)
221
          attribute->mutable_time_options()->set_microseconds(true);
222
      }
223
224
      break;
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
225
    }
226
1215.1.5 by stewart at flamingspork
[patch 05/17] Set Field type in proto in parser.
227
    assert (!use_existing_fields || parser_type == attribute->type());
228
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
229
#ifdef NOTDONE
230
    field_constraints= attribute->mutable_constraints();
231
    constraints->set_is_nullable(field_arg->def->null_value);
232
#endif
233
2420.2.4 by Olaf van der Spek
Refactor
234
    if (not field_arg->comment.empty())
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
235
    {
2420.2.4 by Olaf van der Spek
Refactor
236
      uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
237
						  field_arg->comment.begin(),
238
						  field_arg->comment.end(),
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
239
						  COLUMN_COMMENT_MAXLEN);
240
2420.2.4 by Olaf van der Spek
Refactor
241
      if (tmp_len < field_arg->comment.size())
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
242
      {
2420.2.4 by Olaf van der Spek
Refactor
243
        my_error(ER_WRONG_STRING_LENGTH, MYF(0), field_arg->comment.data(), "COLUMN COMMENT", (uint32_t) COLUMN_COMMENT_MAXLEN);
244
        return true;
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
245
      }
246
2420.2.4 by Olaf van der Spek
Refactor
247
      if (not use_existing_fields)
248
        attribute->set_comment(field_arg->comment.data());
1215.1.4 by stewart at flamingspork
[patch 04/17] field comment set in table proto in parser
249
2420.2.4 by Olaf van der Spek
Refactor
250
      assert(strcmp(attribute->comment().c_str(), field_arg->comment.data())==0);
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
251
    }
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
252
1372.1.3 by Brian Aker
Refactor for table message.
253
    if (field_arg->unireg_check == Field::NEXT_NUMBER)
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
254
    {
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
255
      message::Table::Field::NumericFieldOptions *field_options;
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
256
      field_options= attribute->mutable_numeric_options();
257
      field_options->set_is_autoincrement(true);
258
    }
259
1372.1.3 by Brian Aker
Refactor for table message.
260
    if (field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
261
       || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
262
    {
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
263
      message::Table::Field::FieldOptions *field_options;
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
264
      field_options= attribute->mutable_options();
1638.3.3 by Stewart Smith
store CURRENT_TIMESTAMP instead of NOW() in the table proto as this is what we end up displaying in SHOW CREATE so more accurately represents what's going on. Although it could go either way (it totally does not matter), this paves the way for SHOW CREATE TABLE running through statement_transform.
265
      field_options->set_default_expression("CURRENT_TIMESTAMP");
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
266
    }
267
1372.1.3 by Brian Aker
Refactor for table message.
268
    if (field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
269
       || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
270
    {
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
271
      message::Table::Field::FieldOptions *field_options;
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
272
      field_options= attribute->mutable_options();
1638.3.3 by Stewart Smith
store CURRENT_TIMESTAMP instead of NOW() in the table proto as this is what we end up displaying in SHOW CREATE so more accurately represents what's going on. Although it could go either way (it totally does not matter), this paves the way for SHOW CREATE TABLE running through statement_transform.
273
      field_options->set_update_expression("CURRENT_TIMESTAMP");
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
274
    }
275
2064.2.1 by Brian Aker
Fixes naming conventions and issues around notnull being "true" by default.
276
    if (field_arg->def == NULL  && not attribute->constraints().is_notnull())
1302.3.1 by Stewart Smith
correctly set FieldOptions default_null in table proto
277
    {
278
      message::Table::Field::FieldOptions *field_options;
279
      field_options= attribute->mutable_options();
280
281
      field_options->set_default_null(true);
282
    }
1372.1.3 by Brian Aker
Refactor for table message.
283
    if (field_arg->def)
896.3.3 by Stewart Smith
Start generating a record buffer with default values in proto read path instead of FRM write path.
284
    {
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
285
      message::Table::Field::FieldOptions *field_options;
896.3.3 by Stewart Smith
Start generating a record buffer with default values in proto read path instead of FRM write path.
286
      field_options= attribute->mutable_options();
1302.3.1 by Stewart Smith
correctly set FieldOptions default_null in table proto
287
 
1372.1.3 by Brian Aker
Refactor for table message.
288
      if (field_arg->def->is_null())
896.3.3 by Stewart Smith
Start generating a record buffer with default values in proto read path instead of FRM write path.
289
      {
290
	field_options->set_default_null(true);
291
      }
292
      else
293
      {
896.3.4 by Stewart Smith
Create default_values record from proto instead of reading from FRM. assert if different to FRM.
294
	String d;
295
	String *default_value= field_arg->def->val_str(&d);
296
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
297
	assert(default_value);
298
1372.1.3 by Brian Aker
Refactor for table message.
299
	if ((field_arg->sql_type==DRIZZLE_TYPE_VARCHAR
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
300
	   || field_arg->sql_type==DRIZZLE_TYPE_BLOB)
301
	   && ((field_arg->length / field_arg->charset->mbmaxlen)
302
	   < default_value->length()))
303
	{
304
	  my_error(ER_INVALID_DEFAULT, MYF(0), field_arg->field_name);
2064.2.2 by Brian Aker
Formattting, etc.
305
	  return true;
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
306
	}
307
2167.3.2 by Brian Aker
Make use of isDateTime
308
        if (field::isDateTime(field_arg->sql_type))
1668.7.1 by Stewart Smith
default values for DATE columns were being stored in the original string that the user entered in CREATE TABLE while what actually happens is that this string is typecast to DATE (e.g. if it had a time as well the time vanishes silently). This patch fixes what's stored in the table proto. This is visible when you use statement_transform for SHOW CREATE TABLE
309
        {
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
310
          type::Time ltime;
1668.7.1 by Stewart Smith
default values for DATE columns were being stored in the original string that the user entered in CREATE TABLE while what actually happens is that this string is typecast to DATE (e.g. if it had a time as well the time vanishes silently). This patch fixes what's stored in the table proto. This is visible when you use statement_transform for SHOW CREATE TABLE
311
2104.2.8 by Brian Aker
Merge in reference from pointer.
312
          if (field_arg->def->get_date(ltime, TIME_FUZZY_DATE))
1668.7.1 by Stewart Smith
default values for DATE columns were being stored in the original string that the user entered in CREATE TABLE while what actually happens is that this string is typecast to DATE (e.g. if it had a time as well the time vanishes silently). This patch fixes what's stored in the table proto. This is visible when you use statement_transform for SHOW CREATE TABLE
313
          {
314
            my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR),
315
                     default_value->c_str());
2064.2.2 by Brian Aker
Formattting, etc.
316
            return true;
1668.7.1 by Stewart Smith
default values for DATE columns were being stored in the original string that the user entered in CREATE TABLE while what actually happens is that this string is typecast to DATE (e.g. if it had a time as well the time vanishes silently). This patch fixes what's stored in the table proto. This is visible when you use statement_transform for SHOW CREATE TABLE
317
          }
318
319
          /* We now do the casting down to the appropriate type.
320
321
             Yes, this implicit casting is balls.
322
             It was previously done on reading the proto back in,
323
             but we really shouldn't store the bogus things in the proto,
324
             and instead do the casting behaviour here.
325
326
             the timestamp errors are taken care of elsewhere.
327
          */
328
329
          if (field_arg->sql_type == DRIZZLE_TYPE_DATETIME)
330
          {
331
            Item *typecast= new Item_datetime_typecast(field_arg->def);
332
            typecast->quick_fix_field();
333
            typecast->val_str(default_value);
334
          }
335
          else if (field_arg->sql_type == DRIZZLE_TYPE_DATE)
336
          {
337
            Item *typecast= new Item_date_typecast(field_arg->def);
338
            typecast->quick_fix_field();
339
            typecast->val_str(default_value);
340
          }
341
        }
342
2318.7.23 by Olaf van der Spek
Use operator=
343
	if ((field_arg->sql_type == DRIZZLE_TYPE_VARCHAR && field_arg->charset == &my_charset_bin)
344
	   || (field_arg->sql_type == DRIZZLE_TYPE_BLOB && field_arg->charset == &my_charset_bin))
896.3.4 by Stewart Smith
Create default_values record from proto instead of reading from FRM. assert if different to FRM.
345
	{
2318.7.23 by Olaf van der Spek
Use operator=
346
	  field_options->set_default_bin_value(string(default_value->c_ptr(), default_value->length()));
896.3.4 by Stewart Smith
Create default_values record from proto instead of reading from FRM. assert if different to FRM.
347
	}
348
	else
349
	{
350
	  field_options->set_default_value(default_value->c_ptr());
351
	}
896.3.3 by Stewart Smith
Start generating a record buffer with default values in proto read path instead of FRM write path.
352
      }
353
    }
354
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
355
    assert(field_arg->unireg_check == Field::NONE
356
	   || field_arg->unireg_check == Field::NEXT_NUMBER
357
	   || field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
358
	   || field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
359
	   || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD);
360
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
361
  }
362
1320.1.1 by Brian Aker
Light cleanup for references.
363
  assert(! use_existing_fields || (field_number == table_proto.field_size()));
1215.1.1 by stewart at flamingspork
[patch 01/17] horrible hack to have one lonely codepath produce Fields in the table proto
364
1308.2.11 by Jay Pipes
* Adds CREATE TABLE as a specific CreateTableStatement message in the
365
  if (create_info->table_options & HA_OPTION_PACK_RECORD)
366
    table_options->set_pack_record(true);
869.1.24 by Stewart Smith
Generate all the info needed to get a record size on proto read instead of FRM create time.
367
1668.3.1 by Stewart Smith
upper layer treats empty string for COMMENT as removing comment. Fix this in routine for filling out table protobuf message. (only user visible with statement_transform is used for SHOW CREATE TABLE
368
  if (table_options->has_comment() && table_options->comment().length() == 0)
369
    table_options->clear_comment();
370
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
371
  if (table_options->has_comment())
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
372
  {
373
    uint32_t tmp_len;
374
    tmp_len= system_charset_info->cset->charpos(system_charset_info,
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
375
                                                table_options->comment().c_str(),
376
                                                table_options->comment().c_str() +
377
                                                table_options->comment().length(),
378
                                                TABLE_COMMENT_MAXLEN);
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
379
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
380
    if (tmp_len < table_options->comment().length())
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
381
    {
382
      my_error(ER_WRONG_STRING_LENGTH, MYF(0),
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
383
               table_options->comment().c_str(),"Table COMMENT",
384
               (uint32_t) TABLE_COMMENT_MAXLEN);
2064.2.2 by Brian Aker
Formattting, etc.
385
      return true;
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
386
    }
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
387
  }
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
388
1638.1.5 by Stewart Smith
explicit collation in table proto (from review)
389
  if (create_info->default_table_charset)
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
390
  {
2057.2.8 by Brian Aker
Remove default from switch for field types.
391
    table_options->set_collation_id(create_info->default_table_charset->number);
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
392
    table_options->set_collation(create_info->default_table_charset->name);
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
393
  }
394
1638.10.116 by Stewart Smith
only display AUTO_INCREMENT value if explicitly set by user (and preserve across ALTER TABLE).
395
  if (create_info->used_fields & HA_CREATE_USED_AUTO)
396
    table_options->set_has_user_set_auto_increment_value(true);
397
  else
398
    table_options->set_has_user_set_auto_increment_value(false);
399
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
400
  if (create_info->auto_increment_value)
401
    table_options->set_auto_increment_value(create_info->auto_increment_value);
402
1320.1.1 by Brian Aker
Light cleanup for references.
403
  for (uint32_t i= 0; i < keys; i++)
590.1.2 by Stewart Smith
store indexes in table definition protobuf
404
  {
2385.3.29 by Olaf van der Spek
Refactor
405
    message::Table::Index *idx= table_proto.add_indexes();
406
2430.1.1 by Olaf van der Spek
Use lex_string assign(), data() and size()
407
    assert(test(key_info[i].flags & HA_USES_COMMENT) == (key_info[i].comment.size() > 0));
590.1.2 by Stewart Smith
store indexes in table definition protobuf
408
409
    idx->set_name(key_info[i].name);
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
410
    idx->set_key_length(key_info[i].key_length);
2385.3.29 by Olaf van der Spek
Refactor
411
    idx->set_is_primary(is_primary_key(key_info[i].name));
590.1.2 by Stewart Smith
store indexes in table definition protobuf
412
413
    switch(key_info[i].algorithm)
414
    {
415
    case HA_KEY_ALG_HASH:
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
416
      idx->set_type(message::Table::Index::HASH);
590.1.2 by Stewart Smith
store indexes in table definition protobuf
417
      break;
418
419
    case HA_KEY_ALG_BTREE:
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
420
      idx->set_type(message::Table::Index::BTREE);
590.1.2 by Stewart Smith
store indexes in table definition protobuf
421
      break;
422
423
    case HA_KEY_ALG_UNDEF:
2363.1.5 by Brian Aker
This patch fixes 798940, we will also just now add the type of index to our create statements.
424
      {
425
        idx->set_type(create_info->db_type->default_index_type());
426
        break;
427
      }
590.1.2 by Stewart Smith
store indexes in table definition protobuf
428
    }
429
430
    if (key_info[i].flags & HA_NOSAME)
431
      idx->set_is_unique(true);
432
    else
433
      idx->set_is_unique(false);
434
1537 by Brian Aker
Remove dead options/rename Option and remove the update that we no longer
435
    message::Table::Index::Options *index_options= idx->mutable_options();
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
436
1372.1.3 by Brian Aker
Refactor for table message.
437
    if (key_info[i].flags & HA_USES_BLOCK_SIZE)
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
438
      index_options->set_key_block_size(key_info[i].block_size);
439
1372.1.3 by Brian Aker
Refactor for table message.
440
    if (key_info[i].flags & HA_PACK_KEY)
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
441
      index_options->set_pack_key(true);
442
1372.1.3 by Brian Aker
Refactor for table message.
443
    if (key_info[i].flags & HA_BINARY_PACK_KEY)
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
444
      index_options->set_binary_pack_key(true);
445
1372.1.3 by Brian Aker
Refactor for table message.
446
    if (key_info[i].flags & HA_VAR_LENGTH_PART)
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
447
      index_options->set_var_length_key(true);
448
1372.1.3 by Brian Aker
Refactor for table message.
449
    if (key_info[i].flags & HA_NULL_PART_KEY)
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
450
      index_options->set_null_part_key(true);
451
1372.1.3 by Brian Aker
Refactor for table message.
452
    if (key_info[i].flags & HA_KEY_HAS_PART_KEY_SEG)
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
453
      index_options->set_has_partial_segments(true);
454
1372.1.3 by Brian Aker
Refactor for table message.
455
    if (key_info[i].flags & HA_GENERATED_KEY)
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
456
      index_options->set_auto_generated_key(true);
457
458
    if (key_info[i].flags & HA_USES_COMMENT)
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
459
    {
460
      uint32_t tmp_len;
461
      tmp_len= system_charset_info->cset->charpos(system_charset_info,
2430.1.1 by Olaf van der Spek
Use lex_string assign(), data() and size()
462
						  key_info[i].comment.begin(),
463
						  key_info[i].comment.end(),
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
464
						  TABLE_COMMENT_MAXLEN);
465
2430.1.1 by Olaf van der Spek
Use lex_string assign(), data() and size()
466
      if (tmp_len < key_info[i].comment.size())
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
467
      {
468
	my_error(ER_WRONG_STRING_LENGTH, MYF(0),
2430.1.1 by Olaf van der Spek
Use lex_string assign(), data() and size()
469
		 key_info[i].comment.data(), "Index COMMENT",
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
470
		 (uint32_t) TABLE_COMMENT_MAXLEN);
2064.2.2 by Brian Aker
Formattting, etc.
471
	return true;
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
472
      }
473
2430.1.1 by Olaf van der Spek
Use lex_string assign(), data() and size()
474
      idx->set_comment(key_info[i].comment.data());
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
475
    }
1816.2.6 by Monty Taylor
Fixed three more ICC warnings.
476
    static const uint64_t unknown_index_flag= (HA_NOSAME | HA_PACK_KEY |
477
                                               HA_USES_BLOCK_SIZE | 
478
                                               HA_BINARY_PACK_KEY |
479
                                               HA_VAR_LENGTH_PART |
480
                                               HA_NULL_PART_KEY | 
481
                                               HA_KEY_HAS_PART_KEY_SEG |
482
                                               HA_GENERATED_KEY |
483
                                               HA_USES_COMMENT);
484
    if (key_info[i].flags & ~unknown_index_flag)
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
485
      abort(); // Invalid (unknown) index flag.
590.1.2 by Stewart Smith
store indexes in table definition protobuf
486
487
    for(unsigned int j=0; j< key_info[i].key_parts; j++)
488
    {
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
489
      message::Table::Index::IndexPart *idxpart;
1638.8.1 by Stewart Smith
make the compare_length() of index parts of VARCHAR and BLOB (i.e. those with charsets) be a length in characters instead of bytes. This makes the logic converting a table proto back into a CREATE TABLE statement remotely sane. This does mean that if we increase the number of bytes utf8_general_ci uses, engines may have issues with their on disk formats (if they're not too smart)
490
      const int fieldnr= key_info[i].key_part[j].fieldnr;
491
      int mbmaxlen= 1;
590.1.2 by Stewart Smith
store indexes in table definition protobuf
492
493
      idxpart= idx->add_index_part();
494
1638.8.1 by Stewart Smith
make the compare_length() of index parts of VARCHAR and BLOB (i.e. those with charsets) be a length in characters instead of bytes. This makes the logic converting a table proto back into a CREATE TABLE statement remotely sane. This does mean that if we increase the number of bytes utf8_general_ci uses, engines may have issues with their on disk formats (if they're not too smart)
495
      idxpart->set_fieldnr(fieldnr);
496
497
      if (table_proto.field(fieldnr).type() == message::Table::Field::VARCHAR
498
          || table_proto.field(fieldnr).type() == message::Table::Field::BLOB)
499
      {
500
        uint32_t collation_id;
501
502
        if (table_proto.field(fieldnr).string_options().has_collation_id())
503
          collation_id= table_proto.field(fieldnr).string_options().collation_id();
504
        else
505
          collation_id= table_proto.options().collation_id();
506
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
507
        const charset_info_st *cs= get_charset(collation_id);
1638.8.1 by Stewart Smith
make the compare_length() of index parts of VARCHAR and BLOB (i.e. those with charsets) be a length in characters instead of bytes. This makes the logic converting a table proto back into a CREATE TABLE statement remotely sane. This does mean that if we increase the number of bytes utf8_general_ci uses, engines may have issues with their on disk formats (if they're not too smart)
508
509
        mbmaxlen= cs->mbmaxlen;
510
      }
511
512
      idxpart->set_compare_length(key_info[i].key_part[j].length / mbmaxlen);
590.1.2 by Stewart Smith
store indexes in table definition protobuf
513
    }
514
  }
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
515
1608.1.2 by Brian Aker
Merge enum test
516
  if (not table_proto.IsInitialized())
517
  {
2017.2.1 by Brian Aker
Cleanup error messages around bad table definitions.
518
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
519
             table_proto.name().c_str(),
520
             table_proto.InitializationErrorString().c_str());
521
2064.2.2 by Brian Aker
Formattting, etc.
522
    return true;
1608.1.2 by Brian Aker
Merge enum test
523
  }
524
525
  /*
526
    Here we test to see if we can validate the Table Message before we continue. 
527
    We do this by serializing the protobuffer.
528
  */
529
  {
530
    string tmp_string;
531
532
    try {
533
      table_proto.SerializeToString(&tmp_string);
534
    }
535
536
    catch (...)
537
    {
538
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
2017.2.1 by Brian Aker
Cleanup error messages around bad table definitions.
539
               table_proto.name().c_str(),
540
               table_proto.InitializationErrorString().c_str());
1608.1.2 by Brian Aker
Merge enum test
541
2064.2.2 by Brian Aker
Formattting, etc.
542
      return true;
1608.1.2 by Brian Aker
Merge enum test
543
    }
544
  }
545
2064.2.2 by Brian Aker
Formattting, etc.
546
  return false;
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
547
}
548
1 by brian
clean slate
549
/*
896.4.10 by Stewart Smith
some comment fixes in unireg.cc
550
  Create a table definition proto file and the tables
1 by brian
clean slate
551
552
  SYNOPSIS
553
    rea_create_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
554
    session			Thread handler
1 by brian
clean slate
555
    path		Name of file (including database, without .frm)
556
    db			Data base name
557
    table_name		Table name
558
    create_info		create info parameters
559
    create_fields	Fields to create
560
    keys		number of keys to create
561
    key_info		Keys to create
562
563
  RETURN
564
    0  ok
565
    1  error
566
*/
567
1372.1.3 by Brian Aker
Refactor for table message.
568
bool rea_create_table(Session *session,
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
569
                      const identifier::Table &identifier,
1372.1.3 by Brian Aker
Refactor for table message.
570
                      message::Table &table_proto,
571
                      HA_CREATE_INFO *create_info,
572
                      List<CreateField> &create_fields,
1535 by Brian Aker
Rename of KEY to KeyInfo
573
                      uint32_t keys, KeyInfo *key_info)
1 by brian
clean slate
574
{
1685.2.13 by Brian Aker
Tiny cleanup in pass table_name where we no longer use it.
575
  assert(table_proto.has_name());
2187.7.6 by Brian Aker
This fixes the message such that the table inherits the no replication
576
577
  if (fill_table_proto(identifier,
578
                       table_proto, create_fields, create_info,
1382 by Brian Aker
We were printing out info on charset (which we don't need to do... and don't
579
                       keys, key_info))
2064.2.2 by Brian Aker
Formattting, etc.
580
  {
1372.1.3 by Brian Aker
Refactor for table message.
581
    return false;
2064.2.2 by Brian Aker
Formattting, etc.
582
  }
1095.3.18 by Stewart Smith
move writing the proto out from filling it so we can later override it with engines. Also refactor CREATE TABLE LIKE so that there's only one place where we write the proto (not one for normal and one from I_S).
583
1685.2.13 by Brian Aker
Tiny cleanup in pass table_name where we no longer use it.
584
  assert(table_proto.name() == identifier.getTableName());
585
2068.7.6 by Brian Aker
Fix interface for create table such that it issues error and returns state
586
  if (not plugin::StorageEngine::createTable(*session,
587
                                             identifier,
588
                                             table_proto))
1273.19.11 by Brian Aker
This restores temporary tables back to being viewable via show/select.
589
  {
1372.1.3 by Brian Aker
Refactor for table message.
590
    return false;
1273.19.11 by Brian Aker
This restores temporary tables back to being viewable via show/select.
591
  }
592
1372.1.3 by Brian Aker
Refactor for table message.
593
  return true;
1 by brian
clean slate
594
595
} /* rea_create_table */
1130.3.9 by Monty Taylor
Wrapped table_proto_write.cc code in namespace drizzled.
596
597
} /* namespace drizzled */
1273.10.2 by Stewart Smith
de-duplicate internal field type to field proto field type code. Consolidate that in replication_services and table_proto_write and now just have it in statement_transform.
598