12
12
You should have received a copy of the GNU General Public License
13
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
18
#define DRIZZLE_LEX 1
20
#include <drizzled/item/num.h>
21
#include <drizzled/abort_exception.h>
22
20
#include <drizzled/my_hash.h>
23
21
#include <drizzled/error.h>
24
22
#include <drizzled/nested_join.h>
25
23
#include <drizzled/query_id.h>
26
#include <drizzled/transaction_services.h>
27
24
#include <drizzled/sql_parse.h>
28
25
#include <drizzled/data_home.h>
29
26
#include <drizzled/sql_base.h>
30
27
#include <drizzled/show.h>
28
#include <drizzled/db.h>
31
29
#include <drizzled/function/time/unix_timestamp.h>
32
30
#include <drizzled/function/get_system_var.h>
33
31
#include <drizzled/item/cmpfunc.h>
34
32
#include <drizzled/item/null.h>
35
33
#include <drizzled/session.h>
36
#include <drizzled/session/cache.h>
37
34
#include <drizzled/sql_load.h>
38
35
#include <drizzled/lock.h>
39
36
#include <drizzled/select_send.h>
40
37
#include <drizzled/plugin/client.h>
41
38
#include <drizzled/statement.h>
42
39
#include <drizzled/statement/alter_table.h>
43
#include <drizzled/probes.h>
44
#include <drizzled/global_charset_info.h>
46
#include <drizzled/plugin/logging.h>
47
#include <drizzled/plugin/query_rewrite.h>
48
#include <drizzled/plugin/query_cache.h>
49
#include <drizzled/plugin/authorization.h>
50
#include <drizzled/optimizer/explain_plan.h>
51
#include <drizzled/pthread_globals.h>
52
#include <drizzled/plugin/event_observer.h>
53
#include <drizzled/visibility.h>
55
#include <drizzled/schema.h>
40
#include "drizzled/probes.h"
41
#include "drizzled/session_list.h"
42
#include "drizzled/global_charset_info.h"
43
#include "drizzled/transaction_services.h"
45
#include "drizzled/plugin/logging.h"
46
#include "drizzled/plugin/query_rewrite.h"
47
#include "drizzled/plugin/authorization.h"
48
#include "drizzled/optimizer/explain_plan.h"
49
#include "drizzled/pthread_globals.h"
57
51
#include <limits.h>
60
54
#include <algorithm>
61
#include <boost/date_time.hpp>
62
#include <drizzled/internal/my_sys.h>
56
#include "drizzled/internal/my_sys.h"
64
58
using namespace std;
66
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
60
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
72
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
66
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
73
67
static bool parse_sql(Session *session, Lex_input_stream *lip);
74
void parse(Session *session, const char *inBuf, uint32_t length);
68
void mysql_parse(Session *session, const char *inBuf, uint32_t length);
77
71
@defgroup Runtime_Environment Runtime Environment
81
75
extern size_t my_thread_stack_size;
82
76
extern const CHARSET_INFO *character_set_filesystem;
87
static const std::string command_name[COM_END+1]={
95
"Error" // Last command number
78
const LEX_STRING command_name[COM_END+1]={
79
{ C_STRING_WITH_LEN("Sleep") },
80
{ C_STRING_WITH_LEN("Quit") },
81
{ C_STRING_WITH_LEN("Init DB") },
82
{ C_STRING_WITH_LEN("Query") },
83
{ C_STRING_WITH_LEN("Shutdown") },
84
{ C_STRING_WITH_LEN("Connect") },
85
{ C_STRING_WITH_LEN("Ping") },
86
{ C_STRING_WITH_LEN("Error") } // Last command number
100
89
const char *xa_state_names[]={
101
90
"NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
198
183
/* Increase id and count all other statements. */
200
session->status_var.questions++;
185
statistic_increment(session->status_var.questions, &LOCK_status);
204
/* @todo set session->getLex()->sql_command to SQLCOM_END here */
189
/* TODO: set session->lex->sql_command to SQLCOM_END here */
206
191
plugin::Logging::preDo(session);
207
if (unlikely(plugin::EventObserver::beforeStatement(*session)))
209
// We should do something about an error...
212
193
session->server_status&=
213
194
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
214
195
switch (command) {
215
196
case COM_INIT_DB:
217
if (packet_length == 0)
219
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
198
status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
223
199
string tmp(packet, packet_length);
225
identifier::Schema identifier(tmp);
227
if (not schema::change(*session, identifier))
201
if (not mysql_change_db(session, tmp))
229
203
session->my_ok();
235
if (not session->readAndStoreQuery(packet, packet_length))
209
if (! session->readAndStoreQuery(packet, packet_length))
236
210
break; // fatal error is set
237
DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
211
DRIZZLE_QUERY_START(session->query.c_str(),
238
212
session->thread_id,
239
const_cast<const char *>(session->schema()->c_str()));
213
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
241
parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
215
plugin::QueryRewriter::rewriteQuery(session->query);
216
mysql_parse(session, session->query.c_str(), session->query.length());
297
272
case Diagnostics_area::DA_ERROR:
298
273
/* The query failed, send error to log and abort bootstrap. */
299
session->getClient()->sendError(session->main_da.sql_errno(),
274
session->client->sendError(session->main_da.sql_errno(),
300
275
session->main_da.message());
303
278
case Diagnostics_area::DA_EOF:
304
session->getClient()->sendEOF();
279
session->client->sendEOF();
307
282
case Diagnostics_area::DA_OK:
308
session->getClient()->sendOK();
283
session->client->sendOK();
311
286
case Diagnostics_area::DA_DISABLED:
324
299
session->close_thread_tables();
326
301
plugin::Logging::postDo(session);
327
if (unlikely(plugin::EventObserver::afterStatement(*session)))
329
// We should do something about an error...
332
303
/* Store temp state for processlist */
333
304
session->set_proc_info("cleaning up");
334
305
session->command= COM_SLEEP;
335
session->resetQueryString();
306
memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
307
session->query.clear();
337
309
session->set_proc_info(NULL);
338
session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
310
free_root(session->mem_root,MYF(memory::KEEP_PREALLOC));
340
312
if (DRIZZLE_QUERY_DONE_ENABLED() || DRIZZLE_COMMAND_DONE_ENABLED())
446
static int execute_command(Session *session)
418
mysql_execute_command(Session *session)
421
LEX *lex= session->lex;
450
422
/* first Select_Lex (have special meaning for many of non-SELECTcommands) */
451
Select_Lex *select_lex= &session->getLex()->select_lex;
423
Select_Lex *select_lex= &lex->select_lex;
453
424
/* list of all tables in query */
454
425
TableList *all_tables;
426
/* A peek into the query string */
427
size_t proc_info_len= session->query.length() > PROCESS_LIST_WIDTH ?
428
PROCESS_LIST_WIDTH : session->query.length();
430
memcpy(session->process_list_info, session->query.c_str(), proc_info_len);
431
session->process_list_info[proc_info_len]= '\0';
457
434
In many cases first table of main Select_Lex have special meaning =>
468
445
assert(first_table == all_tables);
469
446
assert(first_table == all_tables && first_table != 0);
471
session->getLex()->first_lists_tables_same();
448
lex->first_lists_tables_same();
473
449
/* should be assigned after making first tables same */
474
all_tables= session->getLex()->query_tables;
450
all_tables= lex->query_tables;
476
451
/* set context for commands which do not use setup_tables */
477
select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
453
context.resolve_in_table_list_only((TableList*)select_lex->
480
457
Reset warning count for each query that uses tables
483
460
variables, but for now this is probably good enough.
484
461
Don't reset warnings when executing a stored routine.
486
if (all_tables || ! session->getLex()->is_single_level_stmt())
463
if (all_tables || ! lex->is_single_level_stmt())
488
465
drizzle_reset_errors(session, 0);
468
status_var_increment(session->status_var.com_stat[lex->sql_command]);
491
470
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
472
/* now we are ready to execute the statement */
505
res= session->getLex()->statement->execute();
473
res= lex->statement->execute();
506
475
session->set_proc_info("query end");
508
478
The return value for ROW_COUNT() is "implementation dependent" if the
509
479
statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
510
480
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)))
483
if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
515
485
session->row_count_func= -1;
518
488
return (res || session->is_error());
520
491
bool execute_sqlcom_select(Session *session, TableList *all_tables)
522
LEX *lex= session->getLex();
493
LEX *lex= session->lex;
523
494
select_result *result=lex->result;
525
496
/* assign global limit variable if limit is not given */
557
515
session->send_explain_fields(result);
558
516
optimizer::ExplainPlan planner;
559
res= planner.explainUnion(session, &session->getLex()->unit, result);
517
res= planner.explainUnion(session, &session->lex->unit, result);
560
518
if (lex->describe & DESCRIBE_EXTENDED)
563
521
String str(buff,(uint32_t) sizeof(buff), system_charset_info);
565
session->getLex()->unit.print(&str, QT_ORDINARY);
523
session->lex->unit.print(&str, QT_ORDINARY);
566
524
str.append('\0');
567
525
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
568
526
ER_YES, str.ptr());
644
new_select(LEX *lex, bool move_down)
595
mysql_new_select(LEX *lex, bool move_down)
646
597
Select_Lex *select_lex;
647
598
Session *session= lex->session;
649
600
if (!(select_lex= new (session->mem_root) Select_Lex()))
652
602
select_lex->select_number= ++session->select_number;
653
603
select_lex->parent_lex= lex; /* Used in init_query. */
654
604
select_lex->init_query();
655
605
select_lex->init_select();
656
606
lex->nest_level++;
658
607
if (lex->nest_level > (int) MAX_SELECT_NESTING)
660
609
my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
664
612
select_lex->nest_level= lex->nest_level;
758
705
@param length Length of the query text
761
void parse(Session *session, const char *inBuf, uint32_t length)
708
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
763
session->getLex()->start(session);
765
711
session->reset_for_next_command();
766
/* Check if the Query is Cached if and return true if yes
767
* TODO the plugin has to make sure that the query is cacheble
768
* by setting the query_safe_cache param to TRUE
771
if (plugin::QueryCache::isCached(session))
773
res= plugin::QueryCache::sendCachedResultset(session);
779
LEX *lex= session->getLex();
713
LEX *lex= session->lex;
780
715
Lex_input_stream lip(session, inBuf, length);
781
717
bool err= parse_sql(session, &lip);
785
if (not session->is_error())
722
if (! session->is_error())
787
DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
724
DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
788
725
session->thread_id,
789
const_cast<const char *>(session->schema()->c_str()));
790
// Implement Views here --Brian
726
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
791
727
/* Actually execute the query */
794
execute_command(session);
798
// Just try to catch any random failures that could have come
728
mysql_execute_command(session);
802
729
DRIZZLE_QUERY_EXEC_DONE(0);
933
870
TableList *Select_Lex::add_table_to_list(Session *session,
936
const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
937
thr_lock_type lock_type,
938
List<Index_hint> *index_hints_arg,
873
uint32_t table_options,
874
thr_lock_type lock_type,
875
List<Index_hint> *index_hints_arg,
878
register TableList *ptr;
942
879
TableList *previous_table_ref; /* The table preceding the current one. */
944
LEX *lex= session->getLex();
881
LEX *lex= session->lex;
947
884
return NULL; // End of memory
948
885
alias_str= alias ? alias->str : table->table.str;
949
if (! table_options.test(TL_OPTION_ALIAS) &&
886
if (!test(table_options & TL_OPTION_ALIAS) &&
950
887
check_table_name(table->table.str, table->table.length))
952
889
my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
956
if (table->is_derived_table() == false && table->db.str)
893
if (table->is_derived_table() == false && table->db.str &&
894
check_db_name(&table->db))
958
my_casedn_str(files_charset_info, table->db.str);
960
identifier::Schema schema_identifier(string(table->db.str));
961
if (not schema::check(*session, schema_identifier))
964
my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
896
my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
969
900
if (!alias) /* Alias is case sensitive */
974
905
ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
977
if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
908
if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
980
911
if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
983
913
if (table->db.str)
985
ptr->setIsFqtn(true);
986
ptr->setSchemaName(table->db.str);
916
ptr->db= table->db.str;
987
917
ptr->db_length= table->db.length;
989
else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
919
else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
992
ptr->setIsFqtn(false);
994
924
ptr->alias= alias_str;
995
ptr->setIsAlias(alias ? true : false);
996
ptr->setTableName(table->table.str);
925
ptr->is_alias= alias ? true : false;
926
if (table->table.length)
927
table->table.length= my_casedn_str(files_charset_info, table->table.str);
928
ptr->table_name=table->table.str;
997
929
ptr->table_name_length=table->table.length;
998
930
ptr->lock_type= lock_type;
999
ptr->force_index= table_options.test(TL_OPTION_FORCE_INDEX);
1000
ptr->ignore_leaves= table_options.test(TL_OPTION_IGNORE_LEAVES);
931
ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
932
ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
1001
933
ptr->derived= table->sel;
1002
934
ptr->select_lex= lex->current_select;
1003
935
ptr->index_hints= index_hints_arg;
1011
943
tables=tables->next_local)
1013
if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1014
not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
945
if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
946
!strcmp(ptr->db, tables->db))
1016
948
my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1075
1007
bool Select_Lex::init_nested_join(Session *session)
1077
1009
TableList *ptr;
1078
NestedJoin *nested_join;
1010
nested_join_st *nested_join;
1080
1012
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1081
sizeof(NestedJoin))))
1013
sizeof(nested_join_st))))
1083
ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1084
nested_join= ptr->getNestedJoin();
1015
nested_join= ptr->nested_join=
1016
((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1085
1018
join_list->push_front(ptr);
1086
ptr->setEmbedding(embedding);
1087
ptr->setJoinList(join_list);
1019
ptr->embedding= embedding;
1020
ptr->join_list= join_list;
1088
1021
ptr->alias= (char*) "(nested_join)";
1089
1022
embedding= ptr;
1090
1023
join_list= &nested_join->join_list;
1110
1043
TableList *Select_Lex::end_nested_join(Session *)
1112
1045
TableList *ptr;
1113
NestedJoin *nested_join;
1046
nested_join_st *nested_join;
1115
1048
assert(embedding);
1116
1049
ptr= embedding;
1117
join_list= ptr->getJoinList();
1118
embedding= ptr->getEmbedding();
1119
nested_join= ptr->getNestedJoin();
1050
join_list= ptr->join_list;
1051
embedding= ptr->embedding;
1052
nested_join= ptr->nested_join;
1120
1053
if (nested_join->join_list.elements == 1)
1122
1055
TableList *embedded= nested_join->join_list.head();
1123
1056
join_list->pop();
1124
embedded->setJoinList(join_list);
1125
embedded->setEmbedding(embedding);
1057
embedded->join_list= join_list;
1058
embedded->embedding= embedding;
1126
1059
join_list->push_front(embedded);
1151
1084
TableList *Select_Lex::nest_last_join(Session *session)
1153
1086
TableList *ptr;
1154
NestedJoin *nested_join;
1087
nested_join_st *nested_join;
1155
1088
List<TableList> *embedded_list;
1157
1090
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1158
sizeof(NestedJoin))))
1091
sizeof(nested_join_st))))
1160
ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1161
nested_join= ptr->getNestedJoin();
1162
ptr->setEmbedding(embedding);
1163
ptr->setJoinList(join_list);
1093
nested_join= ptr->nested_join=
1094
((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1096
ptr->embedding= embedding;
1097
ptr->join_list= join_list;
1164
1098
ptr->alias= (char*) "(nest_last_join)";
1165
1099
embedded_list= &nested_join->join_list;
1166
embedded_list->clear();
1100
embedded_list->empty();
1168
1102
for (uint32_t i=0; i < 2; i++)
1170
1104
TableList *table= join_list->pop();
1171
table->setJoinList(embedded_list);
1172
table->setEmbedding(ptr);
1105
table->join_list= embedded_list;
1106
table->embedding= ptr;
1173
1107
embedded_list->push_back(table);
1174
1108
if (table->natural_join)
1388
@param session Thread class
1390
@param only_kill_query Should it kill the query or the connection
1393
This is written such that we have a short lock on LOCK_thread_count
1397
kill_one_thread(Session *, ulong id, bool only_kill_query)
1400
uint32_t error= ER_NO_SUCH_THREAD;
1401
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1403
for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
1405
if ((*it)->thread_id == id)
1408
pthread_mutex_lock(&tmp->LOCK_delete); // Lock from delete
1412
pthread_mutex_unlock(&LOCK_thread_count);
1416
if (tmp->isViewable())
1418
tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
1422
pthread_mutex_unlock(&tmp->LOCK_delete);
1429
kills a thread and sends response
1433
session Thread class
1435
only_kill_query Should it kill the query or the connection
1438
void sql_kill(Session *session, ulong id, bool only_kill_query)
1441
if (!(error= kill_one_thread(session, id, only_kill_query)))
1444
my_error(error, MYF(0), id);
1452
1449
Check if the select is a simple select (not an union).
1519
1518
bool update_precheck(Session *session, TableList *)
1521
1520
const char *msg= 0;
1522
Select_Lex *select_lex= &session->getLex()->select_lex;
1521
LEX *lex= session->lex;
1522
Select_Lex *select_lex= &lex->select_lex;
1524
if (session->getLex()->select_lex.item_list.elements != session->getLex()->value_list.elements)
1524
if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1526
1526
my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1530
if (session->getLex()->select_lex.table_list.elements > 1)
1530
if (session->lex->select_lex.table_list.elements > 1)
1532
1532
if (select_lex->order_list.elements)
1533
1533
msg= "ORDER BY";
1705
1731
/* Parse the query. */
1707
bool parse_status= base_sql_parse(session) != 0;
1733
bool mysql_parse_status= DRIZZLEparse(session) != 0;
1709
1735
/* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1711
assert(!parse_status || session->is_error());
1737
assert(!mysql_parse_status || session->is_error());
1713
1739
/* Reset Lex_input_stream. */
1715
1741
session->m_lip= NULL;
1717
DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
1743
DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1719
1745
/* That's it. */
1721
return parse_status || session->is_fatal_error;
1747
return mysql_parse_status || session->is_fatal_error;