~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Monty Taylor
  • Date: 2010-08-02 18:35:08 UTC
  • mto: (1680.2.4 build)
  • mto: This revision was merged to the branch mainline in revision 1682.
  • Revision ID: mordred@inaugust.com-20100802183508-05993aozm52kae21
Removed inltool-update string warnings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include <config.h>
 
16
#include "config.h"
17
17
 
18
18
#define DRIZZLE_LEX 1
19
19
 
20
 
#include <drizzled/item/num.h>
21
 
#include <drizzled/abort_exception.h>
22
20
#include <drizzled/my_hash.h>
23
21
#include <drizzled/error.h>
24
22
#include <drizzled/nested_join.h>
25
23
#include <drizzled/query_id.h>
26
 
#include <drizzled/transaction_services.h>
 
24
#include "drizzled/transaction_services.h"
27
25
#include <drizzled/sql_parse.h>
28
26
#include <drizzled/data_home.h>
29
27
#include <drizzled/sql_base.h>
30
28
#include <drizzled/show.h>
 
29
#include <drizzled/db.h>
31
30
#include <drizzled/function/time/unix_timestamp.h>
32
31
#include <drizzled/function/get_system_var.h>
33
32
#include <drizzled/item/cmpfunc.h>
34
33
#include <drizzled/item/null.h>
35
34
#include <drizzled/session.h>
36
 
#include <drizzled/session/cache.h>
37
35
#include <drizzled/sql_load.h>
38
36
#include <drizzled/lock.h>
39
37
#include <drizzled/select_send.h>
40
38
#include <drizzled/plugin/client.h>
41
39
#include <drizzled/statement.h>
42
40
#include <drizzled/statement/alter_table.h>
43
 
#include <drizzled/probes.h>
44
 
#include <drizzled/global_charset_info.h>
45
 
 
46
 
#include <drizzled/plugin/logging.h>
47
 
#include <drizzled/plugin/query_rewrite.h>
48
 
#include <drizzled/plugin/query_cache.h>
49
 
#include <drizzled/plugin/authorization.h>
50
 
#include <drizzled/optimizer/explain_plan.h>
51
 
#include <drizzled/pthread_globals.h>
52
 
#include <drizzled/plugin/event_observer.h>
53
 
#include <drizzled/visibility.h>
54
 
 
55
 
#include <drizzled/schema.h>
 
41
#include "drizzled/probes.h"
 
42
#include "drizzled/session_list.h"
 
43
#include "drizzled/global_charset_info.h"
 
44
 
 
45
#include "drizzled/plugin/logging.h"
 
46
#include "drizzled/plugin/query_rewrite.h"
 
47
#include "drizzled/plugin/authorization.h"
 
48
#include "drizzled/optimizer/explain_plan.h"
 
49
#include "drizzled/pthread_globals.h"
56
50
 
57
51
#include <limits.h>
58
52
 
59
53
#include <bitset>
60
54
#include <algorithm>
61
 
#include <boost/date_time.hpp>
62
 
#include <drizzled/internal/my_sys.h>
 
55
 
 
56
#include "drizzled/internal/my_sys.h"
63
57
 
64
58
using namespace std;
65
59
 
66
 
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
 
60
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
67
61
 
