22 #include <drizzled/parser.h>
23 #include <drizzled/alter_info.h>
24 #include <drizzled/create_field.h>
25 #include <drizzled/item/subselect.h>
26 #include <drizzled/lex_input_stream.h>
27 #include <drizzled/message/alter_table.pb.h>
28 #include <drizzled/session.h>
29 #include <drizzled/sql_lex.h>
46 Item* handle_sql2003_note184_exception(Session *session, Item* left,
bool equal, Item *expr)
70 if (expr->type() == Item::SUBSELECT_ITEM)
72 Item_subselect *expr2 = (Item_subselect*) expr;
74 if (expr2->substype() == Item_subselect::SINGLEROW_SUBS)
76 Item_singlerow_subselect *expr3 = (Item_singlerow_subselect*) expr2;
86 Select_Lex* subselect= expr3->invalidate_and_restore_select_lex();
87 Item* result=
new (session->mem_root) Item_in_subselect(left, subselect);
92 ? (Item*)
new (session->mem_root) Item_func_eq(left, expr)
93 : (Item*) new (session->mem_root) Item_func_ne(left, expr);
111 bool add_select_to_union_list(Session *session, LEX *lex,
bool is_union_distinct)
116 my_error(ER_WRONG_USAGE, MYF(0),
"UNION",
"INTO");
119 if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
121 my_parse_error(session->m_lip);
126 if (new_select(lex, 0))
129 lex->current_select->linkage=UNION_TYPE;
130 if (is_union_distinct)
131 lex->current_select->master_unit()->union_distinct=
143 bool setup_select_in_parentheses(Session *session, LEX *lex)
145 Select_Lex * sel= lex->current_select;
146 if (sel->set_braces(1))
148 my_parse_error(session->m_lip);
151 if (sel->linkage == UNION_TYPE &&
152 !sel->master_unit()->first_select()->braces &&
153 sel->master_unit()->first_select()->linkage ==
156 my_parse_error(session->m_lip);
159 if (sel->linkage == UNION_TYPE &&
160 sel->olap != UNSPECIFIED_OLAP_TYPE &&
161 sel->master_unit()->fake_select_lex)
163 my_error(ER_WRONG_USAGE, MYF(0),
"CUBE/ROLLUP",
"ORDER BY");
167 if (sel->master_unit()->fake_select_lex)
168 sel->master_unit()->global_parameters=
169 sel->master_unit()->fake_select_lex;
173 Item* reserved_keyword_function(Session *session,
const std::string &name, List<Item> *item_list)
175 const plugin::Function *udf= plugin::Function::get(name);
182 my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
"FUNCTION", name.c_str());
197 void my_parse_error(Lex_input_stream *lip)
201 const char *yytext= lip->get_tok_start();
203 my_printf_error(ER_PARSE_ERROR, ER(ER_PARSE_ERROR), MYF(0), ER(ER_SYNTAX_ERROR),
204 (yytext ? yytext :
""),
208 void my_parse_error(
const char *message)
210 my_printf_error(ER_PARSE_ERROR_UNKNOWN, ER(ER_PARSE_ERROR_UNKNOWN), MYF(0), message);
213 bool check_reserved_words(
str_ref name)
215 return not system_charset_info->strcasecmp(name.data(),
"GLOBAL")
216 || not system_charset_info->strcasecmp(name.data(),
"LOCAL")
217 || not system_charset_info->strcasecmp(name.data(),
"SESSION");
243 if (strcmp(s,
"parse error") == 0 || strcmp(s,
"syntax error") == 0)
245 parser::my_parse_error(session->
m_lip);
249 parser::my_parse_error(s);
253 bool buildOrderBy(LEX *lex)
255 Select_Lex *sel= lex->current_select;
256 Select_Lex_Unit *unit= sel-> master_unit();
258 if (sel->linkage != GLOBAL_OPTIONS_TYPE &&
259 sel->olap != UNSPECIFIED_OLAP_TYPE &&
260 (sel->linkage != UNION_TYPE || sel->braces))
262 my_error(ER_WRONG_USAGE, MYF(0),
263 "CUBE/ROLLUP",
"ORDER BY");
267 if (lex->sql_command != SQLCOM_ALTER_TABLE && !unit->fake_select_lex)
277 Select_Lex *first_sl= unit->first_select();
278 if (!unit->is_union() &&
279 (first_sl->order_list.elements ||
280 first_sl->select_limit) &&
281 unit->add_fake_select_lex(lex->session))
290 void buildEngineOption(LEX *lex,
const char *key,
str_ref value)
292 message::Engine::Option *opt= lex->table()->mutable_engine()->add_options();
294 opt->set_state(value.data(), value.size());
297 void buildEngineOption(LEX *lex,
const char *key, uint64_t value)
301 opt->set_state(boost::lexical_cast<std::string>(value));
304 void buildSchemaOption(LEX *lex,
const char *key,
str_ref value)
306 statement::CreateSchema *statement=
static_cast<statement::CreateSchema*
>(lex->statement);
307 message::Engine::Option *opt= statement->schema_message.mutable_engine()->add_options();
309 opt->set_state(value.data(), value.size());
312 void buildSchemaDefiner(LEX *lex,
const identifier::User &user)
314 statement::CreateSchema *statement=
static_cast<statement::CreateSchema*
>(lex->statement);
315 message::set_definer(statement->schema_message, user);
318 void buildSchemaOption(LEX *lex,
const char *key, uint64_t value)
320 statement::CreateSchema *statement=
static_cast<statement::CreateSchema*
>(lex->statement);
321 message::Engine::Option *opt= statement->schema_message.mutable_engine()->add_options();
323 opt->set_state(boost::lexical_cast<std::string>(value));
326 bool checkFieldIdent(LEX *lex,
str_ref schema_name,
str_ref table_name)
328 TableList *table=
reinterpret_cast<TableList*
>(lex->current_select->table_list.first);
330 if (schema_name.size() && table_alias_charset->strcasecmp(schema_name.data(), table->getSchemaName()))
332 my_error(ER_WRONG_DB_NAME, MYF(0), schema_name.data());
336 if (table_alias_charset->strcasecmp(table_name.data(), table->getTableName()))
338 my_error(ER_WRONG_TABLE_NAME, MYF(0), table_name.data());
347 Select_Lex *sel= lex->current_select;
349 if (table_name.size() and sel->no_table_names_allowed)
351 my_error(ER_TABLENAME_NOT_ALLOWED_HERE, MYF(0), table_name.data(), lex->session->where());
354 return sel->parsing_place != IN_HAVING || sel->get_in_sum_expr() > 0
355 ? (Item*)
new Item_field(lex->current_context(), schema_name.data(), table_name.data(), field_name.data())
356 : (Item*)
new Item_ref(lex->current_context(), schema_name.data(), table_name.data(), field_name.data());
359 Item *buildTableWild(LEX *lex,
str_ref schema_name,
str_ref table_name)
361 Select_Lex *sel= lex->current_select;
362 Item *item=
new Item_field(lex->current_context(), schema_name.data(), table_name.data(),
"*");
367 void buildCreateFieldIdent(LEX *lex)
369 statement::CreateTable *statement= (statement::CreateTable *)lex->statement;
370 lex->length= lex->dec=0;
372 statement->default_value= statement->on_update_value= 0;
373 statement->comment= null_lex_string();
375 statement->column_format= COLUMN_FORMAT_TYPE_DEFAULT;
377 message::AddedFields &added_fields_proto= ((statement::CreateTable *)lex->statement)->alter_info.added_fields_proto;
378 lex->setField(added_fields_proto.add_added_field());
381 void storeAlterColumnPosition(LEX *lex,
const char *position)
383 statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
385 lex->last_field->after= position;
386 statement->alter_info.flags.set(ALTER_COLUMN_ORDER);
389 bool buildCollation(LEX *lex,
const charset_info_st *arg)
391 statement::CreateTable *statement= (statement::CreateTable *)lex->statement;
393 HA_CREATE_INFO *cinfo= &statement->create_info();
394 if ((cinfo->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
395 cinfo->default_table_charset && arg &&
396 !my_charset_same(cinfo->default_table_charset, arg))
398 my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
399 arg->name, cinfo->default_table_charset->csname);
402 statement->create_info().default_table_charset= arg;
403 statement->create_info().used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
408 void buildKey(LEX *lex, Key::Keytype type_par,
str_ref name_arg)
410 statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
411 Key *key=
new Key(type_par, name_arg, &statement->key_create_info, 0, lex->col_list);
412 statement->alter_info.key_list.push_back(key);
413 lex->col_list.clear();
418 statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
419 statement->alter_info.key_list.push_back(
new Foreign_key(name_arg, lex->col_list, table, lex->ref_list,
420 statement->fk_delete_opt, statement->fk_update_opt, statement->fk_match_option));
422 statement->alter_info.key_list.push_back(
new Key(Key::MULTIPLE, name_arg, &default_key_create_info, 1, lex->col_list));
423 lex->col_list.clear();
425 statement->alter_info.flags.set(ALTER_FOREIGN_KEY);
428 drizzled::enum_field_types buildIntegerColumn(LEX *lex, drizzled::enum_field_types final_type,
const bool is_unsigned)
434 final_type= DRIZZLE_TYPE_LONGLONG;
439 assert (final_type == DRIZZLE_TYPE_LONG or final_type == DRIZZLE_TYPE_LONGLONG);
443 lex->field()->set_type(message::Table::Field::BIGINT);
444 lex->field()->mutable_constraints()->set_is_unsigned(
true);
446 else if (final_type == DRIZZLE_TYPE_LONG)
448 lex->field()->set_type(message::Table::Field::INTEGER);
450 else if (final_type == DRIZZLE_TYPE_LONGLONG)
452 lex->field()->set_type(message::Table::Field::BIGINT);
459 drizzled::enum_field_types buildSerialColumn(LEX *lex)
461 statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
462 statement->alter_info.flags.set(ALTER_ADD_INDEX);
464 lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG | UNSIGNED_FLAG);
468 lex->field()->mutable_constraints()->set_is_notnull(
true);
469 lex->field()->mutable_constraints()->set_is_unsigned(
true);
471 lex->field()->set_type(message::Table::Field::BIGINT);
474 return DRIZZLE_TYPE_LONGLONG;
477 drizzled::enum_field_types buildVarcharColumn(LEX *lex,
const char *length)
483 lex->field()->set_type(message::Table::Field::VARCHAR);
485 message::Table::Field::StringFieldOptions *string_field_options;
487 string_field_options= lex->field()->mutable_string_options();
489 string_field_options->set_length(atoi(length));
492 return DRIZZLE_TYPE_VARCHAR;
495 drizzled::enum_field_types buildDecimalColumn(LEX *lex)
498 lex->field()->set_type(message::Table::Field::DECIMAL);
500 return DRIZZLE_TYPE_DECIMAL;
503 drizzled::enum_field_types buildVarbinaryColumn(LEX *lex,
const char *length)
506 lex->charset= &my_charset_bin;
510 lex->field()->set_type(message::Table::Field::VARCHAR);
512 message::Table::Field::StringFieldOptions *string_field_options;
514 string_field_options= lex->field()->mutable_string_options();
516 string_field_options->set_length(atoi(length));
517 string_field_options->set_collation_id(my_charset_bin.number);
518 string_field_options->set_collation(my_charset_bin.name);
521 return DRIZZLE_TYPE_VARCHAR;
524 drizzled::enum_field_types buildBlobColumn(LEX *lex)
526 lex->charset=&my_charset_bin;
527 lex->length=(
char*) 0;
531 lex->field()->set_type(message::Table::Field::BLOB);
532 message::Table::Field::StringFieldOptions *string_field_options;
534 string_field_options= lex->field()->mutable_string_options();
535 string_field_options->set_collation_id(my_charset_bin.number);
536 string_field_options->set_collation(my_charset_bin.name);
539 return DRIZZLE_TYPE_BLOB;
542 drizzled::enum_field_types buildBooleanColumn(LEX *lex)
545 lex->field()->set_type(message::Table::Field::BOOLEAN);
547 return DRIZZLE_TYPE_BOOLEAN;
550 drizzled::enum_field_types buildUuidColumn(LEX *lex)
553 lex->field()->set_type(message::Table::Field::UUID);
555 return DRIZZLE_TYPE_UUID;
558 drizzled::enum_field_types buildIPv6Column(LEX *lex)
561 lex->field()->set_type(message::Table::Field::IPV6);
563 return DRIZZLE_TYPE_IPV6;
566 drizzled::enum_field_types buildDoubleColumn(LEX *lex)
570 lex->field()->set_type(message::Table::Field::DOUBLE);
573 return DRIZZLE_TYPE_DOUBLE;
576 drizzled::enum_field_types buildTimestampColumn(LEX *lex,
const char *length)
580 lex->field()->set_type(message::Table::Field::EPOCH);
586 return DRIZZLE_TYPE_MICROTIME;
591 return DRIZZLE_TYPE_TIMESTAMP;
594 void buildKeyOnColumn(LEX *lex)
596 statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
598 lex->type|= UNIQUE_KEY_FLAG;
599 statement->alter_info.flags.set(ALTER_ADD_INDEX);
603 lex->field()->mutable_constraints()->set_is_unique(
true);
607 void buildAutoOnColumn(LEX *lex)
609 lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG;
613 lex->field()->mutable_constraints()->set_is_notnull(
true);
617 void buildPrimaryOnColumn(LEX *lex)
619 statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
621 lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG;
622 statement->alter_info.flags.set(ALTER_ADD_INDEX);
626 lex->field()->mutable_constraints()->set_is_notnull(
true);
630 void buildReplicationOption(LEX *lex,
bool arg)
632 statement::CreateSchema *statement=
static_cast<statement::CreateSchema*
>(lex->statement);
633 message::set_is_replicated(statement->schema_message, arg);
636 void buildAddAlterDropIndex(LEX *lex,
const char *name,
bool is_foreign_key)
638 statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
640 message::AlterTable::AlterTableOperation *operation;
641 operation= lex->alter_table()->add_operations();
642 operation->set_drop_name(name);
644 statement->alter_info.flags.set(ALTER_DROP_INDEX);
647 statement->alter_info.flags.set(ALTER_FOREIGN_KEY);
648 operation->set_operation(message::AlterTable::AlterTableOperation::DROP_FOREIGN_KEY);
652 operation->set_operation(message::AlterTable::AlterTableOperation::DROP_KEY);