~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Brian Aker
  • Date: 2009-02-10 00:14:40 UTC
  • Revision ID: brian@tangent.org-20090210001440-qjg8eofh3h93064b
Adding Multi-threaded Scheduler into the system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
#define DRIZZLE_LEX 1
17
17
#include <drizzled/server_includes.h>
18
 
#include <drizzled/sql_repl.h>
19
 
#include <drizzled/rpl_filter.h>
 
18
#include <libdrizzleclient/libdrizzle.h>
 
19
#include <mysys/hash.h>
20
20
#include <drizzled/logging.h>
 
21
#include <drizzled/db.h>
21
22
#include <drizzled/error.h>
22
23
#include <drizzled/nested_join.h>
23
24
#include <drizzled/query_id.h>
24
25
#include <drizzled/sql_parse.h>
25
26
#include <drizzled/data_home.h>
 
27
#include <drizzled/sql_base.h>
 
28
#include <drizzled/show.h>
 
29
#include <drizzled/rename.h>
 
30
#include <drizzled/function/time/unix_timestamp.h>
 
31
#include <drizzled/function/get_system_var.h>
 
32
#include <drizzled/item/cmpfunc.h>
 
33
#include <drizzled/item/null.h>
 
34
#include <drizzled/session.h>
 
35
#include <drizzled/sql_load.h>
 
36
#include <drizzled/connect.h>
 
37
#include <drizzled/lock.h>
 
38
#include <drizzled/select_send.h>
 
39
#include <bitset>
 
40
 
 
41
using namespace std;
26
42
 
27
43
/**
28
44
  @defgroup Runtime_Environment Runtime Environment
29
45
  @{
30
46
*/
31
47
 
 
48
extern size_t my_thread_stack_size;
32
49
extern const CHARSET_INFO *character_set_filesystem;
33
50
const char *any_db="*any*";     // Special symbol for check_access
34
51
 
48
65
  { C_STRING_WITH_LEN("Ping") },
49
66
  { C_STRING_WITH_LEN("Time") },
50
67
  { C_STRING_WITH_LEN("Change user") },
51
 
  { C_STRING_WITH_LEN("Binlog Dump") },
52
68
  { C_STRING_WITH_LEN("Connect Out") },
53
 
  { C_STRING_WITH_LEN("Register Slave") },
54
69
  { C_STRING_WITH_LEN("Set option") },
55
70
  { C_STRING_WITH_LEN("Daemon") },
56
71
  { C_STRING_WITH_LEN("Error") }  // Last command number
122
137
/**
123
138
  Returns true if all tables should be ignored.
124
139
*/
125
 
inline bool all_tables_not_ok(Session *session, TableList *tables)
126
 
{
127
 
  return rpl_filter->is_on() && tables &&
128
 
         !rpl_filter->tables_ok(session->db, tables);
129
 
}
130
 
 
131
 
 
132
 
static bool some_non_temp_table_to_be_updated(Session *session, TableList *tables)
133
 
{
134
 
  for (TableList *table= tables; table; table= table->next_global)
135
 
  {
136
 
    assert(table->db && table->table_name);
137
 
    if (table->updating &&
138
 
        !find_temporary_table(session, table->db, table->table_name))
139
 
      return 1;
140
 
  }
141
 
  return 0;
142
 
}
143
 
 
 
140
inline bool all_tables_not_ok(Session *, TableList *)
 
141
{
 
142
  return false;
 
143
}
144
144
 
145
145
/**
146
146
  Mark all commands that somehow changes a table.
155
155
          a number of modified rows
156
156
*/
157
157
 
158
 
uint32_t sql_command_flags[SQLCOM_END+1];
 
158
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
159
159
 
160
160
void init_update_queries(void)
161
161
{
162
 
  memset(&sql_command_flags, 0, sizeof(sql_command_flags));
 
162
  uint32_t x;
 
163
 
 
164
  for (x= 0; x <= SQLCOM_END; x++)
 
165
    sql_command_flags[x].reset();
163
166
 
164
167
  sql_command_flags[SQLCOM_CREATE_TABLE]=   CF_CHANGES_DATA;
165
168
  sql_command_flags[SQLCOM_CREATE_INDEX]=   CF_CHANGES_DATA;
187
190
  sql_command_flags[SQLCOM_SHOW_FIELDS]=      CF_STATUS_COMMAND;
188
191
  sql_command_flags[SQLCOM_SHOW_KEYS]=        CF_STATUS_COMMAND;
189
192
  sql_command_flags[SQLCOM_SHOW_VARIABLES]=   CF_STATUS_COMMAND;
190
 
  sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
191
193
  sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
192
194
  sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
193
195
  sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
194
196
  sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
195
197
  sql_command_flags[SQLCOM_SHOW_CREATE_DB]=  CF_STATUS_COMMAND;
196
198
  sql_command_flags[SQLCOM_SHOW_CREATE]=  CF_STATUS_COMMAND;
197
 
  sql_command_flags[SQLCOM_SHOW_MASTER_STAT]=  CF_STATUS_COMMAND;
198
 
  sql_command_flags[SQLCOM_SHOW_SLAVE_STAT]=  CF_STATUS_COMMAND;
199
199
 
200
200
   sql_command_flags[SQLCOM_SHOW_TABLES]=       (CF_STATUS_COMMAND |
201
201
                                               CF_SHOW_TABLE_COMMAND);
214
214
bool is_update_query(enum enum_sql_command command)
215
215
{
216
216
  assert(command >= 0 && command <= SQLCOM_END);
217
 
  return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
 
217
  return (sql_command_flags[command].test(CF_BIT_CHANGES_DATA));
218
218
}
219
219
 
220
220
void execute_init_command(Session *session, sys_var_str *init_command_var,
221
 
                          rw_lock_t *var_mutex)
 
221
                          pthread_rwlock_t *var_mutex)