68
62
namespace drizzled
69
63
{
70
64
 
71
65
/* Prototypes */
72
 
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
 
66
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
73
67
static bool parse_sql(Session *session, Lex_input_stream *lip);
74
 
void parse(Session *session, const char *inBuf, uint32_t length);
 
68
void mysql_parse(Session *session, const char *inBuf, uint32_t length);
75
69
 
76
70
/**
77
71
  @defgroup Runtime_Environment Runtime Environment
81
75
extern size_t my_thread_stack_size;
82
76
extern const CHARSET_INFO *character_set_filesystem;
83
77
 
84
 
namespace
85
 
{
86
 
 
87
 
static const std::string command_name[COM_END+1]={
88
 
  "Sleep",
89
 
  "Quit",
90
 
  "Init DB",
91
 
  "Query",
92
 
  "Shutdown",
93
 
  "Connect",
94
 
  "Ping",
95
 
  "Error"  // Last command number
 
78
const LEX_STRING command_name[COM_END+1]={
 
79
  { C_STRING_WITH_LEN("Sleep") },
 
80
  { C_STRING_WITH_LEN("Quit") },
 
81
  { C_STRING_WITH_LEN("Init DB") },
 
82
  { C_STRING_WITH_LEN("Query") },
 
83
  { C_STRING_WITH_LEN("Shutdown") },
 
84
  { C_STRING_WITH_LEN("Connect") },
 
85
  { C_STRING_WITH_LEN("Ping") },
 
86
  { C_STRING_WITH_LEN("Error") }  // Last command number
96
87
};
97
88
 
98
 
}
99
 
 
100
89
const char *xa_state_names[]={
101
90
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
102
91
};
115
104
*/
116
105
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
117
106
 
118
 
const std::string &getCommandName(const enum_server_command& command)
119
 
{
120
 
  return command_name[command];
121
 
}
122
 
 
123
107
void init_update_queries(void)
124
108
{
125
109
  uint32_t x;
168
152
                         can be zero.
169
153
 
170
154
  @todo
171
 
    set session->getLex()->sql_command to SQLCOM_END here.
 
155
    set session->lex->sql_command to SQLCOM_END here.
172
156
  @todo
173
157
    The following has to be changed to an 8 byte integer
174
158
 
184
168
  bool error= 0;
185
169
  Query_id &query_id= Query_id::get_query_id();
186
170
 
187
 
  DRIZZLE_COMMAND_START(session->thread_id, command);
 
171
  DRIZZLE_COMMAND_START(session->thread_id,
 
172
                        command);
188
173
 
189
174
  session->command= command;
190
 
  session->getLex()->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
175
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
191
176
  session->set_time();
192
177
  session->setQueryId(query_id.value());
193
178
 
197
182
    break;
198
183
  /* Increase id and count all other statements. */
199
184
  default:
200
 
    session->status_var.questions++;
 
185
    statistic_increment(session->status_var.questions, &LOCK_status);
201
186
    query_id.next();
202
187
  }
203
188
 
204
 
  /* @todo set session->getLex()->sql_command to SQLCOM_END here */
 
189
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
205
190
 
206
191
  plugin::Logging::preDo(session);
207
 
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
208
 
  {
209
 
    // We should do something about an error...
210
 
  }
211
192
 
212
193
  session->server_status&=
213
194
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
222
203
 
223
204
    string tmp(packet, packet_length);
224
205
 
225
 
    identifier::Schema identifier(tmp);
 
206
    SchemaIdentifier identifier(tmp);
226
207
 
227
 
    if (not schema::change(*session, identifier))
 
208
    if (not mysql_change_db(session, identifier))
228
209
    {
229
210
      session->my_ok();
230
211
    }
232
213
  }
233
214
  case COM_QUERY:
234
215
  {
235
 
    if (not session->readAndStoreQuery(packet, packet_length))
 
216
    if (! session->readAndStoreQuery(packet, packet_length))
236
217
      break;                                    // fatal error is set
237
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
 
218
    DRIZZLE_QUERY_START(session->query.c_str(),
238
219
                        session->thread_id,
239
 
                        const_cast<const char *>(session->schema()->c_str()));
 
220
                        const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
240
221
 
241
 
    parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
222
    plugin::QueryRewriter::rewriteQuery(session->db, session->query);
 
223
    mysql_parse(session, session->query.c_str(), session->query.length());
242
224
 
243
225
    break;
244
226
  }
249
231
    break;
250
232
  case COM_SHUTDOWN:
251
233
  {
252
 
    session->status_var.com_other++;
 
234
    status_var_increment(session->status_var.com_other);
253
235
    session->my_eof();
254
236
    session->close_thread_tables();                     // Free before kill
255
237
    kill_drizzle();
257
239
    break;
258
240
  }
259
241
  case COM_PING:
260
 
    session->status_var.com_other++;
 
242
    status_var_increment(session->status_var.com_other);
261
243
    session->my_ok();                           // Tell client we are alive
262
244
    break;
263
245
  case COM_SLEEP:
271
253
  /* If commit fails, we should be able to reset the OK status. */
272
254
  session->main_da.can_overwrite_status= true;
273
255
  TransactionServices &transaction_services= TransactionServices::singleton();
274
 
  transaction_services.autocommitOrRollback(*session, session->is_error());
 
256
  transaction_services.autocommitOrRollback(session, session->is_error());
275
257
  session->main_da.can_overwrite_status= false;
276
258
 
277
259
  session->transaction.stmt.reset();
283
265
    if (! session->main_da.is_set())
284
266
      session->send_kill_message();
285
267
  }
286
 
  if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
 
268
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
287
269
  {
288
 
    session->setKilled(Session::NOT_KILLED);
289
 
    session->setAbort(false);
 
270
    session->killed= Session::NOT_KILLED;
 
271
    session->mysys_var->abort= 0;
290
272
  }
291
273
 
292
274
  /* Can not be true, but do not take chances in production. */
