~mdcallag/+junk/5.1-map

« back to all changes in this revision

Viewing changes to sql/sql_parse.cc

  • Committer: msvensson at pilot
  • Date: 2007-04-24 09:11:45 UTC
  • mfrom: (2469.1.106)
  • Revision ID: sp1r-msvensson@pilot.blaudden-20070424091145-10463
Merge pilot.blaudden:/home/msvensson/mysql/my51-m-mysql_upgrade
into  pilot.blaudden:/home/msvensson/mysql/mysql-5.1-maint

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include "sp.h"
27
27
#include "sp_cache.h"
28
28
#include "events.h"
29
 
#include "event_data_objects.h"
30
29
#include "sql_trigger.h"
31
30
 
32
31
/* Used in error handling only */
119
118
    if (ha_commit(thd))
120
119
      error=1;
121
120
  }
122
 
  thd->options&= ~(OPTION_BEGIN | OPTION_STATUS_NO_TRANS_UPDATE |
123
 
                   OPTION_KEEP_LOG);
 
121
  thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
122
  thd->no_trans_update.all= FALSE;
124
123
  DBUG_RETURN(error);
125
124
}
126
125
 
365
364
                      buff[length-1] == ';'))
366
365
      length--;
367
366
    buff[length]=0;
 
367
 
 
368
    /* Skip lines starting with delimiter */
 
369
    if (strncmp(buff, STRING_WITH_LEN("delimiter")) == 0)
 
370
      continue;
 
371
 
368
372
    thd->query_length=length;
369
373
    thd->query= thd->memdup_w_gap(buff, length+1, 
370
374
                                  thd->db_length+1+QUERY_CACHE_FLAGS_SIZE);
546
550
    */
547
551
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
548
552
    res= ha_commit(thd);
549
 
    thd->options&= ~(ulong) (OPTION_BEGIN | OPTION_STATUS_NO_TRANS_UPDATE |
550
 
                             OPTION_KEEP_LOG);
 
553
    thd->options&= ~(ulong) (OPTION_BEGIN | OPTION_KEEP_LOG);
 
554
    thd->no_trans_update.all= FALSE;
551
555
    break;
552
556
  case COMMIT_RELEASE:
553
557
    do_release= 1; /* fall through */
564
568
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
565
569
    if (ha_rollback(thd))
566
570
      res= -1;
567
 
    thd->options&= ~(ulong) (OPTION_BEGIN | OPTION_STATUS_NO_TRANS_UPDATE |
568
 
                             OPTION_KEEP_LOG);
 
571
    thd->options&= ~(ulong) (OPTION_BEGIN | OPTION_KEEP_LOG);
 
572
    thd->no_trans_update.all= FALSE;
569
573
    if (!res && (completion == ROLLBACK_AND_CHAIN))
570
574
      res= begin_trans(thd);
571
575
    break;
689
693
  DBUG_ENTER("dispatch_command");
690
694
 
691
695
  if (thd->killed == THD::KILL_QUERY || thd->killed == THD::KILL_BAD_DATA)
 
696
  {
692
697
    thd->killed= THD::NOT_KILLED;
 
698
    thd->mysys_var->abort= 0;
 
699
  }
693
700
 
694
701
  thd->command=command;
695
702
  /*
717
724
                        &LOCK_status);
718
725
    thd->convert_string(&tmp, system_charset_info,
719
726
                        packet, packet_length-1, thd->charset());
720
 
    if (!mysql_change_db(thd, tmp.str, FALSE))
 
727
    if (!mysql_change_db(thd, &tmp, FALSE))
721
728
    {
722
729
      general_log_print(thd, command, "%s",thd->db);
723
730
      send_ok(thd);
782
789
    char *save_db;
783
790
    uint passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
784
791
                      *passwd++ : strlen(passwd));
785
 
    uint dummy_errors, save_db_length, db_length, res;
 
792
    uint dummy_errors, save_db_length, db_length;
 
793
    int res;
786
794
    Security_context save_security_ctx= *thd->security_ctx;
787
795
    USER_CONN *save_user_connect;
788
796
 
829
837
      /* authentication failure, we shall restore old user */
830
838
      if (res > 0)
831
839
        my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
 
840
      else
 
841
        thd->clear_error();                     // Error already sent to client
832
842
      x_free(thd->security_ctx->user);
833
843
      *thd->security_ctx= save_security_ctx;
834
844
      thd->user_connect= save_user_connect;
958
968
    packet= arg_end + 1;
959
969
 
960
970
    if (!my_strcasecmp(system_charset_info, table_list.db,
961
 
                       information_schema_name.str))
 
971
                       INFORMATION_SCHEMA_NAME.str))
