~ubuntu-branches/ubuntu/quantal/drizzle/quantal

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.2.11) (2.1.16 sid)
  • Revision ID: package-import@ubuntu.com-20120619104649-9ij634mxm4x8pp4l
Tags: 1:7.1.36-stable-1ubuntu1
* Merge from Debian unstable. (LP: #987575)
  Remaining changes:
  - Added upstart script.
* debian/drizzle.upstart: dropped logger since upstart logs job
  output now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <drizzled/abort_exception.h>
22
22
#include <drizzled/error.h>
23
23
#include <drizzled/nested_join.h>
24
 
#include <drizzled/query_id.h>
25
24
#include <drizzled/transaction_services.h>
26
25
#include <drizzled/sql_parse.h>
27
26
#include <drizzled/data_home.h>
40
39
#include <drizzled/statement.h>
41
40
#include <drizzled/statement/alter_table.h>
42
41
#include <drizzled/probes.h>
43
 
#include <drizzled/global_charset_info.h>
 
42
#include <drizzled/charset.h>
44
43
#include <drizzled/plugin/logging.h>
45
44
#include <drizzled/plugin/query_rewrite.h>
46
45
#include <drizzled/plugin/query_cache.h>
53
52
#include <drizzled/kill.h>
54
53
#include <drizzled/schema.h>
55
54
#include <drizzled/item/subselect.h>
 
55
#include <drizzled/diagnostics_area.h>
 
56
#include <drizzled/table_ident.h>
 
57
#include <drizzled/statistics_variables.h>
 
58
#include <drizzled/system_variables.h>
 
59
#include <drizzled/session/times.h>
 
60
#include <drizzled/session/transactions.h>
 
61
#include <drizzled/create_field.h>
 
62
#include <drizzled/lex_input_stream.h>
56
63
 
57
64
#include <limits.h>
58
65
 
65
72
 
66
73
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
67
74
 
68
 
namespace drizzled
69
 
{
 
75
namespace drizzled {
70
76
 
71
77
/* Prototypes */
72
78
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
73
79
static bool parse_sql(Session *session, Lex_input_stream *lip);
74
 
void parse(Session *session, const char *inBuf, uint32_t length);
 
80
void parse(Session&, const char *inBuf, uint32_t length);
75
81
 
76
82
/**
77
83
  @defgroup Runtime_Environment Runtime Environment
79
85
*/
80
86
 
81
87
extern size_t my_thread_stack_size;
82
 
extern const CHARSET_INFO *character_set_filesystem;
 
88
extern const charset_info_st *character_set_filesystem;
 
89
 
 
90
static atomic<uint64_t> g_query_id;
83
91
 
84
92
namespace
85
93
{
86
 
 
87
 
static const std::string command_name[COM_END+1]={
88
 
  "Sleep",
89
 
  "Quit",
90
 
  "Init DB",
91
 
  "Query",
92
 
  "Shutdown",
93
 
  "Connect",
94
 
  "Ping",
95
 
  "Kill",
96
 
  "Error"  // Last command number
97
 
};
98
 
 
 
94
  static const std::string command_name[]=
 
95
  {
 
96
    "Sleep",
 
97
    "Quit",
 
98
    "Init DB",
 
99
    "Query",
 
100
    "Shutdown",
 
101
    "Connect",
 
102
    "Ping",
 
103
    "Kill",
 
104
    "Error"  // Last command number
 
105
  };
99
106
}
100
107
 
101
 
const char *xa_state_names[]={
 
108
const char *xa_state_names[]=
 
109
{
102
110
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
103
111
};
104
112
 
 
113
 
105
114
/**
106
115
  Mark all commands that somehow changes a table.
107
116
 
123
132
 
124
133
void init_update_queries(void)
125
134
{
126
 
  uint32_t x;
127
 
 
128
 
  for (x= 0; x <= SQLCOM_END; x++)
 
135
  for (uint32_t x= uint32_t(SQLCOM_SELECT); 
 
136
       x <= uint32_t(SQLCOM_END); x++)
 
137
  {
129
138
    sql_command_flags[x].reset();
 
139
  }
130
140
 
131
141
  sql_command_flags[SQLCOM_CREATE_TABLE]=   CF_CHANGES_DATA;
132
142
  sql_command_flags[SQLCOM_CREATE_INDEX]=   CF_CHANGES_DATA;
180
190
        COM_QUIT/COM_SHUTDOWN
181
191
*/
182
192
bool dispatch_command(enum_server_command command, Session *session,
183
 
                      char* packet, uint32_t packet_length)
 
193
                      const char* packet, uint32_t packet_length)
184
194
{
185
 
  bool error= 0;
186
 
  Query_id &query_id= Query_id::get_query_id();
 
195
  bool error= false;
187
196
 
188
197
  DRIZZLE_COMMAND_START(session->thread_id, command);
189
198
 
190
199
  session->command= command;
191
200
  session->lex().sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
192
 
  session->set_time();
193
 
  session->setQueryId(query_id.value());
 
201
  session->times.set_time();
 
202
  session->setQueryId(g_query_id.increment());
194
203
 
195
 
  switch( command ) {
196
 
  /* Ignore these statements. */
197
 
  case COM_PING:
198
 
    break;
199
 
  /* Increase id and count all other statements. */
200
 
  default:
 
204
  if (command != COM_PING)
 
205
  {
 
206
    // Increase id and count all other statements
201
207
    session->status_var.questions++;
202
 
    query_id.next();
203
208
  }
204
209
 
205
210
  /* @todo set session->lex().sql_command to SQLCOM_END here */
210
215
    // We should do something about an error...
211
216
  }
212
217
 
213
 
  session->server_status&=
214
 
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
218
  session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
215
219
 
216
 
  switch (command) {
217
 
  case COM_INIT_DB:
 
220
  switch (command)
218
221
  {
219
 
    if (packet_length == 0)
 
222
  case COM_USE_SCHEMA:
220
223
    {
221
 
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
 
224
      if (packet_length == 0)
 
225
      {
 
226
        my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
 
227
        break;
 
228
      }
 
229
      if (not schema::change(*session, identifier::Schema(string(packet, packet_length))))
 
230
      {
 
231
        session->my_ok();
 
232
      }
222
233
      break;
223
234
    }
224
235
 
225
 
    string tmp(packet, packet_length);
226
 
 
227
 
    identifier::Schema identifier(tmp);
228
 
 
229
 
    if (not schema::change(*session, identifier))
230
 
    {
231
 
      session->my_ok();
232
 
    }
233
 
    break;
234
 
  }
235
236
  case COM_QUERY:
236
 
  {
237
 
    if (not session->readAndStoreQuery(packet, packet_length))
238
 
      break;                                    // fatal error is set
239
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
240
 
                        session->thread_id,
241
 
                        const_cast<const char *>(session->schema()->c_str()));
242
 
 
243
 
    parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
244
 
 
245
 
    break;
246
 
  }
 
237
    {
 
238
      session->readAndStoreQuery(packet, packet_length);
 
239
      DRIZZLE_QUERY_START(session->getQueryString()->c_str(), session->thread_id, session->schema()->c_str());
 
240
      parse(*session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
241
      break;
 
242
    }
 
243
 
247
244
  case COM_QUIT:
248
245
    /* We don't calculate statistics for this command */
249
 
    session->main_da.disable_status();              // Don't send anything back
 
246
    session->main_da().disable_status();              // Don't send anything back
250
247
    error= true;                                        // End server
251
248
    break;
 
249
 
252
250
  case COM_KILL:
253
251
    {
254
252
      if (packet_length != 4)
267
265
      session->my_ok();
268
266
      break;
269
267
    }
 
268
 
270
269
  case COM_SHUTDOWN:
271
 
  {
272
 
    session->status_var.com_other++;
273
 
    session->my_eof();
274
 
    session->close_thread_tables();                     // Free before kill
275
 
    kill_drizzle();
276
 
    error= true;
277
 
    break;
278
 
  }
 
270
    {
 
271
      session->status_var.com_other++;
 
272
      session->my_eof();
 
273
      session->close_thread_tables();                   // Free before kill
 
274
      kill_drizzle();
 
275
      error= true;
 
276
      break;
 
277
    }
 
278
 
279
279
  case COM_PING:
280
280
    session->status_var.com_other++;
281
281
    session->my_ok();                           // Tell client we are alive
282
282
    break;
 
283
 
283
284
  case COM_SLEEP:
284
285
  case COM_CONNECT:                             // Impossible here
285
286
  case COM_END:
289
290
  }
290
291
 
291
292
  /* If commit fails, we should be able to reset the OK status. */
292
 
  session->main_da.can_overwrite_status= true;
293
 
  TransactionServices &transaction_services= TransactionServices::singleton();
294
 
  transaction_services.autocommitOrRollback(*session, session->is_error());
295
 
  session->main_da.can_overwrite_status= false;
 
293
  session->main_da().can_overwrite_status= true;
 
294
  TransactionServices::autocommitOrRollback(*session, session->is_error());
 
295
  session->main_da().can_overwrite_status= false;
296
296
 
297
297
  session->transaction.stmt.reset();
298
298
 
300
300
  /* report error issued during command execution */
301
301
  if (session->killed_errno())
302
302
  {
303
 
    if (! session->main_da.is_set())
 
303
    if (not session->main_da().is_set())
304
304
      session->send_kill_message();
305
305
  }
306
306
  if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
310
310
  }
311
311
 
312
312
  /* Can not be true, but do not take chances in production. */
313
 
  assert(! session->main_da.is_sent);
 
313
  assert(! session->main_da().is_sent);
314
314
 
315
 
  switch (session->main_da.status())
 
315
  switch (session->main_da().status())
316
316
  {
317
317
  case Diagnostics_area::DA_ERROR:
318
318
    /* The query failed, send error to log and abort bootstrap. */
319
 
    session->getClient()->sendError(session->main_da.sql_errno(),
320
 
                               session->main_da.message());
 
319
    session->getClient()->sendError(session->main_da().sql_errno(),
 
320
                               session->main_da().message());
321
321
    break;
322
322
 
323
323
  case Diagnostics_area::DA_EOF:
337
337
    break;
338
338
  }
339
339
 
340
 
  session->main_da.is_sent= true;
 
340
  session->main_da().is_sent= true;
341
341
 
342
342
  session->set_proc_info("closing tables");
343
343
  /* Free tables */
395
395
    1                 out of memory or SHOW commands are not allowed
396
396
                      in this version of the server.
397
397
*/
398
 
static bool _schema_select(Session *session, Select_Lex *sel,
399
 
                           const string& schema_table_name)
 
398
static bool _schema_select(Session& session, Select_Lex& sel, const string& schema_table_name)
400
399
{
401
 
  LEX_STRING db, table;
 
400
  lex_string_t db, table;
402
401
  bitset<NUM_OF_TABLE_OPTIONS> table_options;
403
402
  /*
404
403
     We have to make non const db_name & table_name
405
404
     because of lower_case_table_names
406
405
  */
407
 
  session->make_lex_string(&db, "data_dictionary", sizeof("data_dictionary"), false);
408
 
  session->make_lex_string(&table, schema_table_name, false);
409
 
 
410
 
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
411
 
                               NULL, table_options, TL_READ))
412
 
  {
413
 
    return true;
414
 
  }
415
 
  return false;
 
406
  session.make_lex_string(&db, str_ref("data_dictionary"));
 
407
  session.make_lex_string(&table, schema_table_name);
 
408
  return not sel.add_table_to_list(&session, new Table_ident(db, table), NULL, table_options, TL_READ);
416
409
}
417
410
 
418
 
int prepare_new_schema_table(Session *session, LEX& lex,
419
 
                             const string& schema_table_name)
 
411
int prepare_new_schema_table(Session *session, LEX& lex0, const string& schema_table_name)
420
412
{
421
 
  Select_Lex *schema_select_lex= NULL;
422
 
 
423
 
  Select_Lex *select_lex= lex.current_select;
424
 
  assert(select_lex);
425
 
  if (_schema_select(session, select_lex, schema_table_name))
426
 
  {
427
 
    return(1);
428
 
  }
429
 
  TableList *table_list= (TableList*) select_lex->table_list.first;
430
 
  assert(table_list);
431
 
  table_list->schema_select_lex= schema_select_lex;
432
 
 
 
413
  Select_Lex& lex= *lex0.current_select;
 
414
  if (_schema_select(*session, lex, schema_table_name))
 
415
    return 1;
 
416
  TableList *table_list= (TableList*)lex.table_list.first;
 
417
  table_list->schema_select_lex= NULL;
433
418
  return 0;
434
419
}
435
420
 
465
450
 
466
451
static int execute_command(Session *session)
467
452
{
468
 
  bool res= false;
469
 
 
470
453
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
471
454
  Select_Lex *select_lex= &session->lex().select_lex;
472
455
 
473
 
  /* list of all tables in query */
474
 
  TableList *all_tables;
475
 
 
476
456
  /*
477
457
    In many cases first table of main Select_Lex have special meaning =>
478
458
    check that it is first table in global list and relink it first in
490
470
  */
491
471
  session->lex().first_lists_tables_same();
492
472
 
 
473
  /* list of all tables in query */
493
474
  /* should be assigned after making first tables same */
494
 
  all_tables= session->lex().query_tables;
 
475
  TableList* all_tables= session->lex().query_tables;
495
476
 
496
477
  /* set context for commands which do not use setup_tables */
497
478
  select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
505
486
  */
506
487
  if (all_tables || ! session->lex().is_single_level_stmt())
507
488
  {
508
 
    drizzle_reset_errors(session, 0);
 
489
    drizzle_reset_errors(*session, 0);
509
490
  }
510
491
 
511
 
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
 
492
  assert(not session->transaction.stmt.hasModifiedNonTransData());
512
493
 
513
 
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
514
 
      && ! session->inTransaction()
 
494
  if (not (session->server_status & SERVER_STATUS_AUTOCOMMIT)
 
495
      && not session->inTransaction()
515
496
      && session->lex().statement->isTransactional())
516
497
  {
517
 
    if (session->startTransaction() == false)
 
498
    if (not session->startTransaction())
518
499
    {
519
500
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
520
501
      return true;
522
503
  }
523
504
 
524
505
  /* now we are ready to execute the statement */
525
 
  res= session->lex().statement->execute();
 
506
  bool res= session->lex().statement->execute();
526
507
  session->set_proc_info("query end");
527
508
  /*
528
509
    The return value for ROW_COUNT() is "implementation dependent" if the
530
511
    wants. We also keep the last value in case of SQLCOM_CALL or
531
512
    SQLCOM_EXECUTE.
532
513
  */
533
 
  if (! (sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
514
  if (not sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT))
534
515
  {
535
516
    session->row_count_func= -1;
536
517
  }
537
518
 
538
 
  return (res || session->is_error());
 
519
  return res || session->is_error();
539
520
}
 
521
 
540
522
bool execute_sqlcom_select(Session *session, TableList *all_tables)
541
523
{
542
524
  LEX   *lex= &session->lex();
555
537
      && ! session->inTransaction()
556
538
      && ! lex->statement->isShow())
557
539
  {
558
 
    if (session->startTransaction() == false)
 
540
    if (not session->startTransaction())
559
541
    {
560
542
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
561
543
      return true;
572
554
        to prepend EXPLAIN to any query and receive output for it,
573
555
        even if the query itself redirects the output.
574
556
      */
575
 
      if (!(result= new select_send()))
576
 
        return true;
 
557
      result= new select_send();
577
558
      session->send_explain_fields(result);
578
559
      optimizer::ExplainPlan planner;
579
560
      res= planner.explainUnion(session, &session->lex().unit, result);
596
577
    }
597
578
    else
598
579
    {
599
 
      if (!result && !(result= new select_send()))
600
 
        return true;
 
580
      if (not result)
 
581
        result= new select_send();
601
582
 
602
583
      /* Init the Query Cache plugin */
603
584
      plugin::QueryCache::prepareResultset(session);
660
641
}
661
642
 
662
643
 
663
 
bool
664
 
new_select(LEX *lex, bool move_down)
 
644
bool new_select(LEX *lex, bool move_down)
665
645
{
666
 
  Select_Lex *select_lex;
667
 
  Session *session= lex->session;
668
 
 
669
 
  if (!(select_lex= new (session->mem_root) Select_Lex()))
670
 
    return true;
 
646
  Session* session= lex->session;
 
647
  Select_Lex* select_lex= new (session->mem_root) Select_Lex;
671
648
 
672
649
  select_lex->select_number= ++session->select_number;
673
650
  select_lex->parent_lex= lex; /* Used in init_query. */
678
655
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
679
656
  {
680
657
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
681
 
    return(1);
 
658
    return 1;
682
659
  }
683
660
 
684
661
  select_lex->nest_level= lex->nest_level;
685
662
  if (move_down)
686
663
  {
687
 
    Select_Lex_Unit *unit;
688
664
    /* first select_lex of subselect or derived table */
689
 
    if (!(unit= new (session->mem_root) Select_Lex_Unit()))
690
 
      return(1);
 
665
    Select_Lex_Unit* unit= new (session->mem_root) Select_Lex_Unit();
691
666
 
692
667
    unit->init_query();
693
668
    unit->init_select();
717
692
    if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
718
693
      return true;
719
694
 
720
 
    select_lex->context.outer_context=
721
 
                unit->first_select()->context.outer_context;
 
695
    select_lex->context.outer_context= unit->first_select()->context.outer_context;
722
696
  }
723
697
 
724
698
  select_lex->master_unit()->global_parameters= select_lex;
745
719
 
746
720
void create_select_for_variable(Session *session, const char *var_name)
747
721
{
748
 
  LEX *lex;
749
 
  LEX_STRING tmp, null_lex_string;
750
 
  Item *var;
751
 
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
752
 
  char *end= buff;
753
 
 
754
 
  lex= &session->lex();
755
 
  init_select(lex);
756
 
  lex->sql_command= SQLCOM_SELECT;
757
 
  tmp.str= (char*) var_name;
758
 
  tmp.length=strlen(var_name);
759
 
  memset(&null_lex_string.str, 0, sizeof(null_lex_string));
 
722
  LEX& lex= session->lex();
 
723
  init_select(&lex);
 
724
  lex.sql_command= SQLCOM_SELECT;
 
725
  lex_string_t tmp;
 
726
  tmp.assign(var_name, strlen(var_name));
760
727
  /*
761
728
    We set the name of Item to @@session.var_name because that then is used
762
729
    as the column name in the output.
763
730
  */
764
 
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
 
731
  if (Item* var= get_system_var(session, OPT_SESSION, tmp, null_lex_string()))
765
732
  {
 
733
    char buff[MAX_SYS_VAR_LENGTH*2+4+8];
 
734
    char *end= buff;
766
735
    end+= snprintf(buff, sizeof(buff), "@@session.%s", var_name);
767
 
    var->set_name(buff, end-buff, system_charset_info);
 
736
    var->set_name(buff, end-buff);
768
737
    session->add_item_to_list(var);
769
738
  }
770
739
}
778
747
  @param       length  Length of the query text
779
748
*/
780
749
 
781
 
void parse(Session *session, const char *inBuf, uint32_t length)
 
750
void parse(Session& session, const char *inBuf, uint32_t length)
782
751
{
783
 
  session->lex().start(session);
784
 
 
785
 
  session->reset_for_next_command();
 
752
  session.lex().start(&session);
 
753
  session.reset_for_next_command();
786
754
  /* Check if the Query is Cached if and return true if yes
787
755
   * TODO the plugin has to make sure that the query is cacheble
788
756
   * by setting the query_safe_cache param to TRUE
789
757
   */
790
 
  bool res= true;
791
 
  if (plugin::QueryCache::isCached(session))
792
 
  {
793
 
    res= plugin::QueryCache::sendCachedResultset(session);
794
 
  }
795
 
  if (not res)
796
 
  {
 
758
  if (plugin::QueryCache::isCached(&session) && not plugin::QueryCache::sendCachedResultset(&session))
797
759
    return;
798
 
  }
799
 
  LEX *lex= &session->lex();
800
 
  Lex_input_stream lip(session, inBuf, length);
801
 
  bool err= parse_sql(session, &lip);
802
 
  if (!err)
803
 
  {
804
 
    {
805
 
      if (not session->is_error())
806
 
      {
807
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
808
 
                                 session->thread_id,
809
 
                                 const_cast<const char *>(session->schema()->c_str()));
810
 
        // Implement Views here --Brian
811
 
        /* Actually execute the query */
812
 
        try
813
 
        {
814
 
          execute_command(session);
815
 
        }
816
 
        catch (...)
817
 
        {
818
 
          // Just try to catch any random failures that could have come
819
 
          // during execution.
820
 
          DRIZZLE_ABORT;
821
 
        }
822
 
        DRIZZLE_QUERY_EXEC_DONE(0);
823
 
      }
824
 
    }
825
 
  }
826
 
  else
827
 
  {
828
 
    assert(session->is_error());
829
 
  }
830
 
  lex->unit.cleanup();
831
 
  session->set_proc_info("freeing items");
832
 
  session->end_statement();
833
 
  session->cleanup_after_query();
834
 
  session->set_end_timer();
 
760
  Lex_input_stream lip(&session, inBuf, length);
 
761
  if (parse_sql(&session, &lip))
 
762
    assert(session.is_error());
 
763
  else if (not session.is_error())
 
764
  {
 
765
    DRIZZLE_QUERY_EXEC_START(session.getQueryString()->c_str(), session.thread_id, session.schema()->c_str());
 
766
    // Implement Views here --Brian
 
767
    /* Actually execute the query */
 
768
    try
 
769
    {
 
770
      execute_command(&session);
 
771
    }
 
772
    catch (...)
 
773
    {
 
774
      // Just try to catch any random failures that could have come
 
775
      // during execution.
 
776
      DRIZZLE_ABORT;
 
777
    }
 
778
    DRIZZLE_QUERY_EXEC_DONE(0);
 
779
  }
 
780
  session.lex().unit.cleanup();
 
781
  session.set_proc_info("freeing items");
 
782
  session.end_statement();
 
783
  session.cleanup_after_query();
 
784
  session.times.set_end_timer(session);
835
785
}
836
786
 
837
787
 
838
 
 
839
788
/**
840
789
  Store field definition for create.
841
790
 
843
792
    Return 0 if ok
844
793
*/
845
794
 
846
 
bool add_field_to_list(Session *session, LEX_STRING *field_name, enum_field_types type,
847
 
                       char *length, char *decimals,
848
 
                       uint32_t type_modifier,
849
 
                       enum column_format_type column_format,
850
 
                       Item *default_value, Item *on_update_value,
851
 
                       LEX_STRING *comment,
852
 
                       char *change,
853
 
                       List<String> *interval_list, const CHARSET_INFO * const cs)
 
795
bool add_field_to_list(Session *session, str_ref field_name, enum_field_types type,
 
796
                       const char *length, const char *decimals,
 
797
                       uint32_t type_modifier, column_format_type column_format,
 
798
                       Item *default_value, Item *on_update_value, str_ref comment,
 
799
                       const char *change, List<String> *interval_list, const charset_info_st* cs)
854
800
{
855
 
  register CreateField *new_field;
856
801
  LEX  *lex= &session->lex();
857
802
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
858
803
 
861
806
 
862
807
  if (type_modifier & PRI_KEY_FLAG)
863
808
  {
864
 
    Key *key;
865
 
    lex->col_list.push_back(new Key_part_spec(*field_name, 0));
866
 
    key= new Key(Key::PRIMARY, null_lex_str,
867
 
                      &default_key_create_info,
868
 
                      0, lex->col_list);
869
 
    statement->alter_info.key_list.push_back(key);
 
809
    lex->col_list.push_back(new Key_part_spec(field_name, 0));
 
810
    statement->alter_info.key_list.push_back(new Key(Key::PRIMARY, null_lex_string(), &default_key_create_info, 0, lex->col_list));
870
811
    lex->col_list.clear();
871
812
  }
872
813
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
873
814
  {
874
 
    Key *key;
875
 
    lex->col_list.push_back(new Key_part_spec(*field_name, 0));
876
 
    key= new Key(Key::UNIQUE, null_lex_str,
877
 
                 &default_key_create_info, 0,
878
 
                 lex->col_list);
879
 
    statement->alter_info.key_list.push_back(key);
 
815
    lex->col_list.push_back(new Key_part_spec(field_name, 0));
 
816
    statement->alter_info.key_list.push_back(new Key(Key::UNIQUE, null_lex_string(), &default_key_create_info, 0, lex->col_list));
880
817
    lex->col_list.clear();
881
818
  }
882
819
 
893
830
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
894
831
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
895
832
    {
896
 
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
 
833
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
897
834
      return true;
898
835
    }
899
836
    else if (default_value->type() == Item::NULL_ITEM)
901
838
      default_value= 0;
902
839
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
903
840
      {
904
 
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
905
 
        return true;
 
841
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
 
842
        return true;
906
843
      }
907
844
    }
908
845
    else if (type_modifier & AUTO_INCREMENT_FLAG)
909
846
    {
910
 
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
 
847
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
911
848
      return true;
912
849
    }
913
850
  }
914
851
 
915
852
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
916
853
  {
917
 
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
 
854
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name.data());
918
855
    return true;
919
856
  }
920
857
 
921
 
  if (!(new_field= new CreateField()) ||
922
 
      new_field->init(session, field_name->str, type, length, decimals, type_modifier,
923
 
                      default_value, on_update_value, comment, change,
924
 
                      interval_list, cs, 0, column_format))
 
858
  CreateField* new_field= new CreateField;
 
859
  if (new_field->init(session, field_name.data(), type, length, decimals, type_modifier, comment, change, interval_list, cs, 0, column_format)
 
860
      || new_field->setDefaultValue(default_value, on_update_value))
925
861
    return true;
926
862
 
927
863
  statement->alter_info.create_list.push_back(new_field);
952
888
 
953
889
TableList *Select_Lex::add_table_to_list(Session *session,
954
890
                                         Table_ident *table,
955
 
                                         LEX_STRING *alias,
 
891
                                         lex_string_t *alias,
956
892
                                         const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
957
893
                                         thr_lock_type lock_type,
958
894
                                         List<Index_hint> *index_hints_arg,
959
 
                                         LEX_STRING *option)
 
895
                                         lex_string_t *option)
960
896
{
961
 
  TableList *ptr;
962
897
  TableList *previous_table_ref; /* The table preceding the current one. */
963
 
  char *alias_str;
964
898
  LEX *lex= &session->lex();
965
899
 
966
900
  if (!table)
967
901
    return NULL;                                // End of memory
968
 
  alias_str= alias ? alias->str : table->table.str;
969
 
  if (! table_options.test(TL_OPTION_ALIAS) &&
970
 
      check_table_name(table->table.str, table->table.length))
 
902
  const char* alias_str= alias ? alias->data() : table->table.data();
 
903
  if (not table_options.test(TL_OPTION_ALIAS) && check_table_name(table->table))
971
904
  {
972
 
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
 
905
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.data());
973
906
    return NULL;
974
907
  }
975
908
 
976
 
  if (table->is_derived_table() == false && table->db.str)
 
909
  if (not table->is_derived_table() && table->db.data())
977
910
  {
978
 
    my_casedn_str(files_charset_info, table->db.str);
979
 
 
980
 
    identifier::Schema schema_identifier(string(table->db.str));
981
 
    if (not schema::check(*session, schema_identifier))
 
911
    files_charset_info->casedn_str(table->db.str_);
 
912
    if (not schema::check(*session, identifier::Schema(table->db)))
982
913
    {
983
 
 
984
 
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
 
914
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.data());
985
915
      return NULL;
986
916
    }
987
917
  }
