~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Brian Aker
  • Date: 2010-11-04 20:51:35 UTC
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: brian@tangent.org-20101104205135-vci0i54v1d0loai8
Merge of partial set of patches for locks.

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
 
18
18
#define DRIZZLE_LEX 1
19
19
 
20
 
#include <drizzled/item/num.h>
21
 
#include <drizzled/abort_exception.h>
22
20
#include <drizzled/my_hash.h>
23
21
#include <drizzled/error.h>
24
22
#include <drizzled/nested_join.h>
25
23
#include <drizzled/query_id.h>
26
 
#include <drizzled/transaction_services.h>
 
24
#include "drizzled/transaction_services.h"
27
25
#include <drizzled/sql_parse.h>
28
26
#include <drizzled/data_home.h>
29
27
#include <drizzled/sql_base.h>
30
28
#include <drizzled/show.h>
 
29
#include <drizzled/db.h>
31
30
#include <drizzled/function/time/unix_timestamp.h>
32
31
#include <drizzled/function/get_system_var.h>
33
32
#include <drizzled/item/cmpfunc.h>
34
33
#include <drizzled/item/null.h>
35
34
#include <drizzled/session.h>
36
 
#include <drizzled/session/cache.h>
37
35
#include <drizzled/sql_load.h>
38
36
#include <drizzled/lock.h>
39
37
#include <drizzled/select_send.h>
40
38
#include <drizzled/plugin/client.h>
41
39
#include <drizzled/statement.h>
42
40
#include <drizzled/statement/alter_table.h>
43
 
#include <drizzled/probes.h>
44
 
#include <drizzled/global_charset_info.h>
45
 
 
46
 
#include <drizzled/plugin/logging.h>
47
 
#include <drizzled/plugin/query_rewrite.h>
48
 
#include <drizzled/plugin/query_cache.h>
49
 
#include <drizzled/plugin/authorization.h>
50
 
#include <drizzled/optimizer/explain_plan.h>
51
 
#include <drizzled/pthread_globals.h>
52
 
#include <drizzled/plugin/event_observer.h>
53
 
#include <drizzled/visibility.h>
54
 
 
55
 
#include <drizzled/schema.h>
 
41
#include "drizzled/probes.h"
 
42
#include "drizzled/session_list.h"
 
43
#include "drizzled/global_charset_info.h"
 
44
 
 
45
#include "drizzled/plugin/logging.h"
 
46
#include "drizzled/plugin/query_rewrite.h"
 
47
#include "drizzled/plugin/query_cache.h"
 
48
#include "drizzled/plugin/authorization.h"
 
49
#include "drizzled/optimizer/explain_plan.h"
 
50
#include "drizzled/pthread_globals.h"
 
51
#include "drizzled/plugin/event_observer.h"
56
52
 
57
53
#include <limits.h>
58
54
 
59
55
#include <bitset>
60
56
#include <algorithm>
61
 
#include <boost/date_time.hpp>
62
 
#include <drizzled/internal/my_sys.h>
 
57
 
 
58
#include "drizzled/internal/my_sys.h"
63
59
 
64
60
using namespace std;
65
61
 
66
 
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
 
62
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
67
63
 
