~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_parse.cc

Merge/fix in FAQ.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#define DRIZZLE_LEX 1
17
 
#include <drizzled/server_includes.h>
 
16
#define MYSQL_LEX 1
 
17
#include "mysql_priv.h"
18
18
#include "sql_repl.h"
19
19
#include "rpl_filter.h"
20
20
#include "repl_failsafe.h"
21
 
#include "logging.h"
22
 
#include <drizzled/drizzled_error_messages.h>
 
21
#include <m_ctype.h>
 
22
#include <myisam.h>
 
23
#include <my_dir.h>
23
24
 
24
25
/**
25
26
  @defgroup Runtime_Environment Runtime Environment
26
27
  @{
27
28
*/
28
29
 
 
30
static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables);
29
31
 
30
32
const char *any_db="*any*";     // Special symbol for check_access
31
33
 
32
 
const LEX_STRING command_name[COM_END+1]={
 
34
const LEX_STRING command_name[]={
33
35
  { C_STRING_WITH_LEN("Sleep") },
34
36
  { C_STRING_WITH_LEN("Quit") },
35
37
  { C_STRING_WITH_LEN("Init DB") },
39
41
  { C_STRING_WITH_LEN("Drop DB") },
40
42
  { C_STRING_WITH_LEN("Refresh") },
41
43
  { C_STRING_WITH_LEN("Shutdown") },
 
44
  { C_STRING_WITH_LEN("Statistics") },
42
45
  { C_STRING_WITH_LEN("Processlist") },
43
46
  { C_STRING_WITH_LEN("Connect") },
44
47
  { C_STRING_WITH_LEN("Kill") },
 
48
  { C_STRING_WITH_LEN("Debug") },
45
49
  { C_STRING_WITH_LEN("Ping") },
46
50
  { C_STRING_WITH_LEN("Time") },
 
51
  { C_STRING_WITH_LEN("Delayed insert") },
47
52
  { C_STRING_WITH_LEN("Change user") },
48
53
  { C_STRING_WITH_LEN("Binlog Dump") },
 
54
  { C_STRING_WITH_LEN("Table Dump") },
49
55
  { C_STRING_WITH_LEN("Connect Out") },
50
56
  { C_STRING_WITH_LEN("Register Slave") },
 
57
  { C_STRING_WITH_LEN("Prepare") },
 
58
  { C_STRING_WITH_LEN("Execute") },
 
59
  { C_STRING_WITH_LEN("Long Data") },
 
60
  { C_STRING_WITH_LEN("Close stmt") },
 
61
  { C_STRING_WITH_LEN("Reset stmt") },
51
62
  { C_STRING_WITH_LEN("Set option") },
 
63
  { C_STRING_WITH_LEN("Fetch") },
52
64
  { C_STRING_WITH_LEN("Daemon") },
53
65
  { C_STRING_WITH_LEN("Error") }  // Last command number
54
66
};
119
131
    LEX *lex= thd->lex;
120
132
    thd->options|= OPTION_BEGIN;
121
133
    thd->server_status|= SERVER_STATUS_IN_TRANS;
122
 
    if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
 
134
    if (lex->start_transaction_opt & MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
123
135
      error= ha_start_consistent_snapshot(thd);
124
136
  }
125
137
  return error;
128
140
/**
129
141
  Returns true if all tables should be ignored.
130
142
*/
131
 
inline bool all_tables_not_ok(THD *thd, TableList *tables)
 
143
inline bool all_tables_not_ok(THD *thd, TABLE_LIST *tables)
132
144
{
133
145
  return rpl_filter->is_on() && tables &&
134
146
         !rpl_filter->tables_ok(thd->db, tables);
135
147
}
136
148
 
137
149
 
138
 
static bool some_non_temp_table_to_be_updated(THD *thd, TableList *tables)
 
150
static bool some_non_temp_table_to_be_updated(THD *thd, TABLE_LIST *tables)
139
151
{
140
 
  for (TableList *table= tables; table; table= table->next_global)
 
152
  for (TABLE_LIST *table= tables; table; table= table->next_global)
141
153
  {
142
154
    assert(table->db && table->table_name);
143
155
    if (table->updating &&
161
173
          a number of modified rows
162
174
*/
163
175
 
164
 
uint32_t sql_command_flags[SQLCOM_END+1];
 
176
uint sql_command_flags[SQLCOM_END+1];
165
177
 
166
178
void init_update_queries(void)
167
179
{
168
 
  memset(&sql_command_flags, 0, sizeof(sql_command_flags));
 
180
  bzero((uchar*) &sql_command_flags, sizeof(sql_command_flags));
169
181
 
170
182
  sql_command_flags[SQLCOM_CREATE_TABLE]=   CF_CHANGES_DATA;
171
183
  sql_command_flags[SQLCOM_CREATE_INDEX]=   CF_CHANGES_DATA;
241
253
  */
242
254
  rw_rdlock(var_mutex);
243
255
  save_client_capabilities= thd->client_capabilities;
244
 
  thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
 
256
  thd->client_capabilities|= CLIENT_MULTI_QUERIES;
245
257
  /*
246
258
    We don't need return result of execution to client side.
247
259
    To forbid this we should set thd->net.vio to 0.
405
417
  if (packet_length == 0)                       /* safety */
406
418
  {
407
419
    /* Initialize with COM_SLEEP packet */
408
 
    packet[0]= (unsigned char) COM_SLEEP;
 
420
    packet[0]= (uchar) COM_SLEEP;
409
421
    packet_length= 1;
410
422
  }
411
423
  /* Do not rely on my_net_read, extra safety against programming errors. */
412
424
  packet[packet_length]= '\0';                  /* safety */
413
425
 
414
 
  command= (enum enum_server_command) (unsigned char) packet[0];
 
426
  command= (enum enum_server_command) (uchar) packet[0];
415
427
 
416
428
  if (command >= COM_END)
417
429
    command= COM_END;                           // Wrong command
420
432
  my_net_set_read_timeout(net, thd->variables.net_read_timeout);
421
433
 
422
434
  assert(packet_length);
423
 
  return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
 
435
  return_value= dispatch_command(command, thd, packet+1, (uint) (packet_length-1));
424
436
 
425
437
out:
426
438
  return(return_value);
442
454
*/
443
455
 
444
456
static bool deny_updates_if_read_only_option(THD *thd,
445
 
                                                TableList *all_tables)
 
457
                                                TABLE_LIST *all_tables)
446
458
{
447
459
  if (!opt_readonly)
448
460
    return(false);
508
520
        COM_QUIT/COM_SHUTDOWN
509
521
*/
510
522
bool dispatch_command(enum enum_server_command command, THD *thd,
511
 
                      char* packet, uint32_t packet_length)
 
523
                      char* packet, uint packet_length)
512
524
{
513
525
  NET *net= &thd->net;
514
526
  bool error= 0;
521
533
  thd->enable_slow_log= true;
522
534
  thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
523
535
  thd->set_time();
524
 
  pthread_mutex_lock(&LOCK_thread_count);
 
536
  VOID(pthread_mutex_lock(&LOCK_thread_count));
525
537
  thd->query_id= global_query_id;
526
538
 
527
539
  switch( command ) {
528
540
  /* Ignore these statements. */
 
541
  case COM_STATISTICS:
529
542
  case COM_PING:
530
543
    break;
531
544
  /* Increase id and count all other statements. */
536
549
 
537
550
  thread_running++;
538
551
  /* TODO: set thd->lex->sql_command to SQLCOM_END here */
539
 
  pthread_mutex_unlock(&LOCK_thread_count);
540
 
 
541
 
  logging_pre_do(thd);
 
552
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
542
553
 
543
554
  thd->server_status&=
544
555
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
551
562
                        packet, packet_length, thd->charset());
552
563
    if (!mysql_change_db(thd, &tmp, false))
553
564
    {
 
565
      general_log_write(thd, command, thd->db, thd->db_length);
554
566
      my_ok(thd);
555
567
    }
556
568
    break;
557
569
  }
558
570
  case COM_REGISTER_SLAVE:
559
571
  {
560
 
    if (!register_slave(thd, (unsigned char*)packet, packet_length))
 
572
    if (!register_slave(thd, (uchar*)packet, packet_length))
561
573
      my_ok(thd);
562
574
    break;
563
575
  }
566
578
    status_var_increment(thd->status_var.com_other);
567
579
    char *user= (char*) packet, *packet_end= packet + packet_length;
568
580
    /* Safe because there is always a trailing \0 at the end of the packet */
569
 
    char *passwd= strchr(user, '\0')+1;
570
 
 
 
581
    char *passwd= strend(user)+1;
571
582
 
572
583
    thd->clear_error();                         // if errors from rollback
573
584
 
577
588
      terminated, so also '\0' for empty string).
578
589
 
579
590
      Cast *passwd to an unsigned char, so that it doesn't extend the sign
580
 
      for *passwd > 127 and become 2**32-127 after casting to uint32_t.
 
591
      for *passwd > 127 and become 2**32-127 after casting to uint.
581
592
    */
582
593
    char db_buff[NAME_LEN+1];                 // buffer to store db in utf8
583
594
    char *db= passwd;
591
602
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
592
603
      break;
593
604
    }
594
 
    uint32_t passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
595
 
                      (unsigned char)(*passwd++) : strlen(passwd));
596
 
    uint32_t dummy_errors, save_db_length, db_length;
 
605
    uint passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
 
606
                      (uchar)(*passwd++) : strlen(passwd));
 
607
    uint dummy_errors, save_db_length, db_length;
597
608
    int res;
598
609
    Security_context save_security_ctx= *thd->security_ctx;
599
610
    USER_CONN *save_user_connect;
611
622
    db_length= strlen(db);
