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/query_cache.h"
48
#include "drizzled/plugin/authorization.h"
49
#include "drizzled/optimizer/explain_plan.h"
50
#include "drizzled/pthread_globals.h"
57
52
#include <limits.h>
60
55
#include <algorithm>
61
#include <boost/date_time.hpp>
62
#include <drizzled/internal/my_sys.h>
57
#include "drizzled/internal/my_sys.h"
64
59
using namespace std;
66
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
61
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
72
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
67
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
73
68
static bool parse_sql(Session *session, Lex_input_stream *lip);
74
void parse(Session *session, const char *inBuf, uint32_t length);
69
void mysql_parse(Session *session, const char *inBuf, uint32_t length);
77
72
@defgroup Runtime_Environment Runtime Environment
81
76
extern size_t my_thread_stack_size;
82
77
extern const CHARSET_INFO *character_set_filesystem;
87
static const std::string command_name[COM_END+1]={
95
"Error" // Last command number
79
const LEX_STRING command_name[COM_END+1]={
80
{ C_STRING_WITH_LEN("Sleep") },
81
{ C_STRING_WITH_LEN("Quit") },
82
{ C_STRING_WITH_LEN("Init DB") },
83
{ C_STRING_WITH_LEN("Query") },
84
{ C_STRING_WITH_LEN("Shutdown") },
85
{ C_STRING_WITH_LEN("Connect") },
86
{ C_STRING_WITH_LEN("Ping") },
87
{ C_STRING_WITH_LEN("Error") } // Last command number
100
90
const char *xa_state_names[]={
101
91
"NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
235
if (not session->readAndStoreQuery(packet, packet_length))
217
if (! session->readAndStoreQuery(packet, packet_length))
236
218
break; // fatal error is set
237
DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
219
DRIZZLE_QUERY_START(session->query.c_str(),
238
220
session->thread_id,
239
const_cast<const char *>(session->schema()->c_str()));
221
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
241
parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
223
plugin::QueryRewriter::rewriteQuery(session->db, session->query);
224
mysql_parse(session, session->query.c_str(), session->query.length());
297
280
case Diagnostics_area::DA_ERROR:
298
281
/* The query failed, send error to log and abort bootstrap. */
299
session->getClient()->sendError(session->main_da.sql_errno(),
282
session->client->sendError(session->main_da.sql_errno(),
300
283
session->main_da.message());
303
286
case Diagnostics_area::DA_EOF:
304
session->getClient()->sendEOF();
287
session->client->sendEOF();
307
290
case Diagnostics_area::DA_OK:
308
session->getClient()->sendOK();
291
session->client->sendOK();
311
294
case Diagnostics_area::DA_DISABLED:
324
307
session->close_thread_tables();
326
309
plugin::Logging::postDo(session);
327
if (unlikely(plugin::EventObserver::afterStatement(*session)))
329
// We should do something about an error...
332
311
/* Store temp state for processlist */
333
312
session->set_proc_info("cleaning up");
334
313
session->command= COM_SLEEP;
335
session->resetQueryString();
314
memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
315
session->query.clear();
337
317
session->set_proc_info(NULL);
338
318
session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
446
static int execute_command(Session *session)
426
mysql_execute_command(Session *session)
429
LEX *lex= session->lex;
450
430
/* first Select_Lex (have special meaning for many of non-SELECTcommands) */
451
Select_Lex *select_lex= &session->getLex()->select_lex;
431
Select_Lex *select_lex= &lex->select_lex;
453
432
/* list of all tables in query */
454
433
TableList *all_tables;
434
/* A peek into the query string */
435
size_t proc_info_len= session->query.length() > PROCESS_LIST_WIDTH ?
436
PROCESS_LIST_WIDTH : session->query.length();
438
memcpy(session->process_list_info, session->query.c_str(), proc_info_len);
439
session->process_list_info[proc_info_len]= '\0';
457
442
In many cases first table of main Select_Lex have special meaning =>
468
453
assert(first_table == all_tables);
469
454
assert(first_table == all_tables && first_table != 0);
471
session->getLex()->first_lists_tables_same();
456
lex->first_lists_tables_same();
473
457
/* should be assigned after making first tables same */
474
all_tables= session->getLex()->query_tables;
458
all_tables= lex->query_tables;
476
459
/* 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);
461
context.resolve_in_table_list_only((TableList*)select_lex->
480
465
Reset warning count for each query that uses tables
483
468
variables, but for now this is probably good enough.
484
469
Don't reset warnings when executing a stored routine.
486
if (all_tables || ! session->getLex()->is_single_level_stmt())
471
if (all_tables || ! lex->is_single_level_stmt())
488
473
drizzle_reset_errors(session, 0);
491
476
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
478
/* now we are ready to execute the statement */
505
res= session->getLex()->statement->execute();
479
res= lex->statement->execute();
506
480
session->set_proc_info("query end");
508
482
The return value for ROW_COUNT() is "implementation dependent" if the
557
518
session->send_explain_fields(result);
558
519
optimizer::ExplainPlan planner;
559
res= planner.explainUnion(session, &session->getLex()->unit, result);
520
res= planner.explainUnion(session, &session->lex->unit, result);
560
521
if (lex->describe & DESCRIBE_EXTENDED)
563
524
String str(buff,(uint32_t) sizeof(buff), system_charset_info);
565
session->getLex()->unit.print(&str, QT_ORDINARY);
526
session->lex->unit.print(&str, QT_ORDINARY);
566
527
str.append('\0');
567
528
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
568
529
ER_YES, str.ptr());
644
new_select(LEX *lex, bool move_down)
604
mysql_new_select(LEX *lex, bool move_down)
646
606
Select_Lex *select_lex;
647
607
Session *session= lex->session;
649
609
if (!(select_lex= new (session->mem_root) Select_Lex()))
652
611
select_lex->select_number= ++session->select_number;
653
612
select_lex->parent_lex= lex; /* Used in init_query. */
654
613
select_lex->init_query();
655
614
select_lex->init_select();
656
615
lex->nest_level++;
658
616
if (lex->nest_level > (int) MAX_SELECT_NESTING)
660
618
my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
664
621
select_lex->nest_level= lex->nest_level;
771
726
if (plugin::QueryCache::isCached(session))
728
cout << "Results are cached" << endl;
773
729
res= plugin::QueryCache::sendCachedResultset(session);
779
LEX *lex= session->getLex();
733
LEX *lex= session->lex;
780
734
Lex_input_stream lip(session, inBuf, length);
781
735
bool err= parse_sql(session, &lip);
785
if (not session->is_error())
739
if (! session->is_error())
787
DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
741
DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
788
742
session->thread_id,
789
const_cast<const char *>(session->schema()->c_str()));
743
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
790
744
// Implement Views here --Brian
791
745
/* Actually execute the query */
794
execute_command(session);
748
mysql_execute_command(session);
798
752
// Just try to catch any random failures that could have come
799
753
// during execution.
802
755
DRIZZLE_QUERY_EXEC_DONE(0);
933
893
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,
896
uint32_t table_options,
897
thr_lock_type lock_type,
898
List<Index_hint> *index_hints_arg,
901
register TableList *ptr;
942
902
TableList *previous_table_ref; /* The table preceding the current one. */
944
LEX *lex= session->getLex();
904
LEX *lex= session->lex;
947
907
return NULL; // End of memory
948
908
alias_str= alias ? alias->str : table->table.str;
949
if (! table_options.test(TL_OPTION_ALIAS) &&
909
if (!test(table_options & TL_OPTION_ALIAS) &&
950
910
check_table_name(table->table.str, table->table.length))
952
912
my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
974
934
ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
977
if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
937
if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
980
940
if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
983
942
if (table->db.str)
985
944
ptr->setIsFqtn(true);
986
ptr->setSchemaName(table->db.str);
945
ptr->db= table->db.str;
987
946
ptr->db_length= table->db.length;
989
else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
948
else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
992
951
ptr->setIsFqtn(false);
994
953
ptr->alias= alias_str;
995
954
ptr->setIsAlias(alias ? true : false);
996
ptr->setTableName(table->table.str);
955
if (table->table.length)
956
table->table.length= my_casedn_str(files_charset_info, table->table.str);
957
ptr->table_name=table->table.str;
997
958
ptr->table_name_length=table->table.length;
998
959
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);
960
ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
961
ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
1001
962
ptr->derived= table->sel;
1002
963
ptr->select_lex= lex->current_select;
1003
964
ptr->index_hints= index_hints_arg;
1151
1112
TableList *Select_Lex::nest_last_join(Session *session)
1153
1114
TableList *ptr;
1154
NestedJoin *nested_join;
1115
nested_join_st *nested_join;
1155
1116
List<TableList> *embedded_list;
1157
1118
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1158
sizeof(NestedJoin))))
1119
sizeof(nested_join_st))))
1160
ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1121
ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1161
1122
nested_join= ptr->getNestedJoin();
1162
1123
ptr->setEmbedding(embedding);
1163
1124
ptr->setJoinList(join_list);
1164
1125
ptr->alias= (char*) "(nest_last_join)";
1165
1126
embedded_list= &nested_join->join_list;
1166
embedded_list->clear();
1127
embedded_list->empty();
1168
1129
for (uint32_t i=0; i < 2; i++)
1415
@param session Thread class
1417
@param only_kill_query Should it kill the query or the connection
1420
This is written such that we have a short lock on LOCK_thread_count
1424
kill_one_thread(Session *, ulong id, bool only_kill_query)
1427
uint32_t error= ER_NO_SUCH_THREAD;
1428
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1430
for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
1432
if ((*it)->thread_id == id)
1435
pthread_mutex_lock(&tmp->LOCK_delete); // Lock from delete
1439
pthread_mutex_unlock(&LOCK_thread_count);
1443
if (tmp->isViewable())
1445
tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
1449
pthread_mutex_unlock(&tmp->LOCK_delete);
1456
kills a thread and sends response
1460
session Thread class
1462
only_kill_query Should it kill the query or the connection
1465
void sql_kill(Session *session, ulong id, bool only_kill_query)
1468
if (!(error= kill_one_thread(session, id, only_kill_query)))
1471
my_error(error, MYF(0), id);
1452
1476
Check if the select is a simple select (not an union).
1519
1545
bool update_precheck(Session *session, TableList *)
1521
1547
const char *msg= 0;
1522
Select_Lex *select_lex= &session->getLex()->select_lex;
1548
LEX *lex= session->lex;
1549
Select_Lex *select_lex= &lex->select_lex;
1524
if (session->getLex()->select_lex.item_list.elements != session->getLex()->value_list.elements)
1551
if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1526
1553
my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1530
if (session->getLex()->select_lex.table_list.elements > 1)
1557
if (session->lex->select_lex.table_list.elements > 1)
1532
1559
if (select_lex->order_list.elements)
1533
1560
msg= "ORDER BY";
1603
CREATE TABLE query pre-check.
1605
@param session Thread handler
1606
@param tables Global table list
1607
@param create_table Table which will be created
1615
bool create_table_precheck(TableIdentifier &identifier)
1617
if (not plugin::StorageEngine::canCreateTable(identifier))
1619
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
1623
if (not plugin::StorageEngine::doesSchemaExist(identifier))
1625
my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
1574
1634
negate given expression.
1576
1636
@param session thread handler
1705
1764
/* Parse the query. */
1707
bool parse_status= base_sql_parse(session) != 0;
1766
bool mysql_parse_status= DRIZZLEparse(session) != 0;
1709
1768
/* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1711
assert(!parse_status || session->is_error());
1770
assert(!mysql_parse_status || session->is_error());
1713
1772
/* Reset Lex_input_stream. */
1715
1774
session->m_lip= NULL;
1717
DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
1776
DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1719
1778
/* That's it. */
1721
return parse_status || session->is_fatal_error;
1780
return mysql_parse_status || session->is_fatal_error;