990
920
  {
991
921
    if (table->sel)
992
922
    {
993
 
      my_message(ER_DERIVED_MUST_HAVE_ALIAS,
994
 
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
 
923
      my_message(ER_DERIVED_MUST_HAVE_ALIAS, ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
995
924
      return NULL;
996
925
    }
997
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
998
 
      return NULL;
 
926
    alias_str= (char*) session->mem.memdup(alias_str, table->table.size() + 1);
999
927
  }
1000
 
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
1001
 
    return NULL;
 
928
  TableList *ptr = (TableList *) session->mem.calloc(sizeof(TableList));
1002
929
 
1003
 
  if (table->db.str)
 
930
  if (table->db.data())
1004
931
  {
1005
932
    ptr->setIsFqtn(true);
1006
 
    ptr->setSchemaName(table->db.str);
1007
 
    ptr->db_length= table->db.length;
 
933
    ptr->setSchemaName(table->db.data());
1008
934
  }
1009
 
  else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
1010
 
    return NULL;
1011
 
  else
 
935
  else 
 
936
  {
 
937
    str_ref schema = lex->session->copy_db_to();
 
938
    if (schema.empty())
 
939
      return NULL;
1012
940
    ptr->setIsFqtn(false);
 
941
    ptr->setSchemaName(schema.data());
 
942
  }
1013
943
 
1014
944
  ptr->alias= alias_str;
1015
945
  ptr->setIsAlias(alias ? true : false);
1016
 
  ptr->setTableName(table->table.str);
1017
 
  ptr->table_name_length=table->table.length;
 
946
  ptr->setTableName(table->table.data());
1018
947
  ptr->lock_type=   lock_type;
1019
948
  ptr->force_index= table_options.test(TL_OPTION_FORCE_INDEX);
1020
949
  ptr->ignore_leaves= table_options.test(TL_OPTION_IGNORE_LEAVES);
1021
950
  ptr->derived=     table->sel;
1022
951
  ptr->select_lex=  lex->current_select;
1023
952
  ptr->index_hints= index_hints_arg;
1024
 
  ptr->option= option ? option->str : 0;
 
953
  ptr->option= option ? option->data() : NULL;
1025
954
  /* check that used name is unique */
1026
955
  if (lock_type != TL_IGNORE)
1027
956
  {
1028
957
    TableList *first_table= (TableList*) table_list.first;
1029
 
    for (TableList *tables= first_table ;
1030
 
         tables ;
1031
 
         tables=tables->next_local)
 
958
    for (TableList *tables= first_table; tables; tables= tables->next_local)
1032
959
    {
1033
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1034
 
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
 
960
      if (not table_alias_charset->strcasecmp(alias_str, tables->alias) &&
 
961
        not system_charset_info->strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
1035
962
      {
1036
 
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1037
 
        return NULL;
 
963
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
 
964
        return NULL;
1038
965
      }
1039
966
    }
1040
967
  }
1046
973
      element
1047
974
      We don't use the offsetof() macro here to avoid warnings from gcc
1048
975
    */
1049
 
    previous_table_ref= (TableList*) ((char*) table_list.next -
1050
 
                                       ((char*) &(ptr->next_local) -
1051
 
                                        (char*) ptr));
 
976
    previous_table_ref= (TableList*) ((char*) table_list.next - ((char*) &(ptr->next_local) - (char*) ptr));
1052
977
    /*
1053
978
      Set next_name_resolution_table of the previous table reference to point
1054
979
      to the current table reference. In effect the list
1092
1017
    1   otherwise
1093
1018
*/
1094
1019
 
1095
 
bool Select_Lex::init_nested_join(Session *session)
 
1020
void Select_Lex::init_nested_join(Session& session)
1096
1021
{
1097
 
  TableList *ptr;
1098
 
  NestedJoin *nested_join;
1099
 
 
1100
 
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1101
 
                                       sizeof(NestedJoin))))
1102
 
    return true;
 
1022
  TableList* ptr= (TableList*) session.mem.calloc(ALIGN_SIZE(sizeof(TableList)) + sizeof(NestedJoin));
1103
1023
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1104
 
  nested_join= ptr->getNestedJoin();
 
1024
  NestedJoin* nested_join= ptr->getNestedJoin();
1105
1025
  join_list->push_front(ptr);
1106
1026
  ptr->setEmbedding(embedding);
1107
1027
  ptr->setJoinList(join_list);
1109
1029
  embedding= ptr;
1110
1030
  join_list= &nested_join->join_list;
1111
1031
  join_list->clear();
1112
 
  return false;
1113
1032
}
1114
1033
 