612
623
 
613
624
    char *ptr= db + db_length + 1;
614
 
    uint32_t cs_number= 0;
 
625
    uint cs_number= 0;
615
626
 
616
627
    if (ptr < packet_end)
617
628
    {
644
655
 
645
656
    /* Clear variables that are allocated */
646
657
    thd->user_connect= 0;
647
 
    res= check_user(thd, passwd, passwd_len, db, false);
 
658
    thd->security_ctx->priv_user= thd->security_ctx->user;
 
659
    res= check_user(thd, COM_CHANGE_USER, passwd, passwd_len, db, false);
648
660
 
649
661
    if (res)
650
662
    {
651
 
      if (thd->security_ctx->user)
652
 
        free(thd->security_ctx->user);
 
663
      x_free(thd->security_ctx->user);
653
664
      *thd->security_ctx= save_security_ctx;
654
665
      thd->user_connect= save_user_connect;
655
666
      thd->db= save_db;
657
668
    }
658
669
    else
659
670
    {
660
 
      if (save_db)
661
 
        free(save_db);
662
 
      if (save_security_ctx.user)
663
 
        free(save_security_ctx.user);
 
671
      x_free(save_db);
 
672
      x_free(save_security_ctx.user);
664
673
 
665
674
      if (cs_number)
666
675
      {
677
686
    char *packet_end= thd->query + thd->query_length;
678
687
    const char* end_of_stmt= NULL;
679
688
 
 
689
    general_log_write(thd, command, thd->query, thd->query_length);
 
690
 
680
691
    mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
681
692
 
682
693
    while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
699
710
        length--;
700
711
      }
701
712
 
702
 
      pthread_mutex_lock(&LOCK_thread_count);
 
713
      VOID(pthread_mutex_lock(&LOCK_thread_count));
703
714
      thd->query_length= length;
704
715
      thd->query= beginning_of_next_stmt;
705
716
      /*
709
720
      thd->query_id= next_query_id();
710
721
      thd->set_time(); /* Reset the query start time. */
711
722
      /* TODO: set thd->lex->sql_command to SQLCOM_END here */
712
 
      pthread_mutex_unlock(&LOCK_thread_count);
 
723
      VOID(pthread_mutex_unlock(&LOCK_thread_count));
713
724
 
714
725
      mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
715
726
    }
719
730
  {
720
731
    char *fields, *packet_end= packet + packet_length, *arg_end;
721
732
    /* Locked closure of all tables */
722
 
    TableList table_list;
 
733
    TABLE_LIST table_list;
723
734
    LEX_STRING conv_name;
724
735
 
725
736
    /* used as fields initializator */
726
737
    lex_start(thd);
727
738
 
728
739
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
729
 
    memset(&table_list, 0, sizeof(table_list));
 
740
    bzero((char*) &table_list,sizeof(table_list));
730
741
    if (thd->copy_db_to(&table_list.db, &table_list.db_length))
731
742
      break;
732
743
    /*
733
744
      We have name + wildcard in packet, separated by endzero
734
745
    */
735
 
    arg_end= strchr(packet, '\0');
 
746
    arg_end= strend(packet);
736
747
    thd->convert_string(&conv_name, system_charset_info,
737
 
                        packet, (uint32_t) (arg_end - packet), thd->charset());
 
748
                        packet, (uint) (arg_end - packet), thd->charset());
738
749
    table_list.alias= table_list.table_name= conv_name.str;
739
750
    packet= arg_end + 1;
740
751
 
746
757
        table_list.schema_table= schema_table;
747
758
    }
748
759
 
749
 
    thd->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
 
760
    thd->query_length= (uint) (packet_end - packet); // Don't count end \0
750
761
    if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
751
762
      break;
 
763
    general_log_print(thd, command, "%s %s", table_list.table_name, fields);
752
764
    if (lower_case_table_names)
753
765
      my_casedn_str(files_charset_info, table_list.table_name);
754
766
 
759
771
    mysql_reset_thd_for_next_command(thd);
760
772
 
761
773
    thd->lex->
762
 
      select_lex.table_list.link_in_list((unsigned char*) &table_list,
763
 
                                         (unsigned char**) &table_list.next_local);
 
774
      select_lex.table_list.link_in_list((uchar*) &table_list,
 
775
                                         (uchar**) &table_list.next_local);
764
776
    thd->lex->add_to_query_tables(&table_list);
765
777
 
766
778
    /* switch on VIEW optimisation: do not fill temporary tables */
772
784
  }
773
785
  case COM_QUIT:
774
786
    /* We don't calculate statistics for this command */
 
787
    general_log_print(thd, command, NullS);
775
788
    net->error=0;                               // Don't give 'abort' message
776
789
    thd->main_da.disable_status();              // Don't send anything back
777
790
    error=true;                                 // End server
779
792
  case COM_BINLOG_DUMP:
780
793
    {
781
794
      ulong pos;
782
 
      uint16_t flags;
783
 
      uint32_t slave_server_id;
 
795
      ushort flags;
 
796
      uint32 slave_server_id;
784
797
 
785
798
      status_var_increment(thd->status_var.com_other);
786
799
      thd->enable_slow_log= opt_log_slow_admin_statements;
792
805
        kill_zombie_dump_threads(slave_server_id);
793
806
      thd->server_id = slave_server_id;
794
807
 
 
808
      general_log_print(thd, command, "Log: '%s'  Pos: %ld", packet+10,
 
809
                      (long) pos);
795
810
      mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
796
811
      unregister_slave(thd,1,1);
797
812
      /*  fake COM_QUIT -- if we get here, the thread needs to terminate */
801
816
  case COM_SHUTDOWN:
802
817
  {
803
818
    status_var_increment(thd->status_var.com_other);
 
819
    /*
 
820
      If the client is < 4.1.3, it is going to send us no argument; then
 
821
      packet_length is 0, packet[0] is the end 0 of the packet. Note that
 
822
      SHUTDOWN_DEFAULT is 0. If client is >= 4.1.3, the shutdown level is in
 
823
      packet[0].
 
824
    */
 
825
    enum mysql_enum_shutdown_level level=
 
826
      (enum mysql_enum_shutdown_level) (uchar) packet[0];
 
827
    if (level == SHUTDOWN_DEFAULT)
 
828
      level= SHUTDOWN_WAIT_ALL_BUFFERS; // soon default will be configurable
 
829
    else if (level != SHUTDOWN_WAIT_ALL_BUFFERS)
 
830
    {
 
831
      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "this shutdown level");
 
832
      break;
 
833
    }
 
834
    general_log_print(thd, command, NullS);
804
835
    my_eof(thd);
805
836
    close_thread_tables(thd);                   // Free before kill
806
837
    kill_mysql();
807
838
    error=true;
808
839
    break;
809
840
  }
 
841
  case COM_STATISTICS:
 
842
  {
 
843
    STATUS_VAR current_global_status_var;
 
844
    ulong uptime;
 
845
    uint length;
 
846
    uint64_t queries_per_second1000;
 
847
    char buff[250];
 
848
    uint buff_len= sizeof(buff);
 
849
 
 
850
    general_log_print(thd, command, NullS);
 
851
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_STATUS]);
 
852
    calc_sum_of_all_status(&current_global_status_var);
 
853
    if (!(uptime= (ulong) (thd->start_time - server_start_time)))
 
854
      queries_per_second1000= 0;
 
855
    else
 
856
      queries_per_second1000= thd->query_id * 1000LL / uptime;
 
857
 
 
858
    length= snprintf((char*) buff, buff_len - 1,
 
859
                     "Uptime: %lu  Threads: %d  Questions: %lu  "
 
860
                     "Slow queries: %lu  Opens: %lu  Flush tables: %lu  "
 
861
                     "Open tables: %u  Queries per second avg: %u.%u",
 
862
                     uptime,
 
863
                     (int) thread_count, (ulong) thd->query_id,
 
864
                     current_global_status_var.long_query_count,
 
865
                     current_global_status_var.opened_tables,
 
866
                     refresh_version,
 
867
                     cached_open_tables(),
 
868
                     (uint) (queries_per_second1000 / 1000),
 
869
                     (uint) (queries_per_second1000 % 1000));
 
870
    /* Store the buffer in permanent memory */
 
871
    my_ok(thd, 0, 0, buff);
 
872
    VOID(my_net_write(net, (uchar*) buff, length));
 
873
    VOID(net_flush(net));
 
874
    thd->main_da.disable_status();
 
875
    break;
 
876
  }
810
877
  case COM_PING:
811
878
    status_var_increment(thd->status_var.com_other);
812
879
    my_ok(thd);                         // Tell client we are alive
813
880
    break;
814
881
  case COM_PROCESS_INFO:
815
882
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
816
 
    mysqld_list_processes(thd, NULL, 0);
 
883
    general_log_print(thd, command, NullS);
 
884
    mysqld_list_processes(thd, NullS, 0);
817
885
    break;
818
886
  case COM_PROCESS_KILL:
819
887
  {
825
893
  case COM_SET_OPTION:
826
894
  {
827
895
    status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
828
 
    uint32_t opt_command= uint2korr(packet);
 
896
    uint opt_command= uint2korr(packet);
829
897
 
830
898
    switch (opt_command) {
831
 
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
 
899
    case (int) MYSQL_OPTION_MULTI_STATEMENTS_ON:
832
900
      thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
833
901
      my_eof(thd);
834
902
      break;
835
 
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
 
903
    case (int) MYSQL_OPTION_MULTI_STATEMENTS_OFF:
836
904
      thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
837
905
      my_eof(thd);
838
906
      break;
873
941
 
874
942
  net_end_statement(thd);
875
943
 
876
 
  thd->set_proc_info("closing tables");
 
944
  thd->proc_info= "closing tables";
877
945
  /* Free tables */
878
946
  close_thread_tables(thd);
879
947
 
880
948
  log_slow_statement(thd);
881
949
 
882
950
  thd_proc_info(thd, "cleaning up");
883
 
  pthread_mutex_lock(&LOCK_thread_count); // For process list
 
951
  VOID(pthread_mutex_lock(&LOCK_thread_count)); // For process list
884
952
  thd_proc_info(thd, 0);
885
953
  thd->command=COM_SLEEP;
886
954
  thd->query=0;
887
955
  thd->query_length=0;
888
956
  thread_running--;
889
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
957
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
890
958
  thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
891
959
  free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
892
960
  return(error);
903
971
  if (unlikely(thd->in_sub_stmt))
904
972
    return;                           // Don't set time for sub stmt
905
973
 
906
 
  logging_post_do(thd);
 
974
  /*
 
975
    Do not log administrative statements unless the appropriate option is
 
976
    set; do not log into slow log if reading from backup.
 
977
  */
 
978
  if (thd->enable_slow_log && !thd->user_time)
 
979
  {
 
980
    thd_proc_info(thd, "logging slow query");
 
981
    uint64_t end_utime_of_query= thd->current_utime();
907
982
 
 
983
    if (((end_utime_of_query - thd->utime_after_lock) >
 
984
         thd->variables.long_query_time ||
 
985
         ((thd->server_status &
 
986
           (SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED)) &&
 
987
          opt_log_queries_not_using_indexes &&
 
988
           !(sql_command_flags[thd->lex->sql_command] & CF_STATUS_COMMAND))) &&
 
989
        thd->examined_row_count >= thd->variables.min_examined_row_limit)
 
990
    {
 
991
      thd_proc_info(thd, "logging slow query");
 
992
      thd->status_var.long_query_count++;
 
993
      slow_log_print(thd, thd->query, thd->query_length, end_utime_of_query);
 
994
    }
 
995
  }
908
996
  return;
909
997
}
910
998
 
911
999
 
912
1000
/**
913
 
  Create a TableList object for an INFORMATION_SCHEMA table.
 
1001
  Create a TABLE_LIST object for an INFORMATION_SCHEMA table.
914
1002
 
915
1003
    This function is used in the parser to convert a SHOW or DESCRIBE
916
1004
    table_name command to a SELECT from INFORMATION_SCHEMA.
917
 
    It prepares a SELECT_LEX and a TableList object to represent the
 
1005
    It prepares a SELECT_LEX and a TABLE_LIST object to represent the
918
1006
    given command as a SELECT parse tree.
919
1007
 
920
1008
  @param thd              thread handle
969
1057
  case SCH_STATISTICS:
970
1058
  {
971
1059
    assert(table_ident);
972
 
    TableList **query_tables_last= lex->query_tables_last;
 
1060
    TABLE_LIST **query_tables_last= lex->query_tables_last;
973
1061
    schema_select_lex= new SELECT_LEX();
974
1062
    /* 'parent_lex' is used in init_query() so it must be before it. */
975
1063
    schema_select_lex->parent_lex= lex;
997
1085
  {
998
1086
    return(1);
999
1087
  }
1000
 
  TableList *table_list= (TableList*) select_lex->table_list.first;
 
1088
  TABLE_LIST *table_list= (TABLE_LIST*) select_lex->table_list.first;
1001
1089
  assert(table_list);
1002
1090
  table_list->schema_select_lex= schema_select_lex;
1003
1091
  table_list->schema_table_reformed= 1;
1019
1107
    true  error;  In this case thd->fatal_error is set
1020
1108
*/
1021
1109
 
1022
 
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
 
1110
bool alloc_query(THD *thd, const char *packet, uint packet_length)
1023
1111
{
1024
1112
  /* Remove garbage at start and end of query */
1025
1113
  while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
1036
1124
  }
1037
1125
  /* We must allocate some extra memory for query cache */
1038
1126
  thd->query_length= 0;                        // Extra safety: Avoid races
1039
 
  if (!(thd->query= (char*) thd->memdup_w_gap((unsigned char*) (packet),
 
1127
  if (!(thd->query= (char*) thd->memdup_w_gap((uchar*) (packet),
1040
1128
                                              packet_length,
1041
1129
                                              thd->db_length+ 1)))
1042
1130
    return true;
1108
1196
  /* first SELECT_LEX (have special meaning for many of non-SELECTcommands) */
1109
1197
  SELECT_LEX *select_lex= &lex->select_lex;
1110
1198
  /* first table of first SELECT_LEX */
1111
 
  TableList *first_table= (TableList*) select_lex->table_list.first;
 
1199
  TABLE_LIST *first_table= (TABLE_LIST*) select_lex->table_list.first;
1112
1200
  /* list of all tables in query */
1113
 
  TableList *all_tables;
 
1201
  TABLE_LIST *all_tables;
1114
1202
  /* most outer SELECT_LEX_UNIT of query */
1115
1203
  SELECT_LEX_UNIT *unit= &lex->unit;
1116
1204
  /* Saved variable value */
1135
1223
  all_tables= lex->query_tables;
1136
1224
  /* set context for commands which do not use setup_tables */
1137
1225
  select_lex->
1138
 
    context.resolve_in_table_list_only((TableList*)select_lex->
 
1226
    context.resolve_in_table_list_only((TABLE_LIST*)select_lex->
1139
1227
                                       table_list.first);
1140
1228
 
1141
1229
  /*
1146
1234
    Don't reset warnings when executing a stored routine.
1147
1235
  */
1148
1236
  if (all_tables || !lex->is_single_level_stmt())
1149
 
    drizzle_reset_errors(thd, 0);
 
1237
    mysql_reset_errors(thd, 0);
1150
1238
 
1151
1239
  if (unlikely(thd->slave_thread))
1152
1240
  {
1274
1362
  }
1275
1363
  case SQLCOM_SHOW_WARNS:
1276
1364
  {
1277
 
    res= mysqld_show_warnings(thd, (uint32_t)
1278
 
                              ((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1279
 
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1280
 
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
 
1365
    res= mysqld_show_warnings(thd, (ulong)
 
1366
                              ((1L << (uint) MYSQL_ERROR::WARN_LEVEL_NOTE) |
 
1367
                               (1L << (uint) MYSQL_ERROR::WARN_LEVEL_WARN) |
 
1368
                               (1L << (uint) MYSQL_ERROR::WARN_LEVEL_ERROR)
1281
1369
                               ));
1282
1370
    break;
1283
1371
  }
1284
1372
  case SQLCOM_SHOW_ERRORS:
1285
1373
  {
1286
 
    res= mysqld_show_warnings(thd, (uint32_t)
1287
 
                              (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
 
1374
    res= mysqld_show_warnings(thd, (ulong)
 
1375
                              (1L << (uint) MYSQL_ERROR::WARN_LEVEL_ERROR));
1288
1376
    break;
1289
1377
  }
1290
1378
  case SQLCOM_SHOW_SLAVE_HOSTS:
1320
1408
    }
1321
1409
    else
1322
1410
    {
1323
 
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1411
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1324
1412
                   "the master info structure does not exist");
1325
1413
      my_ok(thd);
1326
1414
    }
1352
1440
    assert(first_table == all_tables && first_table != 0);
1353
1441
    bool link_to_local;
1354
1442
    // Skip first table, which is the table we are creating
1355
 
    TableList *create_table= lex->unlink_first_table(&link_to_local);
1356
 
    TableList *select_tables= lex->query_tables;
 
1443
    TABLE_LIST *create_table= lex->unlink_first_table(&link_to_local);
 
1444
    TABLE_LIST *select_tables= lex->query_tables;
1357
1445
    /*
1358
1446
      Code below (especially in mysql_create_table() and select_create
1359
1447
      methods) may modify HA_CREATE_INFO structure in LEX, so we have to
1429
1517
      select_lex->options|= SELECT_NO_UNLOCK;
1430
1518
      unit->set_limit(select_lex);
1431
1519
 
 
1520
      /*
 
1521
        Disable non-empty MERGE tables with CREATE...SELECT. Too
 
1522
        complicated. See Bug #26379. Empty MERGE tables are read-only
 
1523
        and don't allow CREATE...SELECT anyway.
 
1524
      */
 
1525
      if (create_info.used_fields & HA_CREATE_USED_UNION)
 
1526
      {
 
1527
        my_error(ER_WRONG_OBJECT, MYF(0), create_table->db,
 
1528
                 create_table->table_name, "BASE TABLE");
 
1529
        res= 1;
 
1530
        goto end_with_restore_list;
 
1531
      }
 
1532
 
1432
1533
      if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1433
1534
      {
1434
1535
        lex->link_first_table_back(create_table, link_to_local);
1443
1544
        */
1444
1545
        if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1445
1546
        {
1446
 
          TableList *duplicate;
 
1547
          TABLE_LIST *duplicate;
1447
1548
          create_table= lex->unlink_first_table(&link_to_local);
1448
1549
          if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
1449
1550
          {
1452
1553
            goto end_with_restore_list;
1453
1554
          }
1454
1555
        }
 
1556
        /* If we create merge table, we have to test tables in merge, too */
 
1557
        if (create_info.used_fields & HA_CREATE_USED_UNION)
 
1558
        {
 
1559
          TABLE_LIST *tab;
 
1560
          for (tab= (TABLE_LIST*) create_info.merge_list.first;
 
1561
               tab;
 
1562
               tab= tab->next_local)
 
1563
          {
 
1564
            TABLE_LIST *duplicate;
 
1565
            if ((duplicate= unique_table(thd, tab, select_tables, 0)))
 
1566
            {
 
1567
              update_non_unique_table_error(tab, "CREATE", duplicate);
 
1568
              res= 1;
 
1569
              goto end_with_restore_list;
 
1570
            }
 
1571
          }
 
1572
        }
1455
1573
 
1456
1574
        /*
1457
1575
          select_create is currently not re-execution friendly and
1530
1648
    */
1531
1649
    thd->enable_slow_log= opt_log_slow_admin_statements;
1532
1650
 
1533
 
    memset(&create_info, 0, sizeof(create_info));
 
1651
    bzero((char*) &create_info, sizeof(create_info));
1534
1652
    create_info.db_type= 0;
1535
1653
    create_info.row_type= ROW_TYPE_NOT_USED;
1536
1654
    create_info.default_table_charset= thd->variables.collation_database;
1537
1655
 
1538
1656
    res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1539
1657
                           &create_info, first_table, &alter_info,
1540
 
                           0, (order_st*) 0, 0);
 
1658
                           0, (ORDER*) 0, 0);
1541
1659
    break;
1542
1660
  }
1543
1661
  case SQLCOM_SLAVE_START:
1595
1713
      assert(select_lex->db);
1596
1714
 
1597
1715
      { // Rename of table
1598
 
          TableList tmp_table;
1599
 
          memset(&tmp_table, 0, sizeof(tmp_table));
 
1716
          TABLE_LIST tmp_table;
 
1717
          bzero((char*) &tmp_table,sizeof(tmp_table));
1600
1718
          tmp_table.table_name= lex->name.str;
1601
1719
          tmp_table.db=select_lex->db;
1602
1720
      }
1603
1721
 
1604
1722
      /* Don't yet allow changing of symlinks with ALTER TABLE */
1605
1723
      if (create_info.data_file_name)
1606
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1724
        push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1607
1725
                     "DATA DIRECTORY option ignored");
1608
1726
      if (create_info.index_file_name)
1609
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1727
        push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1610
1728
                     "INDEX DIRECTORY option ignored");
1611
1729
      create_info.data_file_name= create_info.index_file_name= NULL;
1612
1730
      /* ALTER TABLE ends previous transaction */
1626
1744
                             first_table,
1627
1745
                             &alter_info,
1628
1746
                             select_lex->order_list.elements,
1629
 
                             (order_st *) select_lex->order_list.first,
 
1747
                             (ORDER *) select_lex->order_list.first,
1630
1748
                             lex->ignore);
1631
1749
      break;
1632
1750
    }
1633
1751
  case SQLCOM_RENAME_TABLE:
1634
1752
  {
1635
1753
    assert(first_table == all_tables && first_table != 0);
1636
 
    TableList *table;
 
1754
    TABLE_LIST *table;
1637
1755
    for (table= first_table; table; table= table->next_local->next_local)
1638
1756
    {
1639
 
      TableList old_list, new_list;
 
1757
      TABLE_LIST old_list, new_list;
1640
1758
      /*
1641
1759
        we do not need initialize old_list and new_list because we will
1642
1760
        come table[0] and table->next[0] there
1674
1792
    thd->enable_slow_log= opt_log_slow_admin_statements;
1675
1793
    res= mysql_repair_table(thd, first_table, &lex->check_opt);
1676
1794
    /* ! we write after unlocking the table */
1677
 
    /*
1678
 
      Presumably, REPAIR and binlog writing doesn't require synchronization
1679
 
    */
1680
 
    write_bin_log(thd, true, thd->query, thd->query_length);
1681
 
    select_lex->table_list.first= (unsigned char*) first_table;
 
1795
    if (!res && !lex->no_write_to_binlog)
 
1796
    {
 
1797
      /*
 
1798
        Presumably, REPAIR and binlog writing doesn't require synchronization
 
1799
      */
 
1800
      write_bin_log(thd, true, thd->query, thd->query_length);
 
1801
    }
 
1802
    select_lex->table_list.first= (uchar*) first_table;
1682
1803
    lex->query_tables=all_tables;
1683
1804
    break;
1684
1805
  }
1687
1808
    assert(first_table == all_tables && first_table != 0);
1688
1809
    thd->enable_slow_log= opt_log_slow_admin_statements;
1689
1810
    res = mysql_check_table(thd, first_table, &lex->check_opt);
1690
 
    select_lex->table_list.first= (unsigned char*) first_table;
 
1811
    select_lex->table_list.first= (uchar*) first_table;
1691
1812
    lex->query_tables=all_tables;
1692
1813
    break;
1693
1814
  }
1697
1818
    thd->enable_slow_log= opt_log_slow_admin_statements;
1698
1819
    res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1699
1820
    /* ! we write after unlocking the table */
1700
 
    write_bin_log(thd, true, thd->query, thd->query_length);
1701
 
    select_lex->table_list.first= (unsigned char*) first_table;
 
1821
    if (!res && !lex->no_write_to_binlog)
 
1822
    {
 
1823
      /*
 
1824
        Presumably, ANALYZE and binlog writing doesn't require synchronization
 
1825
      */
 
1826
      write_bin_log(thd, true, thd->query, thd->query_length);
 
1827
    }
 
1828
    select_lex->table_list.first= (uchar*) first_table;
1702
1829
    lex->query_tables=all_tables;
1703
1830
    break;
1704
1831
  }
1707
1834
  {
1708
1835
    assert(first_table == all_tables && first_table != 0);
1709
1836
    thd->enable_slow_log= opt_log_slow_admin_statements;
1710
 
    res= mysql_optimize_table(thd, first_table, &lex->check_opt);
 
1837
    res= (specialflag & (SPECIAL_SAFE_MODE | SPECIAL_NO_NEW_FUNC)) ?
 
1838
      mysql_recreate_table(thd, first_table) :
 
1839
      mysql_optimize_table(thd, first_table, &lex->check_opt);
1711
1840
    /* ! we write after unlocking the table */
1712
 
    write_bin_log(thd, true, thd->query, thd->query_length);
1713
 
    select_lex->table_list.first= (unsigned char*) first_table;
 
1841
    if (!res && !lex->no_write_to_binlog)
 
1842
    {
 
1843
      /*
 
1844
        Presumably, OPTIMIZE and binlog writing doesn't require synchronization
 
1845
      */
 
1846
      write_bin_log(thd, true, thd->query, thd->query_length);
 
1847
    }
 
1848
    select_lex->table_list.first= (uchar*) first_table;
1714
1849
    lex->query_tables=all_tables;
1715
1850
    break;
1716
1851
  }
1725
1860
                                  lex->value_list,
1726
1861
                                  select_lex->where,
1727
1862
                                  select_lex->order_list.elements,
1728
 
                                  (order_st *) select_lex->order_list.first,
 
1863
                                  (ORDER *) select_lex->order_list.first,
1729
1864
                                  unit->select_limit_cnt,
1730
1865
                                  lex->duplicates, lex->ignore));