68
64
namespace drizzled
69
65
{
70
66
 
71
67
/* Prototypes */
72
 
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
 
68
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
73
69
static bool parse_sql(Session *session, Lex_input_stream *lip);
74
 
void parse(Session *session, const char *inBuf, uint32_t length);
 
70
void mysql_parse(Session *session, const char *inBuf, uint32_t length);
75
71
 
76
72
/**
77
73
  @defgroup Runtime_Environment Runtime Environment
81
77
extern size_t my_thread_stack_size;
82
78
extern const CHARSET_INFO *character_set_filesystem;
83
79
 
84
 
namespace
85
 
{
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
 
  "Error"  // Last command number
 
80
const LEX_STRING command_name[COM_END+1]={
 
81
  { C_STRING_WITH_LEN("Sleep") },
 
82
  { C_STRING_WITH_LEN("Quit") },
 
83
  { C_STRING_WITH_LEN("Init DB") },
 
84
  { C_STRING_WITH_LEN("Query") },
 
85
  { C_STRING_WITH_LEN("Shutdown") },
 
86
  { C_STRING_WITH_LEN("Connect") },
 
87
  { C_STRING_WITH_LEN("Ping") },
 
88
  { C_STRING_WITH_LEN("Error") }  // Last command number
96
89
};
97
90
 
98
 
}
99
 
 
100
91
const char *xa_state_names[]={
101
92
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
102
93
};
115
106
*/
116
107
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
117
108
 
118
 
const std::string &getCommandName(const enum_server_command& command)
119
 
{
120
 
  return command_name[command];
121
 
}
122
 
 
123
109
void init_update_queries(void)
124
110
{
125
111
  uint32_t x;
168
154
                         can be zero.
169
155
 
170
156
  @todo
171
 
    set session->getLex()->sql_command to SQLCOM_END here.
 
157
    set session->lex->sql_command to SQLCOM_END here.
172
158
  @todo
173
159
    The following has to be changed to an 8 byte integer
174
160
 
184
170
  bool error= 0;
185
171
  Query_id &query_id= Query_id::get_query_id();
186
172
 
187
 
  DRIZZLE_COMMAND_START(session->thread_id, command);
 
173
  DRIZZLE_COMMAND_START(session->thread_id,
 
174
                        command);
188
175
 
189
176
  session->command= command;
190
 
  session->getLex()->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
177
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
191
178
  session->set_time();
192
179
  session->setQueryId(query_id.value());
193
180
 
201
188
    query_id.next();
202
189
  }
203
190
 
204
 
  /* @todo set session->getLex()->sql_command to SQLCOM_END here */
 
191
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
205
192
 
206
193
  plugin::Logging::preDo(session);
207
194
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
222
209
 
223
210
    string tmp(packet, packet_length);
224
211
 
225
 
    identifier::Schema identifier(tmp);
 
212
    SchemaIdentifier identifier(tmp);
226
213
 
227
 
    if (not schema::change(*session, identifier))
 
214
    if (not mysql_change_db(session, identifier))
228
215
    {
229
216
      session->my_ok();
230
217
    }
232
219
  }
233
220
  case COM_QUERY:
234
221
  {
235
 
    if (not session->readAndStoreQuery(packet, packet_length))
 
222
    if (! session->readAndStoreQuery(packet, packet_length))
236
223
      break;                                    // fatal error is set
237
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
 
224
    DRIZZLE_QUERY_START(session->query.c_str(),
238
225
                        session->thread_id,
239
 
                        const_cast<const char *>(session->schema()->c_str()));
 
226
                        const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
240
227
 
241
 
    parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
228
    plugin::QueryRewriter::rewriteQuery(session->db, session->query);
 
229
    mysql_parse(session, session->query.c_str(), session->query.length());
242
230
 
243
231
    break;
244
232
  }
271
259
  /* If commit fails, we should be able to reset the OK status. */
272
260
  session->main_da.can_overwrite_status= true;
273
261
  TransactionServices &transaction_services= TransactionServices::singleton();
274
 
  transaction_services.autocommitOrRollback(*session, session->is_error());
 
262
  transaction_services.autocommitOrRollback(session, session->is_error());
275
263
  session->main_da.can_overwrite_status= false;
276
264
 
277
265
  session->transaction.stmt.reset();
283
271
    if (! session->main_da.is_set())
284
272
      session->send_kill_message();
285
273
  }
286
 
  if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
 
274
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
287
275
  {
288
 
    session->setKilled(Session::NOT_KILLED);
 
276
    session->killed= Session::NOT_KILLED;
289
277
    session->setAbort(false);
290
278
  }
291
279
 