1115
1034
 
1127
1046
    - 0, otherwise
1128
1047
*/
1129
1048
 
1130
 
TableList *Select_Lex::end_nested_join(Session *)
 
1049
TableList *Select_Lex::end_nested_join()
1131
1050
{
1132
 
  TableList *ptr;
1133
 
  NestedJoin *nested_join;
1134
 
 
1135
1051
  assert(embedding);
1136
 
  ptr= embedding;
 
1052
  TableList* ptr= embedding;
1137
1053
  join_list= ptr->getJoinList();
1138
1054
  embedding= ptr->getEmbedding();
1139
 
  nested_join= ptr->getNestedJoin();
 
1055
  NestedJoin* nested_join= ptr->getNestedJoin();
1140
1056
  if (nested_join->join_list.size() == 1)
1141
1057
  {
1142
1058
    TableList *embedded= &nested_join->join_list.front();
1144
1060
    embedded->setJoinList(join_list);
1145
1061
    embedded->setEmbedding(embedding);
1146
1062
    join_list->push_front(embedded);
1147
 
    ptr= embedded;
 
1063
    return embedded;
1148
1064
  }
1149
 
  else if (nested_join->join_list.size() == 0)
 
1065
  else if (not nested_join->join_list.size())
1150
1066
  {
1151
1067
    join_list->pop();
1152
 
    ptr= NULL;                                     // return value
 
1068
    return NULL;
1153
1069
  }
1154
1070
  return ptr;
1155
1071
}
1170
1086
 
