483
476
variables, but for now this is probably good enough.
484
477
Don't reset warnings when executing a stored routine.
486
if (all_tables || ! session->getLex()->is_single_level_stmt())
479
if (all_tables || !lex->is_single_level_stmt())
488
480
drizzle_reset_errors(session, 0);
491
assert(session->transaction.stmt.hasModifiedNonTransData() == false);
493
if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
494
&& ! session->inTransaction()
495
&& session->getLex()->statement->isTransactional())
497
if (session->startTransaction() == false)
499
my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
504
/* now we are ready to execute the statement */
505
res= session->getLex()->statement->execute();
482
status_var_increment(session->status_var.com_stat[lex->sql_command]);
484
assert(session->transaction.stmt.modified_non_trans_table == false);
487
switch (lex->sql_command) {
488
case SQLCOM_CREATE_TABLE:
490
/* If CREATE TABLE of non-temporary table, do implicit commit */
491
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
493
if (! session->endActiveTransaction())
499
assert(first_table == all_tables && first_table != 0);
501
// Skip first table, which is the table we are creating
502
TableList *create_table= lex->unlink_first_table(&link_to_local);
503
TableList *select_tables= lex->query_tables;
505
Code below (especially in mysql_create_table() and select_create
506
methods) may modify HA_CREATE_INFO structure in LEX, so we have to
507
use a copy of this structure to make execution prepared statement-
508
safe. A shallow copy is enough as this code won't modify any memory
509
referenced from this structure.
511
HA_CREATE_INFO create_info(lex->create_info);
513
We need to copy alter_info for the same reasons of re-execution
514
safety, only in case of Alter_info we have to do (almost) a deep
517
Alter_info alter_info(lex->alter_info, session->mem_root);
519
if (session->is_fatal_error)
521
/* If out of memory when creating a copy of alter_info. */
523
goto end_with_restore_list;
526
if ((res= create_table_precheck(session, select_tables, create_table)))
527
goto end_with_restore_list;
529
/* Might have been updated in create_table_precheck */
530
create_info.alias= create_table->alias;
533
/* Fix names if symlinked tables */
534
if (append_file_to_dir(session, &create_info.data_file_name,
535
create_table->table_name) ||
536
append_file_to_dir(session, &create_info.index_file_name,
537
create_table->table_name))
538
goto end_with_restore_list;
541
The create-select command will open and read-lock the select table
542
and then create, open and write-lock the new table. If a global
543
read lock steps in, we get a deadlock. The write lock waits for
544
the global read lock, while the global read lock waits for the
545
select table to be closed. So we wait until the global readlock is
546
gone before starting both steps. Note that
547
wait_if_global_read_lock() sets a protection against a new global
548
read lock when it succeeds. This needs to be released by
549
start_waiting_global_read_lock(). We protect the normal CREATE
550
TABLE in the same way. That way we avoid that a new table is
551
created during a gobal read lock.
553
if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
556
goto end_with_restore_list;
558
if (select_lex->item_list.elements) // With select
560
select_result *result;
562
select_lex->options|= SELECT_NO_UNLOCK;
563
unit->set_limit(select_lex);
565
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
567
lex->link_first_table_back(create_table, link_to_local);
568
create_table->create= true;
571
if (!(res= session->openTablesLock(lex->query_tables)))
574
Is table which we are changing used somewhere in other parts
577
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
579
TableList *duplicate;
580
create_table= lex->unlink_first_table(&link_to_local);
581
if ((duplicate= unique_table(session, create_table, select_tables, 0)))
583
my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table->alias);
585
goto end_with_restore_list;
590
select_create is currently not re-execution friendly and
591
needs to be created for every execution of a PS/SP.
593
if ((result= new select_create(create_table,
595
lex->create_table_proto,
597
select_lex->item_list,
603
CREATE from SELECT give its Select_Lex for SELECT,
604
and item_list belong to SELECT
606
res= handle_select(session, lex, result, 0);
610
else if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
611
create_table= lex->unlink_first_table(&link_to_local);
616
/* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
617
if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
618
session->options|= OPTION_KEEP_LOG;
620
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
621
res= mysql_create_like_table(session, create_table, select_tables,
625
res= mysql_create_table(session, create_table->db,
626
create_table->table_name, &create_info,
627
lex->create_table_proto,
634
/* put tables back for PS rexecuting */
635
end_with_restore_list:
636
lex->link_first_table_back(create_table, link_to_local);
639
case SQLCOM_CREATE_INDEX:
641
case SQLCOM_DROP_INDEX:
643
CREATE INDEX and DROP INDEX are implemented by calling ALTER
644
TABLE with proper arguments.
646
In the future ALTER TABLE will notice that the request is to
647
only add indexes and create these one by one for the existing
648
table without having to do a full rebuild.
651
/* Prepare stack copies to be re-execution safe */
652
HA_CREATE_INFO create_info;
653
Alter_info alter_info(lex->alter_info, session->mem_root);
655
if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
658
assert(first_table == all_tables && first_table != 0);
659
if (! session->endActiveTransaction())
662
memset(&create_info, 0, sizeof(create_info));
663
create_info.db_type= 0;
664
create_info.row_type= ROW_TYPE_NOT_USED;
665
create_info.default_table_charset= get_default_db_collation(session->db);
667
res= mysql_alter_table(session, first_table->db, first_table->table_name,
668
&create_info, lex->create_table_proto, first_table,
670
0, (order_st*) 0, 0);
673
case SQLCOM_ALTER_TABLE:
674
assert(first_table == all_tables && first_table != 0);
677
Code in mysql_alter_table() may modify its HA_CREATE_INFO argument,
678
so we have to use a copy of this structure to make execution
679
prepared statement- safe. A shallow copy is enough as no memory
680
referenced from this structure will be modified.
682
HA_CREATE_INFO create_info(lex->create_info);
683
Alter_info alter_info(lex->alter_info, session->mem_root);
685
if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
690
/* Must be set in the parser */
691
assert(select_lex->db);
695
memset(&tmp_table, 0, sizeof(tmp_table));
696
tmp_table.table_name= lex->name.str;
697
tmp_table.db=select_lex->db;
700
/* Don't yet allow changing of symlinks with ALTER TABLE */
701
if (create_info.data_file_name)
702
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
703
"DATA DIRECTORY option ignored");
704
if (create_info.index_file_name)
705
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
706
"INDEX DIRECTORY option ignored");
707
create_info.data_file_name= create_info.index_file_name= NULL;
708
/* ALTER TABLE ends previous transaction */
709
if (! session->endActiveTransaction())
712
if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
718
res= mysql_alter_table(session, select_lex->db, lex->name.str,
720
lex->create_table_proto,
723
select_lex->order_list.elements,
724
(order_st *) select_lex->order_list.first,
728
case SQLCOM_RENAME_TABLE:
730
assert(first_table == all_tables && first_table != 0);
732
for (table= first_table; table; table= table->next_local->next_local)
734
TableList old_list, new_list;
736
we do not need initialize old_list and new_list because we will
737
come table[0] and table->next[0] there
740
new_list= table->next_local[0];
743
if (! session->endActiveTransaction() || drizzle_rename_tables(session, first_table))
751
assert(first_table == all_tables && first_table != 0);
752
res = mysql_check_table(session, first_table, &lex->check_opt);
753
select_lex->table_list.first= (unsigned char*) first_table;
754
lex->query_tables=all_tables;
759
assert(first_table == all_tables && first_table != 0);
760
res= mysql_analyze_table(session, first_table, &lex->check_opt);
761
/* ! we write after unlocking the table */
762
write_bin_log(session, true, session->query, session->query_length);
763
select_lex->table_list.first= (unsigned char*) first_table;
764
lex->query_tables=all_tables;
768
case SQLCOM_OPTIMIZE:
770
assert(first_table == all_tables && first_table != 0);
771
res= mysql_optimize_table(session, first_table, &lex->check_opt);
772
/* ! we write after unlocking the table */
773
write_bin_log(session, true, session->query, session->query_length);
774
select_lex->table_list.first= (unsigned char*) first_table;
775
lex->query_tables=all_tables;
779
assert(first_table == all_tables && first_table != 0);
780
if ((res= update_precheck(session, all_tables)))
782
assert(select_lex->offset_limit == 0);
783
unit->set_limit(select_lex);
784
res= mysql_update(session, all_tables,
785
select_lex->item_list,
788
select_lex->order_list.elements,
789
(order_st *) select_lex->order_list.first,
790
unit->select_limit_cnt,
791
lex->duplicates, lex->ignore);
796
assert(first_table == all_tables && first_table != 0);
797
if ((res= insert_precheck(session, all_tables)))
800
if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
806
res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
807
lex->update_list, lex->value_list,
808
lex->duplicates, lex->ignore);
812
case SQLCOM_REPLACE_SELECT:
813
case SQLCOM_INSERT_SELECT:
815
select_result *sel_result;
816
assert(first_table == all_tables && first_table != 0);
817
if ((res= insert_precheck(session, all_tables)))
820
/* Don't unlock tables until command is written to binary log */
821
select_lex->options|= SELECT_NO_UNLOCK;
823
unit->set_limit(select_lex);
825
if (! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
831
if (!(res= session->openTablesLock(all_tables)))
833
/* Skip first table, which is the table we are inserting in */
834
TableList *second_table= first_table->next_local;
835
select_lex->table_list.first= (unsigned char*) second_table;
836
select_lex->context.table_list=
837
select_lex->context.first_name_resolution_table= second_table;
838
res= mysql_insert_select_prepare(session);
839
if (!res && (sel_result= new select_insert(first_table,
847
res= handle_select(session, lex, sel_result, OPTION_SETUP_TABLES_DONE);
849
Invalidate the table in the query cache if something changed
850
after unlocking when changes become visible.
851
TODO: this is workaround. right way will be move invalidating in
852
the unlock procedure.
854
if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
857
/* INSERT ... SELECT should invalidate only the very first table */
858
TableList *save_table= first_table->next_local;
859
first_table->next_local= 0;
860
first_table->next_local= save_table;
864
/* revert changes for SP */
865
select_lex->table_list.first= (unsigned char*) first_table;
870
case SQLCOM_TRUNCATE:
871
if (! session->endActiveTransaction())
876
assert(first_table == all_tables && first_table != 0);
878
Don't allow this within a transaction because we want to use
881
if (session->inTransaction())
883
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
887
res= mysql_truncate(session, first_table, 0);
890
case SQLCOM_DROP_TABLE:
892
assert(first_table == all_tables && first_table != 0);
893
if (!lex->drop_temporary)
895
if (! session->endActiveTransaction())
900
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
901
session->options|= OPTION_KEEP_LOG;
903
/* DDL and binlog write order protected by LOCK_open */
904
res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
907
case SQLCOM_CHANGE_DB:
909
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
911
if (!mysql_change_db(session, &db_str, false))
916
case SQLCOM_SET_OPTION:
918
List<set_var_base> *lex_var_list= &lex->var_list;
920
if (session->openTablesLock(all_tables))
922
if (!(res= sql_set_variables(session, lex_var_list)))
929
We encountered some sort of error, but no message was sent.
930
Send something semi-generic here since we don't know which
931
assignment in the list caused the error.
933
if (!session->is_error())
934
my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
940
case SQLCOM_CREATE_DB:
943
As mysql_create_db() may modify HA_CREATE_INFO structure passed to
944
it, we need to use a copy of LEX::create_info to make execution
945
prepared statement- safe.
947
HA_CREATE_INFO create_info(lex->create_info);
948
if (! session->endActiveTransaction())
954
if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
955
check_db_name(&lex->name))
957
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
960
res= mysql_create_db(session,(lex->name.str), &create_info);
965
if (! session->endActiveTransaction())
970
if (check_db_name(&lex->name))
972
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
975
if (session->inTransaction())
977
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
980
res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists);
983
case SQLCOM_ALTER_DB:
985
LEX_STRING *db= &lex->name;
986
HA_CREATE_INFO create_info(lex->create_info);
987
if (check_db_name(db))
989
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
992
if (session->inTransaction())
994
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
997
res= mysql_alter_db(session, db->str, &create_info);
1003
reload_cache() will tell us if we are allowed to write to the
1006
if (!reload_cache(session, lex->type, first_table))
1009
We WANT to write and we CAN write.
1010
! we write after unlocking the table.
1013
Presumably, RESET and binlog writing doesn't require synchronization
1015
write_bin_log(session, false, session->query, session->query_length);
1023
Item *it= (Item *)lex->value_list.head();
1025
if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
1027
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
1031
sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
1035
if (session->transaction.xid_state.xa_state != XA_NOTR)
1037
my_error(ER_XAER_RMFAIL, MYF(0),
1038
xa_state_names[session->transaction.xid_state.xa_state]);
1042
Breakpoints for backup testing.
1044
if (! session->startTransaction())
1048
case SQLCOM_RELEASE_SAVEPOINT:
1051
for (sv=session->transaction.savepoints; sv; sv=sv->prev)
1053
if (my_strnncoll(system_charset_info,
1054
(unsigned char *)lex->ident.str, lex->ident.length,
1055
(unsigned char *)sv->name, sv->length) == 0)
1060
if (ha_release_savepoint(session, sv))
1061
res= true; // cannot happen
1064
session->transaction.savepoints=sv->prev;
1067
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
1070
case SQLCOM_ROLLBACK_TO_SAVEPOINT:
1073
for (sv=session->transaction.savepoints; sv; sv=sv->prev)
1075
if (my_strnncoll(system_charset_info,
1076
(unsigned char *)lex->ident.str, lex->ident.length,
1077
(unsigned char *)sv->name, sv->length) == 0)
1082
if (ha_rollback_to_savepoint(session, sv))
1083
res= true; // cannot happen
1086
if ((session->options & OPTION_KEEP_LOG) || session->transaction.all.modified_non_trans_table)
1087
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1088
ER_WARNING_NOT_COMPLETE_ROLLBACK,
1089
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
1092
session->transaction.savepoints=sv;
1095
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
1098
case SQLCOM_SAVEPOINT:
1099
if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
1103
SAVEPOINT **sv, *newsv;
1104
for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
1106
if (my_strnncoll(system_charset_info,
1107
(unsigned char *)lex->ident.str, lex->ident.length,
1108
(unsigned char *)(*sv)->name, (*sv)->length) == 0)
1111
if (*sv) /* old savepoint of the same name exists */
1114
ha_release_savepoint(session, *sv); // it cannot fail
1117
else if ((newsv=(SAVEPOINT *) alloc_root(&session->transaction.mem_root,
1118
savepoint_alloc_size)) == 0)
1120
my_error(ER_OUT_OF_RESOURCES, MYF(0));
1123
newsv->name=strmake_root(&session->transaction.mem_root,
1124
lex->ident.str, lex->ident.length);
1125
newsv->length=lex->ident.length;
1127
if we'll get an error here, don't add new savepoint to the list.
1128
we'll lose a little bit of memory in transaction mem_root, but it'll
1129
be free'd when transaction ends anyway
1131
if (ha_savepoint(session, newsv))
1135
newsv->prev=session->transaction.savepoints;
1136
session->transaction.savepoints=newsv;
1143
* This occurs now because we have extracted some commands in
1144
* to their own classes and thus there is no matching case
1145
* label in this switch statement for those commands. Pretty soon
1146
* this entire switch statement will be gone along with this
1149
comm_not_executed= true;
1153
* The following conditional statement is only temporary until
1154
* the mongo switch statement that occurs above has been
1155
* fully removed. Once that switch statement is gone, every
1156
* command will have its own class and we won't need this
1159
if (comm_not_executed)
1161
/* now we are ready to execute the statement */
1162
res= lex->statement->execute();
506
1165
session->set_proc_info("query end");
508
1168
The return value for ROW_COUNT() is "implementation dependent" if the
509
1169
statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
510
1170
wants. We also keep the last value in case of SQLCOM_CALL or
513
if (! (sql_command_flags[session->getLex()->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
1173
if (!(sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
515
1174
session->row_count_func= -1;
1182
if (need_start_waiting)
1185
Release the protection against the global read lock and wake
1186
everyone, who might want to set a global read lock.
1188
start_waiting_global_read_lock(session);
518
return (res || session->is_error());
1190
return(res || session->is_error());
520
1193
bool execute_sqlcom_select(Session *session, TableList *all_tables)
522
LEX *lex= session->getLex();
1195
LEX *lex= session->lex;
523
1196
select_result *result=lex->result;
524
1197
bool res= false;
525
1198
/* assign global limit variable if limit is not given */