296
284
  {
297
285
  case Diagnostics_area::DA_ERROR:
298
286
    /* The query failed, send error to log and abort bootstrap. */
299
 
    session->getClient()->sendError(session->main_da.sql_errno(),
 
287
    session->client->sendError(session->main_da.sql_errno(),
300
288
                               session->main_da.message());
301
289
    break;
302
290
 
303
291
  case Diagnostics_area::DA_EOF:
304
 
    session->getClient()->sendEOF();
 
292
    session->client->sendEOF();
305
293
    break;
306
294
 
307
295
  case Diagnostics_area::DA_OK:
308
 
    session->getClient()->sendOK();
 
296
    session->client->sendOK();
309
297
    break;
310
298
 
311
299
  case Diagnostics_area::DA_DISABLED:
313
301
 
314
302
  case Diagnostics_area::DA_EMPTY:
315
303
  default:
316
 
    session->getClient()->sendOK();
 
304
    session->client->sendOK();
317
305
    break;
318
306
  }
319
307
 
332
320
  /* Store temp state for processlist */
333
321
  session->set_proc_info("cleaning up");
334
322
  session->command= COM_SLEEP;
335
 
  session->resetQueryString();
 
323
  memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
 
324
  session->query.clear();
336
325
 
337
326
  session->set_proc_info(NULL);
338
327
  session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
443
432
    true        Error
444
433
*/
445
434
 
446
 
static int execute_command(Session *session)
 
435
static int
 
436
mysql_execute_command(Session *session)
447
437
{
448
438
  bool res= false;
449
 
 
 
439
  LEX  *lex= session->lex;
450
440
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
451
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
452
 
 
 
441
  Select_Lex *select_lex= &lex->select_lex;
453
442
  /* list of all tables in query */
454
443
  TableList *all_tables;
 
444
  /* A peek into the query string */
 
445
  size_t proc_info_len= session->query.length() > PROCESS_LIST_WIDTH ?
 
446
                        PROCESS_LIST_WIDTH : session->query.length();
 
447
 
 
448
  memcpy(session->process_list_info, session->query.c_str(), proc_info_len);
 
449
  session->process_list_info[proc_info_len]= '\0';
455
450
 
456
451
  /*
457
452
    In many cases first table of main Select_Lex have special meaning =>
468
463
    assert(first_table == all_tables);
469
464
    assert(first_table == all_tables && first_table != 0);
470
465
  */
471
 
  session->getLex()->first_lists_tables_same();
472
 
 
 
466
  lex->first_lists_tables_same();
473
467
  /* should be assigned after making first tables same */
474
 
  all_tables= session->getLex()->query_tables;
475
 
 
 
468
  all_tables= lex->query_tables;
476
469
  /* set context for commands which do not use setup_tables */
477
 
  select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
 
470
  select_lex->
 
471
    context.resolve_in_table_list_only((TableList*)select_lex->
 
472
                                       table_list.first);
478
473
 
479
474
  /*
480
475
    Reset warning count for each query that uses tables
483
478
    variables, but for now this is probably good enough.
484
479
    Don't reset warnings when executing a stored routine.
485
480
  */
486
 
  if (all_tables || ! session->getLex()->is_single_level_stmt())
 
481
  if (all_tables || ! lex->is_single_level_stmt())
487
482
  {
488
483
    drizzle_reset_errors(session, 0);
489
484
  }
490
485
 
491
486
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
492
487
 
493
 
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
494
 
      && ! session->inTransaction()
495
 
      && session->getLex()->statement->isTransactional())
496
 
  {
497
 
    if (session->startTransaction() == false)
498
 
    {
499
 
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
500
 
      return true;
501
 
    }
502
 
  }
503
 
 
504
488
  /* now we are ready to execute the statement */
505
 
  res= session->getLex()->statement->execute();
 
489
  res= lex->statement->execute();
506
490
  session->set_proc_info("query end");
507
491
  /*
508
492
    The return value for ROW_COUNT() is "implementation dependent" if the
510
494
    wants. We also keep the last value in case of SQLCOM_CALL or
511
495
    SQLCOM_EXECUTE.
512
496
  */
513
 
  if (! (sql_command_flags[session->getLex()->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
497
  if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
514
498
  {
515
499
    session->row_count_func= -1;
516
500
  }
519
503
}
520
504
bool execute_sqlcom_select(Session *session, TableList *all_tables)
521
505
{
522
 
  LEX   *lex= session->getLex();
 
506
  LEX   *lex= session->lex;
523
507
  select_result *result=lex->result;
524
508
  bool res= false;
525
509
  /* assign global limit variable if limit is not given */
529
513
      param->select_limit=
530
514
        new Item_int((uint64_t) session->variables.select_limit);
531
515
  }
532
 
 
533
 
  if (all_tables
534
 
      && ! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
535
 
      && ! session->inTransaction()
536
 
      && ! lex->statement->isShow())
537
 
  {
538
 
    if (session->startTransaction() == false)
539
 
    {
540
 
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
541
 
      return true;
542
 
    }
543
 
  }
544
 
 
545
516
  if (not (res= session->openTablesLock(all_tables)))
546
517
  {
547
518
    if (lex->describe)
556
527
        return true;
557
528
      session->send_explain_fields(result);
558
529
      optimizer::ExplainPlan planner;
559
 
      res= planner.explainUnion(session, &session->getLex()->unit, result);
 
530
      res= planner.explainUnion(session, &session->lex->unit, result);
560
531
      if (lex->describe & DESCRIBE_EXTENDED)
561
532
      {
562
533
        char buff[1024];
563
534
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
564
535
        str.length(0);
565
 
        session->getLex()->unit.print(&str, QT_ORDINARY);
 
536
        session->lex->unit.print(&str, QT_ORDINARY);
566
537
        str.append('\0');
567
538
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
568
539
                     ER_YES, str.ptr());
571
542
        result->abort();
572
543
      else
573
544
        result->send_eof();
574
 
 
575
545
      delete result;
576
546
    }
577
547
    else
596
566
#define MY_YACC_INIT 1000                       // Start with big alloc
597
567
#define MY_YACC_MAX  32000                      // Because of 'short'
598
568
 
599
 
bool my_yyoverflow(short **yyss, ParserType **yyvs, ulong *yystacksize)
 
569
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
600
570
{
601
 
  LEX   *lex= current_session->getLex();
 
571
  LEX   *lex= current_session->lex;
602
572
  ulong old_info=0;
603
573
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
604
574
    return 1;
621
591
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
622
592
  }
623
593
  *yyss=(short*) lex->yacc_yyss;
624
 
  *yyvs=(ParserType*) lex->yacc_yyvs;
 
594
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
625
595
  return 0;
626
596
}
627
597
 
628
598
 
629
599
void
630
 
init_select(LEX *lex)
 
600
mysql_init_select(LEX *lex)
631
601
{
632
602
  Select_Lex *select_lex= lex->current_select;
633
603
  select_lex->init_select();
641
611
 
642
612
 
643
613
bool
644
 
new_select(LEX *lex, bool move_down)
 
614
mysql_new_select(LEX *lex, bool move_down)
645
615
{
646
616
  Select_Lex *select_lex;
647
617
  Session *session= lex->session;
648
618
 
649
619
  if (!(select_lex= new (session->mem_root) Select_Lex()))
650
 
    return true;
651
 
 
 
620
    return(1);
652
621
  select_lex->select_number= ++session->select_number;
653
622
  select_lex->parent_lex= lex; /* Used in init_query. */
654
623
  select_lex->init_query();
655
624
  select_lex->init_select();
656
625
  lex->nest_level++;
657
 
 
658
626
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
659
627
  {
660
628
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
661
629
    return(1);
662
630
  }
663
 
 
664
631
  select_lex->nest_level= lex->nest_level;
665
632
  if (move_down)
666
633
  {
688
655
    if (lex->current_select->order_list.first && !lex->current_select->braces)
689
656
    {
690
657
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
691
 
      return true;
 
658
      return(1);
692
659
    }
693
 
 
694
660
    select_lex->include_neighbour(lex->current_select);
695
661
    Select_Lex_Unit *unit= select_lex->master_unit();
696
 
 
697
 
    if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
698
 
      return true;
699
 
 
 
662
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
663
      return(1);
700
664
    select_lex->context.outer_context=
701
665
                unit->first_select()->context.outer_context;
702
666
  }
709
673
    list
710
674
  */
711
675
  select_lex->context.resolve_in_select_list= true;
712
 
 
713
 
  return false;
 
676
  return(0);
714
677
}
715
678
 
716
679
/**
723
686
  @param var_name               Variable name
724
687
*/
725
688
 
726
 
void create_select_for_variable(Session *session, const char *var_name)
 
689
void create_select_for_variable(const char *var_name)
727
690
{
 
691
  Session *session;
728
692
  LEX *lex;
729
693
  LEX_STRING tmp, null_lex_string;
730
694
  Item *var;
731
695
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
732
696
  char *end= buff;
733
697
 
734
 
  lex= session->getLex();
735
 
  init_select(lex);
 
698
  session= current_session;
 
699
  lex= session->lex;
 
700
  mysql_init_select(lex);
736
701
  lex->sql_command= SQLCOM_SELECT;
737
702
  tmp.str= (char*) var_name;
738
703
  tmp.length=strlen(var_name);
747
712
    var->set_name(buff, end-buff, system_charset_info);
748
713
    session->add_item_to_list(var);
749
714
  }
 
715
  return;
750
716
}
751
717
 
752
718
 
758
724
  @param       length  Length of the query text
759
725
*/
760
726
 
761
 
void parse(Session *session, const char *inBuf, uint32_t length)
 
727
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
762
728
{
763
 
  session->getLex()->start(session);
764
 
 
 
729
  uint64_t start_time= my_getsystime();
 
730
  session->lex->start(session);
765
731
  session->reset_for_next_command();
766
732
  /* Check if the Query is Cached if and return true if yes
767
733
   * TODO the plugin has to make sure that the query is cacheble
776
742
  {
777
743
    return;
778
744
  }
779
 
  LEX *lex= session->getLex();
 
745
  LEX *lex= session->lex;
780
746
  Lex_input_stream lip(session, inBuf, length);
781
747
  bool err= parse_sql(session, &lip);
782
748
  if (!err)
783
749
  {
784
750
    {
785
 
      if (not session->is_error())
 
751
      if (! session->is_error())
786
752
      {
787
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
 
753
        DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
788
754
                                 session->thread_id,
789
 
                                 const_cast<const char *>(session->schema()->c_str()));
 
755
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
790
756
        // Implement Views here --Brian
791
757
        /* Actually execute the query */
792
758
        try 
793
759
        {
794
 
          execute_command(session);
 
760
          mysql_execute_command(session);
795
761
        }
796
762
        catch (...)
797
763
        {
798
764
          // Just try to catch any random failures that could have come
799
765
          // during execution.
800
 
          DRIZZLE_ABORT;
801
766
        }
802
767
        DRIZZLE_QUERY_EXEC_DONE(0);
803
768
      }
811
776
  session->set_proc_info("freeing items");
812
777
  session->end_statement();
813
778
  session->cleanup_after_query();
814
 
  session->set_end_timer();
 
779
  session->status_var.execution_time_nsec+= my_getsystime() - start_time;
815
780
}
816
781
 
817
782
 
833
798
                       List<String> *interval_list, const CHARSET_INFO * const cs)