1171
1087
TableList *Select_Lex::nest_last_join(Session *session)
1172
1088
{
1173
 
  TableList *ptr;
1174
 
  NestedJoin *nested_join;
1175
 
  List<TableList> *embedded_list;
1176
 
 
1177
 
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1178
 
                                          sizeof(NestedJoin))))
1179
 
    return NULL;
 
1089
  TableList* ptr= (TableList*) session->mem.calloc(ALIGN_SIZE(sizeof(TableList)) + sizeof(NestedJoin));
1180
1090
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1181
 
  nested_join= ptr->getNestedJoin();
 
1091
  NestedJoin* nested_join= ptr->getNestedJoin();
1182
1092
  ptr->setEmbedding(embedding);
1183
1093
  ptr->setJoinList(join_list);
1184
1094
  ptr->alias= (char*) "(nest_last_join)";
1185
 
  embedded_list= &nested_join->join_list;
 
1095
  List<TableList>* embedded_list= &nested_join->join_list;
1186
1096
  embedded_list->clear();
1187
1097
 
1188
1098
  for (uint32_t i=0; i < 2; i++)
1286
1196
 
1287
1197
void Select_Lex::set_lock_for_tables(thr_lock_type lock_type)
1288
1198
{
1289
 
  for (TableList *tables= (TableList*) table_list.first;
1290
 
       tables;
1291
 
       tables= tables->next_local)
 
1199
  for (TableList *tables= (TableList*) table_list.first; tables; tables= tables->next_local)
1292
1200
  {
1293
1201
    tables->lock_type= lock_type;
1294
1202
  }
1326
1234
  Select_Lex *first_sl= first_select();
1327
1235
  assert(!fake_select_lex);
1328
1236
 
1329
 
  if (!(fake_select_lex= new (session_arg->mem_root) Select_Lex()))
1330
 
      return(1);
1331
 
  fake_select_lex->include_standalone(this,
1332
 
                                      (Select_Lex_Node**)&fake_select_lex);
 
1237
  fake_select_lex= new (session_arg->mem_root) Select_Lex();
 
1238
  fake_select_lex->include_standalone(this, (Select_Lex_Node**)&fake_select_lex);
1333
1239
  fake_select_lex->select_number= INT_MAX;
1334
1240
  fake_select_lex->parent_lex= &session_arg->lex(); /* Used in init_query. */
1335
1241
  fake_select_lex->make_empty_select();
1354
1260
    session_arg->lex().current_select= fake_select_lex;
1355
1261
  }