296
278
  {
297
279
  case Diagnostics_area::DA_ERROR:
298
280
    /* The query failed, send error to log and abort bootstrap. */
299
 
    session->getClient()->sendError(session->main_da.sql_errno(),
 
281
    session->client->sendError(session->main_da.sql_errno(),
300
282
                               session->main_da.message());
301
283
    break;
302
284
 
303
285
  case Diagnostics_area::DA_EOF:
304
 
    session->getClient()->sendEOF();
 
286
    session->client->sendEOF();
305
287
    break;
306
288
 
307
289
  case Diagnostics_area::DA_OK:
308
 
    session->getClient()->sendOK();
 
290
    session->client->sendOK();
309
291
    break;
310
292
 
311
293
  case Diagnostics_area::DA_DISABLED:
313
295
 
314
296
  case Diagnostics_area::DA_EMPTY:
315
297
  default:
316
 
    session->getClient()->sendOK();
 
298
    session->client->sendOK();
317
299
    break;
318
300
  }
319
301
 
324
306
  session->close_thread_tables();
325
307
 
326
308
  plugin::Logging::postDo(session);
327
 
  if (unlikely(plugin::EventObserver::afterStatement(*session)))
328
 
  {
329
 
    // We should do something about an error...
330
 
  }
331
309
 
332
310
  /* Store temp state for processlist */
333
311
  session->set_proc_info("cleaning up");
334
312
  session->command= COM_SLEEP;
335
 
  session->resetQueryString();
 
313
  memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
 
314
  session->query.clear();
336
315
 
337
316
  session->set_proc_info(NULL);
338
317
  session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
379
358
                           const string& schema_table_name)