834
799
{
835
800
  register CreateField *new_field;
836
 
  LEX  *lex= session->getLex();
 
801
  LEX  *lex= session->lex;
837
802
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
838
803
 
839
804
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
847
812
                      &default_key_create_info,
848
813
                      0, lex->col_list);
849
814
    statement->alter_info.key_list.push_back(key);
850
 
    lex->col_list.clear();
 
815
    lex->col_list.empty();
851
816
  }
852
817
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
853
818
  {
857
822
                 &default_key_create_info, 0,
858
823
                 lex->col_list);
859
824
    statement->alter_info.key_list.push_back(key);
860
 
    lex->col_list.clear();
 
825
    lex->col_list.empty();
861
826
  }
862
827
 
863
828
  if (default_value)
871
836
    */
872
837
    if (default_value->type() == Item::FUNC_ITEM &&
873
838
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
874
 
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
 
839
         type == DRIZZLE_TYPE_TIMESTAMP))
875
840
    {
876
841
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
877
842
      return true;
879
844
    else if (default_value->type() == Item::NULL_ITEM)
880
845
    {
881
846
      default_value= 0;
882
 
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
 
847
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
 
848
          NOT_NULL_FLAG)
883
849
      {
884
850
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
885
851
        return true;
892
858
    }
893
859
  }
