00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <config.h>
00017
00018 #define DRIZZLE_LEX 1
00019
00020 #include <drizzled/item/num.h>
00021 #include <drizzled/abort_exception.h>
00022 #include <drizzled/error.h>
00023 #include <drizzled/nested_join.h>
00024 #include <drizzled/transaction_services.h>
00025 #include <drizzled/sql_parse.h>
00026 #include <drizzled/data_home.h>
00027 #include <drizzled/sql_base.h>
00028 #include <drizzled/show.h>
00029 #include <drizzled/function/time/unix_timestamp.h>
00030 #include <drizzled/function/get_system_var.h>
00031 #include <drizzled/item/cmpfunc.h>
00032 #include <drizzled/item/null.h>
00033 #include <drizzled/session.h>
00034 #include <drizzled/session/cache.h>
00035 #include <drizzled/sql_load.h>
00036 #include <drizzled/lock.h>
00037 #include <drizzled/select_send.h>
00038 #include <drizzled/plugin/client.h>
00039 #include <drizzled/statement.h>
00040 #include <drizzled/statement/alter_table.h>
00041 #include <drizzled/probes.h>
00042 #include <drizzled/charset.h>
00043 #include <drizzled/plugin/logging.h>
00044 #include <drizzled/plugin/query_rewrite.h>
00045 #include <drizzled/plugin/query_cache.h>
00046 #include <drizzled/plugin/authorization.h>
00047 #include <drizzled/optimizer/explain_plan.h>
00048 #include <drizzled/pthread_globals.h>
00049 #include <drizzled/plugin/event_observer.h>
00050 #include <drizzled/display.h>
00051 #include <drizzled/visibility.h>
00052 #include <drizzled/kill.h>
00053 #include <drizzled/schema.h>
00054 #include <drizzled/item/subselect.h>
00055 #include <drizzled/diagnostics_area.h>
00056 #include <drizzled/table_ident.h>
00057 #include <drizzled/statistics_variables.h>
00058 #include <drizzled/system_variables.h>
00059 #include <drizzled/session/times.h>
00060 #include <drizzled/session/transactions.h>
00061 #include <drizzled/create_field.h>
00062 #include <drizzled/lex_input_stream.h>
00063
00064 #include <limits.h>
00065
00066 #include <bitset>
00067 #include <algorithm>
00068 #include <boost/date_time.hpp>
00069 #include <drizzled/internal/my_sys.h>
00070
00071 using namespace std;
00072
00073 extern int base_sql_parse(drizzled::Session *session);
00074
00075 namespace drizzled {
00076
00077
00078 bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
00079 static bool parse_sql(Session *session, Lex_input_stream *lip);
00080 void parse(Session&, const char *inBuf, uint32_t length);
00081
00087 extern size_t my_thread_stack_size;
00088 extern const charset_info_st *character_set_filesystem;
00089
00090 static atomic<uint64_t> g_query_id;
00091
00092 namespace
00093 {
00094 static const std::string command_name[]=
00095 {
00096 "Sleep",
00097 "Quit",
00098 "Init DB",
00099 "Query",
00100 "Shutdown",
00101 "Connect",
00102 "Ping",
00103 "Kill",
00104 "Error"
00105 };
00106 }
00107
00108 const char *xa_state_names[]=
00109 {
00110 "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
00111 };
00112
00113
00126 bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
00127
00128 const std::string &getCommandName(const enum_server_command& command)
00129 {
00130 return command_name[command];
00131 }
00132
00133 void init_update_queries(void)
00134 {
00135 for (uint32_t x= uint32_t(SQLCOM_SELECT);
00136 x <= uint32_t(SQLCOM_END); x++)
00137 {
00138 sql_command_flags[x].reset();
00139 }
00140
00141 sql_command_flags[SQLCOM_CREATE_TABLE]= CF_CHANGES_DATA;
00142 sql_command_flags[SQLCOM_CREATE_INDEX]= CF_CHANGES_DATA;
00143 sql_command_flags[SQLCOM_ALTER_TABLE]= CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND;
00144 sql_command_flags[SQLCOM_TRUNCATE]= CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND;
00145 sql_command_flags[SQLCOM_DROP_TABLE]= CF_CHANGES_DATA;
00146 sql_command_flags[SQLCOM_LOAD]= CF_CHANGES_DATA;
00147 sql_command_flags[SQLCOM_CREATE_DB]= CF_CHANGES_DATA;
00148 sql_command_flags[SQLCOM_DROP_DB]= CF_CHANGES_DATA;
00149 sql_command_flags[SQLCOM_RENAME_TABLE]= CF_CHANGES_DATA;
00150 sql_command_flags[SQLCOM_DROP_INDEX]= CF_CHANGES_DATA;
00151
00152 sql_command_flags[SQLCOM_UPDATE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
00153 sql_command_flags[SQLCOM_INSERT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
00154 sql_command_flags[SQLCOM_INSERT_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
00155 sql_command_flags[SQLCOM_DELETE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
00156 sql_command_flags[SQLCOM_REPLACE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
00157 sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
00158
00159 sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
00160 sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
00161 sql_command_flags[SQLCOM_SHOW_CREATE_DB]= CF_STATUS_COMMAND;
00162 sql_command_flags[SQLCOM_SHOW_CREATE]= CF_STATUS_COMMAND;
00163
00164
00165
00166
00167
00168 sql_command_flags[SQLCOM_ANALYZE]= CF_WRITE_LOGS_COMMAND;
00169 }
00170
00192 bool dispatch_command(enum_server_command command, Session *session,
00193 const char* packet, uint32_t packet_length)
00194 {
00195 bool error= false;
00196
00197 DRIZZLE_COMMAND_START(session->thread_id, command);
00198
00199 session->command= command;
00200 session->lex().sql_command= SQLCOM_END;
00201 session->times.set_time();
00202 session->setQueryId(g_query_id.increment());
00203
00204 if (command != COM_PING)
00205 {
00206
00207 session->status_var.questions++;
00208 }
00209
00210
00211
00212 plugin::Logging::preDo(session);
00213 if (unlikely(plugin::EventObserver::beforeStatement(*session)))
00214 {
00215
00216 }
00217
00218 session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
00219
00220 switch (command)
00221 {
00222 case COM_USE_SCHEMA:
00223 {
00224 if (packet_length == 0)
00225 {
00226 my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
00227 break;
00228 }
00229 if (not schema::change(*session, identifier::Schema(string(packet, packet_length))))
00230 {
00231 session->my_ok();
00232 }
00233 break;
00234 }
00235
00236 case COM_QUERY:
00237 {
00238 session->readAndStoreQuery(packet, packet_length);
00239 DRIZZLE_QUERY_START(session->getQueryString()->c_str(), session->thread_id, session->schema()->c_str());
00240 parse(*session, session->getQueryString()->c_str(), session->getQueryString()->length());
00241 break;
00242 }
00243
00244 case COM_QUIT:
00245
00246 session->main_da().disable_status();
00247 error= true;
00248 break;
00249
00250 case COM_KILL:
00251 {
00252 if (packet_length != 4)
00253 {
00254 my_error(ER_NO_SUCH_THREAD, MYF(0), 0);
00255 break;
00256 }
00257 else
00258 {
00259 uint32_t kill_id;
00260 memcpy(&kill_id, packet, sizeof(uint32_t));
00261
00262 kill_id= ntohl(kill_id);
00263 (void)drizzled::kill(*session->user(), kill_id, true);
00264 }
00265 session->my_ok();
00266 break;
00267 }
00268
00269 case COM_SHUTDOWN:
00270 {
00271 session->status_var.com_other++;
00272 session->my_eof();
00273 session->close_thread_tables();
00274 kill_drizzle();
00275 error= true;
00276 break;
00277 }
00278
00279 case COM_PING:
00280 session->status_var.com_other++;
00281 session->my_ok();
00282 break;
00283
00284 case COM_SLEEP:
00285 case COM_CONNECT:
00286 case COM_END:
00287 default:
00288 my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
00289 break;
00290 }
00291
00292
00293 session->main_da().can_overwrite_status= true;
00294 TransactionServices::autocommitOrRollback(*session, session->is_error());
00295 session->main_da().can_overwrite_status= false;
00296
00297 session->transaction.stmt.reset();
00298
00299
00300
00301 if (session->killed_errno())
00302 {
00303 if (not session->main_da().is_set())
00304 session->send_kill_message();
00305 }
00306 if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
00307 {
00308 session->setKilled(Session::NOT_KILLED);
00309 session->setAbort(false);
00310 }
00311
00312
00313 assert(! session->main_da().is_sent);
00314
00315 switch (session->main_da().status())
00316 {
00317 case Diagnostics_area::DA_ERROR:
00318
00319 session->getClient()->sendError(session->main_da().sql_errno(),
00320 session->main_da().message());
00321 break;
00322
00323 case Diagnostics_area::DA_EOF:
00324 session->getClient()->sendEOF();
00325 break;
00326
00327 case Diagnostics_area::DA_OK:
00328 session->getClient()->sendOK();
00329 break;
00330
00331 case Diagnostics_area::DA_DISABLED:
00332 break;
00333
00334 case Diagnostics_area::DA_EMPTY:
00335 default:
00336 session->getClient()->sendOK();
00337 break;
00338 }
00339
00340 session->main_da().is_sent= true;
00341
00342 session->set_proc_info("closing tables");
00343
00344 session->close_thread_tables();
00345
00346 plugin::Logging::postDo(session);
00347 if (unlikely(plugin::EventObserver::afterStatement(*session)))
00348 {
00349
00350 }
00351
00352
00353 session->set_proc_info("cleaning up");
00354 session->command= COM_SLEEP;
00355 session->resetQueryString();
00356
00357 session->set_proc_info(NULL);
00358 session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
00359
00360 if (DRIZZLE_QUERY_DONE_ENABLED() || DRIZZLE_COMMAND_DONE_ENABLED())
00361 {
00362 if (command == COM_QUERY)
00363 {
00364 DRIZZLE_QUERY_DONE(session->is_error());
00365 }
00366 DRIZZLE_COMMAND_DONE(session->is_error());
00367 }
00368
00369 return error;
00370 }
00371
00372
00398 static bool _schema_select(Session& session, Select_Lex& sel, const string& schema_table_name)
00399 {
00400 lex_string_t db, table;
00401 bitset<NUM_OF_TABLE_OPTIONS> table_options;
00402
00403
00404
00405
00406 session.make_lex_string(&db, str_ref("data_dictionary"));
00407 session.make_lex_string(&table, schema_table_name);
00408 return not sel.add_table_to_list(&session, new Table_ident(db, table), NULL, table_options, TL_READ);
00409 }
00410
00411 int prepare_new_schema_table(Session *session, LEX& lex0, const string& schema_table_name)
00412 {
00413 Select_Lex& lex= *lex0.current_select;
00414 if (_schema_select(*session, lex, schema_table_name))
00415 return 1;
00416 TableList *table_list= (TableList*)lex.table_list.first;
00417 table_list->schema_select_lex= NULL;
00418 return 0;
00419 }
00420
00451 static int execute_command(Session *session)
00452 {
00453
00454 Select_Lex *select_lex= &session->lex().select_lex;
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 session->lex().first_lists_tables_same();
00472
00473
00474
00475 TableList* all_tables= session->lex().query_tables;
00476
00477
00478 select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
00479
00480
00481
00482
00483
00484
00485
00486
00487 if (all_tables || ! session->lex().is_single_level_stmt())
00488 {
00489 drizzle_reset_errors(*session, 0);
00490 }
00491
00492 assert(not session->transaction.stmt.hasModifiedNonTransData());
00493
00494 if (not (session->server_status & SERVER_STATUS_AUTOCOMMIT)
00495 && not session->inTransaction()
00496 && session->lex().statement->isTransactional())
00497 {
00498 if (not session->startTransaction())
00499 {
00500 my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
00501 return true;
00502 }
00503 }
00504
00505
00506 bool res= session->lex().statement->execute();
00507 session->set_proc_info("query end");
00508
00509
00510
00511
00512
00513
00514 if (not sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT))
00515 {
00516 session->row_count_func= -1;
00517 }
00518
00519 return res || session->is_error();
00520 }
00521
00522 bool execute_sqlcom_select(Session *session, TableList *all_tables)
00523 {
00524 LEX *lex= &session->lex();
00525 select_result *result=lex->result;
00526 bool res= false;
00527
00528 {
00529 Select_Lex *param= lex->unit.global_parameters;
00530 if (!param->explicit_limit)
00531 param->select_limit=
00532 new Item_int((uint64_t) session->variables.select_limit);
00533 }
00534
00535 if (all_tables
00536 && ! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
00537 && ! session->inTransaction()
00538 && ! lex->statement->isShow())
00539 {
00540 if (not session->startTransaction())
00541 {
00542 my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
00543 return true;
00544 }
00545 }
00546
00547 if (not (res= session->openTablesLock(all_tables)))
00548 {
00549 if (lex->describe)
00550 {
00551
00552
00553
00554
00555
00556
00557 result= new select_send();
00558 session->send_explain_fields(result);
00559 optimizer::ExplainPlan planner;
00560 res= planner.explainUnion(session, &session->lex().unit, result);
00561 if (lex->describe & DESCRIBE_EXTENDED)
00562 {
00563 char buff[1024];
00564 String str(buff,(uint32_t) sizeof(buff), system_charset_info);
00565 str.length(0);
00566 session->lex().unit.print(&str);
00567 str.append('\0');
00568 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
00569 ER_YES, str.ptr());
00570 }
00571 if (res)
00572 result->abort();
00573 else
00574 result->send_eof();
00575
00576 delete result;
00577 }
00578 else
00579 {
00580 if (not result)
00581 result= new select_send();
00582
00583
00584 plugin::QueryCache::prepareResultset(session);
00585 res= handle_select(session, lex, result, 0);
00586
00587 plugin::QueryCache::setResultset(session);
00588
00589 if (result != lex->result)
00590 delete result;
00591 }
00592 }
00593 return res;
00594 }
00595
00596
00597 #define MY_YACC_INIT 1000 // Start with big alloc
00598 #define MY_YACC_MAX 32000 // Because of 'short'
00599
00600 bool my_yyoverflow(short **yyss, ParserType **yyvs, ulong *yystacksize)
00601 {
00602 LEX *lex= ¤t_session->lex();
00603 ulong old_info=0;
00604 if ((uint32_t) *yystacksize >= MY_YACC_MAX)
00605 return 1;
00606 if (!lex->yacc_yyvs)
00607 old_info= *yystacksize;
00608 *yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
00609 unsigned char *tmpptr= NULL;
00610 if (!(tmpptr= (unsigned char *)realloc(lex->yacc_yyvs,
00611 *yystacksize* sizeof(**yyvs))))
00612 return 1;
00613 lex->yacc_yyvs= tmpptr;
00614 tmpptr= NULL;
00615 if (!(tmpptr= (unsigned char*)realloc(lex->yacc_yyss,
00616 *yystacksize* sizeof(**yyss))))
00617 return 1;
00618 lex->yacc_yyss= tmpptr;
00619 if (old_info)
00620 {
00621 memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
00622 memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
00623 }
00624 *yyss=(short*) lex->yacc_yyss;
00625 *yyvs=(ParserType*) lex->yacc_yyvs;
00626 return 0;
00627 }
00628
00629
00630 void
00631 init_select(LEX *lex)
00632 {
00633 Select_Lex *select_lex= lex->current_select;
00634 select_lex->init_select();
00635 lex->wild= 0;
00636 if (select_lex == &lex->select_lex)
00637 {
00638 assert(lex->result == 0);
00639 lex->exchange= 0;
00640 }
00641 }
00642
00643
00644 bool new_select(LEX *lex, bool move_down)
00645 {
00646 Session* session= lex->session;
00647 Select_Lex* select_lex= new (session->mem_root) Select_Lex;
00648
00649 select_lex->select_number= ++session->select_number;
00650 select_lex->parent_lex= lex;
00651 select_lex->init_query();
00652 select_lex->init_select();
00653 lex->nest_level++;
00654
00655 if (lex->nest_level > (int) MAX_SELECT_NESTING)
00656 {
00657 my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
00658 return 1;
00659 }
00660
00661 select_lex->nest_level= lex->nest_level;
00662 if (move_down)
00663 {
00664
00665 Select_Lex_Unit* unit= new (session->mem_root) Select_Lex_Unit();
00666
00667 unit->init_query();
00668 unit->init_select();
00669 unit->session= session;
00670 unit->include_down(lex->current_select);
00671 unit->link_next= 0;
00672 unit->link_prev= 0;
00673 unit->return_to= lex->current_select;
00674 select_lex->include_down(unit);
00675
00676
00677
00678
00679 select_lex->context.outer_context= &select_lex->outer_select()->context;
00680 }
00681 else
00682 {
00683 if (lex->current_select->order_list.first && !lex->current_select->braces)
00684 {
00685 my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
00686 return true;
00687 }
00688
00689 select_lex->include_neighbour(lex->current_select);
00690 Select_Lex_Unit *unit= select_lex->master_unit();
00691
00692 if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
00693 return true;
00694
00695 select_lex->context.outer_context= unit->first_select()->context.outer_context;
00696 }
00697
00698 select_lex->master_unit()->global_parameters= select_lex;
00699 select_lex->include_global((Select_Lex_Node**)&lex->all_selects_list);
00700 lex->current_select= select_lex;
00701
00702
00703
00704
00705 select_lex->context.resolve_in_select_list= true;
00706
00707 return false;
00708 }
00709
00720 void create_select_for_variable(Session *session, const char *var_name)
00721 {
00722 LEX& lex= session->lex();
00723 init_select(&lex);
00724 lex.sql_command= SQLCOM_SELECT;
00725 lex_string_t tmp;
00726 tmp.assign(var_name, strlen(var_name));
00727
00728
00729
00730
00731 if (Item* var= get_system_var(session, OPT_SESSION, tmp, null_lex_string()))
00732 {
00733 char buff[MAX_SYS_VAR_LENGTH*2+4+8];
00734 char *end= buff;
00735 end+= snprintf(buff, sizeof(buff), "@@session.%s", var_name);
00736 var->set_name(buff, end-buff);
00737 session->add_item_to_list(var);
00738 }
00739 }
00740
00741
00750 void parse(Session& session, const char *inBuf, uint32_t length)
00751 {
00752 session.lex().start(&session);
00753 session.reset_for_next_command();
00754
00755
00756
00757
00758 if (plugin::QueryCache::isCached(&session) && not plugin::QueryCache::sendCachedResultset(&session))
00759 return;
00760 Lex_input_stream lip(&session, inBuf, length);
00761 if (parse_sql(&session, &lip))
00762 assert(session.is_error());
00763 else if (not session.is_error())
00764 {
00765 DRIZZLE_QUERY_EXEC_START(session.getQueryString()->c_str(), session.thread_id, session.schema()->c_str());
00766
00767
00768 try
00769 {
00770 execute_command(&session);
00771 }
00772 catch (...)
00773 {
00774
00775
00776 DRIZZLE_ABORT;
00777 }
00778 DRIZZLE_QUERY_EXEC_DONE(0);
00779 }
00780 session.lex().unit.cleanup();
00781 session.set_proc_info("freeing items");
00782 session.end_statement();
00783 session.cleanup_after_query();
00784 session.times.set_end_timer(session);
00785 }
00786
00787
00795 bool add_field_to_list(Session *session, str_ref field_name, enum_field_types type,
00796 const char *length, const char *decimals,
00797 uint32_t type_modifier, column_format_type column_format,
00798 Item *default_value, Item *on_update_value, str_ref comment,
00799 const char *change, List<String> *interval_list, const charset_info_st* cs)
00800 {
00801 LEX *lex= &session->lex();
00802 statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
00803
00804 if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
00805 return true;
00806
00807 if (type_modifier & PRI_KEY_FLAG)
00808 {
00809 lex->col_list.push_back(new Key_part_spec(field_name, 0));
00810 statement->alter_info.key_list.push_back(new Key(Key::PRIMARY, null_lex_string(), &default_key_create_info, 0, lex->col_list));
00811 lex->col_list.clear();
00812 }
00813 if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
00814 {
00815 lex->col_list.push_back(new Key_part_spec(field_name, 0));
00816 statement->alter_info.key_list.push_back(new Key(Key::UNIQUE, null_lex_string(), &default_key_create_info, 0, lex->col_list));
00817 lex->col_list.clear();
00818 }
00819
00820 if (default_value)
00821 {
00822
00823
00824
00825
00826
00827
00828
00829 if (default_value->type() == Item::FUNC_ITEM &&
00830 !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
00831 (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
00832 {
00833 my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
00834 return true;
00835 }
00836 else if (default_value->type() == Item::NULL_ITEM)
00837 {
00838 default_value= 0;
00839 if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
00840 {
00841 my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
00842 return true;
00843 }
00844 }
00845 else if (type_modifier & AUTO_INCREMENT_FLAG)
00846 {
00847 my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
00848 return true;
00849 }
00850 }
00851
00852 if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
00853 {
00854 my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name.data());
00855 return true;
00856 }
00857
00858 CreateField* new_field= new CreateField;
00859 if (new_field->init(session, field_name.data(), type, length, decimals, type_modifier, comment, change, interval_list, cs, 0, column_format)
00860 || new_field->setDefaultValue(default_value, on_update_value))
00861 return true;
00862
00863 statement->alter_info.create_list.push_back(new_field);
00864 lex->last_field=new_field;
00865
00866 return false;
00867 }
00868
00869
00889 TableList *Select_Lex::add_table_to_list(Session *session,
00890 Table_ident *table,
00891 lex_string_t *alias,
00892 const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
00893 thr_lock_type lock_type,
00894 List<Index_hint> *index_hints_arg,
00895 lex_string_t *option)
00896 {
00897 TableList *previous_table_ref;
00898 LEX *lex= &session->lex();
00899
00900 if (!table)
00901 return NULL;
00902 const char* alias_str= alias ? alias->data() : table->table.data();
00903 if (not table_options.test(TL_OPTION_ALIAS) && check_table_name(table->table))
00904 {
00905 my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.data());
00906 return NULL;
00907 }
00908
00909 if (not table->is_derived_table() && table->db.data())
00910 {
00911 files_charset_info->casedn_str(table->db.str_);
00912 if (not schema::check(*session, identifier::Schema(table->db)))
00913 {
00914 my_error(ER_WRONG_DB_NAME, MYF(0), table->db.data());
00915 return NULL;
00916 }
00917 }
00918
00919 if (!alias)
00920 {
00921 if (table->sel)
00922 {
00923 my_message(ER_DERIVED_MUST_HAVE_ALIAS, ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
00924 return NULL;
00925 }
00926 alias_str= (char*) session->mem.memdup(alias_str, table->table.size() + 1);
00927 }
00928 TableList *ptr = (TableList *) session->mem.calloc(sizeof(TableList));
00929
00930 if (table->db.data())
00931 {
00932 ptr->setIsFqtn(true);
00933 ptr->setSchemaName(table->db.data());
00934 }
00935 else
00936 {
00937 str_ref schema = lex->session->copy_db_to();
00938 if (schema.empty())
00939 return NULL;
00940 ptr->setIsFqtn(false);
00941 ptr->setSchemaName(schema.data());
00942 }
00943
00944 ptr->alias= alias_str;
00945 ptr->setIsAlias(alias ? true : false);
00946 ptr->setTableName(table->table.data());
00947 ptr->lock_type= lock_type;
00948 ptr->force_index= table_options.test(TL_OPTION_FORCE_INDEX);
00949 ptr->ignore_leaves= table_options.test(TL_OPTION_IGNORE_LEAVES);
00950 ptr->derived= table->sel;
00951 ptr->select_lex= lex->current_select;
00952 ptr->index_hints= index_hints_arg;
00953 ptr->option= option ? option->data() : NULL;
00954
00955 if (lock_type != TL_IGNORE)
00956 {
00957 TableList *first_table= (TableList*) table_list.first;
00958 for (TableList *tables= first_table; tables; tables= tables->next_local)
00959 {
00960 if (not table_alias_charset->strcasecmp(alias_str, tables->alias) &&
00961 not system_charset_info->strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
00962 {
00963 my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
00964 return NULL;
00965 }
00966 }
00967 }
00968
00969 if (table_list.size() > 0)
00970 {
00971
00972
00973
00974
00975
00976 previous_table_ref= (TableList*) ((char*) table_list.next - ((char*) &(ptr->next_local) - (char*) ptr));
00977
00978
00979
00980
00981
00982
00983
00984 previous_table_ref->next_name_resolution_table= ptr;
00985 }
00986
00987
00988
00989
00990
00991
00992
00993 table_list.link_in_list((unsigned char*) ptr, (unsigned char**) &ptr->next_local);
00994 ptr->next_name_resolution_table= NULL;
00995
00996 lex->add_to_query_tables(ptr);
00997 return ptr;
00998 }
00999
01000
01020 void Select_Lex::init_nested_join(Session& session)
01021 {
01022 TableList* ptr= (TableList*) session.mem.calloc(ALIGN_SIZE(sizeof(TableList)) + sizeof(NestedJoin));
01023 ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
01024 NestedJoin* nested_join= ptr->getNestedJoin();
01025 join_list->push_front(ptr);
01026 ptr->setEmbedding(embedding);
01027 ptr->setJoinList(join_list);
01028 ptr->alias= (char*) "(nested_join)";
01029 embedding= ptr;
01030 join_list= &nested_join->join_list;
01031 join_list->clear();
01032 }
01033
01034
01049 TableList *Select_Lex::end_nested_join()
01050 {
01051 assert(embedding);
01052 TableList* ptr= embedding;
01053 join_list= ptr->getJoinList();
01054 embedding= ptr->getEmbedding();
01055 NestedJoin* nested_join= ptr->getNestedJoin();
01056 if (nested_join->join_list.size() == 1)
01057 {
01058 TableList *embedded= &nested_join->join_list.front();
01059 join_list->pop();
01060 embedded->setJoinList(join_list);
01061 embedded->setEmbedding(embedding);
01062 join_list->push_front(embedded);
01063 return embedded;
01064 }
01065 else if (not nested_join->join_list.size())
01066 {
01067 join_list->pop();
01068 return NULL;
01069 }
01070 return ptr;
01071 }
01072
01073
01087 TableList *Select_Lex::nest_last_join(Session *session)
01088 {
01089 TableList* ptr= (TableList*) session->mem.calloc(ALIGN_SIZE(sizeof(TableList)) + sizeof(NestedJoin));
01090 ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
01091 NestedJoin* nested_join= ptr->getNestedJoin();
01092 ptr->setEmbedding(embedding);
01093 ptr->setJoinList(join_list);
01094 ptr->alias= (char*) "(nest_last_join)";
01095 List<TableList>* embedded_list= &nested_join->join_list;
01096 embedded_list->clear();
01097
01098 for (uint32_t i=0; i < 2; i++)
01099 {
01100 TableList *table= join_list->pop();
01101 table->setJoinList(embedded_list);
01102 table->setEmbedding(ptr);
01103 embedded_list->push_back(table);
01104 if (table->natural_join)
01105 {
01106 ptr->is_natural_join= true;
01107
01108
01109
01110
01111 if (prev_join_using)
01112 ptr->join_using_fields= prev_join_using;
01113 }
01114 }
01115 join_list->push_front(ptr);
01116 nested_join->used_tables= nested_join->not_null_tables= (table_map) 0;
01117 return ptr;
01118 }
01119
01120
01135 void Select_Lex::add_joined_table(TableList *table)
01136 {
01137 join_list->push_front(table);
01138 table->setJoinList(join_list);
01139 table->setEmbedding(embedding);
01140 }
01141
01142
01174 TableList *Select_Lex::convert_right_join()
01175 {
01176 TableList *tab2= join_list->pop();
01177 TableList *tab1= join_list->pop();
01178
01179 join_list->push_front(tab2);
01180 join_list->push_front(tab1);
01181 tab1->outer_join|= JOIN_TYPE_RIGHT;
01182
01183 return tab1;
01184 }
01185
01197 void Select_Lex::set_lock_for_tables(thr_lock_type lock_type)
01198 {
01199 for (TableList *tables= (TableList*) table_list.first; tables; tables= tables->next_local)
01200 {
01201 tables->lock_type= lock_type;
01202 }
01203 }
01204
01205
01232 bool Select_Lex_Unit::add_fake_select_lex(Session *session_arg)
01233 {
01234 Select_Lex *first_sl= first_select();
01235 assert(!fake_select_lex);
01236
01237 fake_select_lex= new (session_arg->mem_root) Select_Lex();
01238 fake_select_lex->include_standalone(this, (Select_Lex_Node**)&fake_select_lex);
01239 fake_select_lex->select_number= INT_MAX;
01240 fake_select_lex->parent_lex= &session_arg->lex();
01241 fake_select_lex->make_empty_select();
01242 fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
01243 fake_select_lex->select_limit= 0;
01244
01245 fake_select_lex->context.outer_context=first_sl->context.outer_context;
01246
01247 fake_select_lex->context.resolve_in_select_list= true;
01248 fake_select_lex->context.select_lex= fake_select_lex;
01249
01250 if (!is_union())
01251 {
01252
01253
01254
01255
01256
01257
01258 global_parameters= fake_select_lex;
01259 fake_select_lex->no_table_names_allowed= 1;
01260 session_arg->lex().current_select= fake_select_lex;
01261 }
01262 session_arg->lex().pop_context();
01263 return 0;
01264 }
01265
01266
01286 void push_new_name_resolution_context(Session& session, TableList& left_op, TableList& right_op)
01287 {
01288 Name_resolution_context *on_context= new (session.mem_root) Name_resolution_context;
01289 on_context->init();
01290 on_context->first_name_resolution_table= left_op.first_leaf_for_name_resolution();
01291 on_context->last_name_resolution_table= right_op.last_leaf_for_name_resolution();
01292 session.lex().push_context(on_context);
01293 }
01294
01295
01310 void add_join_on(TableList *b, Item *expr)
01311 {
01312 if (expr)
01313 {
01314 if (!b->on_expr)
01315 b->on_expr= expr;
01316 else
01317 {
01318
01319
01320
01321
01322
01323 b->on_expr= new Item_cond_and(b->on_expr,expr);
01324 }
01325 b->on_expr->top_level_item();
01326 }
01327 }
01328
01329
01363 void add_join_natural(TableList *a, TableList *b, List<String> *using_fields,
01364 Select_Lex *lex)
01365 {
01366 b->natural_join= a;
01367 lex->prev_join_using= using_fields;
01368 }
01369
01370
01380 bool check_simple_select(Session* session)
01381 {
01382 if (session->lex().current_select != &session->lex().select_lex)
01383 {
01384 char command[80];
01385 Lex_input_stream *lip= session->m_lip;
01386 strncpy(command, lip->yylval->symbol.str,
01387 min(lip->yylval->symbol.length, (uint32_t)(sizeof(command)-1)));
01388 command[min(lip->yylval->symbol.length, (uint32_t)(sizeof(command)-1))]=0;
01389 my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
01390 return 1;
01391 }
01392 return 0;
01393 }
01394
01395
01407 Item * all_any_subquery_creator(Item *left_expr,
01408 chooser_compare_func_creator cmp,
01409 bool all,
01410 Select_Lex *select_lex)
01411 {
01412 if ((cmp == &comp_eq_creator) && !all)
01413 return new Item_in_subselect(left_expr, select_lex);
01414
01415 if ((cmp == &comp_ne_creator) && all)
01416 return new Item_func_not(new Item_in_subselect(left_expr, select_lex));
01417
01418 Item_allany_subselect *it=
01419 new Item_allany_subselect(left_expr, cmp, select_lex, all);
01420 if (all)
01421 return it->upper_item= new Item_func_not_all(it);
01422
01423 return it->upper_item= new Item_func_nop_all(it);
01424 }
01425
01426
01439 bool update_precheck(Session *session, TableList *)
01440 {
01441 const char *msg= 0;
01442 Select_Lex *select_lex= &session->lex().select_lex;
01443
01444 if (session->lex().select_lex.item_list.size() != session->lex().value_list.size())
01445 {
01446 my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
01447 return true;
01448 }
01449
01450 if (session->lex().select_lex.table_list.size() > 1)
01451 {
01452 if (select_lex->order_list.size())
01453 msg= "ORDER BY";
01454 else if (select_lex->select_limit)
01455 msg= "LIMIT";
01456 if (msg)
01457 {
01458 my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
01459 return true;
01460 }
01461 }
01462 return false;
01463 }
01464
01465
01478 bool insert_precheck(Session *session, TableList *)
01479 {
01480
01481
01482
01483
01484 if (session->lex().update_list.size() != session->lex().value_list.size())
01485 {
01486 my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
01487 return true;
01488 }
01489 return false;
01490 }
01491
01492
01503 Item *negate_expression(Session *session, Item *expr)
01504 {
01505 Item *negated;
01506 if (expr->type() == Item::FUNC_ITEM &&
01507 ((Item_func *) expr)->functype() == Item_func::NOT_FUNC)
01508 {
01509
01510 Item *arg= ((Item_func *) expr)->arguments()[0];
01511 enum_parsing_place place= session->lex().current_select->parsing_place;
01512 if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
01513 return arg;
01514
01515
01516
01517
01518 return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
01519 }
01520
01521 if ((negated= expr->neg_transformer(session)) != 0)
01522 return negated;
01523 return new Item_func_not(expr);
01524 }
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543 bool check_string_char_length(str_ref str, const char *err_msg,
01544 uint32_t max_char_length, const charset_info_st * const cs,
01545 bool no_error)
01546 {
01547 int well_formed_error;
01548 uint32_t res= cs->cset->well_formed_len(*cs, str, max_char_length, &well_formed_error);
01549
01550 if (!well_formed_error && str.size() == res)
01551 return false;
01552
01553 if (not no_error)
01554 my_error(ER_WRONG_STRING_LENGTH, MYF(0), str.data(), err_msg, max_char_length);
01555 return true;
01556 }
01557
01558
01559 bool check_identifier_name(str_ref str, error_t err_code)
01560 {
01561 uint32_t max_char_length= NAME_CHAR_LEN;
01562
01563
01564
01565
01566
01567 const charset_info_st * const cs= &my_charset_utf8mb4_general_ci;
01568
01569 int well_formed_error;
01570 uint32_t res= cs->cset->well_formed_len(*cs, str, max_char_length, &well_formed_error);
01571
01572 if (well_formed_error)
01573 {
01574 my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str.data());
01575 return true;
01576 }
01577
01578 if (str.size() == res)
01579 return false;
01580
01581 switch (err_code)
01582 {
01583 case EE_OK:
01584 break;
01585 case ER_WRONG_STRING_LENGTH:
01586 my_error(err_code, MYF(0), str.data(), "", max_char_length);
01587 break;
01588 case ER_TOO_LONG_IDENT:
01589 my_error(err_code, MYF(0), str.data());
01590 break;
01591 default:
01592 assert(false);
01593 }
01594
01595 return true;
01596 }
01597
01598
01611 static bool parse_sql(Session *session, Lex_input_stream *lip)
01612 {
01613 assert(session->m_lip == NULL);
01614
01615 DRIZZLE_QUERY_PARSE_START(session->getQueryString()->c_str());
01616
01617
01618
01619 session->m_lip= lip;
01620
01621
01622
01623 bool parse_status= base_sql_parse(session) != 0;
01624
01625
01626
01627 assert(!parse_status || session->is_error());
01628
01629
01630
01631 session->m_lip= NULL;
01632
01633 DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
01634
01635
01636
01637 return parse_status || session->is_fatal_error;
01638 }
01639
01644 }