380
359
{
381
360
  LEX_STRING db, table;
382
 
  bitset<NUM_OF_TABLE_OPTIONS> table_options;
383
361
  /*
384
362
     We have to make non const db_name & table_name
385
363
     because of lower_case_table_names
388
366
  session->make_lex_string(&table, schema_table_name, false);
389
367
 
390
368
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
391
 
                               NULL, table_options, TL_READ))
 
369
                               NULL, 0, TL_READ))
392
370
  {
393
371
    return true;
394
372
  }
443
421
    true        Error
444
422
*/
445
423
 
446
 
static int execute_command(Session *session)
 
424
static int
 
425
mysql_execute_command(Session *session)
447
426
{
448
427
  bool res= false;
449
 
 
 
428
  LEX  *lex= session->lex;
450
429
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
451
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
452
 
 
 
430
  Select_Lex *select_lex= &lex->select_lex;
453
431
  /* list of all tables in query */
454
432
  TableList *all_tables;
 
433
  /* A peek into the query string */
 
434
  size_t proc_info_len= session->query.length() > PROCESS_LIST_WIDTH ?
 
435
                        PROCESS_LIST_WIDTH : session->query.length();
 
436
 
 
437
  memcpy(session->process_list_info, session->query.c_str(), proc_info_len);
 
438
  session->process_list_info[proc_info_len]= '\0';
455
439
 
456
440
  /*
457
441
    In many cases first table of main Select_Lex have special meaning =>
468
452
    assert(first_table == all_tables);
469
453
    assert(first_table == all_tables && first_table != 0);
470
454
  */
471
 
  session->getLex()->first_lists_tables_same();
472
 
 
 
455
  lex->first_lists_tables_same();
473
456
  /* should be assigned after making first tables same */
474
 
  all_tables= session->getLex()->query_tables;
475
 
 
 
457
  all_tables= lex->query_tables;
476
458
  /* set context for commands which do not use setup_tables */
477
 
  select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
 
459
  select_lex->
 
460
    context.resolve_in_table_list_only((TableList*)select_lex->
 
461
                                       table_list.first);
478
462
 
479
463
  /*
480
464
    Reset warning count for each query that uses tables
483
467
    variables, but for now this is probably good enough.
484
468
    Don't reset warnings when executing a stored routine.
485
469
  */
486
 
  if (all_tables || ! session->getLex()->is_single_level_stmt())
 
470
  if (all_tables || ! lex->is_single_level_stmt())
487
471
  {
488
472
    drizzle_reset_errors(session, 0);
489
473
  }
490
474
 
491
475
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
492
476
 
493
 
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
494
 
      && ! session->inTransaction()
495
 
      && session->getLex()->statement->isTransactional())
496
 
  {
497
 
    if (session->startTransaction() == false)
498
 
    {
499
 
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
500
 
      return true;
501
 
    }
502
 
  }
503
 
 
504
477
  /* now we are ready to execute the statement */
505
 
  res= session->getLex()->statement->execute();
 
478
  res= lex->statement->execute();
 
479
 
506
480
  session->set_proc_info("query end");
 
481
 
507
482
  /*
508
483
    The return value for ROW_COUNT() is "implementation dependent" if the
509
484
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
510
485
    wants. We also keep the last value in case of SQLCOM_CALL or
511
486
    SQLCOM_EXECUTE.
512
487
  */
513
 
  if (! (sql_command_flags[session->getLex()->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
488
  if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
514
489
  {
515
490
    session->row_count_func= -1;
516
491
  }
517
492
 
518
493
  return (res || session->is_error());
519
494
}
 
495
 
520
496
bool execute_sqlcom_select(Session *session, TableList *all_tables)
521
497
{
522
 
  LEX   *lex= session->getLex();
 
498
  LEX   *lex= session->lex;
523
499
  select_result *result=lex->result;
524
500
  bool res= false;
525
501
  /* assign global limit variable if limit is not given */
529
505
      param->select_limit=
530
506
        new Item_int((uint64_t) session->variables.select_limit);
531
507
  }
532
 
 
533
 
  if (all_tables
534
 
      && ! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
535
 
      && ! session->inTransaction()
536
 
      && ! lex->statement->isShow())
537
 
  {
538
 
    if (session->startTransaction() == false)
539
 
    {
540
 
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
541
 
      return true;
542
 
    }
543
 
  }
544
 
 
545
508
  if (not (res= session->openTablesLock(all_tables)))
546
509
  {
547
510
    if (lex->describe)
556
519
        return true;
557
520
      session->send_explain_fields(result);
558
521
      optimizer::ExplainPlan planner;
559
 
      res= planner.explainUnion(session, &session->getLex()->unit, result);
 
522
      res= planner.explainUnion(session, &session->lex->unit, result);
560
523
      if (lex->describe & DESCRIBE_EXTENDED)
561
524
      {
562
525
        char buff[1024];
563
526
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
564
527
        str.length(0);
565
 
        session->getLex()->unit.print(&str, QT_ORDINARY);
 
528
        session->lex->unit.print(&str, QT_ORDINARY);
566
529
        str.append('\0');
567
530
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
568
531
                     ER_YES, str.ptr());
571
534
        result->abort();
572
535
      else
573
536
        result->send_eof();
574
 
 
575
537
      delete result;
576
538
    }
577
539
    else
578
540
    {
579
541
      if (!result && !(result= new select_send()))
580
542
        return true;
581
 
 
582
 
      /* Init the Query Cache plugin */
583
 
      plugin::QueryCache::prepareResultset(session); 
584
543
      res= handle_select(session, lex, result, 0);
585
 
      /* Send the Resultset to the cache */
586
 
      plugin::QueryCache::setResultset(session); 
587
 
 
588
544
      if (result != lex->result)
589
545
        delete result;
590
546
    }
596
552
#define MY_YACC_INIT 1000                       // Start with big alloc
597
553
#define MY_YACC_MAX  32000                      // Because of 'short'
598
554
 
599
 
bool my_yyoverflow(short **yyss, ParserType **yyvs, ulong *yystacksize)
 
555
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
600
556
{
601
 
  LEX   *lex= current_session->getLex();
 
557
  LEX   *lex= current_session->lex;
602
558
  ulong old_info=0;
603
559
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
604
560
    return 1;
621
577
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
622
578
  }
623
579
  *yyss=(short*) lex->yacc_yyss;
624
 
  *yyvs=(ParserType*) lex->yacc_yyvs;
 
580
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
625
581
  return 0;
626
582
}
627
583
 
628
584
 
629
585
void
630
 
init_select(LEX *lex)
 
586
mysql_init_select(LEX *lex)
631
587
{
632
588
  Select_Lex *select_lex= lex->current_select;
633
589
  select_lex->init_select();
641
597
 
642
598
 
643
599
bool
644
 
new_select(LEX *lex, bool move_down)
 
600
mysql_new_select(LEX *lex, bool move_down)
645
601
{
646
602
  Select_Lex *select_lex;
647
603
  Session *session= lex->session;
648
604
 
649
605
  if (!(select_lex= new (session->mem_root) Select_Lex()))
650
 
    return true;
651
 
 
 
606
    return(1);
652
607
  select_lex->select_number= ++session->select_number;
653
608
  select_lex->parent_lex= lex; /* Used in init_query. */
654
609
  select_lex->init_query();
655
610
  select_lex->init_select();
656
611
  lex->nest_level++;
657
 
 
658
612
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
659
613
  {
660
614
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
661
615
    return(1);
662
616
  }
663
 
 
664
617
  select_lex->nest_level= lex->nest_level;
665
618
  if (move_down)
666
619
  {
688
641
    if (lex->current_select->order_list.first && !lex->current_select->braces)
689
642
    {
690
643
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
691
 
      return true;
 
644
      return(1);
692
645
    }
693
 
 
694
646
    select_lex->include_neighbour(lex->current_select);
695
647
    Select_Lex_Unit *unit= select_lex->master_unit();
696
 
 
697
 
    if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
698
 
      return true;
699
 
 
 
648
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
649
      return(1);
700
650
    select_lex->context.outer_context=
701
651
                unit->first_select()->context.outer_context;
702
652
  }
709
659
    list
710
660
  */
711
661
  select_lex->context.resolve_in_select_list= true;
712
 
 
713
 
  return false;
 
662
  return(0);
714
663
}
715
664
 
716
665
/**
723
672
  @param var_name               Variable name
724
673
*/
725
674
 
726
 
void create_select_for_variable(Session *session, const char *var_name)
 
675
void create_select_for_variable(const char *var_name)
727
676
{
 
677
  Session *session;
728
678
  LEX *lex;
729
679
  LEX_STRING tmp, null_lex_string;
730
680
  Item *var;
731
681
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
732
682
  char *end= buff;
733
683
 
734
 
  lex= session->getLex();
735
 
  init_select(lex);
 
684
  session= current_session;
 
685
  lex= session->lex;
 
686
  mysql_init_select(lex);
736
687
  lex->sql_command= SQLCOM_SELECT;
737
688
  tmp.str= (char*) var_name;
738
689
  tmp.length=strlen(var_name);
747
698
    var->set_name(buff, end-buff, system_charset_info);
748
699
    session->add_item_to_list(var);
749
700
  }
 
701
  return;
750
702
}
751
703
 
752
704
 
758
710
  @param       length  Length of the query text
759
711
*/
760
712
 
761
 
void parse(Session *session, const char *inBuf, uint32_t length)
 
713
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
762
714
{
763
 
  session->getLex()->start(session);
764
 
 
 
715
  uint64_t start_time= my_getsystime();
 
716
  lex_start(session);
765
717
  session->reset_for_next_command();
766
 
  /* Check if the Query is Cached if and return true if yes
767
 
   * TODO the plugin has to make sure that the query is cacheble
768
 
   * by setting the query_safe_cache param to TRUE
769
 
   */
770
 
  bool res= true;
771
 
  if (plugin::QueryCache::isCached(session))
772
 
  {
773
 
    res= plugin::QueryCache::sendCachedResultset(session);
774
 
  }
775
 
  if (not res)
776
 
  {
777
 
    return;
778
 
  }
779
 
  LEX *lex= session->getLex();
 
718
 
 
719
  LEX *lex= session->lex;
 
720
 
780
721
  Lex_input_stream lip(session, inBuf, length);
 
722
 
781
723
  bool err= parse_sql(session, &lip);
 
724
 
782
725
  if (!err)
783
726
  {
784
727
    {
785
 
      if (not session->is_error())
 
728
      if (! session->is_error())
786
729
      {
787
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
 
730
        DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
788
731
                                 session->thread_id,
789
 
                                 const_cast<const char *>(session->schema()->c_str()));
 
732
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
790
733
        // Implement Views here --Brian
 
734
 
791
735
        /* Actually execute the query */
792
 
        try 
793
 
        {
794
 
          execute_command(session);
 
736
        try {
 
737
          mysql_execute_command(session);
795
738
        }
796
739
        catch (...)
797
740
        {
798
741
          // Just try to catch any random failures that could have come
799
742
          // during execution.
800
 
          DRIZZLE_ABORT;
801
743
        }
802
744
        DRIZZLE_QUERY_EXEC_DONE(0);
803
745
      }
807
749
  {
808
750
    assert(session->is_error());
809
751
  }
 
752
 
810
753
  lex->unit.cleanup();
811
754
  session->set_proc_info("freeing items");
812
755
  session->end_statement();
813
756
  session->cleanup_after_query();
814
 
  session->set_end_timer();
 
757
  session->status_var.execution_time_nsec+= my_getsystime() - start_time;
815
758
}
816
759
 
817
760
 
833
776
                       List<String> *interval_list, const CHARSET_INFO * const cs)
834
777
{
835
778
  register CreateField *new_field;
836
 
  LEX  *lex= session->getLex();
 
779
  LEX  *lex= session->lex;
837
780
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
838
781
 
839
782
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
847
790
                      &default_key_create_info,
848
791
                      0, lex->col_list);
849
792
    statement->alter_info.key_list.push_back(key);
850
 
    lex->col_list.clear();
 
793
    lex->col_list.empty();
851
794
  }
852
795
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
853
796
  {
857
800
                 &default_key_create_info, 0,
858
801
                 lex->col_list);
859
802
    statement->alter_info.key_list.push_back(key);
860
 
    lex->col_list.clear();
 
803
    lex->col_list.empty();
861
804
  }
862
805
 
863
806
  if (default_value)
871
814
    */
872
815
    if (default_value->type() == Item::FUNC_ITEM &&
873
816
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
874
 
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
 
817
         type == DRIZZLE_TYPE_TIMESTAMP))