894
860
 
895
 
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
 
861
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
896
862
  {
897
863
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
898
864
    return true;
911
877
}
912
878
 
913
879
 
 
880
/** Store position for column in ALTER TABLE .. ADD column. */
 
881
 
 
882
void store_position_for_column(const char *name)
 
883
{
 
884
  current_session->lex->last_field->after=const_cast<char*> (name);
 
885
}
 
886
 
914
887
/**
915
888
  Add a table to list of used tables.
916
889
 
931
904
*/
932
905
 
933
906
TableList *Select_Lex::add_table_to_list(Session *session,
934
 
                                         Table_ident *table,
935
 
                                         LEX_STRING *alias,
936
 
                                         const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
937
 
                                         thr_lock_type lock_type,
938
 
                                         List<Index_hint> *index_hints_arg,
 
907
                                                             Table_ident *table,
 
908
                                                             LEX_STRING *alias,
 
909
                                                             const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
 
910
                                                             thr_lock_type lock_type,
 
911
                                                             List<Index_hint> *index_hints_arg,
939
912
                                         LEX_STRING *option)
940
913
{
941
914
  TableList *ptr;
942
915
  TableList *previous_table_ref; /* The table preceding the current one. */
943
916
  char *alias_str;
944
 
  LEX *lex= session->getLex();
 
917
  LEX *lex= session->lex;
945
918
 
946
919
  if (!table)
947
920
    return NULL;                                // End of memory
957
930
  {
958
931
    my_casedn_str(files_charset_info, table->db.str);
959
932
 
960
 
    identifier::Schema schema_identifier(string(table->db.str));
961
 
    if (not schema::check(*session, schema_identifier))
 
933
    SchemaIdentifier schema_identifier(string(table->db.str));
 
934
    if (not check_db_name(session, schema_identifier))
962
935
    {
963
936
 
964
937
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
974
947
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
975
948
      return NULL;
976
949
    }
977
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
 
950
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
978
951
      return NULL;
979
952
  }
980
953
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
993
966
 
994
967
  ptr->alias= alias_str;
995
968
  ptr->setIsAlias(alias ? true : false);
 
969
  if (table->table.length)
 
970
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
996
971
  ptr->setTableName(table->table.str);
997
972
  ptr->table_name_length=table->table.length;
998
973
  ptr->lock_type=   lock_type;
1010
985
         tables ;
1011
986
         tables=tables->next_local)
