~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_proto.cc

  • Committer: Olaf van der Spek
  • Date: 2011-04-20 09:27:49 UTC
  • mto: This revision was merged to the branch mainline in revision 2285.
  • Revision ID: olafvdspek@gmail.com-20110420092749-hw1q9rfj1pumc2no
Session Cache

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 */
15
15
 
16
 
#include "config.h"
 
16
#include <config.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"
23
 
 
24
 
#include "drizzled/internal/my_sys.h"
25
 
 
 
20
#include <drizzled/sql_table.h>
 
21
#include <drizzled/global_charset_info.h>
 
22
#include <drizzled/message/statement_transform.h>
 
23
 
 
24
#include <drizzled/plugin/storage_engine.h>
 
25
 
 
26
#include <drizzled/internal/my_sys.h>
 
27
#include <drizzled/typelib.h>
 
28
#include <drizzled/util/test.h>
26
29
 
27
30
/* For proto */
28
31
#include <string>
29
32
#include <fstream>
30
33
#include <fcntl.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>
39
42
 
40
 
#include "drizzled/function/time/typecast.h"
 
43
#include <drizzled/function/time/typecast.h>
41
44
 
42
45
using namespace std;
43
46
 
44
47
namespace drizzled {
45
48
 
46
 
static int fill_table_proto(message::Table &table_proto,
47
 
                            List<CreateField> &create_fields,
48
 
                            HA_CREATE_INFO *create_info,
49
 
                            uint32_t keys,
50
 
                            KeyInfo *key_info)
 
49
static
 
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,
 
54
                      uint32_t keys,
 
55
                      KeyInfo *key_info)
51
56
{
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();
55
60
 
56
 
  if (create_fields.elements > MAX_FIELDS)
 
61
  if (create_fields.size() > MAX_FIELDS)
57
62
  {
58
63
    my_error(ER_TOO_MANY_FIELDS, MYF(0), ER(ER_TOO_MANY_FIELDS));
59
 
    return(1);
 
64
    return true;
60
65
  }
61
66
 
62
67
  assert(strcmp(table_proto.engine().name().c_str(),
63
68
                create_info->db_type->getName().c_str())==0);
64
69
 
 
70
  message::schema::shared_ptr schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
 
71
 
 
72
  if (schema_message and not message::is_replicated(*schema_message))
 
73
  {
 
74
    message::set_is_replicated(table_proto, false);
 
75
  }
 
76
 
65
77
  int field_number= 0;
66
78
  bool use_existing_fields= table_proto.field_size() > 0;
67
79
  while ((field_arg= it++))
73
85
       filled out Field messages */
74
86
 
75
87
    if (use_existing_fields)
 
88
    {
76
89
      attribute= table_proto.mutable_field(field_number++);
 
90
    }
77
91
    else
78
92
    {
79
93
      /* Other code paths still have to fill out the proto */
81
95
 
82
96
      if (field_arg->flags & NOT_NULL_FLAG)
83
97
      {
84
 
        message::Table::Field::FieldConstraints *constraints;
 
98
        attribute->mutable_constraints()->set_is_notnull(true);
 
99
      }
85
100
 
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))
 
103
      {
 
104
        field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
 
105
        attribute->mutable_constraints()->set_is_unsigned(true);
88
106
      }
89
107
 
90
108
      attribute->set_name(field_arg->field_name);
91
109
    }
92
110
 
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);
95
113
 
96
114
 
99
117
    if (field_arg->sql_type == DRIZZLE_TYPE_NULL)
100
118
    {
101
119
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), table_proto.name().c_str(), -1);
102
 
      return -1;
 
120
      return true;
 
121
    }
 
