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 */
16
16
#include "config.h"
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>
31
28
#include <drizzled/db.h>
29
#include <drizzled/plugin/info_schema_table.h>
32
30
#include <drizzled/function/time/unix_timestamp.h>
33
31
#include <drizzled/function/get_system_var.h>
34
32
#include <drizzled/item/cmpfunc.h>
35
33
#include <drizzled/item/null.h>
36
34
#include <drizzled/session.h>
37
#include <drizzled/session/cache.h>
38
35
#include <drizzled/sql_load.h>
39
36
#include <drizzled/lock.h>
40
37
#include <drizzled/select_send.h>
42
39
#include <drizzled/statement.h>
43
40
#include <drizzled/statement/alter_table.h>
44
41
#include "drizzled/probes.h"
42
#include "drizzled/session_list.h"
45
43
#include "drizzled/global_charset_info.h"
44
#include "drizzled/transaction_services.h"
47
46
#include "drizzled/plugin/logging.h"
48
#include "drizzled/plugin/query_rewrite.h"
49
#include "drizzled/plugin/query_cache.h"
50
#include "drizzled/plugin/authorization.h"
47
#include "drizzled/plugin/info_schema_table.h"
51
48
#include "drizzled/optimizer/explain_plan.h"
52
49
#include "drizzled/pthread_globals.h"
53
#include "drizzled/plugin/event_observer.h"
54
#include "drizzled/visibility.h"
56
51
#include <limits.h>
59
54
#include <algorithm>
60
#include <boost/date_time.hpp>
61
56
#include "drizzled/internal/my_sys.h"
63
58
using namespace std;
80
76
extern size_t my_thread_stack_size;
81
77
extern const CHARSET_INFO *character_set_filesystem;
86
static const std::string command_name[COM_END+1]={
94
"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
99
90
const char *xa_state_names[]={
100
91
"NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
144
130
sql_command_flags[SQLCOM_REPLACE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
145
131
sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
133
sql_command_flags[SQLCOM_SHOW_STATUS]= CF_STATUS_COMMAND;
134
sql_command_flags[SQLCOM_SHOW_DATABASES]= CF_STATUS_COMMAND;
135
sql_command_flags[SQLCOM_SHOW_FIELDS]= CF_STATUS_COMMAND;
136
sql_command_flags[SQLCOM_SHOW_KEYS]= CF_STATUS_COMMAND;
137
sql_command_flags[SQLCOM_SHOW_VARIABLES]= CF_STATUS_COMMAND;
147
138
sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
148
139
sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
140
sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
149
141
sql_command_flags[SQLCOM_SHOW_CREATE_DB]= CF_STATUS_COMMAND;
150
142
sql_command_flags[SQLCOM_SHOW_CREATE]= CF_STATUS_COMMAND;
144
sql_command_flags[SQLCOM_SHOW_TABLES]= (CF_STATUS_COMMAND |
145
CF_SHOW_TABLE_COMMAND);
146
sql_command_flags[SQLCOM_SHOW_TABLE_STATUS]= (CF_STATUS_COMMAND |
147
CF_SHOW_TABLE_COMMAND);
153
149
The following admin table operations are allowed
197
194
/* Increase id and count all other statements. */
199
session->status_var.questions++;
196
statistic_increment(session->status_var.questions, &LOCK_status);
203
/* @todo set session->lex->sql_command to SQLCOM_END here */
200
/* TODO: set session->lex->sql_command to SQLCOM_END here */
205
202
plugin::Logging::preDo(session);
206
if (unlikely(plugin::EventObserver::beforeStatement(*session)))
208
// We should do something about an error...
211
204
session->server_status&=
212
205
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
213
206
switch (command) {
214
207
case COM_INIT_DB:
216
if (packet_length == 0)
218
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
222
string tmp(packet, packet_length);
224
identifier::Schema identifier(tmp);
226
if (not change_db(session, identifier))
210
status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
212
tmp.length= packet_length;
213
if (!mysql_change_db(session, &tmp, false))
228
215
session->my_ok();
234
if (not session->readAndStoreQuery(packet, packet_length))
221
if (! session->readAndStoreQuery(packet, packet_length))
235
222
break; // fatal error is set
236
DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
223
DRIZZLE_QUERY_START(session->query,
237
224
session->thread_id,
238
const_cast<const char *>(session->schema()->c_str()));
225
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
226
const char* end_of_stmt= NULL;
240
parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
228
mysql_parse(session, session->query, session->query_length, &end_of_stmt);
296
284
case Diagnostics_area::DA_ERROR:
297
285
/* The query failed, send error to log and abort bootstrap. */
298
session->getClient()->sendError(session->main_da.sql_errno(),
286
session->client->sendError(session->main_da.sql_errno(),
299
287
session->main_da.message());
302
290
case Diagnostics_area::DA_EOF:
303
session->getClient()->sendEOF();
291
session->client->sendEOF();
306
294
case Diagnostics_area::DA_OK:
307
session->getClient()->sendOK();
295
session->client->sendOK();
310
298
case Diagnostics_area::DA_DISABLED:
323
311
session->close_thread_tables();
325
313
plugin::Logging::postDo(session);
326
if (unlikely(plugin::EventObserver::afterStatement(*session)))
328
// We should do something about an error...
331
315
/* Store temp state for processlist */
332
316
session->set_proc_info("cleaning up");
333
317
session->command= COM_SLEEP;
334
session->resetQueryString();
318
memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
320
session->query_length= 0;
336
322
session->set_proc_info(NULL);
337
session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
323
free_root(session->mem_root,MYF(memory::KEEP_PREALLOC));
339
325
if (DRIZZLE_QUERY_DONE_ENABLED() || DRIZZLE_COMMAND_DONE_ENABLED())
374
360
1 out of memory or SHOW commands are not allowed
375
361
in this version of the server.
377
static bool _schema_select(Session *session, Select_Lex *sel,
378
const string& schema_table_name)
380
LEX_STRING db, table;
381
bitset<NUM_OF_TABLE_OPTIONS> table_options;
383
We have to make non const db_name & table_name
384
because of lower_case_table_names
386
session->make_lex_string(&db, "data_dictionary", sizeof("data_dictionary"), false);
387
session->make_lex_string(&table, schema_table_name, false);
389
if (! sel->add_table_to_list(session, new Table_ident(db, table),
390
NULL, table_options, TL_READ))
397
int prepare_new_schema_table(Session *session, LEX *lex,
398
const string& schema_table_name)
364
int prepare_schema_table(Session *session, LEX *lex, Table_ident *table_ident,
365
const string& schema_table_name)
400
367
Select_Lex *schema_select_lex= NULL;
370
if (schema_table_name.compare("TABLES") == 0 ||
371
schema_table_name.compare("TABLE_NAMES") == 0)
375
if (lex->select_lex.db == NULL &&
376
lex->copy_db_to(&lex->select_lex.db, &dummy))
380
schema_select_lex= new Select_Lex();
381
db.str= schema_select_lex->db= lex->select_lex.db;
382
schema_select_lex->table_list.first= NULL;
383
db.length= strlen(db.str);
385
if (check_db_name(&db))
387
my_error(ER_WRONG_DB_NAME, MYF(0), db.str);
391
else if (schema_table_name.compare("COLUMNS") == 0 ||
392
schema_table_name.compare("STATISTICS") == 0)
395
TableList **query_tables_last= lex->query_tables_last;
396
schema_select_lex= new Select_Lex();
397
/* 'parent_lex' is used in init_query() so it must be before it. */
398
schema_select_lex->parent_lex= lex;
399
schema_select_lex->init_query();
400
if (! schema_select_lex->add_table_to_list(session, table_ident, 0, 0, TL_READ))
404
lex->query_tables_last= query_tables_last;
402
407
Select_Lex *select_lex= lex->current_select;
403
408
assert(select_lex);
404
if (_schema_select(session, select_lex, schema_table_name))
409
if (make_schema_select(session, select_lex, schema_table_name))
486
498
drizzle_reset_errors(session, 0);
489
assert(session->transaction.stmt.hasModifiedNonTransData() == false);
501
status_var_increment(session->status_var.com_stat[lex->sql_command]);
491
if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
492
&& ! session->inTransaction()
493
&& lex->statement->isTransactional())
495
if (session->startTransaction() == false)
497
my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
503
assert(session->transaction.stmt.modified_non_trans_table == false);
502
505
/* now we are ready to execute the statement */
503
506
res= lex->statement->execute();
504
508
session->set_proc_info("query end");
506
511
The return value for ROW_COUNT() is "implementation dependent" if the
507
512
statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
641
new_select(LEX *lex, bool move_down)
628
mysql_new_select(LEX *lex, bool move_down)
643
630
Select_Lex *select_lex;
644
631
Session *session= lex->session;
646
633
if (!(select_lex= new (session->mem_root) Select_Lex()))
649
635
select_lex->select_number= ++session->select_number;
650
636
select_lex->parent_lex= lex; /* Used in init_query. */
651
637
select_lex->init_query();
652
638
select_lex->init_select();
653
639
lex->nest_level++;
655
640
if (lex->nest_level > (int) MAX_SELECT_NESTING)
657
642
my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
661
645
select_lex->nest_level= lex->nest_level;
753
736
@param session Current thread
754
737
@param inBuf Begining of the query text
755
738
@param length Length of the query text
739
@param[out] found_semicolon For multi queries, position of the character of
740
the next query in the query text.
758
void parse(Session *session, const char *inBuf, uint32_t length)
743
static void mysql_parse(Session *session, const char *inBuf, uint32_t length,
744
const char ** found_semicolon)
760
session->lex->start(session);
748
The purpose of query_cache_send_result_to_client() is to lookup the
749
query in the query cache first, to avoid parsing and executing it.
750
So, the natural implementation would be to:
751
- first, call query_cache_send_result_to_client,
752
- second, if caching failed, initialise the lexical and syntactic parser.
753
The problem is that the query cache depends on a clean initialization
754
of (among others) lex->safe_to_cache_query and session->server_status,
755
which are reset respectively in
757
- mysql_reset_session_for_next_command()
758
So, initializing the lexical analyser *before* using the query cache
759
is required for the cache to work properly.
760
FIXME: cleanup the dependencies in the code to simplify this.
762
763
session->reset_for_next_command();
763
/* Check if the Query is Cached if and return true if yes
764
* TODO the plugin has to make sure that the query is cacheble
765
* by setting the query_safe_cache param to TRUE
768
if (plugin::QueryCache::isCached(session))
770
res= plugin::QueryCache::sendCachedResultset(session);
776
LEX *lex= session->lex;
777
Lex_input_stream lip(session, inBuf, length);
778
bool err= parse_sql(session, &lip);
766
LEX *lex= session->lex;
768
Lex_input_stream lip(session, inBuf, length);
770
bool err= parse_sql(session, &lip);
771
*found_semicolon= lip.found_semicolon;
782
if (not session->is_error())
784
DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
786
const_cast<const char *>(session->schema()->c_str()));
787
// Implement Views here --Brian
788
/* Actually execute the query */
791
execute_command(session);
795
// Just try to catch any random failures that could have come
799
DRIZZLE_QUERY_EXEC_DONE(0);
776
if (! session->is_error())
779
Binlog logs a string starting from session->query and having length
780
session->query_length; so we set session->query_length correctly (to not
781
log several statements in one event, when we executed only first).
782
We set it to not see the ';' (otherwise it would get into binlog
783
and Query_log_event::print() would give ';;' output).
784
This also helps display only the current query in SHOW
786
Note that we don't need LOCK_thread_count to modify query_length.
788
if (*found_semicolon &&
789
(session->query_length= (ulong)(*found_semicolon - session->query)))
790
session->query_length--;
791
DRIZZLE_QUERY_EXEC_START(session->query,
793
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
794
/* Actually execute the query */
795
mysql_execute_command(session);
796
DRIZZLE_QUERY_EXEC_DONE(0);
805
assert(session->is_error());
808
session->set_proc_info("freeing items");
809
session->end_statement();
810
session->cleanup_after_query();
811
session->set_end_timer();
802
assert(session->is_error());
805
session->set_proc_info("freeing items");
806
session->end_statement();
807
session->cleanup_after_query();
944
951
return NULL; // End of memory
945
952
alias_str= alias ? alias->str : table->table.str;
946
if (! table_options.test(TL_OPTION_ALIAS) &&
953
if (!test(table_options & TL_OPTION_ALIAS) &&
947
954
check_table_name(table->table.str, table->table.length))
949
956
my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
953
if (table->is_derived_table() == false && table->db.str)
960
if (table->is_derived_table() == false && table->db.str &&
961
check_db_name(&table->db))
955
my_casedn_str(files_charset_info, table->db.str);
957
identifier::Schema schema_identifier(string(table->db.str));
958
if (not check_db_name(session, schema_identifier))
961
my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
963
my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
966
967
if (!alias) /* Alias is case sensitive */
971
972
ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
974
if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
975
if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
977
978
if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
980
980
if (table->db.str)
982
ptr->setIsFqtn(true);
983
ptr->setSchemaName(table->db.str);
983
ptr->db= table->db.str;
984
984
ptr->db_length= table->db.length;
986
else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
986
else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
989
ptr->setIsFqtn(false);
991
991
ptr->alias= alias_str;
992
ptr->setIsAlias(alias ? true : false);
993
ptr->setTableName(table->table.str);
992
ptr->is_alias= alias ? true : false;
993
if (table->table.length)
994
table->table.length= my_casedn_str(files_charset_info, table->table.str);
995
ptr->table_name=table->table.str;
994
996
ptr->table_name_length=table->table.length;
995
997
ptr->lock_type= lock_type;
996
ptr->force_index= table_options.test(TL_OPTION_FORCE_INDEX);
997
ptr->ignore_leaves= table_options.test(TL_OPTION_IGNORE_LEAVES);
998
ptr->updating= test(table_options & TL_OPTION_UPDATING);
999
ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
1000
ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
998
1001
ptr->derived= table->sel;
1002
if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
1003
INFORMATION_SCHEMA_NAME.c_str()))
1005
plugin::InfoSchemaTable *schema_table= plugin::InfoSchemaTable::getTable(ptr->table_name);
1006
if (!schema_table ||
1007
(schema_table->isHidden() &&
1008
((sql_command_flags[lex->sql_command].test(CF_BIT_STATUS_COMMAND)) == 0 ||
1010
this check is used for show columns|keys from I_S hidden table
1012
lex->sql_command == SQLCOM_SHOW_FIELDS ||
1013
lex->sql_command == SQLCOM_SHOW_KEYS)))
1015
my_error(ER_UNKNOWN_TABLE, MYF(0),
1016
ptr->table_name, INFORMATION_SCHEMA_NAME.c_str());
999
1020
ptr->select_lex= lex->current_select;
1000
1021
ptr->index_hints= index_hints_arg;
1001
1022
ptr->option= option ? option->str : 0;
1072
1093
bool Select_Lex::init_nested_join(Session *session)
1074
1095
TableList *ptr;
1075
NestedJoin *nested_join;
1096
nested_join_st *nested_join;
1077
1098
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1078
sizeof(NestedJoin))))
1099
sizeof(nested_join_st))))
1080
ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1081
nested_join= ptr->getNestedJoin();
1101
nested_join= ptr->nested_join=
1102
((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1082
1104
join_list->push_front(ptr);
1083
ptr->setEmbedding(embedding);
1084
ptr->setJoinList(join_list);
1105
ptr->embedding= embedding;
1106
ptr->join_list= join_list;
1085
1107
ptr->alias= (char*) "(nested_join)";
1086
1108
embedding= ptr;
1087
1109
join_list= &nested_join->join_list;
1107
1129
TableList *Select_Lex::end_nested_join(Session *)
1109
1131
TableList *ptr;
1110
NestedJoin *nested_join;
1132
nested_join_st *nested_join;
1112
1134
assert(embedding);
1113
1135
ptr= embedding;
1114
join_list= ptr->getJoinList();
1115
embedding= ptr->getEmbedding();
1116
nested_join= ptr->getNestedJoin();
1136
join_list= ptr->join_list;
1137
embedding= ptr->embedding;
1138
nested_join= ptr->nested_join;
1117
1139
if (nested_join->join_list.elements == 1)
1119
1141
TableList *embedded= nested_join->join_list.head();
1120
1142
join_list->pop();
1121
embedded->setJoinList(join_list);
1122
embedded->setEmbedding(embedding);
1143
embedded->join_list= join_list;
1144
embedded->embedding= embedding;
1123
1145
join_list->push_front(embedded);
1148
1170
TableList *Select_Lex::nest_last_join(Session *session)
1150
1172
TableList *ptr;
1151
NestedJoin *nested_join;
1173
nested_join_st *nested_join;
1152
1174
List<TableList> *embedded_list;
1154
1176
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1155
sizeof(NestedJoin))))
1177
sizeof(nested_join_st))))
1157
ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1158
nested_join= ptr->getNestedJoin();
1159
ptr->setEmbedding(embedding);
1160
ptr->setJoinList(join_list);
1179
nested_join= ptr->nested_join=
1180
((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1182
ptr->embedding= embedding;
1183
ptr->join_list= join_list;
1161
1184
ptr->alias= (char*) "(nest_last_join)";
1162
1185
embedded_list= &nested_join->join_list;
1163
1186
embedded_list->empty();
1478
@param session Thread class
1480
@param only_kill_query Should it kill the query or the connection
1483
This is written such that we have a short lock on LOCK_thread_count
1487
kill_one_thread(Session *, ulong id, bool only_kill_query)
1490
uint32_t error=ER_NO_SUCH_THREAD;
1491
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1493
for( vector<Session*>::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
1495
if ((*it)->thread_id == id)
1498
pthread_mutex_lock(&tmp->LOCK_delete); // Lock from delete
1502
pthread_mutex_unlock(&LOCK_thread_count);
1505
tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
1507
pthread_mutex_unlock(&tmp->LOCK_delete);
1514
kills a thread and sends response
1518
session Thread class
1520
only_kill_query Should it kill the query or the connection
1523
void sql_kill(Session *session, ulong id, bool only_kill_query)
1526
if (!(error= kill_one_thread(session, id, only_kill_query)))
1529
my_error(error, MYF(0), id);
1449
1534
Check if the select is a simple select (not an union).