1012
987
    {
1013
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1014
 
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
 
988
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
 
989
          !strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
1015
990
      {
1016
991
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1017
992
        return NULL;
1075
1050
bool Select_Lex::init_nested_join(Session *session)
1076
1051
{
1077
1052
  TableList *ptr;
1078
 
  NestedJoin *nested_join;
 
1053
  nested_join_st *nested_join;
1079
1054
 
1080
1055
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1081
 
                                       sizeof(NestedJoin))))
 
1056
                                       sizeof(nested_join_st))))
1082
1057
    return true;
1083
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1058
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1084
1059
  nested_join= ptr->getNestedJoin();
1085
1060
  join_list->push_front(ptr);
1086
1061
  ptr->setEmbedding(embedding);
1088
1063
  ptr->alias= (char*) "(nested_join)";
1089
1064
  embedding= ptr;
1090
1065
  join_list= &nested_join->join_list;
1091
 
  join_list->clear();
 
1066
  join_list->empty();
1092
1067
  return false;
1093
1068
}
1094
1069
 
1110
1085
TableList *Select_Lex::end_nested_join(Session *)
1111
1086
{
1112
1087
  TableList *ptr;
1113
 
  NestedJoin *nested_join;
 
1088
  nested_join_st *nested_join;
1114
1089
 
1115
1090
  assert(embedding);
1116
1091
  ptr= embedding;
1151
1126
TableList *Select_Lex::nest_last_join(Session *session)
1152
1127
{
1153
1128
  TableList *ptr;
1154
 
  NestedJoin *nested_join;
 
1129
  nested_join_st *nested_join;
1155
1130
  List<TableList> *embedded_list;
1156
1131
 
1157
1132
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1158
 
                                          sizeof(NestedJoin))))
 
1133
                                          sizeof(nested_join_st))))
1159
1134
    return NULL;
1160
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1135
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1161
1136
  nested_join= ptr->getNestedJoin();
1162
1137
  ptr->setEmbedding(embedding);
1163
1138
  ptr->setJoinList(join_list);
1164
1139
  ptr->alias= (char*) "(nest_last_join)";
1165
1140
  embedded_list= &nested_join->join_list;
1166
 
  embedded_list->clear();
 
1141
  embedded_list->empty();
1167
1142
 
1168
1143
  for (uint32_t i=0; i < 2; i++)