1731
1866
    /* mysql_update return 2 if we need to switch to multi-update */
1830
1965
    if (!(res= open_and_lock_tables(thd, all_tables)))
1831
1966
    {
1832
1967
      /* Skip first table, which is the table we are inserting in */
1833
 
      TableList *second_table= first_table->next_local;
1834
 
      select_lex->table_list.first= (unsigned char*) second_table;
 
1968
      TABLE_LIST *second_table= first_table->next_local;
 
1969
      select_lex->table_list.first= (uchar*) second_table;
1835
1970
      select_lex->context.table_list= 
1836
1971
        select_lex->context.first_name_resolution_table= second_table;
1837
1972
      res= mysql_insert_select_prepare(thd);
1854
1989
            thd->lock)
1855
1990
        {
1856
1991
          /* INSERT ... SELECT should invalidate only the very first table */
1857
 
          TableList *save_table= first_table->next_local;
 
1992
          TABLE_LIST *save_table= first_table->next_local;
1858
1993
          first_table->next_local= 0;
1859
1994
          first_table->next_local= save_table;
1860
1995
        }
1861
1996
        delete sel_result;
1862
1997
      }
1863
1998
      /* revert changes for SP */
1864
 
      select_lex->table_list.first= (unsigned char*) first_table;
 
1999
      select_lex->table_list.first= (uchar*) first_table;
1865
2000
    }
1866
2001
 
1867
2002
    break;
1909
2044
  case SQLCOM_DELETE_MULTI:
1910
2045
  {
1911
2046
    assert(first_table == all_tables && first_table != 0);
1912
 
    TableList *aux_tables=
1913
 
      (TableList *)thd->lex->auxiliary_table_list.first;
 
2047
    TABLE_LIST *aux_tables=
 
2048
      (TABLE_LIST *)thd->lex->auxiliary_table_list.first;
1914
2049
    multi_delete *del_result;
1915
2050
 
1916
2051
    if (!thd->locked_tables &&
1944
2079
                        select_lex->with_wild,
1945
2080
                        select_lex->item_list,
1946
2081
                        select_lex->where,
1947
 
                        0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1948
 
                        (order_st *)NULL,
 
2082
                        0, (ORDER *)NULL, (ORDER *)NULL, (Item *)NULL,
 
2083
                        (ORDER *)NULL,
1949
2084
                        select_lex->options | thd->options |
1950
2085
                        SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1951
2086
                        OPTION_SETUP_TABLES_DONE,
1988
2123
  }
1989
2124
  break;
1990
2125
  case SQLCOM_SHOW_PROCESSLIST:
1991
 
    mysqld_list_processes(thd, NULL, lex->verbose);
 
2126
    mysqld_list_processes(thd, NullS, lex->verbose);
1992
2127
    break;
1993
2128
  case SQLCOM_SHOW_ENGINE_LOGS:
1994
2129
    {
2215
2350
    res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
2216
2351
    break;
2217
2352
  }
 
2353
  case SQLCOM_ALTER_DB_UPGRADE:
 
2354
  {
 
2355
    LEX_STRING *db= & lex->name;
 
2356
    if (end_active_trans(thd))
 
2357
    {
 
2358
      res= 1;
 
2359
      break;
 
2360
    }
 
2361
    if (thd->slave_thread && 
 
2362
       (!rpl_filter->db_ok(db->str) ||
 
2363
        !rpl_filter->db_ok_with_wild_table(db->str)))
 
2364
    {
 
2365
      res= 1;
 
2366
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
 
2367
      break;
 
2368
    }
 
2369
    if (check_db_name(db))
 
2370
    {
 
2371
      my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
 
2372
      break;
 
2373
    }
 
2374
    if (thd->locked_tables || thd->active_transaction())
 
2375
    {
 
2376
      res= 1;
 
2377
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
 
2378
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
 
2379
      goto error;
 
2380
    }
 
2381
 
 
2382
    res= mysql_upgrade_db(thd, db);
 
2383
    if (!res)
 
2384
      my_ok(thd);
 
2385
    break;
 
2386
  }
2218
2387
  case SQLCOM_ALTER_DB:
2219
2388
  {
2220
2389
    LEX_STRING *db= &lex->name;
2258
2427
    break;
2259
2428
  }
2260
2429
  case SQLCOM_RESET:
 
2430
    /*
 
2431
      RESET commands are never written to the binary log, so we have to
 
2432
      initialize this variable because RESET shares the same code as FLUSH
 
2433
    */
 
2434
    lex->no_write_to_binlog= 1;
2261
2435
  case SQLCOM_FLUSH:
2262
2436
  {
2263
2437
    bool write_to_binlog;
2275
2449
      /*
2276
2450
        Presumably, RESET and binlog writing doesn't require synchronization
2277
2451
      */
2278
 
      write_bin_log(thd, false, thd->query, thd->query_length);
 
2452
      if (!lex->no_write_to_binlog && write_to_binlog)
 
2453
      {
 
2454
        write_bin_log(thd, false, thd->query, thd->query_length);
 
2455
      }
2279
2456
      my_ok(thd);
2280
2457
    } 
2281
2458
    
2333
2510
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2334
2511
    {
2335
2512
      if (my_strnncoll(system_charset_info,
2336
 
                       (unsigned char *)lex->ident.str, lex->ident.length,
2337
 
                       (unsigned char *)sv->name, sv->length) == 0)
 
2513
                       (uchar *)lex->ident.str, lex->ident.length,
 
2514
                       (uchar *)sv->name, sv->length) == 0)
2338
2515
        break;
2339
2516
    }
2340
2517
    if (sv)
2355
2532
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2356
2533
    {
2357
2534
      if (my_strnncoll(system_charset_info,
2358
 
                       (unsigned char *)lex->ident.str, lex->ident.length,
2359
 
                       (unsigned char *)sv->name, sv->length) == 0)
 
2535
                       (uchar *)lex->ident.str, lex->ident.length,
 
2536
                       (uchar *)sv->name, sv->length) == 0)
2360
2537
        break;
2361
2538
    }
2362
2539
    if (sv)
2368
2545
        if (((thd->options & OPTION_KEEP_LOG) || 
2369
2546
             thd->transaction.all.modified_non_trans_table) &&
2370
2547
            !thd->slave_thread)
2371
 
          push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2548
          push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2372
2549
                       ER_WARNING_NOT_COMPLETE_ROLLBACK,
2373
2550
                       ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2374
2551
        my_ok(thd);
2389
2566
      for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2390
2567
      {
2391
2568
        if (my_strnncoll(system_charset_info,
2392
 
                         (unsigned char *)lex->ident.str, lex->ident.length,
2393
 
                         (unsigned char *)(*sv)->name, (*sv)->length) == 0)
 
2569
                         (uchar *)lex->ident.str, lex->ident.length,
 
2570
                         (uchar *)(*sv)->name, (*sv)->length) == 0)
2394
2571
          break;
2395
2572
      }
2396
2573
      if (*sv) /* old savepoint of the same name exists */
2474
2651
  return(res || thd->is_error());
2475
2652
}
2476
2653
 
2477
 
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
 
2654
 
 
2655
static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)
2478
2656
{
2479
2657
  LEX   *lex= thd->lex;
2480
2658
  select_result *result=lex->result;
2503
2681
      if (lex->describe & DESCRIBE_EXTENDED)
2504
2682
      {
2505
2683
        char buff[1024];
2506
 
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
 
2684
        String str(buff,(uint32) sizeof(buff), system_charset_info);
2507
2685
        str.length(0);
2508
2686
        thd->lex->unit.print(&str, QT_ORDINARY);
2509
2687
        str.append('\0');
2510
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
2688
        push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
2511
2689
                     ER_YES, str.ptr());
2512
2690
      }
2513
2691
      if (res)
2545
2723
  - Passing to check_stack_overrun() prevents the compiler from removing it.
2546
2724
*/
2547
2725
bool check_stack_overrun(THD *thd, long margin,
2548
 
                         unsigned char *buf __attribute__((unused)))
 
2726
                         uchar *buf __attribute__((unused)))
2549
2727
{
2550
2728
  long stack_used;
2551
2729
  assert(thd == current_thd);
2567
2745
{
2568
2746
  LEX   *lex= current_thd->lex;
2569
2747
  ulong old_info=0;
2570
 
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
 
2748
  if ((uint) *yystacksize >= MY_YACC_MAX)
2571
2749
    return 1;
2572
2750
  if (!lex->yacc_yyvs)
2573
2751
    old_info= *yystacksize;
2574
2752
  *yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
2575
 
  if (!(lex->yacc_yyvs= (unsigned char*)
 
2753
  if (!(lex->yacc_yyvs= (uchar*)
2576
2754
        my_realloc(lex->yacc_yyvs,
2577
2755
                   *yystacksize*sizeof(**yyvs),
2578
2756
                   MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))) ||
2579
 
      !(lex->yacc_yyss= (unsigned char*)
 
2757
      !(lex->yacc_yyss= (uchar*)
2580
2758
        my_realloc(lex->yacc_yyss,
2581
2759
                   *yystacksize*sizeof(**yyss),
2582
2760
                   MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))))
2583
2761
    return 1;
2584
2762
  if (old_info)
2585
2763
  {                                             // Copy old info from stack
2586
 
    memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
2587
 
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
 
2764
    memcpy(lex->yacc_yyss, (uchar*) *yyss, old_info*sizeof(**yyss));
 
2765
    memcpy(lex->yacc_yyvs, (uchar*) *yyvs, old_info*sizeof(**yyvs));
2588
2766
  }
2589
2767
  *yyss=(short*) lex->yacc_yyss;
2590
2768
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
2716
2894
  {
2717
2895
    if (lex->current_select->order_list.first && !lex->current_select->braces)
2718
2896
    {
2719
 
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
 
2897
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "ORDER BY");
2720
2898
      return(1);
2721
2899
    }
2722
2900
    select_lex->include_neighbour(lex->current_select);
2762
2940
  lex->sql_command= SQLCOM_SELECT;
2763
2941
  tmp.str= (char*) var_name;
2764
2942
  tmp.length=strlen(var_name);
2765
 
  memset(&null_lex_string.str, 0, sizeof(null_lex_string));
 
2943
  bzero((char*) &null_lex_string.str, sizeof(null_lex_string));
2766
2944
  /*
2767
2945
    We set the name of Item to @@session.var_name because that then is used
2768
2946
    as the column name in the output.
2769
2947
  */
2770
2948
  if ((var= get_system_var(thd, OPT_SESSION, tmp, null_lex_string)))
2771
2949
  {
2772
 
    end= strxmov(buff, "@@session.", var_name, NULL);
 
2950
    end= strxmov(buff, "@@session.", var_name, NullS);
2773
2951
    var->set_name(buff, end-buff, system_charset_info);
2774
2952
    add_item_to_list(thd, var);
2775
2953
  }
2805
2983
                               the next query in the query text.
2806
2984
*/
2807
2985
 
2808
 
void mysql_parse(THD *thd, const char *inBuf, uint32_t length,
 
2986
void mysql_parse(THD *thd, const char *inBuf, uint length,
2809
2987
                 const char ** found_semicolon)
2810
2988
{
2811
2989
  /*
2832
3010
 
2833
3011
    Lex_input_stream lip(thd, inBuf, length);
2834
3012
 
2835
 
    bool err= parse_sql(thd, &lip);
 
3013
    bool err= parse_sql(thd, &lip, NULL);
2836
3014
    *found_semicolon= lip.found_semicolon;
2837
3015
 
2838
3016
    if (!err)
2883
3061
    1   can be ignored
2884
3062
*/
2885
3063
 
2886
 
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint32_t length)
 
3064
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint length)
2887
3065
{
2888
3066
  LEX *lex= thd->lex;
2889
3067
  bool error= 0;
2892
3070
  lex_start(thd);
2893
3071
  mysql_reset_thd_for_next_command(thd);
2894
3072
 
2895
 
  if (!parse_sql(thd, &lip) &&
2896
 
      all_tables_not_ok(thd,(TableList*) lex->select_lex.table_list.first))
 
3073
  if (!parse_sql(thd, &lip, NULL) &&
 
3074
      all_tables_not_ok(thd,(TABLE_LIST*) lex->select_lex.table_list.first))
2897
3075
    error= 1;                  /* Ignore question */
2898
3076
  thd->end_statement();
2899
3077
  thd->cleanup_after_query();
2911
3089
 
2912
3090
bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum_field_types type,
2913
3091
                       char *length, char *decimals,
2914
 
                       uint32_t type_modifier,
 
3092
                       uint type_modifier,
2915
3093
                       enum column_format_type column_format,
2916
3094
                       Item *default_value, Item *on_update_value,
2917
3095
                       LEX_STRING *comment,
2918
3096
                       char *change,
2919
 
                       List<String> *interval_list, const CHARSET_INFO * const cs)
 
3097
                       List<String> *interval_list, CHARSET_INFO *cs)
2920
3098
{
2921
3099
  register Create_field *new_field;
2922
3100
  LEX  *lex= thd->lex;
2956
3134
    */
2957
3135
    if (default_value->type() == Item::FUNC_ITEM && 
2958
3136
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
2959
 
         type == DRIZZLE_TYPE_TIMESTAMP))
 
3137
         type == MYSQL_TYPE_TIMESTAMP))