122
 
 
123
    if (field_arg->flags & UNSIGNED_FLAG and 
 
124
       (field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
 
125
    {
 
126
      message::Table::Field::FieldConstraints *constraints= attribute->mutable_constraints();
 
127
 
 
128
      field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
 
129
      constraints->set_is_unsigned(true);
103
130
    }
104
131
 
105
132
    attribute->set_type(message::internalFieldTypeToFieldProtoType(field_arg->sql_type));
106
133
 
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:
109
142
      break;
110
143
    case message::Table::Field::DOUBLE:
111
144
      {
171
204
        enumeration_options->set_collation(field_arg->charset->name);
172
205
        break;
173
206
      }
 
207
 
174
208
    case message::Table::Field::BLOB:
175
209
      {
176
210
        message::Table::Field::StringFieldOptions *string_field_options;
181
215
      }
182
216
 
183
217
      break;
 
218
 
 
219
    case message::Table::Field::EPOCH:
 
220
      {
 
221
        if (field_arg->sql_type == DRIZZLE_TYPE_MICROTIME)
 
222
          attribute->mutable_time_options()->set_microseconds(true);
 
223
      }
 
224
 
 
225
      break;
184
226
    }
185
227
 
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);
207
 
        return(1);
 
249
        return true;
208
250
      }
209
251
 
210
252
      if (! use_existing_fields)
236
278
      field_options->set_update_expression("CURRENT_TIMESTAMP");
237
279
    }
238
280
 
239
 
    if (field_arg->def == NULL  && attribute->constraints().is_nullable())
 
281
    if (field_arg->def == NULL  && not attribute->constraints().is_notnull())
240
282
    {
241
283
      message::Table::Field::FieldOptions *field_options;
242
284
      field_options= attribute->mutable_options();
265
307
           < default_value->length()))
266
308
        {
267
309
          my_error(ER_INVALID_DEFAULT, MYF(0), field_arg->field_name);
268
 
          return 1;
 
310
          return true;
269
311
        }
270
312
 
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))
274
314
        {
275
 
          DRIZZLE_TIME ltime;
 
315
          type::Time ltime;
276
316
 
277
 
          if (field_arg->def->get_date(&ltime, TIME_FUZZY_DATE))
 
317
          if (field_arg->def->get_date(ltime, TIME_FUZZY_DATE))
278
318
          {
279
319
            my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR),
280
320
                     default_value->c_str());
281
 
            return 1;
 
321
            return true;
282
322
          }
283
323
 
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);
355
 
      return(1);
 
395
      return true;
356
396
    }
357
397
  }
358
398
 
359
399
  if (create_info->default_table_charset)
360
400
  {
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);
364
403
  }
365
404
 
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);
452
 
        return(1);
 
491
        return true;
453
492
      }
454
493
 
455
494
      idx->set_comment(key_info[i].comment.str);
485
524
        else
486
525
          collation_id= table_proto.options().collation_id();
487
526
 
488
 
        const CHARSET_INFO *cs= get_charset(collation_id);
 
527
        const charset_info_st *cs= get_charset(collation_id);
489
528
 
490
529
        mbmaxlen= cs->mbmaxlen;
491
530
      }
496
535
 
497
536
  if (not table_proto.IsInitialized())
498
537
  {
499
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table_proto.InitializationErrorString().c_str());
500
 
    return 1;
 
538
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
539
             table_proto.name().c_str(),
 
540
             table_proto.InitializationErrorString().c_str());
 
541
 
 
542
    return true;
501
543
  }
502
544
 
503
545
  /*
514
556
    catch (...)
515
557
    {
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());
518
561
 
519
 
      return 1;
 
562
      return true;
520
563
    }
521
564
  }
522
565
 
523
 
  return 0;
 
566
  return false;
524
567
}
525
568
 
526
569
/*
543
586
*/
544
587
 
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)
551
594
{
552
595
  assert(table_proto.has_name());
553
 
  if (fill_table_proto(table_proto, create_fields, create_info,
 
596
 
 
597
  if (fill_table_proto(identifier,
 
598
                       table_proto, create_fields, create_info,
554
599
                       keys, key_info))
 
600
  {
555
601
    return false;
 
602
  }
556
603
 
557
604
  assert(table_proto.name() == identifier.getTableName());
558
605
 
559
 
  if (plugin::StorageEngine::createTable(*session,
560
 
                                         identifier,
561
 
                                         table_proto))
 
606
  if (not plugin::StorageEngine::createTable(*session,
 
607
                                             identifier,
 
608
                                             table_proto))
562
609
  {
563
610
    return false;
564
611
  }