875
818
    {
876
819
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
877
820
      return true;
879
822
    else if (default_value->type() == Item::NULL_ITEM)
880
823
    {
881
824
      default_value= 0;
882
 
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
 
825
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
 
826
          NOT_NULL_FLAG)
883
827
      {
884
828
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
885
829
        return true;
892
836
    }
893
837
  }
894
838
 
895
 
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
 
839
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
896
840
  {
897
841
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
898
842
    return true;
911
855
}
912
856
 
913
857
 
 
858
/** Store position for column in ALTER TABLE .. ADD column. */
 
859
 
 
860
void store_position_for_column(const char *name)
 
861
{
 
862
  current_session->lex->last_field->after=const_cast<char*> (name);
 
863
}
 
864
 
914
865
/**
915
866
  Add a table to list of used tables.
916
867
 
931
882
*/
932
883
 
933
884
TableList *Select_Lex::add_table_to_list(Session *session,
934
 
                                         Table_ident *table,
935
 
                                         LEX_STRING *alias,
936
 
                                         const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
937
 
                                         thr_lock_type lock_type,
938
 
                                         List<Index_hint> *index_hints_arg,
939
 
                                         LEX_STRING *option)
 
885
                                             Table_ident *table,
 
886
                                             LEX_STRING *alias,
 