1169
1144
  {
1311
1286
  fake_select_lex->include_standalone(this,
1312
1287
                                      (Select_Lex_Node**)&fake_select_lex);
1313
1288
  fake_select_lex->select_number= INT_MAX;
1314
 
  fake_select_lex->parent_lex= session_arg->getLex(); /* Used in init_query. */
 
1289
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1315
1290
  fake_select_lex->make_empty_select();
1316
1291
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1317
1292
  fake_select_lex->select_limit= 0;
1331
1306
    */
1332
1307
    global_parameters= fake_select_lex;
1333
1308
    fake_select_lex->no_table_names_allowed= 1;
1334
 
    session_arg->getLex()->current_select= fake_select_lex;
 
1309
    session_arg->lex->current_select= fake_select_lex;
1335
1310
  }
1336
 
  session_arg->getLex()->pop_context();
 
1311
  session_arg->lex->pop_context();
1337
1312
  return(0);
1338
1313
}
1339
1314
 
1369
1344
    left_op->first_leaf_for_name_resolution();
1370
1345
  on_context->last_name_resolution_table=
1371
1346
    right_op->last_leaf_for_name_resolution();
1372
 
  return session->getLex()->push_context(on_context);
 
1347
  return session->lex->push_context(on_context);
1373
1348
}
1374
1349
 
1375
1350
 
1449
1424
 
1450
1425
 
1451
1426
/**
 
1427
  kill on thread.
 
1428
 
 
1429
  @param session                        Thread class
 
1430
  @param id                     Thread id
 
1431
  @param only_kill_query        Should it kill the query or the connection
 
1432
 
 
1433
  @note
 
1434
    This is written such that we have a short lock on LOCK_thread_count
 
1435
*/
 
1436
 
 
1437
static unsigned int
 
1438
kill_one_thread(Session *, session_id_t id, bool only_kill_query)
 
1439
{
 
1440
  Session *tmp= NULL;
 
1441
  uint32_t error= ER_NO_SUCH_THREAD;
 
1442
  
 
1443
  {
 
1444
    boost::mutex::scoped_lock scoped(LOCK_thread_count);
 
1445
    for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
 
1446
    {
 
1447
      if ((*it)->thread_id == id)
 
1448
      {
 
1449
        tmp= *it;
 
1450
        tmp->lockForDelete();
 
1451
        break;
 
1452
      }
 
1453
    }
 
1454
  }
 
1455
 
 
1456
  if (tmp)
 
1457
  {
 
1458
 
 
1459
    if (tmp->isViewable())
 
1460
    {
 
1461
      tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
 
1462
      error= 0;
 
1463
    }
 
1464
 
 
1465
    tmp->unlockForDelete();
 
1466
  }
 
1467
  return(error);
 
1468
}
 
1469
 
 
1470
 
 
1471
/*
 
1472
  kills a thread and sends response
 
1473
 
 
1474
  SYNOPSIS
 
1475
    sql_kill()
 
1476
    session                     Thread class
 
1477
    id                  Thread id
 
1478
    only_kill_query     Should it kill the query or the connection
 
1479
*/
 
1480
 
 
1481
void sql_kill(Session *session, int64_t id, bool only_kill_query)
 
1482
{
 
1483
  uint32_t error;
 
1484
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
1485
    session->my_ok();
 
1486
  else
 
1487
    my_error(error, MYF(0), id);
 
1488
}
 
1489
 
 
1490
 
 
1491
/**
1452
1492
  Check if the select is a simple select (not an union).
1453
1493
 
1454
1494
  @retval
1457
1497
    1   error   ; In this case the error messege is sent to the client
1458
1498
*/
1459
1499
 
1460
 
bool check_simple_select(Session::pointer session)
 