1356
1262
  session_arg->lex().pop_context();
1357
 
  return(0);
 
1263
  return 0;
1358
1264
}
1359
1265
 
1360
1266
 
1377
1283
    true   if a memory allocation error occured
1378
1284
*/
1379
1285
 
1380
 
bool
1381
 
push_new_name_resolution_context(Session *session,
1382
 
                                 TableList *left_op, TableList *right_op)
 
1286
void push_new_name_resolution_context(Session& session, TableList& left_op, TableList& right_op)
1383
1287
{
1384
 
  Name_resolution_context *on_context;
1385
 
  if (!(on_context= new (session->mem_root) Name_resolution_context))
1386
 
    return true;
 
1288
  Name_resolution_context *on_context= new (session.mem_root) Name_resolution_context;
1387
1289
  on_context->init();
1388
 
  on_context->first_name_resolution_table=
1389
 
    left_op->first_leaf_for_name_resolution();
1390
 
  on_context->last_name_resolution_table=
1391
 
    right_op->last_leaf_for_name_resolution();
1392
 
  return session->lex().push_context(on_context);
 
1290
  on_context->first_name_resolution_table= left_op.first_leaf_for_name_resolution();
 
1291
  on_context->last_name_resolution_table= right_op.last_leaf_for_name_resolution();
 
1292
  session.lex().push_context(on_context);
1393
1293
}
1394
1294
 