887
                                             uint32_t table_options,
 
888
                                             thr_lock_type lock_type,
 
889
                                             List<Index_hint> *index_hints_arg,
 
890
                                             LEX_STRING *option)
940
891
{
941
 
  TableList *ptr;
 
892
  register TableList *ptr;
942
893
  TableList *previous_table_ref; /* The table preceding the current one. */
943
894
  char *alias_str;
944
 
  LEX *lex= session->getLex();
 
895
  LEX *lex= session->lex;
945
896
 
946
897
  if (!table)
947
898
    return NULL;                                // End of memory
948
899
  alias_str= alias ? alias->str : table->table.str;
949
 
  if (! table_options.test(TL_OPTION_ALIAS) &&
 
900
  if (!test(table_options & TL_OPTION_ALIAS) &&
950
901
      check_table_name(table->table.str, table->table.length))
951
902
  {
952
903
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
957
908
  {
958
909
    my_casedn_str(files_charset_info, table->db.str);
959
910
 
960
 
    identifier::Schema schema_identifier(string(table->db.str));
961
 
    if (not schema::check(*session, schema_identifier))
 
911
    SchemaIdentifier schema_identifier(string(table->db.str));
 
912
    if (not check_db_name(session, schema_identifier))
962
913
    {
963
914
 
964
915
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
974
925
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
975
926
      return NULL;
976
927
    }
977
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
 
928
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
978
929
      return NULL;
979
930
  }
980
931
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
983
934
  if (table->db.str)
984
935
  {
985
936
    ptr->setIsFqtn(true);
986
 
    ptr->setSchemaName(table->db.str);
 
937
    ptr->db= table->db.str;
987
938
    ptr->db_length= table->db.length;
988
939
  }
989
 
  else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
 
940
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
990
941
    return NULL;
991
942
  else
992
943
    ptr->setIsFqtn(false);
993
944
 
994
945
  ptr->alias= alias_str;
995
946
  ptr->setIsAlias(alias ? true : false);
996
 
  ptr->setTableName(table->table.str);
 
947
  if (table->table.length)
 
948
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
 
949
  ptr->table_name=table->table.str;
997
950
  ptr->table_name_length=table->table.length;
998
951
  ptr->lock_type=   lock_type;
999
 
  ptr->force_index= table_options.test(TL_OPTION_FORCE_INDEX);
1000
 
  ptr->ignore_leaves= table_options.test(TL_OPTION_IGNORE_LEAVES);
 
952
  ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
 
953
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
1001
954
  ptr->derived=     table->sel;
1002
955
  ptr->select_lex=  lex->current_select;
1003
956
  ptr->index_hints= index_hints_arg;
1010
963
         tables ;
1011
964
         tables=tables->next_local)
1012
965
    {
1013
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1014
 
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
 
966
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
 
967
          !strcasecmp(ptr->db, tables->db))
1015
968
      {
1016
969
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1017
970
        return NULL;
1075
1028
bool Select_Lex::init_nested_join(Session *session)
1076
1029
{
1077
1030
  TableList *ptr;
1078
 
  NestedJoin *nested_join;
 
1031
  nested_join_st *nested_join;
1079
1032
 
1080
1033
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1081
 
                                       sizeof(NestedJoin))))
 
1034
                                       sizeof(nested_join_st))))
1082
1035
    return true;
1083
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1036
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1084
1037
  nested_join= ptr->getNestedJoin();
1085
1038
  join_list->push_front(ptr);
1086
1039
  ptr->setEmbedding(embedding);
1088
1041
  ptr->alias= (char*) "(nested_join)";
1089
1042
  embedding= ptr;
1090
1043
  join_list= &nested_join->join_list;
1091
 
  join_list->clear();
 
1044
  join_list->empty();
1092
1045
  return false;
1093
1046
}
1094
1047
 
1110
1063
TableList *Select_Lex::end_nested_join(Session *)
1111
1064
{
1112
1065
  TableList *ptr;
1113
 
  NestedJoin *nested_join;
 
1066
  nested_join_st *nested_join;
1114
1067
 
1115
1068
  assert(embedding);
1116
1069
  ptr= embedding;
1151
1104
TableList *Select_Lex::nest_last_join(Session *session)
1152
1105
{
1153
1106
  TableList *ptr;
1154
 
  NestedJoin *nested_join;
 
1107
  nested_join_st *nested_join;
1155
1108
  List<TableList> *embedded_list;
1156
1109
 
1157
1110
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1158
 
                                          sizeof(NestedJoin))))
 
