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