1395
1295
 
1477
1377
    1   error   ; In this case the error messege is sent to the client
1478
1378
*/
1479
1379
 
1480
 
bool check_simple_select(Session::pointer session)
 
1380
bool check_simple_select(Session* session)
1481
1381
{
1482
1382
  if (session->lex().current_select != &session->lex().select_lex)
1483
1383
  {
1544
1444
  if (session->lex().select_lex.item_list.size() != session->lex().value_list.size())
1545
1445
  {
1546
1446
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1547
 
    return(true);
 
1447
    return true;
1548
1448
  }
1549
1449
 
1550
1450
  if (session->lex().select_lex.table_list.size() > 1)
1556
1456
    if (msg)
1557
1457
    {
1558
1458
      my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
1559
 
      return(true);
 
1459
      return true;
1560
1460
    }
1561
1461
  }
1562
 
  return(false);
 
1462
  return false;
1563
1463
}
1564
1464
 
1565
1465
 
1584
1484
  if (session->lex().update_list.size() != session->lex().value_list.size())
1585
1485
  {
1586
1486
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1587
 
    return(true);
 
1487
    return true;
1588
1488
  }
1589
 
  return(false);
 
1489
  return false;
1590
1490
}
1591
1491
 
1592
1492
 
1640
1540
*/
1641
1541
 