2960
3138
    {
2961
3139
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
2962
3140
      return(1);
2978
3156
    }
2979
3157
  }
2980
3158
 
2981
 
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
 
3159
  if (on_update_value && type != MYSQL_TYPE_TIMESTAMP)
2982
3160
  {
2983
3161
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
2984
3162
    return(1);
3000
3178
 
3001
3179
void store_position_for_column(const char *name)
3002
3180
{
3003
 
  current_thd->lex->last_field->after=const_cast<char*> (name);
 
3181
  current_thd->lex->last_field->after=my_const_cast(char*) (name);
3004
3182
}
3005
3183
 
3006
3184
bool
3007
3185
add_proc_to_list(THD* thd, Item *item)
3008
3186
{
3009
 
  order_st *order;
 
3187
  ORDER *order;
3010
3188
  Item  **item_ptr;
3011
3189
 
3012
 
  if (!(order = (order_st *) thd->alloc(sizeof(order_st)+sizeof(Item*))))
 
3190
  if (!(order = (ORDER *) thd->alloc(sizeof(ORDER)+sizeof(Item*))))
3013
3191
    return 1;
3014
3192
  item_ptr = (Item**) (order+1);
3015
3193
  *item_ptr= item;
3016
3194
  order->item=item_ptr;
3017
3195
  order->free_me=0;
3018
 
  thd->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
 
3196
  thd->lex->proc_list.link_in_list((uchar*) order,(uchar**) &order->next);
3019
3197
  return 0;
3020
3198
}
3021
3199
 
3026
3204
 
3027
3205
bool add_to_list(THD *thd, SQL_LIST &list,Item *item,bool asc)
3028
3206
{
3029
 
  order_st *order;
3030
 
  if (!(order = (order_st *) thd->alloc(sizeof(order_st))))
 
3207
  ORDER *order;
 
3208
  if (!(order = (ORDER *) thd->alloc(sizeof(ORDER))))
3031
3209
    return(1);
3032
3210
  order->item_ptr= item;
3033
3211
  order->item= &order->item_ptr;
3035
3213
  order->free_me=0;
3036
3214
  order->used=0;
3037
3215
  order->counter_used= 0;
3038
 
  list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
 
3216
  list.link_in_list((uchar*) order,(uchar**) &order->next);
3039
3217
  return(0);
3040
3218
}
3041
3219
 
3056
3234
  @retval
3057
3235
      0         Error
3058
3236
  @retval
3059
 
    \#  Pointer to TableList element added to the total table list
 
3237
    \#  Pointer to TABLE_LIST element added to the total table list
3060
3238
*/
3061
3239
 
3062
 
TableList *st_select_lex::add_table_to_list(THD *thd,
 
3240
TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
3063
3241
                                             Table_ident *table,
3064
3242
                                             LEX_STRING *alias,
3065
3243
                                             uint32_t table_options,
3067
3245
                                             List<Index_hint> *index_hints_arg,
3068
3246
                                             LEX_STRING *option)
3069
3247
{
3070
 
  register TableList *ptr;
3071
 
  TableList *previous_table_ref; /* The table preceding the current one. */
 
3248
  register TABLE_LIST *ptr;
 
3249
  TABLE_LIST *previous_table_ref; /* The table preceding the current one. */
3072
3250
  char *alias_str;
3073
3251
  LEX *lex= thd->lex;
3074
3252
 
3100
3278
    if (!(alias_str= (char*) thd->memdup(alias_str,table->table.length+1)))
3101
3279
      return(0);
3102
3280
  }
3103
 
  if (!(ptr = (TableList *) thd->calloc(sizeof(TableList))))
 
3281
  if (!(ptr = (TABLE_LIST *) thd->calloc(sizeof(TABLE_LIST))))
3104
3282
    return(0);                          /* purecov: inspected */
3105
3283
  if (table->db.str)
3106
3284
  {
3153
3331
  /* check that used name is unique */
3154
3332
  if (lock_type != TL_IGNORE)
3155
3333
  {
3156
 
    TableList *first_table= (TableList*) table_list.first;
3157
 
    for (TableList *tables= first_table ;
 
3334
    TABLE_LIST *first_table= (TABLE_LIST*) table_list.first;
 
3335
    for (TABLE_LIST *tables= first_table ;
3158
3336
         tables ;
3159
3337
         tables=tables->next_local)
3160
3338
    {
3170
3348
  if (table_list.elements > 0)
3171
3349
  {
3172
3350
    /*
3173
 
      table_list.next points to the last inserted TableList->next_local'
 
3351
      table_list.next points to the last inserted TABLE_LIST->next_local'
3174
3352
      element
3175
3353
      We don't use the offsetof() macro here to avoid warnings from gcc
3176
3354
    */
3177
 
    previous_table_ref= (TableList*) ((char*) table_list.next -
 
3355
    previous_table_ref= (TABLE_LIST*) ((char*) table_list.next -
3178
3356
                                       ((char*) &(ptr->next_local) -
3179
3357
                                        (char*) ptr));
3180
3358
    /*
3181
3359
      Set next_name_resolution_table of the previous table reference to point
3182
3360
      to the current table reference. In effect the list
3183
 
      TableList::next_name_resolution_table coincides with
3184
 
      TableList::next_local. Later this may be changed in
 
3361
      TABLE_LIST::next_name_resolution_table coincides with
 
3362
      TABLE_LIST::next_local. Later this may be changed in
3185
3363
      store_top_level_join_columns() for NATURAL/USING joins.
3186
3364
    */
3187
3365
    previous_table_ref->next_name_resolution_table= ptr;
3193
3371
    previous table reference to 'ptr'. Here we also add one element to the
3194
3372
    list 'table_list'.
3195
3373
  */
3196
 
  table_list.link_in_list((unsigned char*) ptr, (unsigned char**) &ptr->next_local);
 
3374
  table_list.link_in_list((uchar*) ptr, (uchar**) &ptr->next_local);
3197
3375
  ptr->next_name_resolution_table= NULL;
3198
3376
  /* Link table in global list (all used tables) */
3199
3377
  lex->add_to_query_tables(ptr);
3204
3382
/**
3205
3383
  Initialize a new table list for a nested join.
3206
3384
 
3207
 
    The function initializes a structure of the TableList type
 
3385
    The function initializes a structure of the TABLE_LIST type
3208
3386
    for a nested join. It sets up its nested join list as empty.
3209
3387
    The created structure is added to the front of the current
3210
3388
    join list in the st_select_lex object. Then the function
3222
3400
 
3223
3401
bool st_select_lex::init_nested_join(THD *thd)
3224
3402
{
3225
 
  TableList *ptr;
3226
 
  nested_join_st *nested_join;
 
3403
  TABLE_LIST *ptr;
 
3404
  NESTED_JOIN *nested_join;
3227
3405
 
3228
 
  if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
3229
 
                                       sizeof(nested_join_st))))
 
3406
  if (!(ptr= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
 
3407
                                       sizeof(NESTED_JOIN))))
3230
3408
    return(1);
3231
3409
  nested_join= ptr->nested_join=
3232
 
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
3410
    ((NESTED_JOIN*) ((uchar*) ptr + ALIGN_SIZE(sizeof(TABLE_LIST))));
3233
3411
 
3234
3412
  join_list->push_front(ptr);
3235
3413
  ptr->embedding= embedding;
3252
3430
  @param thd         current thread
3253
3431
 
3254
3432
  @return
3255
 
    - Pointer to TableList element added to the total table list, if success
 
3433
    - Pointer to TABLE_LIST element added to the total table list, if success
3256
3434
    - 0, otherwise
3257
3435
*/
3258
3436
 
3259
 
TableList *st_select_lex::end_nested_join(THD *thd __attribute__((unused)))
 
3437
TABLE_LIST *st_select_lex::end_nested_join(THD *thd __attribute__((__unused__)))
3260
3438
{
3261
 
  TableList *ptr;
3262
 
  nested_join_st *nested_join;
 
3439
  TABLE_LIST *ptr;
 
3440
  NESTED_JOIN *nested_join;
3263
3441
 
3264
3442
  assert(embedding);
3265
3443
  ptr= embedding;
3268
3446
  nested_join= ptr->nested_join;
3269
3447
  if (nested_join->join_list.elements == 1)
3270
3448
  {
3271
 
    TableList *embedded= nested_join->join_list.head();
 
3449
    TABLE_LIST *embedded= nested_join->join_list.head();
3272
3450
    join_list->pop();
3273
3451
    embedded->join_list= join_list;
3274
3452
    embedded->embedding= embedding;
3294
3472
  @retval
3295
3473
    0  Error
3296
3474
  @retval
3297
 
    \#  Pointer to TableList element created for the new nested join
 
3475
    \#  Pointer to TABLE_LIST element created for the new nested join
3298
3476
*/
3299
3477
 
3300
 
TableList *st_select_lex::nest_last_join(THD *thd)
 
3478
TABLE_LIST *st_select_lex::nest_last_join(THD *thd)
3301
3479
{
3302
 
  TableList *ptr;
3303
 
  nested_join_st *nested_join;
3304
 
  List<TableList> *embedded_list;
 
3480
  TABLE_LIST *ptr;
 
3481
  NESTED_JOIN *nested_join;
 
3482
  List<TABLE_LIST> *embedded_list;
3305
3483
 
3306
 
  if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
3307
 
                                       sizeof(nested_join_st))))
 
3484
  if (!(ptr= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
 
3485
                                       sizeof(NESTED_JOIN))))
3308
3486
    return(0);
3309
3487
  nested_join= ptr->nested_join=
3310
 
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
3488
    ((NESTED_JOIN*) ((uchar*) ptr + ALIGN_SIZE(sizeof(TABLE_LIST))));
3311
3489
 
3312
3490
  ptr->embedding= embedding;
3313
3491
  ptr->join_list= join_list;
3315
3493
  embedded_list= &nested_join->join_list;
3316
3494
  embedded_list->empty();
3317
3495
 
3318
 
  for (uint32_t i=0; i < 2; i++)
 
3496
  for (uint i=0; i < 2; i++)
3319
3497
  {
3320
 
    TableList *table= join_list->pop();
 
3498
    TABLE_LIST *table= join_list->pop();
3321
3499
    table->join_list= embedded_list;
3322
3500
    table->embedding= ptr;
3323
3501
    embedded_list->push_back(table);
3352
3530
    None
3353
3531
*/
3354
3532
 
3355
 
void st_select_lex::add_joined_table(TableList *table)
 
3533
void st_select_lex::add_joined_table(TABLE_LIST *table)
3356
3534
{
3357
3535
  join_list->push_front(table);
3358
3536
  table->join_list= join_list;
3392
3570
    - 0, otherwise
3393
3571
*/
3394
3572
 
3395
 
TableList *st_select_lex::convert_right_join()
 
3573
TABLE_LIST *st_select_lex::convert_right_join()
3396
3574
{
3397
 
  TableList *tab2= join_list->pop();
3398
 
  TableList *tab1= join_list->pop();
 
3575
  TABLE_LIST *tab2= join_list->pop();
 
3576
  TABLE_LIST *tab1= join_list->pop();
3399
3577
 
3400
3578
  join_list->push_front(tab2);
3401
3579
  join_list->push_front(tab1);
3419
3597
{
3420
3598
  bool for_update= lock_type >= TL_READ_NO_INSERT;
3421
3599
 
3422
 
  for (TableList *tables= (TableList*) table_list.first;
 
3600
  for (TABLE_LIST *tables= (TABLE_LIST*) table_list.first;
3423
3601
       tables;
3424
3602
       tables= tables->next_local)
3425
3603
  {
3437
3615
    This object is created for any union construct containing a union
3438
3616
    operation and also for any single select union construct of the form
3439
3617
    @verbatim
3440
 
    (SELECT ... order_st BY order_list [LIMIT n]) order_st BY ... 
 
3618
    (SELECT ... ORDER BY order_list [LIMIT n]) ORDER BY ... 
3441
3619
    @endvarbatim
3442
3620
    or of the form
3443
3621
    @varbatim
3444
 
    (SELECT ... order_st BY LIMIT n) order_st BY ...
 
3622
    (SELECT ... ORDER BY LIMIT n) ORDER BY ...
3445
3623
    @endvarbatim
3446
3624
  
3447
3625
  @param thd_arg                   thread handle
3472
3650
  fake_select_lex->select_limit= 0;
3473
3651
 
3474
3652
  fake_select_lex->context.outer_context=first_sl->context.outer_context;
3475
 
  /* allow item list resolving in fake select for order_st BY */
 
3653
  /* allow item list resolving in fake select for ORDER BY */
3476
3654
  fake_select_lex->context.resolve_in_select_list= true;
3477
3655
  fake_select_lex->context.select_lex= fake_select_lex;
3478
3656
 
3480
3658
  {
3481
3659
    /* 
3482
3660
      This works only for 
3483
 
      (SELECT ... order_st BY list [LIMIT n]) order_st BY order_list [LIMIT m],
3484
 
      (SELECT ... LIMIT n) order_st BY order_list [LIMIT m]
 
3661
      (SELECT ... ORDER BY list [LIMIT n]) ORDER BY order_list [LIMIT m],
 
3662
      (SELECT ... LIMIT n) ORDER BY order_list [LIMIT m]
3485
3663
      just before the parser starts processing order_list
3486
3664
    */ 
3487
3665
    global_parameters= fake_select_lex;
3514
3692
 
3515
3693
bool
3516
3694
push_new_name_resolution_context(THD *thd,
3517
 
                                 TableList *left_op, TableList *right_op)
 
3695
                                 TABLE_LIST *left_op, TABLE_LIST *right_op)
3518
3696
{
3519
3697
  Name_resolution_context *on_context;
3520
3698
  if (!(on_context= new (thd->mem_root) Name_resolution_context))
3542
3720
    true   if all is OK
3543
3721
*/
3544
3722
 
3545
 
void add_join_on(TableList *b, Item *expr)
 
3723
void add_join_on(TABLE_LIST *b, Item *expr)
3546
3724
{
3547
3725
  if (expr)
3548
3726
  {
3595
3773
  @param using_fields    Field names from USING clause
3596
3774
*/
3597
3775
 
3598
 
void add_join_natural(TableList *a, TableList *b, List<String> *using_fields,
 
3776
void add_join_natural(TABLE_LIST *a, TABLE_LIST *b, List<String> *using_fields,
3599
3777
                      SELECT_LEX *lex)
3600
3778
{
3601
3779
  b->natural_join= a;
3622
3800
    @retval !=0  Error; thd->killed is set or thd->is_error() is true
3623
3801
*/
3624
3802
 
3625
 
bool reload_cache(THD *thd, ulong options, TableList *tables,
 
3803
bool reload_cache(THD *thd, ulong options, TABLE_LIST *tables,
3626
3804
                          bool *write_to_binlog)
3627
3805
{
3628
3806
  bool result=0;
3747
3925
    This is written such that we have a short lock on LOCK_thread_count
3748
3926
*/
3749
3927
 
3750
 
static unsigned int
3751
 
kill_one_thread(THD *thd __attribute__((unused)),
3752
 
                ulong id, bool only_kill_query)
 
3928
uint kill_one_thread(THD *thd __attribute__((__unused__)),
 
3929
                     ulong id, bool only_kill_query)
3753
3930
{
3754
3931
  THD *tmp;
3755
 
  uint32_t error=ER_NO_SUCH_THREAD;
3756
 
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
3932
  uint error=ER_NO_SUCH_THREAD;
 
3933
  VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list
3757
3934
  I_List_iterator<THD> it(threads);
3758
3935
  while ((tmp=it++))
3759
3936
  {
3765
3942
      break;
3766
3943
    }
3767
3944
  }
3768
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
3945
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
3769
3946
  if (tmp)
3770
3947
  {
3771
3948
    tmp->awake(only_kill_query ? THD::KILL_QUERY : THD::KILL_CONNECTION);
3788
3965
 
3789
3966
void sql_kill(THD *thd, ulong id, bool only_kill_query)
3790
3967
{
3791
 
  uint32_t error;
 
3968
  uint error;
3792
3969
  if (!(error= kill_one_thread(thd, id, only_kill_query)))
3793
3970
    my_ok(thd);
3794
3971
  else
3813
3990
    return 1;
3814
3991
  }
3815
3992
  /* Fix is using unix filename format on dos */
3816
 
  my_stpcpy(buff,*filename_ptr);
3817
 
  end=convert_dirname(buff, *filename_ptr, NULL);
 
3993
  strmov(buff,*filename_ptr);
 
3994
  end=convert_dirname(buff, *filename_ptr, NullS);
3818
3995
  if (!(ptr= (char*) thd->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3819
3996
    return 1;                                   // End of memory
3820
3997
  *filename_ptr=ptr;
3821
 
  strxmov(ptr,buff,table_name,NULL);
 
3998
  strxmov(ptr,buff,table_name,NullS);
3822
3999
  return 0;
3823
4000
}
3824
4001
 
3841
4018
    char command[80];
3842
4019
    Lex_input_stream *lip= thd->m_lip;
3843
4020
    strmake(command, lip->yylval->symbol.str,
3844
 
            cmin((ulong)lip->yylval->symbol.length, sizeof(command)-1));
 
4021
            min(lip->yylval->symbol.length, sizeof(command)-1));
3845
4022
    my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
3846
4023
    return 1;
3847
4024
  }
3929
4106
*/
3930
4107
 
3931
4108
bool multi_update_precheck(THD *thd,
3932
 
                           TableList *tables __attribute__((unused)))
 
4109
                           TABLE_LIST *tables __attribute__((__unused__)))
3933
4110
{
3934
4111
  const char *msg= 0;
3935
4112
  LEX *lex= thd->lex;
3966
4143
*/
3967
4144
 
3968
4145
bool multi_delete_precheck(THD *thd,
3969
 
                           TableList *tables __attribute__((unused)))
 
4146
                           TABLE_LIST *tables __attribute__((__unused__)))
3970
4147
{
3971
4148
  SELECT_LEX *select_lex= &thd->lex->select_lex;
3972
 
  TableList **save_query_tables_own_last= thd->lex->query_tables_own_last;
 
4149
  TABLE_LIST **save_query_tables_own_last= thd->lex->query_tables_own_last;
3973
4150
 
3974
4151
  thd->lex->query_tables_own_last= 0;
3975
4152
  thd->lex->query_tables_own_last= save_query_tables_own_last;
4001
4178
  @return Matching table, NULL otherwise.
4002
4179
*/
4003
4180
 
4004
 
static TableList *multi_delete_table_match(LEX *lex __attribute__((unused)),
4005
 
                                            TableList *tbl,
4006
 
                                            TableList *tables)
 
4181
static TABLE_LIST *multi_delete_table_match(LEX *lex __attribute__((__unused__)),
 
4182
                                            TABLE_LIST *tbl,
 
4183
                                            TABLE_LIST *tables)
4007
4184
{
4008
 
  TableList *match= NULL;
 
4185
  TABLE_LIST *match= NULL;
4009
4186
 
4010
 
  for (TableList *elem= tables; elem; elem= elem->next_local)
 
4187
  for (TABLE_LIST *elem= tables; elem; elem= elem->next_local)
4011
4188
  {
4012
4189
    int cmp;
4013
4190
 
4055
4232
 
4056
4233
bool multi_delete_set_locks_and_link_aux_tables(LEX *lex)
4057
4234
{
4058
 
  TableList *tables= (TableList*)lex->select_lex.table_list.first;
4059
 
  TableList *target_tbl;
 
4235
  TABLE_LIST *tables= (TABLE_LIST*)lex->select_lex.table_list.first;
 
4236
  TABLE_LIST *target_tbl;
4060
4237
 
4061
4238
  lex->table_count= 0;
4062
4239
 
4063
 
  for (target_tbl= (TableList *)lex->auxiliary_table_list.first;
 
4240
  for (target_tbl= (TABLE_LIST *)lex->auxiliary_table_list.first;
4064
4241
       target_tbl; target_tbl= target_tbl->next_local)
4065
4242
  {
4066
4243
    lex->table_count++;
4067
4244
    /* All tables in aux_tables must be found in FROM PART */
4068
 
    TableList *walk= multi_delete_table_match(lex, target_tbl, tables);
 
4245
    TABLE_LIST *walk= multi_delete_table_match(lex, target_tbl, tables);
4069
4246
    if (!walk)
4070
4247
      return(true);
4071
4248
    if (!walk->derived)
4093
4270
    true  Error
4094
4271
*/
4095
4272
 
4096
 
bool update_precheck(THD *thd, TableList *tables __attribute__((unused)))
 
4273
bool update_precheck(THD *thd, TABLE_LIST *tables __attribute__((__unused__)))
4097
4274
{
4098
4275
  if (thd->lex->select_lex.item_list.elements != thd->lex->value_list.elements)
4099
4276
  {
4116
4293
    true   error
4117
4294
*/
4118
4295
 
4119
 
bool insert_precheck(THD *thd, TableList *tables __attribute__((unused)))
 
4296
bool insert_precheck(THD *thd, TABLE_LIST *tables __attribute__((__unused__)))
4120
4297
{
4121
4298
  LEX *lex= thd->lex;
4122
4299
 
4146
4323
    true   Error
4147
4324
*/
4148
4325
 
4149
 
bool create_table_precheck(THD *thd __attribute__((unused)),
4150
 
                           TableList *tables __attribute__((unused)),
4151
 
                           TableList *create_table)
 
4326
bool create_table_precheck(THD *thd,
 
4327
                           TABLE_LIST *tables __attribute__((__unused__)),
 
4328
                           TABLE_LIST *create_table)
4152
4329
{
 
4330
  LEX *lex= thd->lex;
 
4331
  SELECT_LEX *select_lex= &lex->select_lex;
4153
4332
  bool error= true;                                 // Error message is given
4154
4333
 
4155
4334
  if (create_table && (strcmp(create_table->db, "information_schema") == 0))
4158
4337
    return(true);
4159
4338
  }
4160
4339
 
 
4340
  if (select_lex->item_list.elements)
 
4341
  {
 
4342
    /* Check permissions for used tables in CREATE TABLE ... SELECT */
 
4343
 
 
4344
#ifdef NOT_NECESSARY_TO_CHECK_CREATE_TABLE_EXIST_WHEN_PREPARING_STATEMENT
 
4345
    /* This code throws an ill error for CREATE TABLE t1 SELECT * FROM t1 */
 
4346
    /*
 
4347
      Only do the check for PS, because we on execute we have to check that
 
4348
      against the opened tables to ensure we don't use a table that is part
 
4349
      of the view (which can only be done after the table has been opened).
 
4350
    */
 
4351
    if (thd->stmt_arena->is_stmt_prepare_or_first_sp_execute())
 
4352
    {
 
4353
      /*
 
4354
        For temporary tables we don't have to check if the created table exists
 
4355
      */
 
4356
      if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) &&
 
4357
          find_table_in_global_list(tables, create_table->db,
 
4358
                                    create_table->table_name))
 
4359
      {
 
4360
        error= false;
 
4361
        goto err;
 
4362
      }
 
4363
    }
 
4364
#endif
 
4365
  }
4161
4366
  error= false;
4162
4367
 
4163
4368
  return(error);
4215
4420
*/
4216
4421
 
4217
4422
bool check_string_byte_length(LEX_STRING *str, const char *err_msg,
4218
 
                              uint32_t max_byte_length)
 
4423
                              uint max_byte_length)
4219
4424
{
4220
4425
  if (str->length <= max_byte_length)
4221
4426
    return false;
4243
4448
 
4244
4449
 
4245
4450
bool check_string_char_length(LEX_STRING *str, const char *err_msg,
4246
 
                              uint32_t max_char_length, const CHARSET_INFO * const cs,
 
4451
                              uint max_char_length, CHARSET_INFO *cs,
4247
4452
                              bool no_error)
4248
4453
{
4249
4454
  int well_formed_error;
4250
 
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
 
4455
  uint res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4251
4456
                                      max_char_length, &well_formed_error);
4252
4457
 
4253
4458
  if (!well_formed_error &&  str->length == res)
4259
4464
}
4260
4465
 
4261
4466
 
4262
 
bool check_identifier_name(LEX_STRING *str, uint32_t max_char_length,
4263
 
                           uint32_t err_code, const char *param_for_err_msg)
 
4467
bool check_identifier_name(LEX_STRING *str, uint max_char_length,
 
4468
                           uint err_code, const char *param_for_err_msg)
4264
4469
{
4265
4470
#ifdef HAVE_CHARSET_utf8mb3
4266
4471
  /*
4268
4473
    so they should be prohibited until such support is done.
4269
4474
    This is why we use the 3-byte utf8 to check well-formedness here.
4270
4475
  */
4271
 
  const CHARSET_INFO * const cs= &my_charset_utf8mb3_general_ci;
 
4476
  CHARSET_INFO *cs= &my_charset_utf8mb3_general_ci;
4272
4477
#else
4273
 
  const CHARSET_INFO * const cs= system_charset_info;
 
4478
  CHARSET_INFO *cs= system_charset_info;
4274
4479
#endif
4275
4480
  int well_formed_error;
4276
 
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
 
4481
  uint res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4277
4482
                                      max_char_length, &well_formed_error);
4278
4483
 
4279
4484
  if (well_formed_error)
4319
4524
bool test_if_data_home_dir(const char *dir)
4320
4525
{
4321
4526
  char path[FN_REFLEN], conv_path[FN_REFLEN];
4322
 
  uint32_t dir_len, home_dir_len= strlen(mysql_unpacked_real_data_home);
 
4527
  uint dir_len, home_dir_len= strlen(mysql_unpacked_real_data_home);
4323
4528
 
4324
4529
  if (!dir)
4325
4530
    return(0);
4330
4535
 
4331
4536
  if (home_dir_len < dir_len)
4332
4537
  {
4333
 
    if (!my_strnncoll(character_set_filesystem,
4334
 
                      (const unsigned char*) conv_path, home_dir_len,
4335
 
                      (const unsigned char*) mysql_unpacked_real_data_home,
4336
 
                      home_dir_len))
 
4538
    if (lower_case_file_system)
 
4539
    {
 
4540
      if (!my_strnncoll(character_set_filesystem,
 
4541
                        (const uchar*) conv_path, home_dir_len,
 
4542
                        (const uchar*) mysql_unpacked_real_data_home,
 
4543
                        home_dir_len))
 
4544
        return(1);
 
4545
    }
 
4546
    else if (!memcmp(conv_path, mysql_unpacked_real_data_home, home_dir_len))
4337
4547
      return(1);
4338
4548
  }
4339
4549
  return(0);
4349
4559
 
4350
4560
  @param thd Thread context.
4351
4561
  @param lip Lexer context.
 
4562
  @param creation_ctx Object creation context.
4352
4563
 
4353
4564
  @return Error status.
4354
4565
    @retval false on success.
4355
4566
    @retval true on parsing error.
4356
4567
*/
4357
4568
 
4358
 
bool parse_sql(THD *thd, Lex_input_stream *lip)
 
4569
bool parse_sql(THD *thd,
 
4570
               Lex_input_stream *lip,
 
4571
               Object_creation_ctx *creation_ctx)
4359
4572
{
4360
4573
  assert(thd->m_lip == NULL);
4361
4574
 
 
4575
  /* Backup creation context. */
 
4576
 
 
4577
  Object_creation_ctx *backup_ctx= NULL;
 
4578
 
 
4579
  if (creation_ctx)
 
4580
    backup_ctx= creation_ctx->set_n_backup(thd);
 
4581
 
4362
4582
  /* Set Lex_input_stream. */
4363
4583
 
4364
4584
  thd->m_lip= lip;
4375
4595
 
4376
4596
  thd->m_lip= NULL;
4377
4597
 
 
4598
  /* Restore creation context. */
 
4599
 
 
4600
  if (creation_ctx)
 
4601
    creation_ctx->restore_env(thd, backup_ctx);
 
4602
 
4378
4603
  /* That's it. */
4379
4604
 
4380
4605
  return mysql_parse_status || thd->is_fatal_error;