1500
bool check_simple_select()
1461
1501
{
1462
 
  if (session->getLex()->current_select != &session->getLex()->select_lex)
 
1502
  Session *session= current_session;
 
1503
  LEX *lex= session->lex;
 
1504
  if (lex->current_select != &lex->select_lex)
1463
1505
  {
1464
1506
    char command[80];
1465
1507
    Lex_input_stream *lip= session->m_lip;
1519
1561
bool update_precheck(Session *session, TableList *)
1520
1562
{
1521
1563
  const char *msg= 0;
1522
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
1564
  LEX *lex= session->lex;
 
1565
  Select_Lex *select_lex= &lex->select_lex;
1523
1566
 
1524
 
  if (session->getLex()->select_lex.item_list.elements != session->getLex()->value_list.elements)
 
1567
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1525
1568
  {
1526
1569
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1527
1570
    return(true);
1528
1571
  }
1529
1572
 
1530
 
  if (session->getLex()->select_lex.table_list.elements > 1)
 
1573
  if (session->lex->select_lex.table_list.elements > 1)
1531
1574
  {
1532
1575
    if (select_lex->order_list.elements)
1533
1576
      msg= "ORDER BY";
1557
1600
 
1558
1601
bool insert_precheck(Session *session, TableList *)
1559
1602
{
 
1603
  LEX *lex= session->lex;
 
1604
 
1560
1605
  /*
1561
1606
    Check that we have modify privileges for the first table and
1562
1607
    select privileges for the rest
1563
1608
  */
1564
 
  if (session->getLex()->update_list.elements != session->getLex()->value_list.elements)
 
1609
  if (lex->update_list.elements != lex->value_list.elements)
1565
1610
  {
1566
1611
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1567
1612
    return(true);
1571
1616
 
1572
1617
 
1573
1618
/**
 
1619
  CREATE TABLE query pre-check.
 
1620
 
 
1621
  @param session                        Thread handler
 
1622
  @param tables         Global table list
 
1623
  @param create_table           Table which will be created
 
1624
 
 
1625
  @retval
 
1626
    false   OK
 
1627
  @retval
 
1628
    true   Error
 
1629
*/
 
1630
 
 
1631
bool create_table_precheck(TableIdentifier &identifier)
 
1632
{
 
1633
  if (not plugin::StorageEngine::canCreateTable(identifier))
 
1634
  {
 
1635
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
 
1636
    return true;
 
1637
  }
 
1638
 
 
1639
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
1640
  {
 
1641
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
 
1642
    return true;
 
1643
  }
 
1644
 
 
1645
  return false;
 
1646
}
 
1647
 
 
1648
 
 
1649
/**
1574
1650
  negate given expression.
1575
1651
 
1576
1652
  @param session  thread handler
1588
1664
  {
1589
1665
    /* it is NOT(NOT( ... )) */
1590
1666
    Item *arg= ((Item_func *) expr)->arguments()[0];
1591
 
    enum_parsing_place place= session->getLex()->current_select->parsing_place;
 
1667
    enum_parsing_place place= session->lex->current_select->parsing_place;
1592
1668
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1593
1669
      return arg;
1594
1670
    /*
1637
1713
}
1638
1714
 
1639
1715
 
1640
 
bool check_identifier_name(LEX_STRING *str, error_t err_code,
 
1716
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
1641
1717
                           uint32_t max_char_length,
1642
1718
                           const char *param_for_err_msg)
1643
1719
{
1663
1739
 
1664
1740
  switch (err_code)
1665
1741
  {
1666
 
  case EE_OK:
 
1742
  case 0:
1667
1743
    break;
1668
1744
  case ER_WRONG_STRING_LENGTH:
1669
1745
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1675
1751
    assert(0);
1676
1752
    break;
1677
1753
  }
1678
 
 
1679
1754
  return true;
1680
1755
}
1681
1756
 
1696
1771
{
1697
1772
  assert(session->m_lip == NULL);
1698
1773
 
1699
 
  DRIZZLE_QUERY_PARSE_START(session->getQueryString()->c_str());
 
1774
  DRIZZLE_QUERY_PARSE_START(session->query.c_str());
1700
1775
 
1701
1776
  /* Set Lex_input_stream. */
1702
1777
 
1704
1779
 
1705
1780
  /* Parse the query. */
1706
1781
 
1707
 
  bool parse_status= base_sql_parse(session) != 0;
 
1782
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
1708
1783
 
1709
1784
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1710
1785
 
1711
 
  assert(!parse_status || session->is_error());
 
1786
  assert(!mysql_parse_status || session->is_error());
1712
1787
 
1713
1788
  /* Reset Lex_input_stream. */
1714
1789
 
1715
1790
  session->m_lip= NULL;
1716
1791
 
1717
 
  DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
 
1792
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1718
1793
 
1719
1794
  /* That's it. */
1720
1795
 
1721
 
  return parse_status || session->is_fatal_error;
 
1796
  return mysql_parse_status || session->is_fatal_error;
1722
1797
}
1723
1798
 
1724
1799
/**