1111
                                          sizeof(nested_join_st))))
1159
1112
    return NULL;
1160
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1113
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1161
1114
  nested_join= ptr->getNestedJoin();
1162
1115
  ptr->setEmbedding(embedding);
1163
1116
  ptr->setJoinList(join_list);
1164
1117
  ptr->alias= (char*) "(nest_last_join)";
1165
1118
  embedded_list= &nested_join->join_list;
1166
 
  embedded_list->clear();
 
1119
  embedded_list->empty();
1167
1120
 
1168
1121
  for (uint32_t i=0; i < 2; i++)
1169
1122
  {
1311
1264
  fake_select_lex->include_standalone(this,
1312
1265
                                      (Select_Lex_Node**)&fake_select_lex);
1313
1266
  fake_select_lex->select_number= INT_MAX;
1314
 
  fake_select_lex->parent_lex= session_arg->getLex(); /* Used in init_query. */
 
1267
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1315
1268
  fake_select_lex->make_empty_select();
1316
1269
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1317
1270
  fake_select_lex->select_limit= 0;
1331
1284
    */
1332
1285
    global_parameters= fake_select_lex;
1333
1286
    fake_select_lex->no_table_names_allowed= 1;
1334
 
    session_arg->getLex()->current_select= fake_select_lex;
 
1287
    session_arg->lex->current_select= fake_select_lex;
1335
1288
  }
1336
 
  session_arg->getLex()->pop_context();
 
1289
  session_arg->lex->pop_context();
1337
1290
  return(0);
1338
1291
}
1339
1292
 
1369
1322
    left_op->first_leaf_for_name_resolution();
1370
1323
  on_context->last_name_resolution_table=
1371
1324
    right_op->last_leaf_for_name_resolution();
1372
 
  return session->getLex()->push_context(on_context);
 
1325
  return session->lex->push_context(on_context);
1373
1326
}
1374
1327
 
1375
1328
 
1449
1402
 
1450
1403
 
1451
1404
/**
 
1405
  kill on thread.
 
1406
 
 
1407
  @param session                        Thread class
 
1408
  @param id                     Thread id
 
1409
  @param only_kill_query        Should it kill the query or the connection
 
1410
 
 
1411
  @note
 
1412
    This is written such that we have a short lock on LOCK_thread_count
 
1413
*/
 
1414
 
 
1415
static unsigned int
 
1416
kill_one_thread(Session *, ulong id, bool only_kill_query)
 
1417
{
 
1418
  Session *tmp= NULL;
 
1419
  uint32_t error= ER_NO_SUCH_THREAD;
 
1420
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
1421
  
 
1422
  for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
 
1423
  {
 
1424
    if ((*it)->thread_id == id)
 
1425
    {
 
1426
      tmp= *it;
 
1427
      pthread_mutex_lock(&tmp->LOCK_delete);    // Lock from delete
 
1428
      break;
 
1429
    }
 
1430
  }
 
1431
  pthread_mutex_unlock(&LOCK_thread_count);
 
1432
  if (tmp)
 
1433
  {
 
1434
 
 
1435
    if (tmp->isViewable())
 
1436
    {
 
1437
      tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
 
1438
      error= 0;
 
1439
    }
 
1440
 
 
1441
    pthread_mutex_unlock(&tmp->LOCK_delete);
 
1442
  }
 
1443
  return(error);
 
1444
}
 
1445
 
 
1446
 
 
1447
/*
 
1448
  kills a thread and sends response
 
1449
 
 
1450
  SYNOPSIS
 
1451
    sql_kill()
 
1452
    session                     Thread class
 
1453
    id                  Thread id
 
1454
    only_kill_query     Should it kill the query or the connection
 
1455
*/
 
1456
 
 
1457
void sql_kill(Session *session, ulong id, bool only_kill_query)
 
1458
{
 
1459
  uint32_t error;
 
1460
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
1461
    session->my_ok();
 
1462
  else
 
1463
    my_error(error, MYF(0), id);
 
1464
}
 
1465
 
 
1466
 
 
1467
/**
1452
1468
  Check if the select is a simple select (not an union).
1453
1469
 
1454
1470
  @retval
1457
1473
    1   error   ; In this case the error messege is sent to the client
1458
1474
*/
1459
1475
 
1460
 
bool check_simple_select(Session::pointer session)
 