962
972
    {
963
973
      ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
964
974
      if (schema_table)
980
990
      break;
981
991
    /* init structures for VIEW processing */
982
992
    table_list.select_lex= &(thd->lex->select_lex);
983
 
    mysql_init_query(thd, (uchar*)"", 0);
 
993
    mysql_init_query(thd, "", 0);
984
994
    thd->lex->
985
995
      select_lex.table_list.link_in_list((byte*) &table_list,
986
996
                                         (byte**) &table_list.next_local);
1138
1148
    STATUS_VAR current_global_status_var;
1139
1149
    ulong uptime;
1140
1150
    uint length;
 
1151
    ulonglong queries_per_second1000;
1141
1152
#ifndef EMBEDDED_LIBRARY
1142
1153
    char buff[250];
1143
1154
    uint buff_len= sizeof(buff);
1150
1161
    statistic_increment(thd->status_var.com_stat[SQLCOM_SHOW_STATUS],
1151
1162
                        &LOCK_status);
1152
1163
    calc_sum_of_all_status(&current_global_status_var);
1153
 
    uptime= (ulong) (thd->start_time - server_start_time);
 
1164
    if (!(uptime= (ulong) (thd->start_time - server_start_time)))
 
1165
      queries_per_second1000= 0;
 
1166
    else
 
1167
      queries_per_second1000= thd->query_id * LL(1000) / uptime;
 
1168
 
1154
1169
    length= my_snprintf((char*) buff, buff_len - 1,
1155
1170
                        "Uptime: %lu  Threads: %d  Questions: %lu  "
1156
1171
                        "Slow queries: %lu  Opens: %lu  Flush tables: %lu  "
1157
 
                        "Open tables: %u  Queries per second avg: %.3f",
 
1172
                        "Open tables: %u  Queries per second avg: %u.%u",
1158
1173
                        uptime,
1159
1174
                        (int) thread_count, (ulong) thd->query_id,
1160
1175
                        current_global_status_var.long_query_count,
1161
1176
                        current_global_status_var.opened_tables,
1162
1177
                        refresh_version,
1163
1178
                        cached_open_tables(),
1164
 
                        (uptime ? (ulonglong2double(thd->query_id) /
1165
 
                                   (double) uptime) : (double) 0));
 
1179
                        (uint) (queries_per_second1000 / 1000),
 
1180
                        (uint) (queries_per_second1000 % 1000));
1166
1181
#ifdef SAFEMALLOC
1167
1182
    if (sf_malloc_cur_memory)                           // Using SAFEMALLOC
1168
1183
    {
1348
1363
int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
1349
1364
                         enum enum_schema_tables schema_table_idx)
1350
1365
{
 
1366
  SELECT_LEX *schema_select_lex= NULL;
1351
1367
  DBUG_ENTER("prepare_schema_table");
1352
 
  SELECT_LEX *sel= 0;
 
1368
 
1353
1369
  switch (schema_table_idx) {
1354
1370
  case SCH_SCHEMATA:
1355
1371
#if defined(DONT_ALLOW_SHOW_COMMANDS)
1357
1373
               ER(ER_NOT_ALLOWED_COMMAND), MYF(0));   /* purecov: inspected */
1358
1374
    DBUG_RETURN(1);
1359
1375
#else
1360
 
    if ((specialflag & SPECIAL_SKIP_SHOW_DB) &&
1361
 
        check_global_access(thd, SHOW_DB_ACL))
1362
 
      DBUG_RETURN(1);
1363
1376
    break;
1364
1377
#endif
 
1378
 
1365
1379
  case SCH_TABLE_NAMES:
1366
1380
  case SCH_TABLES:
1367
1381
  case SCH_VIEWS:
1380
1394
      {
1381
1395
        DBUG_RETURN(1);
1382
1396
      }
1383
 
      db.str= lex->select_lex.db;
 
1397
      schema_select_lex= new SELECT_LEX();
 
1398
      db.str= schema_select_lex->db= lex->select_lex.db;
 
1399
      schema_select_lex->table_list.first= NULL;
1384
1400
      db.length= strlen(db.str);
 
1401
 
1385
1402
      if (check_db_name(&db))
1386
1403
      {
1387
1404
        my_error(ER_WRONG_DB_NAME, MYF(0), db.str);
1388
1405
        DBUG_RETURN(1);
1389
1406
      }
1390
 
      if (check_access(thd, SELECT_ACL, db.str, &thd->col_access, 0, 0,
1391
 
                       is_schema_db(db.str)))
1392
 
        DBUG_RETURN(1);                         /* purecov: inspected */
1393
 
      if (!thd->col_access && check_grant_db(thd, db.str))
1394
 
      {
1395
 
        my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
1396
 
                 thd->security_ctx->priv_user, thd->security_ctx->priv_host,
1397
 
                 db.str);
1398
 
        DBUG_RETURN(1);
1399
 
      }
1400
1407
      break;
1401
1408
    }
1402
1409
#endif
1403
1410
  case SCH_COLUMNS:
1404
1411
  case SCH_STATISTICS:
 
1412
  {
1405
1413
#ifdef DONT_ALLOW_SHOW_COMMANDS
1406
1414
    my_message(ER_NOT_ALLOWED_COMMAND,
1407
1415
               ER(ER_NOT_ALLOWED_COMMAND), MYF(0)); /* purecov: inspected */
1408
1416
    DBUG_RETURN(1);
1409
1417
#else
1410
 
    if (table_ident)
1411
 
    {
1412
 
      TABLE_LIST **query_tables_last= lex->query_tables_last;
1413
 
      sel= new SELECT_LEX();
1414
 
      /* 'parent_lex' is used in init_query() so it must be before it. */
1415
 
      sel->parent_lex= lex;
1416
 
      sel->init_query();
1417
 
      if (!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ, 
1418
 
                                 (List<String> *) 0, (List<String> *) 0))
1419
 
        DBUG_RETURN(1);
1420
 
      lex->query_tables_last= query_tables_last;
1421
 
      TABLE_LIST *table_list= (TABLE_LIST*) sel->table_list.first;
1422
 
      char *db= table_list->db;
1423
 
      if (check_access(thd,SELECT_ACL | EXTRA_ACL,db,
1424
 
                       &table_list->grant.privilege, 0, 0,
1425
 
                       test(table_list->schema_table)))
1426
 
        DBUG_RETURN(1);                         /* purecov: inspected */
1427
 
      if (grant_option && check_grant(thd, SELECT_ACL, table_list, 2,
1428
 
                                      UINT_MAX, 0))
1429
 
        DBUG_RETURN(1);
1430
 
      break;
1431
 
    }
 