222
222
{
223
223
  Vio* save_vio;
224
224
  ulong save_client_capabilities;
229
229
    during execution of init_command_var query
230
230
    values of init_command_var can't be changed
231
231
  */
232
 
  rw_rdlock(var_mutex);
 
232
  pthread_rwlock_rdlock(var_mutex);
233
233
  save_client_capabilities= session->client_capabilities;
234
234
  session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
235
235
  /*
241
241
  dispatch_command(COM_QUERY, session,
242
242
                   init_command_var->value,
243
243
                   init_command_var->value_length);
244
 
  rw_unlock(var_mutex);
 
244
  pthread_rwlock_unlock(var_mutex);
245
245
  session->client_capabilities= save_client_capabilities;
246
246
  session->net.vio= save_vio;
247
247
}
347
347
    the client, the connection is closed or "net_wait_timeout"
348
348
    number of seconds has passed
349
349
  */
350
 
  my_net_set_read_timeout(net, session->variables.net_wait_timeout);
 
350
  drizzleclient_net_set_read_timeout(net, session->variables.net_wait_timeout);
351
351
 
352
352
  /*
353
 
    XXX: this code is here only to clear possible errors of init_connect. 
 
353
    XXX: this code is here only to clear possible errors of init_connect.
354
354
    Consider moving to init_connect() instead.
355
355
  */
356
356
  session->clear_error();                               // Clear error message
358
358
 
359
359
  net_new_transaction(net);
360
360
 
361
 
  packet_length= my_net_read(net);
 
361
  packet_length= drizzleclient_net_read(net);
362
362
  if (packet_length == packet_error)
363
363
  {
364
364
    /* Check if we can continue without closing the connection */
365
365
 
366
366
    /* The error must be set. */
367
 
    assert(session->is_error());
368
 
    net_end_statement(session);
 
367
    /* This assert is killing me - and tracking down why the error isn't
 
368
     * set here is a waste since the protocol lib is being replaced. */ 
 
369
    //assert(session->is_error());
 
370
    drizzleclient_net_end_statement(session);
369
371
 
370
372
    if (net->error != 3)
371
373
    {
381
383
  packet= (char*) net->read_pos;
382
384
  /*
383
385
    'packet_length' contains length of data, as it was stored in packet
384
 
    header. In case of malformed header, my_net_read returns zero.
385
 
    If packet_length is not zero, my_net_read ensures that the returned
 
386
    header. In case of malformed header, drizzleclient_net_read returns zero.
 
387
    If packet_length is not zero, drizzleclient_net_read ensures that the returned
386
388
    number of bytes was actually read from network.
387
 
    There is also an extra safety measure in my_net_read:
 
389
    There is also an extra safety measure in drizzleclient_net_read:
388
390
    it sets packet[packet_length]= 0, but only for non-zero packets.
389
391
  */
390
392
  if (packet_length == 0)                       /* safety */
393
395
    packet[0]= (unsigned char) COM_SLEEP;
394
396
    packet_length= 1;
395
397
  }
396
 
  /* Do not rely on my_net_read, extra safety against programming errors. */
 
398
  /* Do not rely on drizzleclient_net_read, extra safety against programming errors. */
397
399
  packet[packet_length]= '\0';                  /* safety */
398
400
 
399
401
  command= (enum enum_server_command) (unsigned char) packet[0];
402
404
    command= COM_END;                           // Wrong command
403
405
 
404
406
  /* Restore read timeout value */
405
 
  my_net_set_read_timeout(net, session->variables.net_read_timeout);
 
407
  drizzleclient_net_set_read_timeout(net, session->variables.net_read_timeout);
406
408
 
407
409
  assert(packet_length);
408
410
  return_value= dispatch_command(command, session, packet+1, (uint32_t) (packet_length-1));
412
414
}
413
415
 
414
416
/**
415
 
  Determine if an attempt to update a non-temporary table while the
416
 
  read-only option was enabled has been made.
417
 
 
418
 
  This is a helper function to mysql_execute_command.
419
 
 
420
 
  @note SQLCOM_MULTI_UPDATE is an exception and dealt with elsewhere.
421
 
 
422
 
  @see mysql_execute_command
423
 
 
424
 
  @returns Status code
425
 
    @retval true The statement should be denied.
426
 
    @retval false The statement isn't updating any relevant tables.
427
 
*/
428
 
 
429
 
static bool deny_updates_if_read_only_option(Session *session,
430
 
                                                TableList *all_tables)
431
 
{
432
 
  if (!opt_readonly)
433
 
    return(false);
434
 
 
435
 
  LEX *lex= session->lex;
436
 
 
437
 
  if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
438
 
    return(false);
439
 
 
440
 
  /* Multi update is an exception and is dealt with later. */
441
 
  if (lex->sql_command == SQLCOM_UPDATE_MULTI)
442
 
    return(false);
443
 
 
444
 
  const bool create_temp_tables= 
445
 
    (lex->sql_command == SQLCOM_CREATE_TABLE) &&
446
 
    (lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
447
 
 
448
 
  const bool drop_temp_tables= 
449
 
    (lex->sql_command == SQLCOM_DROP_TABLE) &&
450
 
    lex->drop_temporary;
451
 
 
452
 
  const bool update_real_tables=
453
 
    some_non_temp_table_to_be_updated(session, all_tables) &&
454
 
    !(create_temp_tables || drop_temp_tables);
455
 
 
456
 
 
457
 
  const bool create_or_drop_databases=
458
 
    (lex->sql_command == SQLCOM_CREATE_DB) ||
459
 
    (lex->sql_command == SQLCOM_DROP_DB);
460
 
 
461
 
  if (update_real_tables || create_or_drop_databases)
462
 
  {
463
 
      /*
464
 
        An attempt was made to modify one or more non-temporary tables.
465
 
      */
466
 
      return(true);
467
 
  }
468
 
 
469
 
 
470
 
  /* Assuming that only temporary tables are modified. */
471
 
  return(false);
472
 
}
473
 
 
474
 
/**
475
417
  Perform one connection-level (COM_XXXX) command.
476
418
 
477
419
  @param command         type of command to perform
532
474
                        packet, packet_length, session->charset());
533
475
    if (!mysql_change_db(session, &tmp, false))
534
476
    {
535
 
      my_ok(session);
 
477
      session->my_ok();
536
478
    }
537
479
    break;
538
480
  }
570
512
                      (unsigned char)(*passwd++) : strlen(passwd));
571
513
    uint32_t dummy_errors, save_db_length, db_length;
572
514
    int res;
573
 
    Security_context save_security_ctx= *session->security_ctx;
574
515
    USER_CONN *save_user_connect;
 
516
    string old_username;
575
517
 
576
518
    db+= passwd_len + 1;
577
519
    /*
610
552
    save_db= session->db;
611
553
    save_user_connect= session->user_connect;
612
554
 
613
 
    if (!(session->security_ctx->user= my_strdup(user, MYF(0))))
614
 
    {
615
 
      session->security_ctx->user= save_security_ctx.user;
616
 
      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
617
 
      break;
618
 
    }
 
555
    old_username= session->security_ctx.user;
 
556
    session->security_ctx.user.assign(user);
619
557
 
620
558
    /* Clear variables that are allocated */
621
559
    session->user_connect= 0;
622
 
    res= check_user(session, passwd, passwd_len, db, false);
 
560
    res= check_user(session, passwd, passwd_len, db);
623
561
 
624
562
    if (res)
625
563
    {
626
 
      if (session->security_ctx->user)
627
 
        free(session->security_ctx->user);
628
 
      *session->security_ctx= save_security_ctx;
 
564
      session->security_ctx.user= old_username;
629
565
      session->user_connect= save_user_connect;
630
566
      session->db= save_db;
631
567
      session->db_length= save_db_length;
634
570
    {
635
571
      if (save_db)
636
572
        free(save_db);
637
 
      if (save_security_ctx.user)
638
 
        free(save_security_ctx.user);
639
573
 
640
574
      if (cs_number)
641
575
      {
642
 
        session_init_client_charset(session, cs_number);
643
576
        session->update_charset();
644
577
      }
645
578
    }
658
591
    {
659
592
      char *beginning_of_next_stmt= (char*) end_of_stmt;
660
593
 
661
 
      net_end_statement(session);
 
594
      drizzleclient_net_end_statement(session);
662
595
      /*
663
596
        Multiple queries exits, execute them individually
664
597
      */
714
647
    packet= arg_end + 1;
715
648
 
716
649
    if (!my_strcasecmp(system_charset_info, table_list.db,
717
 
                       INFORMATION_SCHEMA_NAME.str))
 
650
                       INFORMATION_SCHEMA_NAME.c_str()))
718
651
    {
719
652
      ST_SCHEMA_TABLE *schema_table= find_schema_table(session, table_list.alias);
720
653
      if (schema_table)
731
664
    table_list.select_lex= &(session->lex->select_lex);
732
665
 
733
666
    lex_start(session);
734
 
    mysql_reset_session_for_next_command(session);
 
667
    session->reset_for_next_command();
735
668
 
736
669
    session->lex->
737
670
      select_lex.table_list.link_in_list((unsigned char*) &table_list,
751
684
    session->main_da.disable_status();              // Don't send anything back
752
685
    error=true;                                 // End server
753
686
    break;
754
 
  case COM_BINLOG_DUMP:
755
 
    {
756
 
      ulong pos;
757
 
      uint16_t flags;
758
 
      uint32_t slave_server_id;
759
 
 
760
 
      status_var_increment(session->status_var.com_other);
761
 
      /* TODO: The following has to be changed to an 8 byte integer */
762
 
      pos = uint4korr(packet);
763
 
      flags = uint2korr(packet + 4);
764
 
      session->server_id=0; /* avoid suicide */
765
 
      if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
766
 
        kill_zombie_dump_threads(slave_server_id);
767
 
      session->server_id = slave_server_id;
768
 
 
769
 
      mysql_binlog_send(session, session->strdup(packet + 10), (my_off_t) pos, flags);
770
 
      /*  fake COM_QUIT -- if we get here, the thread needs to terminate */
771
 
      error = true;
772
 
      break;
773
 
    }
774
687
  case COM_SHUTDOWN:
775
688
  {
776
689
    status_var_increment(session->status_var.com_other);
777
 
    my_eof(session);
 
690
    session->my_eof();
778
691
    close_thread_tables(session);                       // Free before kill
779
 
    kill_mysql();
 
692
    kill_drizzle();
780
693
    error=true;
781
694
    break;
782
695
  }
783
696
  case COM_PING:
784
697
    status_var_increment(session->status_var.com_other);
785
 
    my_ok(session);                             // Tell client we are alive
 
698
    session->my_ok();                           // Tell client we are alive
786
699
    break;
787
700
  case COM_PROCESS_INFO:
788
701
    status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
803
716
    switch (opt_command) {
804
717
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
805
718
      session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
806
 
      my_eof(session);
 
719
      session->my_eof();
807
720
      break;
808
721
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
809
722
      session->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
810
 
      my_eof(session);
 
723
      session->my_eof();
811
724
      break;
812
725
    default:
813
726
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
844
757
    session->mysys_var->abort= 0;
845
758
  }
846
759
 
847
 
  net_end_statement(session);
 
760
  drizzleclient_net_end_statement(session);
848
761
 
849
762
  session->set_proc_info("closing tables");
850
763
  /* Free tables */
879
792
 
880
793
    This function is used in the parser to convert a SHOW or DESCRIBE
881
794
    table_name command to a SELECT from INFORMATION_SCHEMA.
882
 
    It prepares a SELECT_LEX and a TableList object to represent the
 
795
    It prepares a Select_Lex and a TableList object to represent the
883
796
    given command as a SELECT parse tree.
884
797
 
885
798
  @param session              thread handle
903
816
int prepare_schema_table(Session *session, LEX *lex, Table_ident *table_ident,
904
817
                         enum enum_schema_tables schema_table_idx)
905
818
{
906
 
  SELECT_LEX *schema_select_lex= NULL;
 
819
  Select_Lex *schema_select_lex= NULL;
907
820
 
908
821
  switch (schema_table_idx) {
909
822
  case SCH_SCHEMATA:
918
831
      {
919
832
        return(1);
920
833
      }
921
 
      schema_select_lex= new SELECT_LEX();
 
834
      schema_select_lex= new Select_Lex();
922
835
      db.str= schema_select_lex->db= lex->select_lex.db;
923
836
      schema_select_lex->table_list.first= NULL;
924
837
      db.length= strlen(db.str);
935
848
  {
936
849
    assert(table_ident);
937
850
    TableList **query_tables_last= lex->query_tables_last;
938
 
    schema_select_lex= new SELECT_LEX();
 
851
    schema_select_lex= new Select_Lex();
939
852
    /* 'parent_lex' is used in init_query() so it must be before it. */
940
853
    schema_select_lex->parent_lex= lex;
941
854
    schema_select_lex->init_query();
955
868
  default:
956
869
    break;
957
870
  }
958
 
  
959
 
  SELECT_LEX *select_lex= lex->current_select;
 
871
 
 
872
  Select_Lex *select_lex= lex->current_select;
960
873
  assert(select_lex);
961
874
  if (make_schema_select(session, select_lex, schema_table_idx))
962
875
  {
1015
928
  return false;
1016
929
}
1017
930
 
1018
 
static void reset_one_shot_variables(Session *session) 
1019
 
{
1020
 
  session->variables.character_set_client=
1021
 
    global_system_variables.character_set_client;
1022
 
  session->variables.collation_connection=
1023
 
    global_system_variables.collation_connection;
1024
 
  session->variables.collation_database=
1025
 
    global_system_variables.collation_database;
1026
 
  session->variables.collation_server=
1027
 
    global_system_variables.collation_server;
1028
 
  session->update_charset();
1029
 
  session->variables.time_zone=
1030
 
    global_system_variables.time_zone;
1031
 
  session->variables.lc_time_names= &my_locale_en_US;
1032
 
  session->one_shot_set= 0;
1033
 
}
1034
 
 
1035
 
 
1036
931
/**
1037
932
  Execute command saved in session and lex->sql_command.
1038
933
 
1068
963
{
1069
964
  int res= false;
1070
965
  bool need_start_waiting= false; // have protection against global read lock
1071
 
  int  up_result= 0;
1072
966
  LEX  *lex= session->lex;
1073
 
  /* first SELECT_LEX (have special meaning for many of non-SELECTcommands) */
1074
 
  SELECT_LEX *select_lex= &lex->select_lex;
1075
 
  /* first table of first SELECT_LEX */
 
967
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
 
968
  Select_Lex *select_lex= &lex->select_lex;
 
969
  /* first table of first Select_Lex */
1076
970
  TableList *first_table= (TableList*) select_lex->table_list.first;
1077
971
  /* list of all tables in query */
1078
972
  TableList *all_tables;
1079
 
  /* most outer SELECT_LEX_UNIT of query */
1080
 
  SELECT_LEX_UNIT *unit= &lex->unit;
 
973
  /* most outer Select_Lex_Unit of query */
 
974
  Select_Lex_Unit *unit= &lex->unit;
1081
975
  /* Saved variable value */
1082
976
 
1083
977
  /*
1084
 
    In many cases first table of main SELECT_LEX have special meaning =>
1085
 
    check that it is first table in global list and relink it first in 
 
978
    In many cases first table of main Select_Lex have special meaning =>
 
979
    check that it is first table in global list and relink it first in
1086
980
    queries_tables list if it is necessary (we need such relinking only
1087
981
    for queries with subqueries in select list, in this case tables of
1088
982
    subqueries will go to global list first)
1089
983
 
1090
 
    all_tables will differ from first_table only if most upper SELECT_LEX
 
984
    all_tables will differ from first_table only if most upper Select_Lex
1091
985
    do not contain tables.
1092
986
 
1093
987
    Because of above in place where should be at least one table in most
1094
 
    outer SELECT_LEX we have following check:
 
988
    outer Select_Lex we have following check:
1095
989
    assert(first_table == all_tables);
1096
990
    assert(first_table == all_tables && first_table != 0);
1097
991
  */
1113
1007
  if (all_tables || !lex->is_single_level_stmt())
1114
1008
    drizzle_reset_errors(session, 0);
1115
1009
 
1116
 
  if (unlikely(session->slave_thread))
1117
 
  {
1118
 
    /*
1119
 
      Check if statment should be skipped because of slave filtering
1120
 
      rules
1121
 
 
1122
 
      Exceptions are:
1123
 
      - UPDATE MULTI: For this statement, we want to check the filtering
1124
 
        rules later in the code
1125
 
      - SET: we always execute it (Not that many SET commands exists in
1126
 
        the binary log anyway -- only 4.1 masters write SET statements,
1127
 
        in 5.0 there are no SET statements in the binary log)
1128
 
      - DROP TEMPORARY TABLE IF EXISTS: we always execute it (otherwise we
1129
 
        have stale files on slave caused by exclusion of one tmp table).
1130
 
    */
1131
 
    if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
1132
 
        !(lex->sql_command == SQLCOM_SET_OPTION) &&
1133
 
        !(lex->sql_command == SQLCOM_DROP_TABLE &&
1134
 
          lex->drop_temporary && lex->drop_if_exists) &&
1135
 
        all_tables_not_ok(session, all_tables))
1136
 
    {
1137
 
      /* we warn the slave SQL thread */
1138
 
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1139
 
      if (session->one_shot_set)
1140
 
      {
1141
 
        /*
1142
 
          It's ok to check session->one_shot_set here:
1143
 
 
1144
 
          The charsets in a MySQL 5.0 slave can change by both a binlogged
1145
 
          SET ONE_SHOT statement and the event-internal charset setting, 
1146
 
          and these two ways to change charsets do not seems to work
1147
 
          together.
1148
 
 
1149
 
          At least there seems to be problems in the rli cache for
1150
 
          charsets if we are using ONE_SHOT.  Note that this is normally no
1151
 
          problem because either the >= 5.0 slave reads a 4.1 binlog (with
1152
 
          ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1153
 
        */
1154
 
        reset_one_shot_variables(session);
1155
 
      }
1156
 
      return(0);
1157
 
    }
1158
 
  }
1159
 
  else
1160
 
  {
1161
 
    /*
1162
 
      When option readonly is set deny operations which change non-temporary
1163
 
      tables. Except for the replication thread and the 'super' users.
1164
 
    */
1165
 
    if (deny_updates_if_read_only_option(session, all_tables))
1166
 
    {
1167
 
      my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1168
 
      return(-1);
1169
 
    }
1170
 
  } /* endif unlikely slave */
1171
1010
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
1172
1011
 
1173
1012
  assert(session->transaction.stmt.modified_non_trans_table == false);
1174
 
  
 
1013
 
1175
1014
  switch (lex->sql_command) {
1176
1015
  case SQLCOM_SHOW_STATUS:
1177
1016
  {
1206
1045
    break;
1207
1046
  }
1208
1047
  case SQLCOM_EMPTY_QUERY:
1209
 
    my_ok(session);
1210
 
    break;
1211
 
 
1212
 
  case SQLCOM_PURGE:
1213
 
  {
1214
 
    res = purge_master_logs(session, lex->to_log);
1215
 
    break;
1216
 
  }
1217
 
  case SQLCOM_PURGE_BEFORE:
1218
 
  {
1219
 
    Item *it;
1220
 
 
1221
 
    /* PURGE MASTER LOGS BEFORE 'data' */
1222
 
    it= (Item *)lex->value_list.head();
1223
 
    if ((!it->fixed && it->fix_fields(lex->session, &it)) ||
1224
 
        it->check_cols(1))
1225
 
    {
1226
 
      my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1227
 
      goto error;
1228
 
    }
1229
 
    it= new Item_func_unix_timestamp(it);
1230
 
    /*
1231
 
      it is OK only emulate fix_fieds, because we need only
1232
 
      value of constant
1233
 
    */
1234
 
    it->quick_fix_field();
1235
 
    res = purge_master_logs_before_date(session, (ulong)it->val_int());
1236
 
    break;
1237
 
  }
 
1048
    session->my_ok();
 
1049
    break;
 
1050
 
1238
1051
  case SQLCOM_SHOW_WARNS:
1239
1052
  {
1240
1053
    res= mysqld_show_warnings(session, (uint32_t)
1256
1069
    res= mysql_assign_to_keycache(session, first_table, &lex->ident);
1257
1070
    break;
1258
1071
  }
1259
 
  case SQLCOM_CHANGE_MASTER:
1260
 
  {
1261
 
    pthread_mutex_lock(&LOCK_active_mi);
1262
 
    res = change_master(session,active_mi);
1263
 
    pthread_mutex_unlock(&LOCK_active_mi);
1264
 
    break;
1265
 
  }
1266
 
  case SQLCOM_SHOW_SLAVE_STAT:
1267
 
  {
1268
 
    pthread_mutex_lock(&LOCK_active_mi);
1269
 
    if (active_mi != NULL)
1270
 
    {
1271
 
      res = show_master_info(session, active_mi);
1272
 
    }
1273
 
    else
1274
 
    {
1275
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1276
 
                   "the master info structure does not exist");
1277
 
      my_ok(session);
1278
 
    }
1279
 
    pthread_mutex_unlock(&LOCK_active_mi);
1280
 
    break;
1281
 
  }
1282
 
  case SQLCOM_SHOW_MASTER_STAT:
1283
 
  {
1284
 
    res = show_binlog_info(session);
1285
 
    break;
1286
 
  }
1287
 
 
1288
1072
  case SQLCOM_SHOW_ENGINE_STATUS:
1289
1073
    {
1290
1074
      res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
1418
1202
                                       select_tables)))
1419
1203
        {
1420
1204
          /*
1421
 
            CREATE from SELECT give its SELECT_LEX for SELECT,
 
1205
            CREATE from SELECT give its Select_Lex for SELECT,
1422
1206
            and item_list belong to SELECT
1423
1207
          */
1424
1208
          res= handle_select(session, lex, result, 0);
1445
1229
                                &alter_info, 0, 0);
1446
1230
      }
1447
1231
      if (!res)
1448
 
        my_ok(session);
 
1232
        session->my_ok();
1449
1233
    }
1450
1234
 
1451
1235
    /* put tables back for PS rexecuting */
1486
1270
                           0, (order_st*) 0, 0);
1487
1271
    break;
1488
1272
  }