1642
1542
 
1643
 
bool check_string_char_length(LEX_STRING *str, const char *err_msg,
1644
 
                              uint32_t max_char_length, const CHARSET_INFO * const cs,
 
1543
bool check_string_char_length(str_ref str, const char *err_msg,
 
1544
                              uint32_t max_char_length, const charset_info_st * const cs,
1645
1545
                              bool no_error)
1646
1546
{
1647
1547
  int well_formed_error;
1648
 
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1649
 
                                      max_char_length, &well_formed_error);
 
1548
  uint32_t res= cs->cset->well_formed_len(*cs, str, max_char_length, &well_formed_error);
1650
1549
 
1651
 
  if (!well_formed_error &&  str->length == res)
 
1550
  if (!well_formed_error && str.size() == res)
1652
1551
    return false;
1653
1552
 
1654
 
  if (!no_error)
1655
 
    my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_char_length);
 
1553
  if (not no_error)
 
1554
    my_error(ER_WRONG_STRING_LENGTH, MYF(0), str.data(), err_msg, max_char_length);
1656
1555
  return true;
1657
1556
}
1658
1557
 
1659
1558
 
1660
 
bool check_identifier_name(LEX_STRING *str, error_t err_code,
1661
 
                           uint32_t max_char_length,
1662
 
                           const char *param_for_err_msg)
 
