13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18
18
#define DRIZZLE_LEX 1
20
#include <drizzled/my_hash.h>
20
#include <drizzled/item/num.h>
21
#include <drizzled/abort_exception.h>
21
22
#include <drizzled/error.h>
22
23
#include <drizzled/nested_join.h>
23
24
#include <drizzled/query_id.h>
24
#include "drizzled/transaction_services.h"
25
#include <drizzled/transaction_services.h>
25
26
#include <drizzled/sql_parse.h>
26
27
#include <drizzled/data_home.h>
27
28
#include <drizzled/sql_base.h>
28
29
#include <drizzled/show.h>
29
#include <drizzled/db.h>
30
30
#include <drizzled/function/time/unix_timestamp.h>
31
31
#include <drizzled/function/get_system_var.h>
32
32
#include <drizzled/item/cmpfunc.h>
33
33
#include <drizzled/item/null.h>
34
34
#include <drizzled/session.h>
35
#include <drizzled/session/cache.h>
35
36
#include <drizzled/sql_load.h>
36
37
#include <drizzled/lock.h>
37
38
#include <drizzled/select_send.h>
38
39
#include <drizzled/plugin/client.h>
39
40
#include <drizzled/statement.h>
40
41
#include <drizzled/statement/alter_table.h>
41
#include "drizzled/probes.h"
42
#include "drizzled/session/cache.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"
51
#include "drizzled/plugin/event_observer.h"
42
#include <drizzled/probes.h>
43
#include <drizzled/global_charset_info.h>
44
#include <drizzled/plugin/logging.h>
45
#include <drizzled/plugin/query_rewrite.h>
46
#include <drizzled/plugin/query_cache.h>
47
#include <drizzled/plugin/authorization.h>
48
#include <drizzled/optimizer/explain_plan.h>
49
#include <drizzled/pthread_globals.h>
50
#include <drizzled/plugin/event_observer.h>
51
#include <drizzled/display.h>
52
#include <drizzled/visibility.h>
53
#include <drizzled/kill.h>
54
#include <drizzled/schema.h>
55
#include <drizzled/item/subselect.h>
56
#include <drizzled/diagnostics_area.h>
57
#include <drizzled/table_ident.h>
58
#include <drizzled/statistics_variables.h>
59
#include <drizzled/system_variables.h>
60
#include <drizzled/session/transactions.h>
53
62
#include <limits.h>
56
65
#include <algorithm>
57
66
#include <boost/date_time.hpp>
58
#include "drizzled/internal/my_sys.h"
67
#include <drizzled/internal/my_sys.h>
60
69
using namespace std;
62
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
71
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
68
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
76
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
69
77
static bool parse_sql(Session *session, Lex_input_stream *lip);
70
void mysql_parse(Session *session, const char *inBuf, uint32_t length);
78
void parse(Session *session, const char *inBuf, uint32_t length);
73
81
@defgroup Runtime_Environment Runtime Environment
77
85
extern size_t my_thread_stack_size;
78
86
extern const CHARSET_INFO *character_set_filesystem;
80
const LEX_STRING command_name[COM_END+1]={
81
{ C_STRING_WITH_LEN("Sleep") },
82
{ C_STRING_WITH_LEN("Quit") },
83
{ C_STRING_WITH_LEN("Init DB") },
84
{ C_STRING_WITH_LEN("Query") },
85
{ C_STRING_WITH_LEN("Shutdown") },
86
{ C_STRING_WITH_LEN("Connect") },
87
{ C_STRING_WITH_LEN("Ping") },
88
{ C_STRING_WITH_LEN("Error") } // Last command number
91
static const std::string command_name[COM_END+1]={
100
"Error" // Last command number
91
105
const char *xa_state_names[]={
92
106
"NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
224
244
session->thread_id,
225
245
const_cast<const char *>(session->schema()->c_str()));
227
mysql_parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
247
parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
232
252
/* We don't calculate statistics for this command */
233
session->main_da.disable_status(); // Don't send anything back
234
error=true; // End server
253
session->main_da().disable_status(); // Don't send anything back
254
error= true; // End server
258
if (packet_length != 4)
260
my_error(ER_NO_SUCH_THREAD, MYF(0), 0);
266
memcpy(&kill_id, packet, sizeof(uint32_t));
268
kill_id= ntohl(kill_id);
269
(void)drizzled::kill(*session->user(), kill_id, true);
236
274
case COM_SHUTDOWN:
238
276
session->status_var.com_other++;
239
277
session->my_eof();
240
278
session->close_thread_tables(); // Free before kill
278
316
/* Can not be true, but do not take chances in production. */
279
assert(! session->main_da.is_sent);
317
assert(! session->main_da().is_sent);
281
switch (session->main_da.status())
319
switch (session->main_da().status())
283
321
case Diagnostics_area::DA_ERROR:
284
322
/* The query failed, send error to log and abort bootstrap. */
285
session->client->sendError(session->main_da.sql_errno(),
286
session->main_da.message());
323
session->getClient()->sendError(session->main_da().sql_errno(),
324
session->main_da().message());
289
327
case Diagnostics_area::DA_EOF:
290
session->client->sendEOF();
328
session->getClient()->sendEOF();
293
331
case Diagnostics_area::DA_OK:
294
session->client->sendOK();
332
session->getClient()->sendOK();
297
335
case Diagnostics_area::DA_DISABLED:
373
411
session->make_lex_string(&db, "data_dictionary", sizeof("data_dictionary"), false);
374
412
session->make_lex_string(&table, schema_table_name, false);
376
if (! sel->add_table_to_list(session, new Table_ident(db, table),
377
NULL, table_options, TL_READ))
414
if (not sel->add_table_to_list(session, new Table_ident(db, table), NULL, table_options, TL_READ))
384
int prepare_new_schema_table(Session *session, LEX *lex,
421
int prepare_new_schema_table(Session *session, LEX& lex,
385
422
const string& schema_table_name)
387
424
Select_Lex *schema_select_lex= NULL;
389
Select_Lex *select_lex= lex->current_select;
426
Select_Lex *select_lex= lex.current_select;
390
427
assert(select_lex);
391
428
if (_schema_select(session, select_lex, schema_table_name))
453
491
assert(first_table == all_tables);
454
492
assert(first_table == all_tables && first_table != 0);
456
lex->first_lists_tables_same();
494
session->lex().first_lists_tables_same();
457
496
/* should be assigned after making first tables same */
458
all_tables= lex->query_tables;
497
all_tables= session->lex().query_tables;
459
499
/* set context for commands which do not use setup_tables */
461
context.resolve_in_table_list_only((TableList*)select_lex->
500
select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
465
503
Reset warning count for each query that uses tables
468
506
variables, but for now this is probably good enough.
469
507
Don't reset warnings when executing a stored routine.
471
if (all_tables || ! lex->is_single_level_stmt())
509
if (all_tables || ! session->lex().is_single_level_stmt())
473
511
drizzle_reset_errors(session, 0);
476
514
assert(session->transaction.stmt.hasModifiedNonTransData() == false);
516
if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
517
&& ! session->inTransaction()
518
&& session->lex().statement->isTransactional())
520
if (session->startTransaction() == false)
522
my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
478
527
/* now we are ready to execute the statement */
479
res= lex->statement->execute();
528
res= session->lex().statement->execute();
480
529
session->set_proc_info("query end");
482
531
The return value for ROW_COUNT() is "implementation dependent" if the
518
580
session->send_explain_fields(result);
519
581
optimizer::ExplainPlan planner;
520
res= planner.explainUnion(session, &session->lex->unit, result);
582
res= planner.explainUnion(session, &session->lex().unit, result);
521
583
if (lex->describe & DESCRIBE_EXTENDED)
524
586
String str(buff,(uint32_t) sizeof(buff), system_charset_info);
526
session->lex->unit.print(&str, QT_ORDINARY);
588
session->lex().unit.print(&str);
527
589
str.append('\0');
528
590
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
529
591
ER_YES, str.ptr());
604
mysql_new_select(LEX *lex, bool move_down)
667
new_select(LEX *lex, bool move_down)
606
669
Select_Lex *select_lex;
607
670
Session *session= lex->session;
609
672
if (!(select_lex= new (session->mem_root) Select_Lex()))
611
675
select_lex->select_number= ++session->select_number;
612
676
select_lex->parent_lex= lex; /* Used in init_query. */
613
677
select_lex->init_query();
614
678
select_lex->init_select();
615
679
lex->nest_level++;
616
681
if (lex->nest_level > (int) MAX_SELECT_NESTING)
618
683
my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
621
687
select_lex->nest_level= lex->nest_level;
854
if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
918
if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
856
920
my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
860
if (!(new_field= new CreateField()) ||
861
new_field->init(session, field_name->str, type, length, decimals, type_modifier,
862
default_value, on_update_value, comment, change,
863
interval_list, cs, 0, column_format))
924
if (!(new_field= new CreateField())
925
|| new_field->init(session, field_name->str, type, length, decimals,
926
type_modifier, comment, change, interval_list,
927
cs, 0, column_format)
928
|| new_field->setDefaultValue(default_value, on_update_value))
866
931
statement->alter_info.create_list.push_back(new_field);
1076
1132
TableList *Select_Lex::end_nested_join(Session *)
1078
1134
TableList *ptr;
1079
nested_join_st *nested_join;
1135
NestedJoin *nested_join;
1081
1137
assert(embedding);
1082
1138
ptr= embedding;
1083
1139
join_list= ptr->getJoinList();
1084
1140
embedding= ptr->getEmbedding();
1085
1141
nested_join= ptr->getNestedJoin();
1086
if (nested_join->join_list.elements == 1)
1142
if (nested_join->join_list.size() == 1)
1088
TableList *embedded= nested_join->join_list.head();
1144
TableList *embedded= &nested_join->join_list.front();
1089
1145
join_list->pop();
1090
1146
embedded->setJoinList(join_list);
1091
1147
embedded->setEmbedding(embedding);
1092
1148
join_list->push_front(embedded);
1095
else if (nested_join->join_list.elements == 0)
1151
else if (nested_join->join_list.size() == 0)
1097
1153
join_list->pop();
1098
1154
ptr= NULL; // return value
1117
1173
TableList *Select_Lex::nest_last_join(Session *session)
1119
1175
TableList *ptr;
1120
nested_join_st *nested_join;
1176
NestedJoin *nested_join;
1121
1177
List<TableList> *embedded_list;
1123
1179
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1124
sizeof(nested_join_st))))
1180
sizeof(NestedJoin))))
1126
ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1182
ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1127
1183
nested_join= ptr->getNestedJoin();
1128
1184
ptr->setEmbedding(embedding);
1129
1185
ptr->setJoinList(join_list);
1130
1186
ptr->alias= (char*) "(nest_last_join)";
1131
1187
embedded_list= &nested_join->join_list;
1132
embedded_list->empty();
1188
embedded_list->clear();
1134
1190
for (uint32_t i=0; i < 2; i++)
1323
1379
true if a memory allocation error occured
1327
push_new_name_resolution_context(Session *session,
1328
TableList *left_op, TableList *right_op)
1382
void push_new_name_resolution_context(Session& session, TableList& left_op, TableList& right_op)
1330
Name_resolution_context *on_context;
1331
if (!(on_context= new (session->mem_root) Name_resolution_context))
1384
Name_resolution_context *on_context= new (session.mem_root) Name_resolution_context;
1333
1385
on_context->init();
1334
on_context->first_name_resolution_table=
1335
left_op->first_leaf_for_name_resolution();
1336
on_context->last_name_resolution_table=
1337
right_op->last_leaf_for_name_resolution();
1338
return session->lex->push_context(on_context);
1386
on_context->first_name_resolution_table= left_op.first_leaf_for_name_resolution();
1387
on_context->last_name_resolution_table= right_op.last_leaf_for_name_resolution();
1388
session.lex().push_context(on_context);
1487
1535
bool update_precheck(Session *session, TableList *)
1489
1537
const char *msg= 0;
1490
LEX *lex= session->lex;
1491
Select_Lex *select_lex= &lex->select_lex;
1538
Select_Lex *select_lex= &session->lex().select_lex;
1493
if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1540
if (session->lex().select_lex.item_list.size() != session->lex().value_list.size())
1495
1542
my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1499
if (session->lex->select_lex.table_list.elements > 1)
1546
if (session->lex().select_lex.table_list.size() > 1)
1501
if (select_lex->order_list.elements)
1548
if (select_lex->order_list.size())
1502
1549
msg= "ORDER BY";
1503
1550
else if (select_lex->select_limit)