1489
 
  case SQLCOM_SLAVE_START:
1490
 
  {
1491
 
    pthread_mutex_lock(&LOCK_active_mi);
1492
 
    start_slave(session,active_mi,1 /* net report*/);
1493
 
    pthread_mutex_unlock(&LOCK_active_mi);
1494
 
    break;
1495
 
  }
1496
 
  case SQLCOM_SLAVE_STOP:
1497
 
  /*
1498
 
    If the client thread has locked tables, a deadlock is possible.
1499
 
    Assume that
1500
 
    - the client thread does LOCK TABLE t READ.
1501
 
    - then the master updates t.
1502
 
    - then the SQL slave thread wants to update t,
1503
 
      so it waits for the client thread because t is locked by it.
1504
 
    - then the client thread does SLAVE STOP.
1505
 
      SLAVE STOP waits for the SQL slave thread to terminate its
1506
 
      update t, which waits for the client thread because t is locked by it.
1507
 
    To prevent that, refuse SLAVE STOP if the
1508
 
    client thread has locked tables
1509
 
  */
1510
 
  if (session->locked_tables || session->active_transaction() || session->global_read_lock)
1511
 
  {
1512
 
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1513
 
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1514
 
    goto error;
1515
 
  }
1516
 
  {
1517
 
    pthread_mutex_lock(&LOCK_active_mi);
1518
 
    stop_slave(session,active_mi,1/* net report*/);
1519
 
    pthread_mutex_unlock(&LOCK_active_mi);
1520
 
    break;
1521
 
  }