1418
    DBUG_ASSERT(table_ident);
 
1419
    TABLE_LIST **query_tables_last= lex->query_tables_last;
 
1420
    schema_select_lex= new SELECT_LEX();
 
1421
    /* 'parent_lex' is used in init_query() so it must be before it. */
 
1422
    schema_select_lex->parent_lex= lex;
 
1423
    schema_select_lex->init_query();
 
1424
    if (!schema_select_lex->add_table_to_list(thd, table_ident, 0, 0, TL_READ))
 
1425
      DBUG_RETURN(1);
 
1426
    lex->query_tables_last= query_tables_last;
 
1427
    break;
 
1428
  }
1432
1429
#endif
1433
1430
  case SCH_OPEN_TABLES:
1434
1431
  case SCH_VARIABLES:
1454
1451
    DBUG_RETURN(1);
1455
1452
  }
1456
1453
  TABLE_LIST *table_list= (TABLE_LIST*) select_lex->table_list.first;
1457
 
  table_list->schema_select_lex= sel;
 
1454
  table_list->schema_select_lex= schema_select_lex;
1458
1455
  table_list->schema_table_reformed= 1;
1459
1456
  DBUG_RETURN(0);
1460
1457
}
1687
1684
    Don't reset warnings when executing a stored routine.
1688
1685
  */
1689
1686
  if ((all_tables || &lex->select_lex != lex->all_selects_list ||
1690
 
       lex->sroutines.records) && !thd->spcont ||
1691
 
      lex->time_zone_tables_used)
 
1687
       lex->sroutines.records) && !thd->spcont)
1692
1688
    mysql_reset_errors(thd, 0);
1693
1689
 
1694
1690
#ifdef HAVE_REPLICATION
2171
2167
        if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
