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>
24
#include "drizzled/transaction_services.h"
27
25
#include <drizzled/sql_parse.h>
28
26
#include <drizzled/data_home.h>
29
27
#include <drizzled/sql_base.h>
30
28
#include <drizzled/show.h>
29
#include <drizzled/db.h>
31
30
#include <drizzled/function/time/unix_timestamp.h>
32
31
#include <drizzled/function/get_system_var.h>
33
32
#include <drizzled/item/cmpfunc.h>
34
33
#include <drizzled/item/null.h>
35
34
#include <drizzled/session.h>
36
#include <drizzled/session/cache.h>
37
35
#include <drizzled/sql_load.h>
38
36
#include <drizzled/lock.h>
39
37
#include <drizzled/select_send.h>
40
38
#include <drizzled/plugin/client.h>
41
39
#include <drizzled/statement.h>
42
40
#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>
41
#include "drizzled/probes.h"
42
#include "drizzled/session_list.h"
43
#include "drizzled/global_charset_info.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"
235
if (not session->readAndStoreQuery(packet, packet_length))
216
if (! session->readAndStoreQuery(packet, packet_length))
236
217
break; // fatal error is set
237
DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
218
DRIZZLE_QUERY_START(session->query.c_str(),
238
219
session->thread_id,
239
const_cast<const char *>(session->schema()->c_str()));
220
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
241
parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
222
plugin::QueryRewriter::rewriteQuery(session->db, session->query);
223
mysql_parse(session, session->query.c_str(), session->query.length());
297
279
case Diagnostics_area::DA_ERROR:
298
280
/* The query failed, send error to log and abort bootstrap. */
299
session->getClient()->sendError(session->main_da.sql_errno(),
281
session->client->sendError(session->main_da.sql_errno(),
300
282
session->main_da.message());
303
285
case Diagnostics_area::DA_EOF:
304
session->getClient()->sendEOF();
286
session->client->sendEOF();
307
289
case Diagnostics_area::DA_OK:
308
session->getClient()->sendOK();
290
session->client->sendOK();
311
293
case Diagnostics_area::DA_DISABLED:
324
306
session->close_thread_tables();
326
308
plugin::Logging::postDo(session);
327
if (unlikely(plugin::EventObserver::afterStatement(*session)))
329
// We should do something about an error...
332
310
/* Store temp state for processlist */
333
311
session->set_proc_info("cleaning up");
334
312
session->command= COM_SLEEP;
335
session->resetQueryString();
313
memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
314
session->query.clear();
337
316
session->set_proc_info(NULL);
338
317
session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
446
static int execute_command(Session *session)
425
mysql_execute_command(Session *session)
428
LEX *lex= session->lex;
450
429
/* first Select_Lex (have special meaning for many of non-SELECTcommands) */
451
Select_Lex *select_lex= &session->getLex()->select_lex;
430
Select_Lex *select_lex= &lex->select_lex;
453
431
/* list of all tables in query */
454
432
TableList *all_tables;
433
/* A peek into the query string */
434
size_t proc_info_len= session->query.length() > PROCESS_LIST_WIDTH ?
435
PROCESS_LIST_WIDTH : session->query.length();
437
memcpy(session->process_list_info, session->query.c_str(), proc_info_len);
438
session->process_list_info[proc_info_len]= '\0';
457
441
In many cases first table of main Select_Lex have special meaning =>
468
452
assert(first_table == all_tables);
469
453
assert(first_table == all_tables && first_table != 0);
471
session->getLex()->first_lists_tables_same();
455
lex->first_lists_tables_same();
473
456
/* should be assigned after making first tables same */
474
all_tables= session->getLex()->query_tables;
457
all_tables= lex->query_tables;
476
458
/* 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);
460
context.resolve_in_table_list_only((TableList*)select_lex->
480
464
Reset warning count for each query that uses tables
483
467
variables, but for now this is probably good enough.
484
468
Don't reset warnings when executing a stored routine.
486
if (all_tables || ! session->getLex()->is_single_level_stmt())
470
if (all_tables || ! lex->is_single_level_stmt())
488
472
drizzle_reset_errors(session, 0);
491
475
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
477
/* now we are ready to execute the statement */
505
res= session->getLex()->statement->execute();
478
res= lex->statement->execute();
506
480
session->set_proc_info("query end");
508
483
The return value for ROW_COUNT() is "implementation dependent" if the
509
484
statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
510
485
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)))
488
if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
515
490
session->row_count_func= -1;
518
493
return (res || session->is_error());
520
496
bool execute_sqlcom_select(Session *session, TableList *all_tables)
522
LEX *lex= session->getLex();
498
LEX *lex= session->lex;
523
499
select_result *result=lex->result;
525
501
/* assign global limit variable if limit is not given */
557
520
session->send_explain_fields(result);
558
521
optimizer::ExplainPlan planner;
559
res= planner.explainUnion(session, &session->getLex()->unit, result);
522
res= planner.explainUnion(session, &session->lex->unit, result);
560
523
if (lex->describe & DESCRIBE_EXTENDED)
563
526
String str(buff,(uint32_t) sizeof(buff), system_charset_info);
565
session->getLex()->unit.print(&str, QT_ORDINARY);
528
session->lex->unit.print(&str, QT_ORDINARY);
566
529
str.append('\0');
567
530
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
568
531
ER_YES, str.ptr());
644
new_select(LEX *lex, bool move_down)
600
mysql_new_select(LEX *lex, bool move_down)
646
602
Select_Lex *select_lex;
647
603
Session *session= lex->session;
649
605
if (!(select_lex= new (session->mem_root) Select_Lex()))
652
607
select_lex->select_number= ++session->select_number;
653
608
select_lex->parent_lex= lex; /* Used in init_query. */
654
609
select_lex->init_query();
655
610
select_lex->init_select();
656
611
lex->nest_level++;
658
612
if (lex->nest_level > (int) MAX_SELECT_NESTING)
660
614
my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
664
617
select_lex->nest_level= lex->nest_level;
758
710
@param length Length of the query text
761
void parse(Session *session, const char *inBuf, uint32_t length)
713
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
763
session->getLex()->start(session);
765
716
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();
718
LEX *lex= session->lex;
780
720
Lex_input_stream lip(session, inBuf, length);
781
722
bool err= parse_sql(session, &lip);
785
if (not session->is_error())
727
if (! session->is_error())
787
DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
729
DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
788
730
session->thread_id,
789
const_cast<const char *>(session->schema()->c_str()));
790
// Implement Views here --Brian
731
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
791
732
/* Actually execute the query */
794
execute_command(session);
798
// Just try to catch any random failures that could have come
733
mysql_execute_command(session);
802
734
DRIZZLE_QUERY_EXEC_DONE(0);
933
875
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,
878
uint32_t table_options,
879
thr_lock_type lock_type,
880
List<Index_hint> *index_hints_arg,
883
register TableList *ptr;
942
884
TableList *previous_table_ref; /* The table preceding the current one. */
944
LEX *lex= session->getLex();
886
LEX *lex= session->lex;
947
889
return NULL; // End of memory
948
890
alias_str= alias ? alias->str : table->table.str;
949
if (! table_options.test(TL_OPTION_ALIAS) &&
891
if (!test(table_options & TL_OPTION_ALIAS) &&
950
892
check_table_name(table->table.str, table->table.length))
952
894
my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
974
916
ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
977
if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
919
if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
980
922
if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
983
924
if (table->db.str)
985
ptr->setIsFqtn(true);
986
ptr->setSchemaName(table->db.str);
927
ptr->db= table->db.str;
987
928
ptr->db_length= table->db.length;
989
else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
930
else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
992
ptr->setIsFqtn(false);
994
935
ptr->alias= alias_str;
995
ptr->setIsAlias(alias ? true : false);
996
ptr->setTableName(table->table.str);
936
ptr->is_alias= alias ? true : false;
937
if (table->table.length)
938
table->table.length= my_casedn_str(files_charset_info, table->table.str);
939
ptr->table_name=table->table.str;
997
940
ptr->table_name_length=table->table.length;
998
941
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);
942
ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
943
ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
1001
944
ptr->derived= table->sel;
1002
945
ptr->select_lex= lex->current_select;
1003
946
ptr->index_hints= index_hints_arg;
1011
954
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()))
956
if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
957
!strcasecmp(ptr->db, tables->db))
1016
959
my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1075
1018
bool Select_Lex::init_nested_join(Session *session)
1077
1020
TableList *ptr;
1078
NestedJoin *nested_join;
1021
nested_join_st *nested_join;
1080
1023
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1081
sizeof(NestedJoin))))
1024
sizeof(nested_join_st))))
1083
ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1084
nested_join= ptr->getNestedJoin();
1026
nested_join= ptr->nested_join=
1027
((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1085
1029
join_list->push_front(ptr);
1086
ptr->setEmbedding(embedding);
1087
ptr->setJoinList(join_list);
1030
ptr->embedding= embedding;
1031
ptr->join_list= join_list;
1088
1032
ptr->alias= (char*) "(nested_join)";
1089
1033
embedding= ptr;
1090
1034
join_list= &nested_join->join_list;
1110
1054
TableList *Select_Lex::end_nested_join(Session *)
1112
1056
TableList *ptr;
1113
NestedJoin *nested_join;
1057
nested_join_st *nested_join;
1115
1059
assert(embedding);
1116
1060
ptr= embedding;
1117
join_list= ptr->getJoinList();
1118
embedding= ptr->getEmbedding();
1119
nested_join= ptr->getNestedJoin();
1061
join_list= ptr->join_list;
1062
embedding= ptr->embedding;
1063
nested_join= ptr->nested_join;
1120
1064
if (nested_join->join_list.elements == 1)
1122
1066
TableList *embedded= nested_join->join_list.head();
1123
1067
join_list->pop();
1124
embedded->setJoinList(join_list);
1125
embedded->setEmbedding(embedding);
1068
embedded->join_list= join_list;
1069
embedded->embedding= embedding;
1126
1070
join_list->push_front(embedded);
1151
1095
TableList *Select_Lex::nest_last_join(Session *session)
1153
1097
TableList *ptr;
1154
NestedJoin *nested_join;
1098
nested_join_st *nested_join;
1155
1099
List<TableList> *embedded_list;
1157
1101
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1158
sizeof(NestedJoin))))
1102
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);
1104
nested_join= ptr->nested_join=
1105
((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1107
ptr->embedding= embedding;
1108
ptr->join_list= join_list;
1164
1109
ptr->alias= (char*) "(nest_last_join)";
1165
1110
embedded_list= &nested_join->join_list;
1166
embedded_list->clear();
1111
embedded_list->empty();
1168
1113
for (uint32_t i=0; i < 2; i++)
1170
1115
TableList *table= join_list->pop();
1171
table->setJoinList(embedded_list);
1172
table->setEmbedding(ptr);
1116
table->join_list= embedded_list;
1117
table->embedding= ptr;
1173
1118
embedded_list->push_back(table);
1174
1119
if (table->natural_join)
1399
@param session Thread class
1401
@param only_kill_query Should it kill the query or the connection
1404
This is written such that we have a short lock on LOCK_thread_count
1408
kill_one_thread(Session *, ulong id, bool only_kill_query)
1411
uint32_t error= ER_NO_SUCH_THREAD;
1412
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1414
for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
1416
if ((*it)->thread_id == id)
1419
pthread_mutex_lock(&tmp->LOCK_delete); // Lock from delete
1423
pthread_mutex_unlock(&LOCK_thread_count);
1427
if (tmp->isViewable())
1429
tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
1433
pthread_mutex_unlock(&tmp->LOCK_delete);
1440
kills a thread and sends response
1444
session Thread class
1446
only_kill_query Should it kill the query or the connection
1449
void sql_kill(Session *session, ulong id, bool only_kill_query)
1452
if (!(error= kill_one_thread(session, id, only_kill_query)))
1455
my_error(error, MYF(0), id);
1452
1460
Check if the select is a simple select (not an union).
1519
1529
bool update_precheck(Session *session, TableList *)
1521
1531
const char *msg= 0;
1522
Select_Lex *select_lex= &session->getLex()->select_lex;
1532
LEX *lex= session->lex;
1533
Select_Lex *select_lex= &lex->select_lex;
1524
if (session->getLex()->select_lex.item_list.elements != session->getLex()->value_list.elements)
1535
if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1526
1537
my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1530
if (session->getLex()->select_lex.table_list.elements > 1)
1541
if (session->lex->select_lex.table_list.elements > 1)
1532
1543
if (select_lex->order_list.elements)
1533
1544
msg= "ORDER BY";
1587
CREATE TABLE query pre-check.
1589
@param session Thread handler
1590
@param tables Global table list
1591
@param create_table Table which will be created
1599
bool create_table_precheck(TableIdentifier &identifier)
1601
if (not plugin::StorageEngine::canCreateTable(identifier))
1603
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
1607
if (not plugin::StorageEngine::doesSchemaExist(identifier))
1609
my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
1574
1618
negate given expression.
1576
1620
@param session thread handler
1705
1748
/* Parse the query. */
1707
bool parse_status= base_sql_parse(session) != 0;
1750
bool mysql_parse_status= DRIZZLEparse(session) != 0;
1709
1752
/* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1711
assert(!parse_status || session->is_error());
1754
assert(!mysql_parse_status || session->is_error());
1713
1756
/* Reset Lex_input_stream. */
1715
1758
session->m_lip= NULL;
1717
DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
1760
DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1719
1762
/* That's it. */
1721
return parse_status || session->is_fatal_error;
1764
return mysql_parse_status || session->is_fatal_error;