1559
bool check_identifier_name(str_ref str, error_t err_code)
1663
1560
{
 
1561
  uint32_t max_char_length= NAME_CHAR_LEN;
1664
1562
  /*
1665
1563
    We don't support non-BMP characters in identifiers at the moment,
1666
1564
    so they should be prohibited until such support is done.
1667
1565
    This is why we use the 3-byte utf8 to check well-formedness here.
1668
1566
  */
1669
 
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
 
1567
  const charset_info_st * const cs= &my_charset_utf8mb4_general_ci;
1670
1568
 
1671
1569
  int well_formed_error;
1672
 
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1673
 
                                      max_char_length, &well_formed_error);
 
1570
  uint32_t res= cs->cset->well_formed_len(*cs, str, max_char_length, &well_formed_error);
1674
1571
 
1675
1572
  if (well_formed_error)
1676
1573
  {
1677
 
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str->str);
 
1574
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str.data());
1678
1575
    return true;
1679
1576
  }
1680
1577
 
1681
 
  if (str->length == res)
 
1578
  if (str.size() == res)
1682
1579
    return false;
1683
1580
 
1684
1581
  switch (err_code)
1686
1583
  case EE_OK:
1687
1584
    break;
1688
1585
  case ER_WRONG_STRING_LENGTH:
1689
 
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
 
1586
    my_error(err_code, MYF(0), str.data(), "", max_char_length);
1690
1587
    break;
1691
1588
  case ER_TOO_LONG_IDENT:
1692
 
    my_error(err_code, MYF(0), str->str);
 
1589
    my_error(err_code, MYF(0), str.data());
1693
1590
    break;
1694
1591
  default:
1695
 
    assert(0);
1696
 
    break;
 
1592
    assert(false);
1697
1593
  }
1698
1594
 
1699
1595
  return true;