2172
2168
        {
2173
2169
          TABLE_LIST *duplicate;
2174
 
          if ((duplicate= unique_table(thd, create_table, select_tables)))
 
2170
          if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
2175
2171
          {
2176
2172
            update_non_unique_table_error(create_table, "CREATE", duplicate);
2177
2173
            res= 1;
2187
2183
               tab= tab->next_local)
2188
2184
          {
2189
2185
            TABLE_LIST *duplicate;
2190
 
            if ((duplicate= unique_table(thd, tab, select_tables)))
 
2186
            if ((duplicate= unique_table(thd, tab, select_tables, 0)))
2191
2187
            {
2192
2188
              update_non_unique_table_error(tab, "CREATE", duplicate);
2193
2189
              res= 1;
2594
2590
    break;
2595
2591
  }
2596
2592
  case SQLCOM_REPLACE:
 
2593
#ifndef DBUG_OFF
 
2594
    if (mysql_bin_log.is_open())
 
2595
    {
 
2596
      /*
 
2597
        Generate an incident log event before writing the real event
 
2598
        to the binary log.  We put this event is before the statement
 
2599
        since that makes it simpler to check that the statement was
 
2600
        not executed on the slave (since incidents usually stop the
 
2601
        slave).
 
2602
 
 
2603
        Observe that any row events that are generated will be
 
2604
        generated before.
 
2605
 
 
2606
        This is only for testing purposes and will not be present in a
 
2607
        release build.
 
2608
      */
 
2609
 
 
2610
      Incident incident= INCIDENT_NONE;
 
2611
      DBUG_PRINT("debug", ("Just before generate_incident()"));
 
2612
      DBUG_EXECUTE_IF("incident_database_resync_on_replace",
 
2613
                      incident= INCIDENT_LOST_EVENTS;);
 
2614
      if (incident)
 
2615
      {
 
2616
        Incident_log_event ev(thd, incident);
 
2617
        mysql_bin_log.write(&ev);
 
2618
        mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
 
2619
      }
 
2620
      DBUG_PRINT("debug", ("Just after generate_incident()"));
 
2621
    }
 
2622
#endif
2597
2623
  case SQLCOM_INSERT:
2598
2624
  {
2599
2625
    DBUG_ASSERT(first_table == all_tables && first_table != 0);
2813
2839
        we silently add IF EXISTS if TEMPORARY was used.
2814
2840
      */
2815
2841
      if (thd->slave_thread)
2816
 
        lex->drop_if_exists= 1;
 
2842
        lex->drop_if_exists= 1;
2817
2843
 
2818
2844
      /* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
2819
2845
      thd->options|= OPTION_KEEP_LOG;
2870
2896
    }
2871
2897
#endif
2872
2898
  case SQLCOM_CHANGE_DB:
2873
 
    if (!mysql_change_db(thd,select_lex->db,FALSE))
 
2899
  {
 
2900
    LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
 
2901
 
 
2902
    if (!mysql_change_db(thd, &db_str, FALSE))
2874
2903
      send_ok(thd);
 
2904
 
2875
2905
    break;
 
2906
  }
2876
2907
 
2877
2908
  case SQLCOM_LOAD:
2878
2909
  {
3150
3181
 
3151
3182
    switch (lex->sql_command) {
3152
3183
    case SQLCOM_CREATE_EVENT:
3153
 
      res= Events::get_instance()->
3154
 
            create_event(thd, lex->event_parse_data,
3155
 
                         lex->create_info.options & HA_LEX_CREATE_IF_NOT_EXISTS);
 
3184
    {
 
3185
      bool if_not_exists= (lex->create_info.options &
 
3186
                           HA_LEX_CREATE_IF_NOT_EXISTS);
 
3187
      res= Events::create_event(thd, lex->event_parse_data, if_not_exists);
3156
3188
      break;
 
3189
    }
3157
3190
    case SQLCOM_ALTER_EVENT:
3158
 
      res= Events::get_instance()->update_event(thd, lex->event_parse_data,
3159
 
                                                lex->spname);
 
3191
      res= Events::update_event(thd, lex->event_parse_data,
 
3192
                                lex->spname ? &lex->spname->m_db : NULL,
 
3193
                                lex->spname ? &lex->spname->m_name : NULL);
3160
3194
      break;
3161
3195
    default:
3162
3196
      DBUG_ASSERT(0);
3174
3208
  }
3175
3209
  /* lex->unit.cleanup() is called outside, no need to call it here */
3176
3210
  break;
 
3211
  case SQLCOM_SHOW_CREATE_EVENT:
 
3212
    res= Events::show_create_event(thd, lex->spname->m_db,
 
3213
                                   lex->spname->m_name);
 
3214
    break;
3177
3215
  case SQLCOM_DROP_EVENT:
3178
 
  case SQLCOM_SHOW_CREATE_EVENT:
3179
 
  {
3180
 
    DBUG_ASSERT(lex->spname);
3181
 
    if (! lex->spname->m_db.str)
3182
 
    {
3183
 
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
3184
 
      goto error;
3185
 
    }
3186
 
    if (check_access(thd, EVENT_ACL, lex->spname->m_db.str, 0, 0, 0,
3187
 
                     is_schema_db(lex->spname->m_db.str)))
3188
 
      break;
3189
 
 
3190
 
    if (lex->spname->m_name.length > NAME_LEN)
3191
 
    {
3192
 
      my_error(ER_TOO_LONG_IDENT, MYF(0), lex->spname->m_name.str);
3193
 
      /* this jumps to the end of the function and skips own messaging */
3194
 
      goto error;
3195
 
    }
3196
 
 
3197
 
    if (lex->sql_command == SQLCOM_SHOW_CREATE_EVENT)
3198
 
      res= Events::get_instance()->show_create_event(thd, lex->spname->m_db,
3199
 
                                                     lex->spname->m_name);
3200
 
    else
3201
 
    {
3202
 
      if (!(res= Events::get_instance()->drop_event(thd,
3203
 
                                                    lex->spname->m_db,
3204
 
                                                    lex->spname->m_name,
3205
 
                                                    lex->drop_if_exists)))
3206
 
        send_ok(thd);
3207
 
    }
 
3216
    if (!(res= Events::drop_event(thd,
 
3217
                                  lex->spname->m_db, lex->spname->m_name,
 
3218
                                  lex->drop_if_exists)))
 
3219
      send_ok(thd);
3208
3220
    break;
3209
 
  }
3210
3221
  case SQLCOM_CREATE_FUNCTION:                  // UDF function
3211
3222
  {
3212
3223
    if (check_access(thd,INSERT_ACL,"mysql",0,1,0,0))
3535
3546
        res= TRUE; // cannot happen
3536
3547
      else
3537
3548
      {
3538
 
        if ((thd->options &
3539
 
             (OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG)) &&
 
3549
        if (((thd->options & OPTION_KEEP_LOG) || thd->no_trans_update.all) &&
3540
3550
            !thd->slave_thread)
3541
3551
          push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
3542
3552
                       ER_WARNING_NOT_COMPLETE_ROLLBACK,
3922
3932
            if (check_access(thd, DELETE_ACL, "mysql", 0, 1, 0, 0))
3923
3933
              goto error;
3924
3934
 
3925
 
            /* Does NOT write to binlog */
3926
3935
            if (!(res = mysql_drop_function(thd, &lex->spname->m_name)))
3927
3936
            {
3928
3937
              send_ok(thd);
3966
3975
    }
3967
3976
  case SQLCOM_SHOW_CREATE_PROC:
3968
3977
    {
3969
 
      if (lex->spname->m_name.length > NAME_LEN)
3970
 
      {
3971
 
        my_error(ER_TOO_LONG_IDENT, MYF(0), lex->spname->m_name.str);
3972
 
        goto error;
3973
 
      }
3974
3978
      if (sp_show_create_procedure(thd, lex->spname) != SP_OK)
3975
3979
      {                 /* We don't distinguish between errors for now */
3976
3980
        my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
3981
3985
    }
3982
3986
  case SQLCOM_SHOW_CREATE_FUNC:
3983
3987
    {
3984
 
      if (lex->spname->m_name.length > NAME_LEN)
3985
 
      {
3986
 
        my_error(ER_TOO_LONG_IDENT, MYF(0), lex->spname->m_name.str);
3987
 
        goto error;
3988
 
      }
3989
3988
      if (sp_show_create_function(thd, lex->spname) != SP_OK)
3990
3989
      {                 /* We don't distinguish between errors for now */
3991
3990
        my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
4014
4013
    {
4015
4014
      sp_head *sp;
4016
4015
 
4017
 
      if (lex->spname->m_name.length > NAME_LEN)
4018
 
      {
4019
 
        my_error(ER_TOO_LONG_IDENT, MYF(0), lex->spname->m_name.str);
4020
 
        goto error;
4021
 
      }
4022
4016
      if (lex->sql_command == SQLCOM_SHOW_PROC_CODE)
4023
4017
        sp= sp_find_routine(thd, TYPE_ENUM_PROCEDURE, lex->spname,
4024
4018
                            &thd->sp_proc_cache, FALSE);
4109
4103
    thd->transaction.xid_state.xa_state=XA_ACTIVE;
4110
4104
    thd->transaction.xid_state.xid.set(thd->lex->xid);
4111
4105
    xid_cache_insert(&thd->transaction.xid_state);
4112
 
    thd->options= ((thd->options & ~(OPTION_STATUS_NO_TRANS_UPDATE |
4113
 
                                     OPTION_KEEP_LOG)) |
4114
 
                   OPTION_BEGIN);
 
4106
    thd->options= ((thd->options & ~(OPTION_KEEP_LOG)) | OPTION_BEGIN);
 
4107
    thd->no_trans_update.all= FALSE;
4115
4108
    thd->server_status|= SERVER_STATUS_IN_TRANS;
4116
4109
    send_ok(thd);
4117
4110
    break;
4204
4197
               xa_state_names[thd->transaction.xid_state.xa_state]);
4205
4198
      break;
4206
4199
    }
4207
 
    thd->options&= ~(OPTION_BEGIN | OPTION_STATUS_NO_TRANS_UPDATE |
4208
 
                     OPTION_KEEP_LOG);
 
4200
    thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
4201
    thd->no_trans_update.all= FALSE;
4209
4202
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
4210
4203
    xid_cache_delete(&thd->transaction.xid_state);
4211
4204
    thd->transaction.xid_state.xa_state=XA_NOTR;
4235
4228
      my_error(ER_XAER_RMERR, MYF(0));
4236
4229
    else
4237
4230
      send_ok(thd);
4238
 
    thd->options&= ~(OPTION_BEGIN | OPTION_STATUS_NO_TRANS_UPDATE |
4239
 
                     OPTION_KEEP_LOG);
 
4231
    thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
4232
    thd->no_trans_update.all= FALSE;
4240
4233
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
4241
4234
    xid_cache_delete(&thd->transaction.xid_state);
4242
4235
    thd->transaction.xid_state.xa_state=XA_NOTR;
4273
4266
    int error;
4274
4267
    LEX *lex= thd->lex;
4275
4268
    DBUG_PRINT("info", ("case SQLCOM_CREATE_SERVER"));
 
4269
 
 
4270
    if (check_global_access(thd, SUPER_ACL))
 
4271
      break;
 
4272
 
4276
4273
    if ((error= create_server(thd, &lex->server_options)))
4277
4274
    {
4278
4275
      DBUG_PRINT("info", ("problem creating server <%s>",
4288
4285
    int error;
4289
4286
    LEX *lex= thd->lex;
4290
4287
    DBUG_PRINT("info", ("case SQLCOM_ALTER_SERVER"));
 
4288
 
 
4289
    if (check_global_access(thd, SUPER_ACL))
 
4290
      break;
 
4291
 
4291
4292
    if ((error= alter_server(thd, &lex->server_options)))
4292
4293
    {
4293
4294
      DBUG_PRINT("info", ("problem altering server <%s>",
4303
4304
    int err_code;
4304
4305
    LEX *lex= thd->lex;
4305
4306
    DBUG_PRINT("info", ("case SQLCOM_DROP_SERVER"));
 
4307
 
 
4308
    if (check_global_access(thd, SUPER_ACL))
 
4309
      break;
 
4310
 
4306
4311
    if ((err_code= drop_server(thd, &lex->server_options)))
4307
4312
    {
4308
 
      if (! lex->drop_if_exists && err_code == ER_FOREIGN_SERVER_EXISTS)
 
4313
      if (! lex->drop_if_exists && err_code == ER_FOREIGN_SERVER_DOESNT_EXIST)
4309
4314
      {
4310
4315
        DBUG_PRINT("info", ("problem dropping server %s",
4311
4316
                            lex->server_options.server_name));
4431
4436
    thd                 Thread handler
4432
4437
    privilege           requested privilege
4433
4438
    all_tables          global table list of query
 
4439
    no_errors           FALSE/TRUE - report/don't report error to
 
4440
                            the client (using my_error() call).
4434
4441
 
4435
4442
  RETURN
4436
4443
    0 - OK
4438
4445
*/
4439
4446
 
4440
4447
bool check_single_table_access(THD *thd, ulong privilege, 
4441
 
                               TABLE_LIST *all_tables)
 
4448
                               TABLE_LIST *all_tables, bool no_errors)
4442
4449
{
4443
4450
  Security_context * backup_ctx= thd->security_ctx;
4444
4451
 
4454
4461
    db_name= all_tables->db;
4455
4462
 
4456
4463
  if (check_access(thd, privilege, db_name,
4457
 
                   &all_tables->grant.privilege, 0, 0,
 
4464
                   &all_tables->grant.privilege, 0, no_errors,
4458
4465
                   test(all_tables->schema_table)))
4459
4466
    goto deny;
4460
4467
 
4461
4468
  /* Show only 1 table for check_grant */
4462
 
  if (grant_option && check_grant(thd, privilege, all_tables, 0, 1, 0))
 
4469
  if (grant_option &&
 
4470
      !(all_tables->belong_to_view &&
 
4471
        (thd->lex->sql_command == SQLCOM_SHOW_FIELDS)) &&
 
4472
      check_grant(thd, privilege, all_tables, 0, 1, no_errors))
4463
4473
    goto deny;
4464
4474
 
4465
4475
  thd->security_ctx= backup_ctx;
4487
4497
 
4488
4498
bool check_one_table_access(THD *thd, ulong privilege, TABLE_LIST *all_tables)
4489
4499
{
4490
 
  if (check_single_table_access (thd,privilege,all_tables))
 
4500
  if (check_single_table_access (thd,privilege,all_tables, FALSE))
4491
4501
    return 1;
4492
4502
 
4493
4503
  /* Check rights on tables of subselects and implictly opened tables */
4500
4510
    */
4501
4511
    if (view && subselects_tables->belong_to_view == view)
4502
4512
    {
4503
 
      if (check_single_table_access (thd, privilege, subselects_tables))
 
4513
      if (check_single_table_access (thd, privilege, subselects_tables, FALSE))
4504
4514
        return 1;
4505
4515
      subselects_tables= subselects_tables->next_global;
4506
4516
    }
4676
4686
}
4677
4687
 
4678
4688
 
 
4689
static bool check_show_access(THD *thd, TABLE_LIST *table)
 
4690
{
 
4691
  switch (get_schema_table_idx(table->schema_table)) {
 
4692
  case SCH_SCHEMATA:
 
4693
    return (specialflag & SPECIAL_SKIP_SHOW_DB) &&
 
4694
      check_global_access(thd, SHOW_DB_ACL);
 
4695
 
 
4696
  case SCH_TABLE_NAMES:
 
4697
  case SCH_TABLES:
 
4698
  case SCH_VIEWS:
 
4699
  case SCH_TRIGGERS:
 
4700
  case SCH_EVENTS:
 
4701
  {
 
4702
    const char *dst_db_name= table->schema_select_lex->db;
 
4703
 
 
4704
    DBUG_ASSERT(dst_db_name);
 
4705
 
 
4706
    if (check_access(thd, SELECT_ACL, dst_db_name,
 
4707
                     &thd->col_access, FALSE, FALSE,
 
4708
                     is_schema_db(dst_db_name)))
 
4709
      return TRUE;
 
4710
 
 
4711
    if (!thd->col_access && check_grant_db(thd, dst_db_name))
 
4712
    {
 
4713
      my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
 
4714
               thd->security_ctx->priv_user,
 
4715
               thd->security_ctx->priv_host,
 
4716
               dst_db_name);
 
4717
      return TRUE;
 
4718
    }
 
4719
 
 
4720
    return FALSE;
 
4721
  }
 
4722
 
 
4723
  case SCH_COLUMNS:
 
4724
  case SCH_STATISTICS:
 
4725
  {
 
4726
    TABLE_LIST *dst_table;
 
4727
    dst_table= (TABLE_LIST *) table->schema_select_lex->table_list.first;
 
4728
 
 
4729
    DBUG_ASSERT(dst_table);
 
4730
 
 
4731
    if (check_access(thd, SELECT_ACL | EXTRA_ACL,
 
4732
                     dst_table->db,
 
4733
                     &dst_table->grant.privilege,
 
4734
                     FALSE, FALSE,
 
4735
                     test(dst_table->schema_table)))
 
4736
      return FALSE;
 
4737
 
 
4738
    return (grant_option &&
 
4739
            check_grant(thd, SELECT_ACL, dst_table, 2, UINT_MAX, FALSE));
 
4740
  }
 
4741
  default:
 
4742
    break;
 
4743
  }
 
4744
 
 
4745
  return FALSE;
 
4746
}
 
4747
 
 
4748
 
4679
4749
/*
4680
4750
  Check the privilege for all used tables.
4681
4751
 
4705
4775
{
4706
4776
  uint found=0;
4707
4777
  ulong found_access=0;
4708
 
#ifndef EMBEDDED_LIBRARY
 
4778
#ifndef NO_EMBEDDED_ACCESS_CHECKS
4709
4779
  TABLE_LIST *org_tables= tables;
4710
4780
#endif
4711
4781
  TABLE_LIST *first_not_own_table= thd->lex->first_not_own_table();
4728
4798
      if (!no_errors)
4729
4799
        my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
4730
4800
                 sctx->priv_user, sctx->priv_host,
4731
 
                 information_schema_name.str);
 
4801
                 INFORMATION_SCHEMA_NAME.str);
4732
4802
      return TRUE;
4733
4803
    }
4734
4804
    /*
4736
4806
       Remove SHOW_VIEW_ACL, because it will be checked during making view
4737
4807
     */
4738
4808
    tables->grant.orig_want_privilege= (want_access & ~SHOW_VIEW_ACL);
4739
 
    if (tables->derived || tables->schema_table ||
4740
 
        (tables->table && (int)tables->table->s->tmp_table) ||
4741
 
        my_tz_check_n_skip_implicit_tables(&tables,
4742
 
                                           thd->lex->time_zone_tables_used))
 
4809
 
 
4810
    if (tables->schema_table_reformed)
 
4811
    {
 
4812
      if (check_show_access(thd, tables))
 
4813
        goto deny;
 
4814
 
 
4815
      continue;
 
4816
    }
 
4817
 
 
4818
    if (tables->derived ||
 
4819
        (tables->table && (int)tables->table->s->tmp_table))
4743
4820
      continue;
4744
4821
    thd->security_ctx= sctx;
4745
4822
    if ((sctx->master_access & want_access) ==
4971
5048
****************************************************************************/
4972
5049
 
4973
5050
void
4974
 
mysql_init_query(THD *thd, uchar *buf, uint length)
 
5051
mysql_init_query(THD *thd, const char *buf, uint length)
4975
5052
{
4976
5053
  DBUG_ENTER("mysql_init_query");
4977
5054
  lex_start(thd, buf, length);
5018
5095
    in ha_rollback_trans() about some tables couldn't be rolled back.
5019
5096
  */
5020
5097
  if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
5021
 
    thd->options&= ~(OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG);
5022
 
 
 
5098
  {
 
5099
    thd->options&= ~OPTION_KEEP_LOG;
 
5100
    thd->no_trans_update.all= FALSE;
 
5101
  }
5023
5102
  DBUG_ASSERT(thd->security_ctx== &thd->main_security_ctx);
5024
5103
  thd->tmp_table_used= 0;
5025
5104
  if (!thd->in_sub_stmt)
5183
5262
  lex->query_tables_last= &lex->query_tables;
5184
5263
}
5185
5264
 
 
5265
 
5186
5266
/*
5187
5267
  When you modify mysql_parse(), you may need to mofify
5188
5268
  mysql_test_parse_for_slave() in this same file.
5194
5274
 
5195
5275
  DBUG_EXECUTE_IF("parser_debug", turn_parser_debug_on(););
5196
5276
 
5197
 
  mysql_init_query(thd, (uchar*) inBuf, length);
 
5277
  mysql_init_query(thd, inBuf, length);
 
5278
 
5198
5279
  if (query_cache_send_result_to_client(thd, inBuf, length) <= 0)
5199
5280
  {
5200
5281
    LEX *lex= thd->lex;
5273
5354
  bool error= 0;
5274
5355
  DBUG_ENTER("mysql_test_parse_for_slave");
5275
5356
 
5276
 
  mysql_init_query(thd, (uchar*) inBuf, length);
 
5357
  mysql_init_query(thd, inBuf, length);
5277
5358
  if (!MYSQLparse((void*) thd) && ! thd->is_fatal_error &&
5278
5359
      all_tables_not_ok(thd,(TABLE_LIST*) lex->select_lex.table_list.first))
5279
5360
    error= 1;                  /* Ignore question */
5290
5371
** Return 0 if ok
5291
5372
******************************************************************************/
5292
5373
 
5293
 
bool add_field_to_list(THD *thd, char *field_name, enum_field_types type,
 
5374
bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum_field_types type,
5294
5375
                       char *length, char *decimals,
5295
5376
                       uint type_modifier,
5296
5377
                       Item *default_value, Item *on_update_value,
5303
5384
  LEX  *lex= thd->lex;
5304
5385
  DBUG_ENTER("add_field_to_list");
5305
5386
 
5306
 
  if (strlen(field_name) > NAME_LEN)
 
5387
  if (check_string_char_length(field_name, "", NAME_CHAR_LEN,
 
5388
                               system_charset_info, 1))
5307
5389
  {
5308
 
    my_error(ER_TOO_LONG_IDENT, MYF(0), field_name); /* purecov: inspected */
 
5390
    my_error(ER_TOO_LONG_IDENT, MYF(0), field_name->str); /* purecov: inspected */
5309
5391
    DBUG_RETURN(1);                             /* purecov: inspected */
5310
5392
  }
5311
5393
  if (type_modifier & PRI_KEY_FLAG)
5312
5394
  {
5313
 
    lex->col_list.push_back(new key_part_spec(field_name,0));
 
5395
    lex->col_list.push_back(new key_part_spec(field_name->str, 0));
5314
5396
    lex->key_list.push_back(new Key(Key::PRIMARY, NullS,
5315
5397
                                    &default_key_create_info,
5316
5398
                                    0, lex->col_list));
5318
5400
  }
5319
5401
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
5320
5402
  {
5321
 
    lex->col_list.push_back(new key_part_spec(field_name,0));
 
5403
    lex->col_list.push_back(new key_part_spec(field_name->str, 0));
5322
5404
    lex->key_list.push_back(new Key(Key::UNIQUE, NullS,
5323
5405
                                    &default_key_create_info, 0,
5324
5406
                                    lex->col_list));
5338
5420
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
5339
5421
         type == MYSQL_TYPE_TIMESTAMP))
5340
5422
    {
5341
 
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name);
 
5423
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
5342
5424
      DBUG_RETURN(1);
5343
5425
    }
5344
5426
    else if (default_value->type() == Item::NULL_ITEM)
5347
5429
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
5348
5430
          NOT_NULL_FLAG)
5349
5431
      {
5350
 
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name);
 
5432
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
5351
5433
        DBUG_RETURN(1);
5352
5434
      }
5353
5435
    }
5354
5436
    else if (type_modifier & AUTO_INCREMENT_FLAG)
5355
5437
    {
5356
 
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name);
 
5438
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
5357
5439
      DBUG_RETURN(1);
5358
5440
    }
5359
5441
  }
5360
5442
 
5361
5443
  if (on_update_value && type != MYSQL_TYPE_TIMESTAMP)
5362
5444
  {
5363
 
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name);
 
5445
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
5364
5446
    DBUG_RETURN(1);
5365
5447
  }
5366
5448
 
5376
5458
  }
5377
5459
 
5378
5460
  if (!(new_field= new create_field()) ||
5379
 
      new_field->init(thd, field_name, type, length, decimals, type_modifier,
 
5461
      new_field->init(thd, field_name->str, type, length, decimals, type_modifier,
5380
5462
                      default_value, on_update_value, comment, change,
5381
5463
                      interval_list, cs, uint_geom_type))
5382
5464
    DBUG_RETURN(1);
5458
5540
                                             LEX_STRING *alias,
5459
5541
                                             ulong table_options,
5460
5542
                                             thr_lock_type lock_type,
5461
 
                                             List<String> *use_index_arg,
5462
 
                                             List<String> *ignore_index_arg,
 
5543
                                             List<index_hint> *index_hints_arg,
5463
5544
                                             LEX_STRING *option)
5464
5545
{
5465
5546
  register TABLE_LIST *ptr;
5518
5599
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
5519
5600
  ptr->derived=     table->sel;
5520
5601
  if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
5521
 
                                      information_schema_name.str))
 
5602
                                      INFORMATION_SCHEMA_NAME.str))
5522
5603
  {
5523
5604
    ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, ptr->table_name);
5524
5605
    if (!schema_table ||
5526
5607
         (sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0))
5527
5608
    {
5528
5609
      my_error(ER_UNKNOWN_TABLE, MYF(0),
5529
 
               ptr->table_name, information_schema_name.str);
 
5610
               ptr->table_name, INFORMATION_SCHEMA_NAME.str);
5530
5611
      DBUG_RETURN(0);
5531
5612
    }
5532
5613
    ptr->schema_table_name= ptr->table_name;
5534
5615
  }
5535
5616
  ptr->select_lex=  lex->current_select;
5536
5617
  ptr->cacheable_table= 1;
5537
 
  if (use_index_arg)
5538
 
    ptr->use_index=(List<String> *) thd->memdup((gptr) use_index_arg,
5539
 
                                                sizeof(*use_index_arg));
5540
 
  if (ignore_index_arg)
5541
 
    ptr->ignore_index=(List<String> *) thd->memdup((gptr) ignore_index_arg,
5542
 
                                                   sizeof(*ignore_index_arg));
 
5618
  ptr->index_hints= index_hints_arg;
5543
5619
  ptr->option= option ? option->str : 0;
5544
5620
  /* check that used name is unique */
5545
5621
  if (lock_type != TL_IGNORE)
6507
6583
  /*
6508
6584
    Is there tables of subqueries?
6509
6585
  */
6510
 
  if (&lex->select_lex != lex->all_selects_list || lex->time_zone_tables_used)
 
6586
  if (&lex->select_lex != lex->all_selects_list)
6511
6587
  {
6512
6588
    DBUG_PRINT("info",("Checking sub query list"));
6513
6589
    for (table= tables; table; table= table->next_global)
6514
6590
    {
6515
 
      if (!my_tz_check_n_skip_implicit_tables(&table,
6516
 
                                              lex->time_zone_tables_used) &&
6517
 
          !table->table_in_first_from_clause)
 
6591
      if (!table->table_in_first_from_clause)
6518
6592
      {
6519
6593
        if (check_access(thd, SELECT_ACL, table->db,
6520
6594
                         &table->grant.privilege, 0, 0,
6936
7010
 
6937
7011
 
6938
7012
/*
6939
 
  Check that length of a string does not exceed some limit.
 
7013
  Check that byte length of a string does not exceed some limit.
6940
7014
 
6941
7015
  SYNOPSIS
6942
 
    check_string_length()
6943
 
      str         string to be checked
6944
 
      err_msg     error message to be displayed if the string is too long
6945
 
      max_length  max length
 
7016
  check_string_byte_length()
 
7017
      str              string to be checked
 
7018
      err_msg          error message to be displayed if the string is too long
 
7019
      max_byte_length  max length in bytes
6946
7020
 
6947
7021
  RETURN
6948
7022
    FALSE   the passed string is not longer than max_length
6949
7023
    TRUE    the passed string is longer than max_length
6950
 
*/
6951
 
 
6952
 
bool check_string_length(LEX_STRING *str, const char *err_msg,
6953
 
                         uint max_length)
6954
 
{
6955
 
  if (str->length <= max_length)
6956
 
    return FALSE;
6957
 
 
6958
 
  my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_length);
6959
 
 
 
7024
 
 
7025
  NOTE
 
7026
    The function is not used in existing code but can be useful later?
 
7027
*/
 
7028
 
 
7029
bool check_string_byte_length(LEX_STRING *str, const char *err_msg,
 
7030
                              uint max_byte_length)
 
7031
{
 
7032
  if (str->length <= max_byte_length)
 
7033
    return FALSE;
 
7034
 
 
7035
  my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_byte_length);
 
7036
 
 
7037
  return TRUE;
 
7038
}
 
7039
 
 
7040
 
 
7041
/*
 
7042
  Check that char length of a string does not exceed some limit.
 
7043
 
 
7044
  SYNOPSIS
 
7045
  check_string_char_length()
 
7046
      str              string to be checked
 
7047
      err_msg          error message to be displayed if the string is too long
 
7048
      max_char_length  max length in symbols
 
7049
      cs               string charset
 
7050
 
 
7051
  RETURN
 
7052
    FALSE   the passed string is not longer than max_char_length
 
7053
    TRUE    the passed string is longer than max_char_length
 
7054
*/
 
7055
 
 
7056
 
 
7057
bool check_string_char_length(LEX_STRING *str, const char *err_msg,
 
7058
                              uint max_char_length, CHARSET_INFO *cs,
 
7059
                              bool no_error)
 
7060
{
 
7061
  int well_formed_error;
 
7062
  uint res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
 
7063
                                      max_char_length, &well_formed_error);
 
7064
 
 
7065
  if (!well_formed_error &&  str->length == res)
 
7066
    return FALSE;
 
7067
 
 
7068
  if (!no_error)
 
7069
    my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_char_length);
6960
7070
  return TRUE;
6961
7071
}