1476
bool check_simple_select()
1461
1477
{
1462
 
  if (session->getLex()->current_select != &session->getLex()->select_lex)
 
1478
  Session *session= current_session;
 
1479
  LEX *lex= session->lex;
 
1480
  if (lex->current_select != &lex->select_lex)
1463
1481
  {
1464
1482
    char command[80];
1465
1483
    Lex_input_stream *lip= session->m_lip;
1519
1537
bool update_precheck(Session *session, TableList *)
1520
1538
{
1521
1539
  const char *msg= 0;
1522
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
1540
  LEX *lex= session->lex;
 
1541
  Select_Lex *select_lex= &lex->select_lex;
1523
1542
 
1524
 
  if (session->getLex()->select_lex.item_list.elements != session->getLex()->value_list.elements)
 
1543
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1525
1544
  {
1526
1545
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1527
1546
    return(true);
1528
1547
  }
1529
1548
 
1530
 
  if (session->getLex()->select_lex.table_list.elements > 1)
 
1549
  if (session->lex->select_lex.table_list.elements > 1)
1531
1550
  {
1532
1551
    if (select_lex->order_list.elements)
1533
1552
      msg= "ORDER BY";
1557
1576
 
1558
1577
bool insert_precheck(Session *session, TableList *)
1559
1578
{
 
1579
  LEX *lex= session->lex;
 
1580
 
1560
1581
  /*
1561
1582
    Check that we have modify privileges for the first table and
1562
1583
    select privileges for the rest
1563
1584
  */
1564
 
  if (session->getLex()->update_list.elements != session->getLex()->value_list.elements)
 
1585
  if (lex->update_list.elements != lex->value_list.elements)
1565
1586
  {
1566
1587
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1567
1588
    return(true);
1571
1592
 
1572
1593
 
1573
1594
/**
 
1595
  CREATE TABLE query pre-check.
 
1596
 
 
1597
  @param session                        Thread handler
 
1598
  @param tables         Global table list
 
1599
  @param create_table           Table which will be created
 
1600
 
 
1601
  @retval
 
1602
    false   OK
 
1603
  @retval
 
1604
    true   Error
 
1605
*/
 
1606
 
 
1607
bool create_table_precheck(TableIdentifier &identifier)
 
1608
{
 
1609
  if (not plugin::StorageEngine::canCreateTable(identifier))
 
1610
  {
 
1611
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
 
1612
    return true;
 
1613
  }
 
1614
 
 
1615
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
1616
  {
 
1617
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
 
1618
    return true;
 
1619
  }
 
1620
 
 
1621
  return false;
 
1622
}
 
1623
 
 
1624
 
 
1625
/**
1574
1626
  negate given expression.
1575
1627
 
1576
1628
  @param session  thread handler
1588
1640
  {
1589
1641
    /* it is NOT(NOT( ... )) */
1590
1642
    Item *arg= ((Item_func *) expr)->arguments()[0];
1591
 
    enum_parsing_place place= session->getLex()->current_select->parsing_place;
 
1643
    enum_parsing_place place= session->lex->current_select->parsing_place;
1592
1644
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1593
1645
      return arg;
1594
1646
    /*
1637
1689
}
1638
1690
 
1639
1691
 
1640
 
bool check_identifier_name(LEX_STRING *str, error_t err_code,
 
1692
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
1641
1693
                           uint32_t max_char_length,
1642
1694
                           const char *param_for_err_msg)
1643
1695
{
1663
1715
 
1664
1716
  switch (err_code)
1665
1717
  {
1666
 
  case EE_OK:
 
1718
  case 0:
1667
1719
    break;
1668
1720
  case ER_WRONG_STRING_LENGTH:
1669
1721
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1675
1727
    assert(0);
1676
1728
    break;
1677
1729
  }
1678
 
 
1679
1730
  return true;
1680
1731
}
1681
1732
 
1696
1747
{
1697
1748
  assert(session->m_lip == NULL);
1698
1749
 
1699
 
  DRIZZLE_QUERY_PARSE_START(session->getQueryString()->c_str());
 
1750
  DRIZZLE_QUERY_PARSE_START(session->query.c_str());
1700
1751
 
1701
1752
  /* Set Lex_input_stream. */
1702
1753
 
1704
1755
 
1705
1756
  /* Parse the query. */
1706
1757
 
1707
 
  bool parse_status= base_sql_parse(session) != 0;
 
1758
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
1708
1759
 
1709
1760
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1710
1761
 
1711
 
  assert(!parse_status || session->is_error());
 
1762
  assert(!mysql_parse_status || session->is_error());
1712
1763
 
1713
1764
  /* Reset Lex_input_stream. */
1714
1765
 
1715
1766
  session->m_lip= NULL;
1716
1767
 
1717
 
  DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
 
1768
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1718
1769
 
1719
1770
  /* That's it. */
1720
1771
 
1721
 
  return parse_status || session->is_fatal_error;
 
1772
  return mysql_parse_status || session->is_fatal_error;
1722
1773
}
1723
1774
 
1724
1775
/**