1522
 
 
1523
1273
  case SQLCOM_ALTER_TABLE:
1524
1274
    assert(first_table == all_tables && first_table != 0);
1525
1275
    {
1596
1346
      }
1597
1347
    break;
1598
1348
  }
1599
 
  case SQLCOM_SHOW_BINLOGS:
1600
 
    {
1601
 
      res = show_binlogs(session);
1602
 
      break;
1603
 
    }
1604
1349
  case SQLCOM_SHOW_CREATE:
1605
1350
    assert(first_table == all_tables && first_table != 0);
1606
1351
    {
1657
1402
  }
1658
1403
  case SQLCOM_UPDATE:
1659
1404
    assert(first_table == all_tables && first_table != 0);
1660
 
    if (update_precheck(session, all_tables))
 
1405
    if ((res= update_precheck(session, all_tables)))
1661
1406
      break;
1662
1407
    assert(select_lex->offset_limit == 0);
1663
1408
    unit->set_limit(select_lex);
1664
 
    res= (up_result= mysql_update(session, all_tables,
1665
 
                                  select_lex->item_list,
1666
 
                                  lex->value_list,
1667
 
                                  select_lex->where,
1668
 
                                  select_lex->order_list.elements,
1669
 
                                  (order_st *) select_lex->order_list.first,
1670
 
                                  unit->select_limit_cnt,
1671
 
                                  lex->duplicates, lex->ignore));
1672
 
    /* mysql_update return 2 if we need to switch to multi-update */
1673
 
    if (up_result != 2)
1674
 
      break;
1675
 
    /* Fall through */
 
1409
    res= mysql_update(session, all_tables,
 
1410
                      select_lex->item_list,
 
1411
                      lex->value_list,
 
1412
                      select_lex->where,
 
1413
                      select_lex->order_list.elements,
 
1414
                      (order_st *) select_lex->order_list.first,
 
1415
                      unit->select_limit_cnt,
 
1416
                      lex->duplicates, lex->ignore);
 
1417
    break;
1676
1418
  case SQLCOM_UPDATE_MULTI:
1677
1419
  {
1678
1420
    assert(first_table == all_tables && first_table != 0);
1679
 
    /* if we switched from normal update, rights are checked */
1680
 
    if (up_result != 2)
1681
 
    {
1682
 
      if ((res= multi_update_precheck(session, all_tables)))
1683
 
        break;
1684
 
    }
1685
 
    else
1686
 
      res= 0;
1687
 
 
1688
 
    res= mysql_multi_update_prepare(session);
1689
 
 
1690
 
    /* Check slave filtering rules */
1691
 
    if (unlikely(session->slave_thread))
1692
 
    {
1693
 
      if (all_tables_not_ok(session, all_tables))
1694
 
      {
1695
 
        if (res!= 0)
1696
 
        {
1697
 
          res= 0;             /* don't care of prev failure  */
1698
 
          session->clear_error(); /* filters are of highest prior */
1699
 
        }
1700
 
        /* we warn the slave SQL thread */
1701
 
        my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1702
 
        break;
1703
 
      }
1704
 
      if (res)
1705
 
        break;
1706
 
    }
1707
 
    else
1708
 
    {
1709
 
      if (res)
1710
 
        break;
1711
 
      if (opt_readonly &&
1712
 
          some_non_temp_table_to_be_updated(session, all_tables))
1713
 
      {
1714
 
        my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1715
 
        break;
1716
 
      }
1717
 
    }  /* unlikely */
 
1421
    if ((res= update_precheck(session, all_tables)))
 
1422
      break;
 
1423
 
 
1424
    if ((res= mysql_multi_update_prepare(session)))
 
1425
      break;
1718
1426
 
1719
1427
    res= mysql_multi_update(session, all_tables,
1720
1428
                            &select_lex->item_list,
1773
1481
      /* Skip first table, which is the table we are inserting in */
1774
1482
      TableList *second_table= first_table->next_local;
1775
1483
      select_lex->table_list.first= (unsigned char*) second_table;
1776
 
      select_lex->context.table_list= 
 
1484
      select_lex->context.table_list=
1777
1485
        select_lex->context.first_name_resolution_table= second_table;
1778
1486
      res= mysql_insert_select_prepare(session);
1779
1487
      if (!res && (sel_result= new select_insert(first_table,
1867
1575
    /* condition will be true on SP re-excuting */
1868
1576
    if (select_lex->item_list.elements != 0)
1869
1577
      select_lex->item_list.empty();
1870
 
    if (add_item_to_list(session, new Item_null()))
 
1578
    if (session->add_item_to_list(new Item_null()))
1871
1579
      goto error;
1872
1580
 
1873
1581
    session->set_proc_info("init");
1910
1618
    }
1911
1619
    else
1912
1620
    {
1913
 
      /*
1914
 
        If this is a slave thread, we may sometimes execute some 
1915
 
        DROP / * 40005 TEMPORARY * / TABLE
1916
 
        that come from parts of binlogs (likely if we use RESET SLAVE or CHANGE
1917
 
        MASTER TO), while the temporary table has already been dropped.
1918
 
        To not generate such irrelevant "table does not exist errors",
1919
 
        we silently add IF EXISTS if TEMPORARY was used.
1920
 
      */
1921
 
      if (session->slave_thread)
1922
 
        lex->drop_if_exists= 1;
1923
 
 
1924
1621
      /* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
1925
1622
      session->options|= OPTION_KEEP_LOG;
1926
1623
    }
1941
1638
    LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
1942
1639
 
1943
1640
    if (!mysql_change_db(session, &db_str, false))
1944
 
      my_ok(session);
 
1641
      session->my_ok();
1945
1642
 
1946
1643
    break;
1947
1644
  }
1974
1671
 
1975
1672
    if (open_and_lock_tables(session, all_tables))
1976
1673
      goto error;
1977
 
    if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
1978
 
    {
1979
 
      my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
1980
 
      goto error;
1981
 
    }
1982
1674
    if (!(res= sql_set_variables(session, lex_var_list)))
1983
1675
    {
1984
 
      /*
1985
 
        If the previous command was a SET ONE_SHOT, we don't want to forget
1986
 
        about the ONE_SHOT property of that SET. So we use a |= instead of = .
1987
 
      */
1988
 
      session->one_shot_set|= lex->one_shot_set;
1989
 
      my_ok(session);
 
1676
      session->my_ok();
1990
1677
    }
1991
1678
    else
1992
1679
    {
2018
1705
    }
2019
1706
    if (session->global_read_lock)
2020
1707
      unlock_global_read_lock(session);
2021
 
    my_ok(session);
 
1708
    session->my_ok();
2022
1709
    break;
2023
1710
  case SQLCOM_LOCK_TABLES:
2024
1711
    /*
2037
1724
        goto error;
2038
1725
      if (rc == 0)
2039
1726
      {
2040
 
        my_ok(session);
 
1727
        session->my_ok();
2041
1728
        break;
2042
1729
      }
2043
1730
      /*
2068
1755
      session->locked_tables=session->lock;
2069
1756
      session->lock=0;
2070
1757
      (void) set_handler_table_locks(session, all_tables, false);
2071
 
      my_ok(session);
 
1758
      session->my_ok();
2072
1759
    }
2073
1760
    else
2074
1761
    {
2075
 
      /* 
 
1762
      /*
2076
1763
        Need to end the current transaction, so the storage engine (InnoDB)
2077
1764
        can free its locks if LOCK TABLES locked some tables before finding
2078
1765
        that it can't lock a table in its list
2103
1790
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2104
1791
      break;
2105
1792
    }
2106
 
    /*
2107
 
      If in a slave thread :
2108
 
      CREATE DATABASE DB was certainly not preceded by USE DB.
2109
 
      For that reason, db_ok() in sql/slave.cc did not check the
2110
 
      do_db/ignore_db. And as this query involves no tables, tables_ok()
2111
 
      above was not called. So we have to check rules again here.
2112
 
    */
2113
 
    if (session->slave_thread && 
2114
 
        (!rpl_filter->db_ok(lex->name.str) ||
2115
 
         !rpl_filter->db_ok_with_wild_table(lex->name.str)))
2116
 
    {
2117
 
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2118
 
      break;
2119
 
    }
2120
1793
    res= mysql_create_db(session,(lower_case_table_names == 2 ? alias :
2121
1794
                              lex->name.str), &create_info, 0);
2122
1795
    break;
2133
1806
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2134
1807
      break;
2135
1808
    }
2136
 
    /*
2137
 
      If in a slave thread :
2138
 
      DROP DATABASE DB may not be preceded by USE DB.
2139
 
      For that reason, maybe db_ok() in sql/slave.cc did not check the 
2140
 
      do_db/ignore_db. And as this query involves no tables, tables_ok()
2141
 
      above was not called. So we have to check rules again here.
2142
 
    */
2143
 
    if (session->slave_thread && 
2144
 
        (!rpl_filter->db_ok(lex->name.str) ||
2145
 
         !rpl_filter->db_ok_with_wild_table(lex->name.str)))
2146
 
    {
2147
 
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2148
 
      break;
2149
 
    }
2150
1809
    if (session->locked_tables || session->active_transaction())
2151
1810
    {
2152
1811
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2165
1824
      my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2166
1825
      break;
2167
1826
    }
2168
 
    /*
2169
 
      If in a slave thread :
2170
 
      ALTER DATABASE DB may not be preceded by USE DB.
2171
 
      For that reason, maybe db_ok() in sql/slave.cc did not check the
2172
 
      do_db/ignore_db. And as this query involves no tables, tables_ok()
2173
 
      above was not called. So we have to check rules again here.
2174
 
    */
2175
 
    if (session->slave_thread &&
2176
 
        (!rpl_filter->db_ok(db->str) ||
2177
 
         !rpl_filter->db_ok_with_wild_table(db->str)))
2178
 
    {
2179
 
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2180
 
      break;
2181
 
    }
2182
1827
    if (session->locked_tables || session->active_transaction())
2183
1828
    {
2184
1829
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2198
1843
    res= mysqld_show_create_db(session, lex->name.str, &lex->create_info);
2199
1844
    break;
2200
1845
  }
2201
 
  case SQLCOM_RESET:
2202
1846
  case SQLCOM_FLUSH:
2203
1847
  {
2204
1848
    bool write_to_binlog;
2217
1861
        Presumably, RESET and binlog writing doesn't require synchronization
2218
1862
      */
2219
1863
      write_bin_log(session, false, session->query, session->query_length);
2220
 
      my_ok(session);
2221
 
    } 
2222
 
    
 
1864
      session->my_ok();
 
1865
    }
 
1866
 
2223
1867
    break;
2224
1868
  }
2225
1869
  case SQLCOM_KILL:
2226
1870
  {
2227
1871
    Item *it= (Item *)lex->value_list.head();
2228
1872
 
2229
 
    if (lex->table_or_sp_used())
2230
 
    {
2231
 
      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
2232
 
               "function calls as part of this statement");
2233
 
      break;
2234
 
    }
2235
 
 
2236
1873
    if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
2237
1874
    {
2238
1875
      my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2254
1891
    */
2255
1892
    if (begin_trans(session))
2256
1893
      goto error;
2257
 
    my_ok(session);
 
1894
    session->my_ok();
2258
1895
    break;
2259
1896
  case SQLCOM_COMMIT:
2260
1897
    if (end_trans(session, lex->tx_release ? COMMIT_RELEASE :
2261
1898
                              lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2262
1899
      goto error;
2263
 
    my_ok(session);
 
1900
    session->my_ok();
2264
1901
    break;
2265
1902
  case SQLCOM_ROLLBACK:
2266
1903
    if (end_trans(session, lex->tx_release ? ROLLBACK_RELEASE :
2267
1904
                              lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2268
1905
      goto error;
2269
 
    my_ok(session);
 
1906
    session->my_ok();
2270
1907
    break;
2271
1908
  case SQLCOM_RELEASE_SAVEPOINT:
2272
1909
  {
2283
1920
      if (ha_release_savepoint(session, sv))
2284
1921
        res= true; // cannot happen
2285
1922
      else
2286
 
        my_ok(session);
 
1923
        session->my_ok();
2287
1924
      session->transaction.savepoints=sv->prev;
2288
1925
    }
2289
1926
    else
2306
1943
        res= true; // cannot happen
2307
1944
      else
2308
1945
      {
2309
 
        if (((session->options & OPTION_KEEP_LOG) || 
2310
 
             session->transaction.all.modified_non_trans_table) &&
2311
 
            !session->slave_thread)
 
1946
        if ((session->options & OPTION_KEEP_LOG) || session->transaction.all.modified_non_trans_table)
2312
1947
          push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2313
1948
                       ER_WARNING_NOT_COMPLETE_ROLLBACK,
2314
1949
                       ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2315
 
        my_ok(session);
 
1950
        session->my_ok();
2316
1951
      }
2317
1952
      session->transaction.savepoints=sv;
2318
1953
    }
2322
1957
  }
2323
1958
  case SQLCOM_SAVEPOINT:
2324
1959
    if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
2325
 
      my_ok(session);
 
1960
      session->my_ok();
2326
1961
    else
2327
1962
    {
2328
1963
      SAVEPOINT **sv, *newsv;
2359
1994
      {
2360
1995
        newsv->prev=session->transaction.savepoints;
2361
1996
        session->transaction.savepoints=newsv;
2362
 
        my_ok(session);
 
1997
        session->my_ok();
2363
1998
      }
2364
1999
    }
2365
2000
    break;
2366
 
  case SQLCOM_BINLOG_BASE64_EVENT:
2367
 
  {
2368
 
    mysql_client_binlog_statement(session);
2369
 
    break;
2370
 
  }
2371
2001
  default:
2372
2002
    assert(0);                             /* Impossible */
2373
 
    my_ok(session);
 
2003
    session->my_ok();
2374
2004
    break;
2375
2005
  }
2376
2006
  session->set_proc_info("query end");
2377
2007
 
2378
2008
  /*
2379
 
    Binlog-related cleanup:
2380
 
    Reset system variables temporarily modified by SET ONE SHOT.
2381
 
 
2382
 
    Exception: If this is a SET, do nothing. This is to allow
2383
 
    mysqlbinlog to print many SET commands (in this case we want the
2384
 
    charset temp setting to live until the real query). This is also
2385
 
    needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2386
 
    immediately.
2387
 
  */
2388
 
  if (session->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2389
 
    reset_one_shot_variables(session);
2390
 
 
2391
 
  /*
2392
2009
    The return value for ROW_COUNT() is "implementation dependent" if the
2393
2010
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
2394
2011
    wants. We also keep the last value in case of SQLCOM_CALL or
2395
2012
    SQLCOM_EXECUTE.
2396
2013
  */
2397
 
  if (!(sql_command_flags[lex->sql_command] & CF_HAS_ROW_COUNT))
 
2014
  if (!(sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
2398
2015
    session->row_count_func= -1;
2399
2016
 
2400
2017
  goto finish;
2421
2038
  bool res;
2422
2039
  /* assign global limit variable if limit is not given */
2423
2040
  {
2424
 
    SELECT_LEX *param= lex->unit.global_parameters;
 
2041
    Select_Lex *param= lex->unit.global_parameters;
2425
2042
    if (!param->explicit_limit)
2426
2043
      param->select_limit=
2427
2044
        new Item_int((uint64_t) session->variables.select_limit);
2468
2085
  return res;
2469
2086
}
2470
2087
 
2471
 
/****************************************************************************
2472
 
        Check stack size; Send error if there isn't enough stack to continue
2473
 
****************************************************************************/
2474
 
#if STACK_DIRECTION < 0
2475
 
#define used_stack(A,B) (long) (A - B)
2476
 
#else
2477
 
#define used_stack(A,B) (long) (B - A)
2478
 
#endif
2479
 
 
2480
 
/**
2481
 
  @note
2482
 
  Note: The 'buf' parameter is necessary, even if it is unused here.
2483
 
  - fix_fields functions has a "dummy" buffer large enough for the
2484
 
    corresponding exec. (Thus we only have to check in fix_fields.)
2485
 
  - Passing to check_stack_overrun() prevents the compiler from removing it.
2486
 
*/
2487
 
bool check_stack_overrun(Session *session, long margin, unsigned char *)
2488
 
{
2489
 
  long stack_used;
2490
 
  assert(session == current_session);
2491
 
  if ((stack_used=used_stack(session->thread_stack,(char*) &stack_used)) >=
2492
 
      (long) (my_thread_stack_size - margin))
2493
 
  {
2494
 
    sprintf(errbuff[0],ER(ER_STACK_OVERRUN_NEED_MORE),
2495
 
            stack_used,my_thread_stack_size,margin);
2496
 
    my_message(ER_STACK_OVERRUN_NEED_MORE,errbuff[0],MYF(ME_FATALERROR));
2497
 
    return 1;
2498
 
  }
2499
 
  return 0;
2500
 
}
2501
2088
 
2502
2089
#define MY_YACC_INIT 1000                       // Start with big alloc
2503
2090
#define MY_YACC_MAX  32000                      // Because of 'short'
2511
2098
  if (!lex->yacc_yyvs)
2512
2099
    old_info= *yystacksize;
2513
2100
  *yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
2514
 
  if (!(lex->yacc_yyvs= (unsigned char*)
2515
 
        my_realloc(lex->yacc_yyvs,
2516
 
                   *yystacksize*sizeof(**yyvs),
2517
 
                   MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))) ||
2518
 
      !(lex->yacc_yyss= (unsigned char*)
2519
 
        my_realloc(lex->yacc_yyss,
2520
 
                   *yystacksize*sizeof(**yyss),
2521
 
                   MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))))
2522
 
    return 1;
 
2101
  unsigned char *tmpptr= NULL;
 
2102
  if (!(tmpptr= (unsigned char *)realloc(lex->yacc_yyvs,
 
2103
                                         *yystacksize* sizeof(**yyvs))))
 
2104
      return 1;
 
2105
  lex->yacc_yyvs= tmpptr;
 
2106
  tmpptr= NULL;
 
2107
  if (!(tmpptr= (unsigned char*)realloc(lex->yacc_yyss,
 
2108
                                        *yystacksize* sizeof(**yyss))))
 
2109
      return 1;
 
2110
  lex->yacc_yyss= tmpptr;
2523
2111
  if (old_info)
2524
2112
  {                                             // Copy old info from stack
2525
2113
    memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
2531
2119
}
2532
2120
 
2533
2121
 
2534
 
/**
2535
 
 Reset Session part responsible for command processing state.
2536
 
 
2537
 
   This needs to be called before execution of every statement
2538
 
   (prepared or conventional).
2539
 
   It is not called by substatements of routines.
2540
 
 
2541
 
  @todo
2542
 
   Make it a method of Session and align its name with the rest of
2543
 
   reset/end/start/init methods.
2544
 
  @todo
2545
 
   Call it after we use Session for queries, not before.
2546
 
*/
2547
 
 
2548
 
void mysql_reset_session_for_next_command(Session *session)
2549
 
{
2550
 
  session->free_list= 0;
2551
 
  session->select_number= 1;
2552
 
  /*
2553
 
    Those two lines below are theoretically unneeded as
2554
 
    Session::cleanup_after_query() should take care of this already.
2555
 
  */
2556
 
  session->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
2557
 
  session->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
2558
 
 
2559
 
  session->query_start_used= 0;
2560
 
  session->is_fatal_error= session->time_zone_used= 0;
2561
 
  session->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS | 
2562
 
                          SERVER_QUERY_NO_INDEX_USED |
2563
 
                          SERVER_QUERY_NO_GOOD_INDEX_USED);
2564
 
  /*
2565
 
    If in autocommit mode and not in a transaction, reset
2566
 
    OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG to not get warnings
2567
 
    in ha_rollback_trans() about some tables couldn't be rolled back.
2568
 
  */
2569
 
  if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2570
 
  {
2571
 
    session->options&= ~OPTION_KEEP_LOG;
2572
 
    session->transaction.all.modified_non_trans_table= false;
2573
 
  }
2574
 
  assert(session->security_ctx== &session->main_security_ctx);
2575
 
  session->thread_specific_used= false;
2576
 
 
2577
 
  if (opt_bin_log)
2578
 
  {
2579
 
    reset_dynamic(&session->user_var_events);
2580
 
    session->user_var_events_alloc= session->mem_root;
2581
 
  }
2582
 
  session->clear_error();
2583
 
  session->main_da.reset_diagnostics_area();
2584
 
  session->total_warn_count=0;                  // Warnings for this query
2585
 
  session->rand_used= 0;
2586
 
  session->sent_row_count= session->examined_row_count= 0;
2587
 
 
2588
 
  /*
2589
 
    Because we come here only for start of top-statements, binlog format is
2590
 
    constant inside a complex statement (using stored functions) etc.
2591
 
  */
2592
 
  session->reset_current_stmt_binlog_row_based();
2593
 
 
2594
 
  return;
2595
 
}
2596
 
 
2597
 
 
2598
2122
void
2599
2123
mysql_init_select(LEX *lex)
2600
2124
{
2601
 
  SELECT_LEX *select_lex= lex->current_select;
 
2125
  Select_Lex *select_lex= lex->current_select;
2602
2126
  select_lex->init_select();
2603
2127
  lex->wild= 0;
2604
2128
  if (select_lex == &lex->select_lex)
2612
2136
bool
2613
2137
mysql_new_select(LEX *lex, bool move_down)
2614
2138
{
2615
 
  SELECT_LEX *select_lex;
 
2139
  Select_Lex *select_lex;
2616
2140
  Session *session= lex->session;
2617
2141
 
2618
 
  if (!(select_lex= new (session->mem_root) SELECT_LEX()))
 
2142
  if (!(select_lex= new (session->mem_root) Select_Lex()))
2619
2143
    return(1);
2620
2144
  select_lex->select_number= ++session->select_number;
2621
2145
  select_lex->parent_lex= lex; /* Used in init_query. */
2630
2154
  select_lex->nest_level= lex->nest_level;
2631
2155
  if (move_down)
2632
2156
  {
2633
 
    SELECT_LEX_UNIT *unit;
 
2157
    Select_Lex_Unit *unit;
2634
2158
    lex->subqueries= true;
2635
2159
    /* first select_lex of subselect or derived table */
2636
 
    if (!(unit= new (session->mem_root) SELECT_LEX_UNIT()))
 
2160
    if (!(unit= new (session->mem_root) Select_Lex_Unit()))
2637
2161
      return(1);
2638
2162
 
2639
2163
    unit->init_query();
2658
2182
      return(1);
2659
2183
    }
2660
2184
    select_lex->include_neighbour(lex->current_select);
2661
 
    SELECT_LEX_UNIT *unit= select_lex->master_unit();                              
 
2185
    Select_Lex_Unit *unit= select_lex->master_unit();
2662
2186
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
2663
2187
      return(1);
2664
 
    select_lex->context.outer_context= 
 
2188
    select_lex->context.outer_context=
2665
2189
                unit->first_select()->context.outer_context;
2666
2190
  }
2667
2191
 
2668
2192
  select_lex->master_unit()->global_parameters= select_lex;
2669
 
  select_lex->include_global((st_select_lex_node**)&lex->all_selects_list);
 
2193
  select_lex->include_global((Select_Lex_Node**)&lex->all_selects_list);
2670
2194
  lex->current_select= select_lex;
2671
2195
  /*
2672
2196
    in subquery is SELECT query and we allow resolution of names in SELECT
2692
2216
  LEX *lex;
2693
2217
  LEX_STRING tmp, null_lex_string;
2694
2218
  Item *var;
2695
 
  char buff[MAX_SYS_VAR_LENGTH*2+4+8], *end;
 
2219
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
 
2220
  char *end= buff;
2696
2221
 
2697
2222
  session= current_session;
2698
2223
  lex= session->lex;
2707
2232
  */
2708
2233
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
2709
2234
  {
2710
 
    end= strxmov(buff, "@@session.", var_name, NULL);
 
2235
    end+= sprintf(buff, "@@session.%s", var_name);
2711
2236
    var->set_name(buff, end-buff, system_charset_info);
2712
 
    add_item_to_list(session, var);
 
2237
    session->add_item_to_list(var);
2713
2238
  }
2714
2239
  return;
2715
2240
}
2722
2247
  lex->select_lex.select_limit= 0;
2723
2248
  lex->unit.select_limit_cnt= HA_POS_ERROR;
2724
2249
  lex->select_lex.table_list.save_and_clear(&lex->auxiliary_table_list);
2725
 
  lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
 
2250
  lex->lock_option= TL_READ;
2726
2251
  lex->query_tables= 0;
2727
2252
  lex->query_tables_last= &lex->query_tables;
2728
2253
}
2763
2288
    FIXME: cleanup the dependencies in the code to simplify this.
2764
2289
  */
2765
2290
  lex_start(session);
2766
 
  mysql_reset_session_for_next_command(session);
 
2291
  session->reset_for_next_command();
2767
2292
 
2768
2293
  {
2769
2294
    LEX *lex= session->lex;
2828
2353
 
2829
2354
  Lex_input_stream lip(session, inBuf, length);
2830
2355
  lex_start(session);
2831
 
  mysql_reset_session_for_next_command(session);
 
2356
  session->reset_for_next_command();
2832
2357
 
2833
2358
  if (!parse_sql(session, &lip) &&
2834
2359
      all_tables_not_ok(session,(TableList*) lex->select_lex.table_list.first))
2886
2411
 
2887
2412
  if (default_value)
2888
2413
  {
2889
 
    /* 
 
2414
    /*
2890
2415
      Default value should be literal => basic constants =>
2891
2416
      no need fix_fields()
2892
 
      
2893
 
      We allow only one function as part of default value - 
 
2417
 
 
2418
      We allow only one function as part of default value -
2894
2419
      NOW() as default for TIMESTAMP type.
2895
2420
    */
2896
 
    if (default_value->type() == Item::FUNC_ITEM && 
 
2421
    if (default_value->type() == Item::FUNC_ITEM &&
2897
2422
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
2898
2423
         type == DRIZZLE_TYPE_TIMESTAMP))
2899
2424
    {
2999
2524
    \#  Pointer to TableList element added to the total table list
3000
2525
*/
3001
2526
 
3002
 
TableList *st_select_lex::add_table_to_list(Session *session,
 
2527
TableList *Select_Lex::add_table_to_list(Session *session,
3003
2528
                                             Table_ident *table,
3004
2529
                                             LEX_STRING *alias,
3005
2530
                                             uint32_t table_options,
3015
2540
  if (!table)
3016
2541
    return(0);                          // End of memory
3017
2542
  alias_str= alias ? alias->str : table->table.str;
3018
 
  if (!test(table_options & TL_OPTION_ALIAS) && 
 
2543
  if (!test(table_options & TL_OPTION_ALIAS) &&
3019
2544
      check_table_name(table->table.str, table->table.length))
3020
2545
  {
3021
2546
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
3067
2592
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
3068
2593
  ptr->derived=     table->sel;
3069
2594
  if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
3070
 
                                      INFORMATION_SCHEMA_NAME.str))
 
2595
                                      INFORMATION_SCHEMA_NAME.c_str()))
3071
2596
  {
3072
2597
    ST_SCHEMA_TABLE *schema_table= find_schema_table(session, ptr->table_name);
3073
2598
    if (!schema_table ||
3074
 
        (schema_table->hidden && 
3075
 
         ((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 || 
 
2599
        (schema_table->hidden &&
 
2600
         ((sql_command_flags[lex->sql_command].test(CF_BIT_STATUS_COMMAND)) == 0 ||
3076
2601
          /*
3077
2602
            this check is used for show columns|keys from I_S hidden table
3078
2603
          */
3080
2605
          lex->sql_command == SQLCOM_SHOW_KEYS)))
3081
2606
    {
3082
2607
      my_error(ER_UNKNOWN_TABLE, MYF(0),
3083
 
               ptr->table_name, INFORMATION_SCHEMA_NAME.str);
 
2608
               ptr->table_name, INFORMATION_SCHEMA_NAME.c_str());
3084
2609
      return(0);
3085
2610
    }
3086
2611
    ptr->schema_table_name= ptr->table_name;
3147
2672
    The function initializes a structure of the TableList type
3148
2673
    for a nested join. It sets up its nested join list as empty.
3149
2674
    The created structure is added to the front of the current
3150
 
    join list in the st_select_lex object. Then the function
 
2675
    join list in the Select_Lex object. Then the function
3151
2676
    changes the current nest level for joins to refer to the newly
3152
2677
    created empty list after having saved the info on the old level
3153
2678
    in the initialized structure.
3160
2685
    1   otherwise
3161
2686
*/
3162
2687
 
3163
 
bool st_select_lex::init_nested_join(Session *session)
 
2688
bool Select_Lex::init_nested_join(Session *session)
3164
2689
{
3165
2690
  TableList *ptr;
3166
2691
  nested_join_st *nested_join;
3196
2721
    - 0, otherwise
3197
2722
*/
3198
2723
 
3199
 
TableList *st_select_lex::end_nested_join(Session *)
 
2724
TableList *Select_Lex::end_nested_join(Session *)
3200
2725
{
3201
2726
  TableList *ptr;
3202
2727
  nested_join_st *nested_join;
3237
2762
    \#  Pointer to TableList element created for the new nested join
3238
2763
*/
3239
2764
 
3240
 
TableList *st_select_lex::nest_last_join(Session *session)
 
2765
TableList *Select_Lex::nest_last_join(Session *session)
3241
2766
{
3242
2767
  TableList *ptr;
3243
2768
  nested_join_st *nested_join;
3282
2807
  Add a table to the current join list.
3283
2808
 
3284
2809
    The function puts a table in front of the current join list
3285
 
    of st_select_lex object.
 
2810
    of Select_Lex object.
3286
2811
    Thus, joined tables are put into this list in the reverse order
3287
2812
    (the most outer join operation follows first).
3288
2813
 
3292
2817
    None
3293
2818
*/
3294
2819
 
3295
 
void st_select_lex::add_joined_table(TableList *table)
 
2820
void Select_Lex::add_joined_table(TableList *table)
3296
2821
{
3297
2822
  join_list->push_front(table);
3298
2823
  table->join_list= join_list;
3332
2857
    - 0, otherwise
3333
2858
*/
3334
2859
 
3335
 
TableList *st_select_lex::convert_right_join()
 
2860
TableList *Select_Lex::convert_right_join()
3336
2861
{
3337
2862
  TableList *tab2= join_list->pop();
3338
2863
  TableList *tab1= join_list->pop();
3355
2880
    query
3356
2881
*/
3357
2882
 
3358
 
void st_select_lex::set_lock_for_tables(thr_lock_type lock_type)
 
2883
void Select_Lex::set_lock_for_tables(thr_lock_type lock_type)
3359
2884
{
3360
2885
  bool for_update= lock_type >= TL_READ_NO_INSERT;
3361
2886
 
3371
2896
 
3372
2897
 
3373
2898
/**
3374
 
  Create a fake SELECT_LEX for a unit.
 
2899
  Create a fake Select_Lex for a unit.
3375
2900
 
3376
 
    The method create a fake SELECT_LEX object for a unit.
 
2901
    The method create a fake Select_Lex object for a unit.
3377
2902
    This object is created for any union construct containing a union
3378
2903
    operation and also for any single select union construct of the form
3379
2904
    @verbatim
3380
 
    (SELECT ... order_st BY order_list [LIMIT n]) order_st BY ... 
 
2905
    (SELECT ... order_st BY order_list [LIMIT n]) order_st BY ...
3381
2906
    @endvarbatim
3382
2907
    or of the form
3383
2908
    @varbatim
3384
2909
    (SELECT ... order_st BY LIMIT n) order_st BY ...
3385
2910
    @endvarbatim
3386
 
  
 
2911
 
3387
2912
  @param session_arg               thread handle
3388
2913
 
3389
2914
  @note
3396
2921
    0     on success
3397
2922
*/
3398
2923
 
3399
 
bool st_select_lex_unit::add_fake_select_lex(Session *session_arg)
 
2924
bool Select_Lex_Unit::add_fake_select_lex(Session *session_arg)
3400
2925
{
3401
 
  SELECT_LEX *first_sl= first_select();
 
2926
  Select_Lex *first_sl= first_select();
3402
2927
  assert(!fake_select_lex);
3403
2928
 
3404
 
  if (!(fake_select_lex= new (session_arg->mem_root) SELECT_LEX()))
 
2929
  if (!(fake_select_lex= new (session_arg->mem_root) Select_Lex()))
3405
2930
      return(1);
3406
 
  fake_select_lex->include_standalone(this, 
3407
 
                                      (SELECT_LEX_NODE**)&fake_select_lex);
 
2931
  fake_select_lex->include_standalone(this,
 
2932
                                      (Select_Lex_Node**)&fake_select_lex);
3408
2933
  fake_select_lex->select_number= INT_MAX;
3409
2934
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
3410
2935
  fake_select_lex->make_empty_select();
3418
2943
 
3419
2944
  if (!is_union())
3420
2945
  {
3421
 
    /* 
3422
 
      This works only for 
 
2946
    /*
 
2947
      This works only for
3423
2948
      (SELECT ... order_st BY list [LIMIT n]) order_st BY order_list [LIMIT m],
3424
2949
      (SELECT ... LIMIT n) order_st BY order_list [LIMIT m]
3425
2950
      just before the parser starts processing order_list
3426
 
    */ 
 
2951
    */
3427
2952
    global_parameters= fake_select_lex;
3428
2953
    fake_select_lex->no_table_names_allowed= 1;
3429
2954
    session_arg->lex->current_select= fake_select_lex;
3536
3061
*/
3537
3062
 
3538
3063
void add_join_natural(TableList *a, TableList *b, List<String> *using_fields,
3539
 
                      SELECT_LEX *lex)
 
3064
                      Select_Lex *lex)
3540
3065
{
3541
3066
  b->natural_join= a;
3542
3067
  lex->prev_join_using= using_fields;
3550
3075
  @param options What should be reset/reloaded (tables, privileges, slave...)
3551
3076
  @param tables Tables to flush (if any)
3552
3077
  @param write_to_binlog True if we can write to the binlog.
3553
 
               
 
3078
 
3554
3079
  @note Depending on 'options', it may be very bad to write the
3555
3080
    query to the binlog (e.g. FLUSH SLAVE); this is a
3556
3081
    pointer where reload_cache() will put 0 if
3562
3087
    @retval !=0  Error; session->killed is set or session->is_error() is true
3563
3088
*/
3564
3089
 
3565
 
bool reload_cache(Session *session, ulong options, TableList *tables,
3566
 
                          bool *write_to_binlog)
 
3090
bool reload_cache(Session *session, ulong options, TableList *tables, bool *write_to_binlog)
3567
3091
{
3568
3092
  bool result=0;
3569
3093
  select_errors=0;                              /* Write if more errors */
3584
3108
      than it would help them)
3585
3109
    */
3586
3110
    tmp_write_to_binlog= 0;
3587
 
    if( mysql_bin_log.is_open() )
3588
 
    {
3589
 
      mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
3590
 
    }
3591
 
    pthread_mutex_lock(&LOCK_active_mi);
3592
 
    rotate_relay_log(active_mi);
3593
 
    pthread_mutex_unlock(&LOCK_active_mi);
3594
 
 
3595
 
    /* flush slow and general logs */
3596
 
    logger.flush_logs(session);
3597
3111
 
3598
3112
    if (ha_flush_logs(NULL))
3599
3113
      result=1;
3600
 
    if (flush_error_log())
3601
 
      result=1;
3602
3114
  }
3603
3115
  /*
3604
3116
    Note that if REFRESH_READ_LOCK bit is set then REFRESH_TABLES is set too
3605
3117
    (see sql_yacc.yy)
3606
3118
  */
3607
 
  if (options & (REFRESH_TABLES | REFRESH_READ_LOCK)) 
 
3119
  if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3608
3120
  {
3609
3121
    if ((options & REFRESH_READ_LOCK) && session)
3610
3122
    {
3646
3158
    else
3647
3159
      result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3648
3160
                                  false : true, false);
3649
 
    my_dbopt_cleanup();
3650
3161
  }
3651
3162
  if (session && (options & REFRESH_STATUS))
3652
 
    refresh_status(session);
3653
 
  if (options & REFRESH_MASTER)
3654
 
  {
3655
 
    assert(session);
3656
 
    tmp_write_to_binlog= 0;
3657
 
    if (reset_master(session))
3658
 
    {
3659
 
      result=1;
3660
 
    }
3661
 
  }
3662
 
 if (options & REFRESH_SLAVE)
3663
 
 {
3664
 
   tmp_write_to_binlog= 0;
3665
 
   pthread_mutex_lock(&LOCK_active_mi);
3666
 
   if (reset_slave(session, active_mi))
3667
 
     result=1;
3668
 
   pthread_mutex_unlock(&LOCK_active_mi);
3669
 
 }
 
3163
    session->refresh_status();
3670
3164
 *write_to_binlog= tmp_write_to_binlog;
 
3165
 
3671
3166
 return result;
3672
3167
}
3673
3168
 
3725
3220
{
3726
3221
  uint32_t error;
3727
3222
  if (!(error= kill_one_thread(session, id, only_kill_query)))
3728
 
    my_ok(session);
 
3223
    session->my_ok();
3729
3224
  else
3730
3225
    my_error(error, MYF(0), id);
3731
3226
}
3748
3243
    return 1;
3749
3244
  }
3750
3245
  /* Fix is using unix filename format on dos */
3751
 
  my_stpcpy(buff,*filename_ptr);
 
3246
  strcpy(buff,*filename_ptr);
3752
3247
  end=convert_dirname(buff, *filename_ptr, NULL);
3753
3248
  if (!(ptr= (char*) session->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3754
3249
    return 1;                                   // End of memory
3755
3250
  *filename_ptr=ptr;
3756
 
  strxmov(ptr,buff,table_name,NULL);
 
3251
  sprintf(ptr,"%s%s",buff,table_name);
3757
3252
  return 0;
3758
3253
}
3759
3254
 
3775
3270
  {
3776
3271
    char command[80];
3777
3272
    Lex_input_stream *lip= session->m_lip;
3778
 
    strmake(command, lip->yylval->symbol.str,
3779
 
            cmin((ulong)lip->yylval->symbol.length, sizeof(command)-1));
 
3273
    strncpy(command, lip->yylval->symbol.str,
 
3274
            cmin(lip->yylval->symbol.length, sizeof(command)-1));
 
3275
    command[cmin(lip->yylval->symbol.length, sizeof(command)-1)]=0;
3780
3276
    my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
3781
3277
    return 1;
3782
3278
  }
3784
3280
}
3785
3281
 
3786
3282
 
3787
 
Comp_creator *comp_eq_creator(bool invert)
3788
 
{
3789
 
  return invert?(Comp_creator *)&ne_creator:(Comp_creator *)&eq_creator;
3790
 
}
3791
 
 
3792
 
 
3793
 
Comp_creator *comp_ge_creator(bool invert)
3794
 
{
3795
 
  return invert?(Comp_creator *)&lt_creator:(Comp_creator *)&ge_creator;
3796
 
}
3797
 
 
3798
 
 
3799
 
Comp_creator *comp_gt_creator(bool invert)
3800
 
{
3801
 
  return invert?(Comp_creator *)&le_creator:(Comp_creator *)&gt_creator;
3802
 
}
3803
 
 
3804
 
 
3805
 
Comp_creator *comp_le_creator(bool invert)
3806
 
{
3807
 
  return invert?(Comp_creator *)&gt_creator:(Comp_creator *)&le_creator;
3808
 
}
3809
 
 
3810
 
 
3811
 
Comp_creator *comp_lt_creator(bool invert)
3812
 
{
3813
 
  return invert?(Comp_creator *)&ge_creator:(Comp_creator *)&lt_creator;
3814
 
}
3815
 
 
3816
 
 
3817
 
Comp_creator *comp_ne_creator(bool invert)
3818
 
{
3819
 
  return invert?(Comp_creator *)&eq_creator:(Comp_creator *)&ne_creator;
3820
 
}
3821
 
 
3822
 
 
3823
3283
/**
3824
3284
  Construct ALL/ANY/SOME subquery Item.
3825
3285
 
3832
3292
    constructed Item (or 0 if out of memory)
3833
3293
*/
3834
3294
Item * all_any_subquery_creator(Item *left_expr,
3835
 
                                chooser_compare_func_creator cmp,
3836
 
                                bool all,
3837
 
                                SELECT_LEX *select_lex)
 
3295
                                chooser_compare_func_creator cmp,
 
3296
                                bool all,
 
3297
                                Select_Lex *select_lex)
3838
3298
{
3839
3299
  if ((cmp == &comp_eq_creator) && !all)       //  = ANY <=> IN
3840
3300
    return new Item_in_subselect(left_expr, select_lex);
3852
3312
 
3853
3313
 
3854
3314
/**
3855
 
  Multi update query pre-check.
 
3315
  Update query pre-check.
3856
3316
 
3857
3317
  @param session                Thread handler
3858
3318
  @param tables Global/local table list (have to be the same)
3863
3323
    true  Error
3864
3324
*/
3865
3325
 
3866
 
bool multi_update_precheck(Session *session, TableList *)
 
3326
bool update_precheck(Session *session, TableList *)
3867
3327
{
3868
3328
  const char *msg= 0;
3869
3329
  LEX *lex= session->lex;
3870
 
  SELECT_LEX *select_lex= &lex->select_lex;
 
3330
  Select_Lex *select_lex= &lex->select_lex;
3871
3331
 
3872
 
  if (select_lex->item_list.elements != lex->value_list.elements)
 
3332
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
3873
3333
  {
3874
3334
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
3875
3335
    return(true);
3876
3336
  }
3877
3337
 
3878
 
  if (select_lex->order_list.elements)
3879
 
    msg= "ORDER BY";
3880
 
  else if (select_lex->select_limit)
3881
 
    msg= "LIMIT";
3882
 
  if (msg)
 
3338
  if (session->lex->select_lex.table_list.elements > 1)
3883
3339
  {
3884
 
    my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
3885
 
    return(true);
 
3340
    if (select_lex->order_list.elements)
 
3341
      msg= "ORDER BY";
 
3342
    else if (select_lex->select_limit)
 
3343
      msg= "LIMIT";
 
3344
    if (msg)
 
3345
    {
 
3346
      my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
 
3347
      return(true);
 
3348
    }
3886
3349
  }
3887
3350
  return(false);
3888
3351
}
3901
3364
 
3902
3365
bool multi_delete_precheck(Session *session, TableList *)
3903
3366
{
3904
 
  SELECT_LEX *select_lex= &session->lex->select_lex;
 
3367
  Select_Lex *select_lex= &session->lex->select_lex;
3905
3368
  TableList **save_query_tables_own_last= session->lex->query_tables_own_last;
3906
3369
 
3907
3370
  session->lex->query_tables_own_last= 0;
4014
3477
 
4015
3478
 
4016
3479
/**
4017
 
  simple UPDATE query pre-check.
4018
 
 
4019
 
  @param session                Thread handler
4020
 
  @param tables Global table list
4021
 
 
4022
 
  @retval
4023
 
    false OK
4024
 
  @retval
4025
 
    true  Error
4026
 
*/
4027
 
 
4028
 
bool update_precheck(Session *session, TableList *)
4029
 
{
4030
 
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
4031
 
  {
4032
 
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
4033
 
    return(true);
4034
 
  }
4035
 
  return(false);
4036
 
}
4037
 
 
4038
 
 
4039
 
/**
4040
3480
  simple INSERT query pre-check.
4041
3481
 
4042
3482
  @param session                Thread handler
4085
3525
 
4086
3526
  if (create_table && (strcmp(create_table->db, "information_schema") == 0))
4087
3527
  {
4088
 
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
 
3528
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
4089
3529
    return(true);
4090
3530
  }
4091
3531
 
4162
3602
}
4163
3603
 
4164
3604
 
4165
 
bool check_identifier_name(LEX_STRING *str, uint32_t max_char_length,
4166
 
                           uint32_t err_code, const char *param_for_err_msg)
 
3605
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
 
3606
                           uint32_t max_char_length,
 
3607
                           const char *param_for_err_msg)
4167
3608
{
4168
 
#ifdef HAVE_CHARSET_utf8mb3
4169
3609
  /*
4170
3610
    We don't support non-BMP characters in identifiers at the moment,
4171
3611
    so they should be prohibited until such support is done.
4172
3612
    This is why we use the 3-byte utf8 to check well-formedness here.
4173
3613
  */
4174
 
  const CHARSET_INFO * const cs= &my_charset_utf8mb3_general_ci;
4175
 
#else
4176
 
  const CHARSET_INFO * const cs= system_charset_info;
4177
 
#endif
 
3614
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
 
3615
 
4178
3616
  int well_formed_error;
4179
3617
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4180
3618
                                      max_char_length, &well_formed_error);
4184
3622
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str->str);
4185
3623
    return true;
4186
3624
  }
4187
 
  
 
3625
 
4188
3626
  if (str->length == res)
4189
3627
    return false;
4190
3628
 
4216
3654
 
4217
3655
  RETURN VALUES
4218
3656
    0   ok
4219
 
    1   error  
 
3657
    1   error
4220
3658
*/
4221
3659
 
4222
3660
bool test_if_data_home_dir(const char *dir)