~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_proto_write.cc

  • Committer: patrick crews
  • Date: 2011-02-23 17:17:25 UTC
  • mto: (2195.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2196.
  • Revision ID: gleebix@gmail.com-20110223171725-4tgewemxhsw1m7q8
Integrated randgen with dbqp.  We now have mode=randgen and a set of randgen test suites (very basic now).  Output = same as dtr : )  We also have mode=cleanup to kill any servers we have started.  Docs updates too.  Gendata utility allows us to populate test servers 

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>
26
28
 
27
29
/* For proto */
28
30
#include <string>
29
31
#include <fstream>
30
32
#include <fcntl.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>
39
41
 
40
 
#include "drizzled/function/time/typecast.h"
 
42
#include <drizzled/function/time/typecast.h>
41
43
 
42
44
using namespace std;
43
45
 
44
46
namespace drizzled {
45
47
 
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)
 
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
                      uint32_t keys,
 
53
                      KeyInfo *key_info)
51
54
{
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();
55
58
 
56
59
  if (create_fields.elements > MAX_FIELDS)
57
60
  {
58
61
    my_error(ER_TOO_MANY_FIELDS, MYF(0), ER(ER_TOO_MANY_FIELDS));
59
 
    return(1);
 
62
    return true;
60
63
  }
61
64
 
62
65
  assert(strcmp(table_proto.engine().name().c_str(),
63
66
                create_info->db_type->getName().c_str())==0);
64
67
 
 
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
 
65
75
  int field_number= 0;
66
76
  bool use_existing_fields= table_proto.field_size() > 0;
67
77
  while ((field_arg= it++))
73
83
       filled out Field messages */
74
84
 
75
85
    if (use_existing_fields)
 
86
    {
76
87
      attribute= table_proto.mutable_field(field_number++);
 
88
    }
77
89
    else
78
90
    {
79
91
      /* Other code paths still have to fill out the proto */
81
93
 
82
94
      if (field_arg->flags & NOT_NULL_FLAG)
83
95
      {
84
 
        message::Table::Field::FieldConstraints *constraints;
 
96
        attribute->mutable_constraints()->set_is_notnull(true);
 
97
      }
85
98
 
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))
 
101
      {
 
102
        field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
 
103
        attribute->mutable_constraints()->set_is_unsigned(true);
88
104
      }
89
105
 
90
106
      attribute->set_name(field_arg->field_name);
91
107
    }
92
108
 
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);
95
111
 
96
112
 
99
115
    if (field_arg->sql_type == DRIZZLE_TYPE_NULL)
100
116
    {
101
117
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), table_proto.name().c_str(), -1);
102
 
      return -1;
 
118
      return true;
 
119
    }
 
120
 
 
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);
103
128
    }
104
129
 
105
130
    attribute->set_type(message::internalFieldTypeToFieldProtoType(field_arg->sql_type));
106
131
 
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:
109
140
      break;
110
141
    case message::Table::Field::DOUBLE:
111
142
      {
171
202
        enumeration_options->set_collation(field_arg->charset->name);
172
203
        break;
173
204
      }
 
205
 
174
206
    case message::Table::Field::BLOB:
175
207
      {
176
208
        message::Table::Field::StringFieldOptions *string_field_options;
181
213
      }
182
214
 
183
215
      break;
 
216
 
 
217
    case message::Table::Field::EPOCH:
 
218
      {
 
219
        if (field_arg->sql_type == DRIZZLE_TYPE_MICROTIME)
 
220
          attribute->mutable_time_options()->set_microseconds(true);
 
221
      }
 
222
 
 
223
      break;
184
224
    }
185
225
 
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);
207
 
        return(1);
 
247
        return true;
208
248
      }
209
249
 
210
250
      if (! use_existing_fields)
236
276
      field_options->set_update_expression("CURRENT_TIMESTAMP");
237
277
    }
238
278
 
239
 
    if (field_arg->def == NULL  && attribute->constraints().is_nullable())
 
279
    if (field_arg->def == NULL  && not attribute->constraints().is_notnull())
240
280
    {
241
281
      message::Table::Field::FieldOptions *field_options;
242
282
      field_options= attribute->mutable_options();
265
305
           < default_value->length()))
266
306
        {
267
307
          my_error(ER_INVALID_DEFAULT, MYF(0), field_arg->field_name);
268
 
          return 1;
 
308
          return true;
269
309
        }
270
310
 
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))
274
312
        {
275
 
          DRIZZLE_TIME ltime;
 
313
          type::Time ltime;
276
314
 
277
 
          if (field_arg->def->get_date(&ltime, TIME_FUZZY_DATE))
 
315
          if (field_arg->def->get_date(ltime, TIME_FUZZY_DATE))
278
316
          {
279
317
            my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR),
280
318
                     default_value->c_str());
281
 
            return 1;
 
319
            return true;
282
320
          }
283
321
 
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);
355
 
      return(1);
 
393
      return true;
356
394
    }
357
395
  }
358
396
 
359
397
  if (create_info->default_table_charset)
360
398
  {
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);
364
401
  }
365
402
 
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);
452
 
        return(1);
 
489
        return true;
453
490
      }
454
491
 
455
492
      idx->set_comment(key_info[i].comment.str);
496
533
 
497
534
  if (not table_proto.IsInitialized())
498
535
  {
499
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table_proto.InitializationErrorString().c_str());
500
 
    return 1;
 
536
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
537
             table_proto.name().c_str(),
 
538
             table_proto.InitializationErrorString().c_str());
 
539
 
 
540
    return true;
501
541
  }
502
542
 
503
543
  /*
514
554
    catch (...)
515
555
    {
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());
518
559
 
519
 
      return 1;
 
560
      return true;
520
561
    }
521
562
  }
522
563
 
523
 
  return 0;
 
564
  return false;
524
565
}
525
566
 
526
567
/*
543
584
*/
544
585
 
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)
551
592
{
552
593
  assert(table_proto.has_name());
553
 
  if (fill_table_proto(table_proto, create_fields, create_info,
 
594
 
 
595
  if (fill_table_proto(identifier,
 
596
                       table_proto, create_fields, create_info,
554
597
                       keys, key_info))
 
598
  {
555
599
    return false;
 
600
  }
556
601
 
557
602
  assert(table_proto.name() == identifier.getTableName());
558
603
 
559
 
  if (plugin::StorageEngine::createTable(*session,
560
 
                                         identifier,
561
 
                                         table_proto))
 
604
  if (not plugin::StorageEngine::createTable(*session,
 
605
                                             identifier,
 
606
                                             table_proto))
562
607
  {
563
608
    return false;
564
609
  }