21
21
#include <drizzled/abort_exception.h>
22
22
#include <drizzled/error.h>
23
23
#include <drizzled/nested_join.h>
24
#include <drizzled/query_id.h>
25
24
#include <drizzled/transaction_services.h>
26
25
#include <drizzled/sql_parse.h>
27
26
#include <drizzled/data_home.h>
192
193
const char* packet, uint32_t packet_length)
194
195
bool error= false;
195
Query_id& query_id= Query_id::get_query_id();
197
197
DRIZZLE_COMMAND_START(session->thread_id, command);
199
199
session->command= command;
200
200
session->lex().sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
201
201
session->times.set_time();
202
session->setQueryId(query_id.value());
202
session->setQueryId(g_query_id.increment());
204
204
if (command != COM_PING)
206
206
// Increase id and count all other statements
207
207
session->status_var.questions++;
211
210
/* @todo set session->lex().sql_command to SQLCOM_END here */
452
451
static int execute_command(Session *session)
456
453
/* first Select_Lex (have special meaning for many of non-SELECTcommands) */
457
454
Select_Lex *select_lex= &session->lex().select_lex;
459
/* list of all tables in query */
460
TableList *all_tables;
463
457
In many cases first table of main Select_Lex have special meaning =>
464
458
check that it is first table in global list and relink it first in
477
471
session->lex().first_lists_tables_same();
473
/* list of all tables in query */
479
474
/* should be assigned after making first tables same */
480
all_tables= session->lex().query_tables;
475
TableList* all_tables= session->lex().query_tables;
482
477
/* set context for commands which do not use setup_tables */
483
478
select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
497
492
assert(not session->transaction.stmt.hasModifiedNonTransData());
499
if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
500
&& ! session->inTransaction()
494
if (not (session->server_status & SERVER_STATUS_AUTOCOMMIT)
495
&& not session->inTransaction()
501
496
&& session->lex().statement->isTransactional())
503
498
if (not session->startTransaction())
510
505
/* now we are ready to execute the statement */
511
res= session->lex().statement->execute();
506
bool res= session->lex().statement->execute();
512
507
session->set_proc_info("query end");
514
509
The return value for ROW_COUNT() is "implementation dependent" if the
516
511
wants. We also keep the last value in case of SQLCOM_CALL or
519
if (! (sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT)))
514
if (not sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT))
521
516
session->row_count_func= -1;
524
return (res || session->is_error());
519
return res || session->is_error();
526
522
bool execute_sqlcom_select(Session *session, TableList *all_tables)
528
524
LEX *lex= &session->lex();
696
692
if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
699
select_lex->context.outer_context=
700
unit->first_select()->context.outer_context;
695
select_lex->context.outer_context= unit->first_select()->context.outer_context;
703
698
select_lex->master_unit()->global_parameters= select_lex;
728
723
init_select(&lex);
729
724
lex.sql_command= SQLCOM_SELECT;
730
725
lex_string_t tmp;
731
tmp.str= (char*) var_name;
732
tmp.length=strlen(var_name);
733
lex_string_t null_lex_string;
734
memset(&null_lex_string.str, 0, sizeof(null_lex_string));
726
tmp.assign(var_name, strlen(var_name));
736
728
We set the name of Item to @@session.var_name because that then is used
737
729
as the column name in the output.
739
if (Item* var= get_system_var(session, OPT_SESSION, tmp, null_lex_string))
731
if (Item* var= get_system_var(session, OPT_SESSION, tmp, null_lex_string()))
741
733
char buff[MAX_SYS_VAR_LENGTH*2+4+8];
764
756
* by setting the query_safe_cache param to TRUE
766
758
if (plugin::QueryCache::isCached(&session) && not plugin::QueryCache::sendCachedResultset(&session))
768
760
Lex_input_stream lip(&session, inBuf, length);
769
761
if (parse_sql(&session, &lip))
770
762
assert(session.is_error());
771
763
else if (not session.is_error())
773
DRIZZLE_QUERY_EXEC_START(session.getQueryString()->c_str(), session.thread_id,
774
const_cast<const char *>(session.schema()->c_str()));
765
DRIZZLE_QUERY_EXEC_START(session.getQueryString()->c_str(), session.thread_id, session.schema()->c_str());
775
766
// Implement Views here --Brian
776
767
/* Actually execute the query */
804
bool add_field_to_list(Session *session, lex_string_t *field_name, enum_field_types type,
795
bool add_field_to_list(Session *session, str_ref field_name, enum_field_types type,
805
796
const char *length, const char *decimals,
806
uint32_t type_modifier,
807
enum column_format_type column_format,
808
Item *default_value, Item *on_update_value,
809
lex_string_t *comment,
811
List<String> *interval_list, const charset_info_st * const cs)
797
uint32_t type_modifier, column_format_type column_format,
798
Item *default_value, Item *on_update_value, str_ref comment,
799
const char *change, List<String> *interval_list, const charset_info_st* cs)
813
register CreateField *new_field;
814
801
LEX *lex= &session->lex();
815
802
statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
820
807
if (type_modifier & PRI_KEY_FLAG)
823
lex->col_list.push_back(new Key_part_spec(*field_name, 0));
824
key= new Key(Key::PRIMARY, null_lex_string(),
825
&default_key_create_info,
827
statement->alter_info.key_list.push_back(key);
809
lex->col_list.push_back(new Key_part_spec(field_name, 0));
810
statement->alter_info.key_list.push_back(new Key(Key::PRIMARY, null_lex_string(), &default_key_create_info, 0, lex->col_list));
828
811
lex->col_list.clear();
830
813
if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
833
lex->col_list.push_back(new Key_part_spec(*field_name, 0));
834
key= new Key(Key::UNIQUE, null_lex_string(),
835
&default_key_create_info, 0,
837
statement->alter_info.key_list.push_back(key);
815
lex->col_list.push_back(new Key_part_spec(field_name, 0));
816
statement->alter_info.key_list.push_back(new Key(Key::UNIQUE, null_lex_string(), &default_key_create_info, 0, lex->col_list));
838
817
lex->col_list.clear();
851
830
!(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
852
831
(type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
854
my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
833
my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
857
836
else if (default_value->type() == Item::NULL_ITEM)
859
838
default_value= 0;
860
839
if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
862
my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
841
my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
866
845
else if (type_modifier & AUTO_INCREMENT_FLAG)
868
my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
847
my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
873
852
if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
875
my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
854
my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name.data());
879
new_field= new CreateField;
880
if (new_field->init(session, field_name->str, type, length, decimals,
881
type_modifier, comment, change, interval_list, cs, 0, column_format)
858
CreateField* new_field= new CreateField;
859
if (new_field->init(session, field_name.data(), type, length, decimals, type_modifier, comment, change, interval_list, cs, 0, column_format)
882
860
|| new_field->setDefaultValue(default_value, on_update_value))
923
901
return NULL; // End of memory
924
const char* alias_str= alias ? alias->str : table->table.str;
925
if (! table_options.test(TL_OPTION_ALIAS) &&
926
check_table_name(table->table.str, table->table.length))
902
const char* alias_str= alias ? alias->data() : table->table.data();
903
if (not table_options.test(TL_OPTION_ALIAS) && check_table_name(table->table))
928
my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
905
my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.data());
932
if (not table->is_derived_table() && table->db.str)
909
if (not table->is_derived_table() && table->db.data())
934
my_casedn_str(files_charset_info, table->db.str);
936
identifier::Schema schema_identifier(string(table->db.str));
937
if (not schema::check(*session, schema_identifier))
911
files_charset_info->casedn_str(table->db.str_);
912
if (not schema::check(*session, identifier::Schema(table->db)))
939
my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
914
my_error(ER_WRONG_DB_NAME, MYF(0), table->db.data());
948
923
my_message(ER_DERIVED_MUST_HAVE_ALIAS, ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
951
alias_str= (char*) session->mem.memdup(alias_str,table->table.length+1);
926
alias_str= (char*) session->mem.memdup(alias_str, table->table.size() + 1);
953
928
TableList *ptr = (TableList *) session->mem.calloc(sizeof(TableList));
930
if (table->db.data())
959
932
ptr->setIsFqtn(true);
960
ptr->setSchemaName(table->db.str);
933
ptr->setSchemaName(table->db.data());
962
else if (lex->session->copy_db_to(name, name_size))
937
str_ref schema = lex->session->copy_db_to();
966
940
ptr->setIsFqtn(false);
967
ptr->setSchemaName(name);
941
ptr->setSchemaName(schema.data());
970
944
ptr->alias= alias_str;
971
945
ptr->setIsAlias(alias ? true : false);
972
ptr->setTableName(table->table.str);
946
ptr->setTableName(table->table.data());
973
947
ptr->lock_type= lock_type;
974
948
ptr->force_index= table_options.test(TL_OPTION_FORCE_INDEX);
975
949
ptr->ignore_leaves= table_options.test(TL_OPTION_IGNORE_LEAVES);
976
950
ptr->derived= table->sel;
977
951
ptr->select_lex= lex->current_select;
978
952
ptr->index_hints= index_hints_arg;
979
ptr->option= option ? option->str : 0;
953
ptr->option= option ? option->data() : NULL;
980
954
/* check that used name is unique */
981
955
if (lock_type != TL_IGNORE)
983
957
TableList *first_table= (TableList*) table_list.first;
984
958
for (TableList *tables= first_table; tables; tables= tables->next_local)
986
if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
987
not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
960
if (not table_alias_charset->strcasecmp(alias_str, tables->alias) &&
961
not system_charset_info->strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
989
963
my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1000
974
We don't use the offsetof() macro here to avoid warnings from gcc
1002
previous_table_ref= (TableList*) ((char*) table_list.next -
1003
((char*) &(ptr->next_local) -
976
previous_table_ref= (TableList*) ((char*) table_list.next - ((char*) &(ptr->next_local) - (char*) ptr));
1006
978
Set next_name_resolution_table of the previous table reference to point
1007
979
to the current table reference. In effect the list
1077
TableList *Select_Lex::end_nested_join(Session *)
1049
TableList *Select_Lex::end_nested_join()
1080
NestedJoin *nested_join;
1082
1051
assert(embedding);
1052
TableList* ptr= embedding;
1084
1053
join_list= ptr->getJoinList();
1085
1054
embedding= ptr->getEmbedding();
1086
nested_join= ptr->getNestedJoin();
1055
NestedJoin* nested_join= ptr->getNestedJoin();
1087
1056
if (nested_join->join_list.size() == 1)
1089
1058
TableList *embedded= &nested_join->join_list.front();
1091
1060
embedded->setJoinList(join_list);
1092
1061
embedded->setEmbedding(embedding);
1093
1062
join_list->push_front(embedded);
1096
else if (nested_join->join_list.size() == 0)
1065
else if (not nested_join->join_list.size())
1098
1067
join_list->pop();
1099
ptr= NULL; // return value
1228
1197
void Select_Lex::set_lock_for_tables(thr_lock_type lock_type)
1230
for (TableList *tables= (TableList*) table_list.first;
1232
tables= tables->next_local)
1199
for (TableList *tables= (TableList*) table_list.first; tables; tables= tables->next_local)
1234
1201
tables->lock_type= lock_type;
1576
bool check_string_char_length(lex_string_t *str, const char *err_msg,
1543
bool check_string_char_length(str_ref str, const char *err_msg,
1577
1544
uint32_t max_char_length, const charset_info_st * const cs,
1580
1547
int well_formed_error;
1581
uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1582
max_char_length, &well_formed_error);
1548
uint32_t res= cs->cset->well_formed_len(*cs, str, max_char_length, &well_formed_error);
1584
if (!well_formed_error && str->length == res)
1550
if (!well_formed_error && str.size() == res)
1588
my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_char_length);
1554
my_error(ER_WRONG_STRING_LENGTH, MYF(0), str.data(), err_msg, max_char_length);
1593
bool check_identifier_name(lex_string_t *str, error_t err_code,
1594
uint32_t max_char_length,
1595
const char *param_for_err_msg)
1559
bool check_identifier_name(str_ref str, error_t err_code)
1561
uint32_t max_char_length= NAME_CHAR_LEN;
1598
1563
We don't support non-BMP characters in identifiers at the moment,
1599
1564
so they should be prohibited until such support is done.
1602
1567
const charset_info_st * const cs= &my_charset_utf8mb4_general_ci;
1604
1569
int well_formed_error;
1605
uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1606
max_char_length, &well_formed_error);
1570
uint32_t res= cs->cset->well_formed_len(*cs, str, max_char_length, &well_formed_error);
1608
1572
if (well_formed_error)
1610
my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str->str);
1574
my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str.data());
1614
if (str->length == res)
1578
if (str.size() == res)
1617
1581
switch (err_code)
1621
1585
case ER_WRONG_STRING_LENGTH:
1622
my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1586
my_error(err_code, MYF(0), str.data(), "", max_char_length);
1624
1588
case ER_TOO_LONG_IDENT:
1625
my_error(err_code, MYF(0), str->str);
1589
my_error(err_code, MYF(0), str.data());