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/item/num.h"
21
#include "drizzled/abort_exception.h"
22
#include <drizzled/my_hash.h>
20
#include <drizzled/item/num.h>
21
#include <drizzled/abort_exception.h>
23
22
#include <drizzled/error.h>
24
23
#include <drizzled/nested_join.h>
25
24
#include <drizzled/query_id.h>
26
#include "drizzled/transaction_services.h"
25
#include <drizzled/transaction_services.h>
27
26
#include <drizzled/sql_parse.h>
28
27
#include <drizzled/data_home.h>
29
28
#include <drizzled/sql_base.h>
30
29
#include <drizzled/show.h>
31
#include <drizzled/db.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>
41
39
#include <drizzled/plugin/client.h>
42
40
#include <drizzled/statement.h>
43
41
#include <drizzled/statement/alter_table.h>
44
#include "drizzled/probes.h"
45
#include "drizzled/global_charset_info.h"
47
#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"
51
#include "drizzled/optimizer/explain_plan.h"
52
#include "drizzled/pthread_globals.h"
53
#include "drizzled/plugin/event_observer.h"
54
#include "drizzled/visibility.h"
42
#include <drizzled/probes.h>
43
#include <drizzled/charset.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/times.h>
61
#include <drizzled/session/transactions.h>
56
63
#include <limits.h>
59
66
#include <algorithm>
60
67
#include <boost/date_time.hpp>
61
#include "drizzled/internal/my_sys.h"
68
#include <drizzled/internal/my_sys.h>
63
70
using namespace std;
65
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
72
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
71
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
77
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
72
78
static bool parse_sql(Session *session, Lex_input_stream *lip);
73
void parse(Session *session, const char *inBuf, uint32_t length);
79
void parse(Session&, const char *inBuf, uint32_t length);
76
82
@defgroup Runtime_Environment Runtime Environment
177
184
1 request of thread shutdown, i. e. if command is
178
185
COM_QUIT/COM_SHUTDOWN
180
bool dispatch_command(enum enum_server_command command, Session *session,
187
bool dispatch_command(enum_server_command command, Session *session,
181
188
char* packet, uint32_t packet_length)
186
193
DRIZZLE_COMMAND_START(session->thread_id, command);
188
195
session->command= command;
189
session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
196
session->lex().sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
197
session->times.set_time();
191
198
session->setQueryId(query_id.value());
193
200
switch( command ) {
218
226
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))
229
if (not schema::change(*session, identifier::Schema(string(packet, packet_length))))
228
231
session->my_ok();
234
if (not session->readAndStoreQuery(packet, packet_length))
235
break; // fatal error is set
236
DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
238
const_cast<const char *>(session->schema()->c_str()));
240
parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
237
session->readAndStoreQuery(packet, packet_length);
238
DRIZZLE_QUERY_START(session->getQueryString()->c_str(), session->thread_id, session->schema()->c_str());
239
parse(*session, session->getQueryString()->c_str(), session->getQueryString()->length());
245
243
/* We don't calculate statistics for this command */
246
session->main_da.disable_status(); // Don't send anything back
247
error=true; // End server
244
session->main_da().disable_status(); // Don't send anything back
245
error= true; // End server
249
if (packet_length != 4)
251
my_error(ER_NO_SUCH_THREAD, MYF(0), 0);
257
memcpy(&kill_id, packet, sizeof(uint32_t));
259
kill_id= ntohl(kill_id);
260
(void)drizzled::kill(*session->user(), kill_id, true);
249
265
case COM_SHUTDOWN:
251
267
session->status_var.com_other++;
252
268
session->my_eof();
253
269
session->close_thread_tables(); // Free before kill
270
286
/* If commit fails, we should be able to reset the OK status. */
271
session->main_da.can_overwrite_status= true;
287
session->main_da().can_overwrite_status= true;
272
288
TransactionServices &transaction_services= TransactionServices::singleton();
273
289
transaction_services.autocommitOrRollback(*session, session->is_error());
274
session->main_da.can_overwrite_status= false;
290
session->main_da().can_overwrite_status= false;
276
292
session->transaction.stmt.reset();
291
307
/* Can not be true, but do not take chances in production. */
292
assert(! session->main_da.is_sent);
308
assert(! session->main_da().is_sent);
294
switch (session->main_da.status())
310
switch (session->main_da().status())
296
312
case Diagnostics_area::DA_ERROR:
297
313
/* The query failed, send error to log and abort bootstrap. */
298
session->getClient()->sendError(session->main_da.sql_errno(),
299
session->main_da.message());
314
session->getClient()->sendError(session->main_da().sql_errno(),
315
session->main_da().message());
302
318
case Diagnostics_area::DA_EOF:
386
402
session->make_lex_string(&db, "data_dictionary", sizeof("data_dictionary"), false);
387
403
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))
405
if (not sel->add_table_to_list(session, new Table_ident(db, table), NULL, table_options, TL_READ))
397
int prepare_new_schema_table(Session *session, LEX *lex,
412
int prepare_new_schema_table(Session *session, LEX& lex,
398
413
const string& schema_table_name)
400
415
Select_Lex *schema_select_lex= NULL;
402
Select_Lex *select_lex= lex->current_select;
417
Select_Lex *select_lex= lex.current_select;
403
418
assert(select_lex);
404
419
if (_schema_select(session, select_lex, schema_table_name))
466
482
assert(first_table == all_tables);
467
483
assert(first_table == all_tables && first_table != 0);
469
lex->first_lists_tables_same();
485
session->lex().first_lists_tables_same();
470
487
/* should be assigned after making first tables same */
471
all_tables= lex->query_tables;
488
all_tables= session->lex().query_tables;
472
490
/* set context for commands which do not use setup_tables */
474
context.resolve_in_table_list_only((TableList*)select_lex->
491
select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
478
494
Reset warning count for each query that uses tables
555
571
session->send_explain_fields(result);
556
572
optimizer::ExplainPlan planner;
557
res= planner.explainUnion(session, &session->lex->unit, result);
573
res= planner.explainUnion(session, &session->lex().unit, result);
558
574
if (lex->describe & DESCRIBE_EXTENDED)
561
577
String str(buff,(uint32_t) sizeof(buff), system_charset_info);
563
session->lex->unit.print(&str, QT_ORDINARY);
579
session->lex().unit.print(&str);
564
580
str.append('\0');
565
581
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
566
582
ER_YES, str.ptr());
579
596
/* Init the Query Cache plugin */
580
plugin::QueryCache::prepareResultset(session);
597
plugin::QueryCache::prepareResultset(session);
581
598
res= handle_select(session, lex, result, 0);
582
599
/* Send the Resultset to the cache */
583
plugin::QueryCache::setResultset(session);
600
plugin::QueryCache::setResultset(session);
585
602
if (result != lex->result)
593
610
#define MY_YACC_INIT 1000 // Start with big alloc
594
611
#define MY_YACC_MAX 32000 // Because of 'short'
596
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
613
bool my_yyoverflow(short **yyss, ParserType **yyvs, ulong *yystacksize)
598
LEX *lex= current_session->lex;
615
LEX *lex= ¤t_session->lex();
599
616
ulong old_info=0;
600
617
if ((uint32_t) *yystacksize >= MY_YACC_MAX)
723
740
void create_select_for_variable(Session *session, const char *var_name)
726
LEX_STRING tmp, null_lex_string;
728
char buff[MAX_SYS_VAR_LENGTH*2+4+8];
733
lex->sql_command= SQLCOM_SELECT;
742
LEX& lex= session->lex();
744
lex.sql_command= SQLCOM_SELECT;
734
746
tmp.str= (char*) var_name;
735
747
tmp.length=strlen(var_name);
748
LEX_STRING null_lex_string;
736
749
memset(&null_lex_string.str, 0, sizeof(null_lex_string));
738
751
We set the name of Item to @@session.var_name because that then is used
739
752
as the column name in the output.
741
if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
754
if (Item* var= get_system_var(session, OPT_SESSION, tmp, null_lex_string))
756
char buff[MAX_SYS_VAR_LENGTH*2+4+8];
743
758
end+= snprintf(buff, sizeof(buff), "@@session.%s", var_name);
744
759
var->set_name(buff, end-buff, system_charset_info);
745
760
session->add_item_to_list(var);
755
770
@param length Length of the query text
758
void parse(Session *session, const char *inBuf, uint32_t length)
773
void parse(Session& session, const char *inBuf, uint32_t length)
760
session->lex->start(session);
762
session->reset_for_next_command();
775
session.lex().start(&session);
776
session.reset_for_next_command();
763
777
/* Check if the Query is Cached if and return true if yes
764
778
* TODO the plugin has to make sure that the query is cacheble
765
779
* 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);
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);
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();
781
if (plugin::QueryCache::isCached(&session) && not plugin::QueryCache::sendCachedResultset(&session))
783
Lex_input_stream lip(&session, inBuf, length);
784
if (parse_sql(&session, &lip))
785
assert(session.is_error());
786
else if (not session.is_error())
788
DRIZZLE_QUERY_EXEC_START(session.getQueryString()->c_str(), session.thread_id,
789
const_cast<const char *>(session.schema()->c_str()));
790
// Implement Views here --Brian
791
/* Actually execute the query */
794
execute_command(&session);
798
// Just try to catch any random failures that could have come
802
DRIZZLE_QUERY_EXEC_DONE(0);
804
session.lex().unit.cleanup();
805
session.set_proc_info("freeing items");
806
session.end_statement();
807
session.cleanup_after_query();
808
session.times.set_end_timer(session);
817
813
Store field definition for create.
827
823
Item *default_value, Item *on_update_value,
828
824
LEX_STRING *comment,
830
List<String> *interval_list, const CHARSET_INFO * const cs)
826
List<String> *interval_list, const charset_info_st * const cs)
832
828
register CreateField *new_field;
833
LEX *lex= session->lex;
829
LEX *lex= &session->lex();
834
830
statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
836
832
if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
898
if (!(new_field= new CreateField()) ||
899
new_field->init(session, field_name->str, type, length, decimals, type_modifier,
900
default_value, on_update_value, comment, change,
901
interval_list, cs, 0, column_format))
894
if (!(new_field= new CreateField())
895
|| new_field->init(session, field_name->str, type, length, decimals,
896
type_modifier, comment, change, interval_list,
897
cs, 0, column_format)
898
|| new_field->setDefaultValue(default_value, on_update_value))
904
901
statement->alter_info.create_list.push_back(new_field);
955
951
my_casedn_str(files_charset_info, table->db.str);
957
953
identifier::Schema schema_identifier(string(table->db.str));
958
if (not check_db_name(session, schema_identifier))
954
if (not schema::check(*session, schema_identifier))
961
957
my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
1114
1109
join_list= ptr->getJoinList();
1115
1110
embedding= ptr->getEmbedding();
1116
1111
nested_join= ptr->getNestedJoin();
1117
if (nested_join->join_list.elements == 1)
1112
if (nested_join->join_list.size() == 1)
1119
TableList *embedded= nested_join->join_list.head();
1114
TableList *embedded= &nested_join->join_list.front();
1120
1115
join_list->pop();
1121
1116
embedded->setJoinList(join_list);
1122
1117
embedded->setEmbedding(embedding);
1123
1118
join_list->push_front(embedded);
1126
else if (nested_join->join_list.elements == 0)
1121
else if (nested_join->join_list.size() == 0)
1128
1123
join_list->pop();
1129
1124
ptr= NULL; // return value
1308
1303
fake_select_lex->include_standalone(this,
1309
1304
(Select_Lex_Node**)&fake_select_lex);
1310
1305
fake_select_lex->select_number= INT_MAX;
1311
fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1306
fake_select_lex->parent_lex= &session_arg->lex(); /* Used in init_query. */
1312
1307
fake_select_lex->make_empty_select();
1313
1308
fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1314
1309
fake_select_lex->select_limit= 0;
1354
1349
true if a memory allocation error occured
1358
push_new_name_resolution_context(Session *session,
1359
TableList *left_op, TableList *right_op)
1352
void push_new_name_resolution_context(Session& session, TableList& left_op, TableList& right_op)
1361
Name_resolution_context *on_context;
1362
if (!(on_context= new (session->mem_root) Name_resolution_context))
1354
Name_resolution_context *on_context= new (session.mem_root) Name_resolution_context;
1364
1355
on_context->init();
1365
on_context->first_name_resolution_table=
1366
left_op->first_leaf_for_name_resolution();
1367
on_context->last_name_resolution_table=
1368
right_op->last_leaf_for_name_resolution();
1369
return session->lex->push_context(on_context);
1356
on_context->first_name_resolution_table= left_op.first_leaf_for_name_resolution();
1357
on_context->last_name_resolution_table= right_op.last_leaf_for_name_resolution();
1358
session.lex().push_context(on_context);
1517
1505
bool update_precheck(Session *session, TableList *)
1519
1507
const char *msg= 0;
1520
LEX *lex= session->lex;
1521
Select_Lex *select_lex= &lex->select_lex;
1508
Select_Lex *select_lex= &session->lex().select_lex;
1523
if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1510
if (session->lex().select_lex.item_list.size() != session->lex().value_list.size())
1525
1512
my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1529
if (session->lex->select_lex.table_list.elements > 1)
1516
if (session->lex().select_lex.table_list.size() > 1)
1531
if (select_lex->order_list.elements)
1518
if (select_lex->order_list.size())
1532
1519
msg= "ORDER BY";
1533
1520
else if (select_lex->select_limit)
1557
1544
bool insert_precheck(Session *session, TableList *)
1559
LEX *lex= session->lex;
1562
1547
Check that we have modify privileges for the first table and
1563
1548
select privileges for the rest
1565
if (lex->update_list.elements != lex->value_list.elements)
1550
if (session->lex().update_list.size() != session->lex().value_list.size())
1567
1552
my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1647
1632
so they should be prohibited until such support is done.
1648
1633
This is why we use the 3-byte utf8 to check well-formedness here.
1650
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
1635
const charset_info_st * const cs= &my_charset_utf8mb4_general_ci;
1652
1637
int well_formed_error;
1653
1638
uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,