~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Monty Taylor
  • Date: 2010-03-06 02:08:13 UTC
  • mto: This revision was merged to the branch mainline in revision 1381.
  • Revision ID: mordred@inaugust.com-20100306020813-c37d0b39004nl1zf
Remove plugin deinit.

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>
27
24
#include <drizzled/sql_parse.h>
28
25
#include <drizzled/data_home.h>
29
26
#include <drizzled/sql_base.h>
30
27
#include <drizzled/show.h>
 
28
#include <drizzled/db.h>
31
29
#include <drizzled/function/time/unix_timestamp.h>
32
30
#include <drizzled/function/get_system_var.h>
33
31
#include <drizzled/item/cmpfunc.h>
34
32
#include <drizzled/item/null.h>
35
33
#include <drizzled/session.h>
36
 
#include <drizzled/session/cache.h>
37
34
#include <drizzled/sql_load.h>
38
35
#include <drizzled/lock.h>
39
36
#include <drizzled/select_send.h>
40
37
#include <drizzled/plugin/client.h>
41
38
#include <drizzled/statement.h>
42
39
#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>
 
40
#include "drizzled/probes.h"
 
41
#include "drizzled/session_list.h"
 
42
#include "drizzled/global_charset_info.h"
 
43
#include "drizzled/transaction_services.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);
214
195
  switch (command) {
215
196
  case COM_INIT_DB:
216
197
  {
217
 
    if (packet_length == 0)
218
 
    {
219
 
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
220
 
      break;
221
 
    }
222
 
 
 
198
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
223
199
    string tmp(packet, packet_length);
224
200
 
225
 
    identifier::Schema identifier(tmp);
226
 
 
227
 
    if (not schema::change(*session, identifier))
 
201
    if (not mysql_change_db(session, tmp))
228
202
    {
229
203
      session->my_ok();
230
204
    }
232
206
  }
233
207
  case COM_QUERY:
234
208
  {
235
 
    if (not session->readAndStoreQuery(packet, packet_length))
 
209
    if (! session->readAndStoreQuery(packet, packet_length))
236
210
      break;                                    // fatal error is set
237
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
 
211
    DRIZZLE_QUERY_START(session->query.c_str(),
238
212
                        session->thread_id,
239
 
                        const_cast<const char *>(session->schema()->c_str()));
 
213
                        const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
240
214
 
241
 
    parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
215
    plugin::QueryRewriter::rewriteQuery(session->query);
 
216
    mysql_parse(session, session->query.c_str(), session->query.length());
242
217
 
243
218
    break;
244
219
  }
249
224
    break;
250
225
  case COM_SHUTDOWN:
251
226
  {
252
 
    session->status_var.com_other++;
 
227
    status_var_increment(session->status_var.com_other);
253
228
    session->my_eof();
254
229
    session->close_thread_tables();                     // Free before kill
255
230
    kill_drizzle();
257
232
    break;
258
233
  }
259
234
  case COM_PING:
260
 
    session->status_var.com_other++;
 
235
    status_var_increment(session->status_var.com_other);
261
236
    session->my_ok();                           // Tell client we are alive
262
237
    break;
263
238
  case COM_SLEEP:
271
246
  /* If commit fails, we should be able to reset the OK status. */
272
247
  session->main_da.can_overwrite_status= true;
273
248
  TransactionServices &transaction_services= TransactionServices::singleton();
274
 
  transaction_services.autocommitOrRollback(*session, session->is_error());
 
249
  transaction_services.ha_autocommit_or_rollback(session, session->is_error());
275
250
  session->main_da.can_overwrite_status= false;
276
251
 
277
252
  session->transaction.stmt.reset();
283
258
    if (! session->main_da.is_set())
284
259
      session->send_kill_message();
285
260
  }
286
 
  if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
 
261
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
287
262
  {
288
 
    session->setKilled(Session::NOT_KILLED);
289
 
    session->setAbort(false);
 
263
    session->killed= Session::NOT_KILLED;
 
264
    session->mysys_var->abort= 0;
290
265
  }
291
266
 
292
267
  /* Can not be true, but do not take chances in production. */
296
271
  {
297
272
  case Diagnostics_area::DA_ERROR:
298
273
    /* The query failed, send error to log and abort bootstrap. */
299
 
    session->getClient()->sendError(session->main_da.sql_errno(),
 
274
    session->client->sendError(session->main_da.sql_errno(),
300
275
                               session->main_da.message());
301
276
    break;
302
277
 
303
278
  case Diagnostics_area::DA_EOF:
304
 
    session->getClient()->sendEOF();
 
279
    session->client->sendEOF();
305
280
    break;
306
281
 
307
282
  case Diagnostics_area::DA_OK:
308
 
    session->getClient()->sendOK();
 
283
    session->client->sendOK();
309
284
    break;
310
285
 
311
286
  case Diagnostics_area::DA_DISABLED:
313
288
 
314
289
  case Diagnostics_area::DA_EMPTY:
315
290
  default:
316
 
    session->getClient()->sendOK();
 
291
    session->client->sendOK();
317
292
    break;
318
293
  }
319
294
 
324
299
  session->close_thread_tables();
325
300
 
326
301
  plugin::Logging::postDo(session);
327
 
  if (unlikely(plugin::EventObserver::afterStatement(*session)))
328
 
  {
329
 
    // We should do something about an error...
330
 
  }
331
302
 
332
303
  /* Store temp state for processlist */
333
304
  session->set_proc_info("cleaning up");
334
305
  session->command= COM_SLEEP;
335
 
  session->resetQueryString();
 
306
  memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
 
307
  session->query.clear();
336
308
 
337
309
  session->set_proc_info(NULL);
338
 
  session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
 
310
  free_root(session->mem_root,MYF(memory::KEEP_PREALLOC));
339
311
 
340
312
  if (DRIZZLE_QUERY_DONE_ENABLED() || DRIZZLE_COMMAND_DONE_ENABLED())
341
313
  {
379
351
                           const string& schema_table_name)
380
352
{
381
353
  LEX_STRING db, table;
382
 
  bitset<NUM_OF_TABLE_OPTIONS> table_options;
383
354
  /*
384
355
     We have to make non const db_name & table_name
385
356
     because of lower_case_table_names
388
359
  session->make_lex_string(&table, schema_table_name, false);
389
360
 
390
361
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
391
 
                               NULL, table_options, TL_READ))
 
362
                               NULL, 0, TL_READ))
392
363
  {
393
364
    return true;
394
365
  }
443
414
    true        Error
444
415
*/
445
416
 
446
 
static int execute_command(Session *session)
 
417
static int
 
418
mysql_execute_command(Session *session)
447
419
{
448
420
  bool res= false;
449
 
 
 
421
  LEX  *lex= session->lex;
450
422
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
451
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
452
 
 
 
423
  Select_Lex *select_lex= &lex->select_lex;
453
424
  /* list of all tables in query */
454
425
  TableList *all_tables;
 
426
  /* A peek into the query string */
 
427
  size_t proc_info_len= session->query.length() > PROCESS_LIST_WIDTH ?
 
428
                        PROCESS_LIST_WIDTH : session->query.length();
 
429
 
 
430
  memcpy(session->process_list_info, session->query.c_str(), proc_info_len);
 
431
  session->process_list_info[proc_info_len]= '\0';
455
432
 
456
433
  /*
457
434
    In many cases first table of main Select_Lex have special meaning =>
468
445
    assert(first_table == all_tables);
469
446
    assert(first_table == all_tables && first_table != 0);
470
447
  */
471
 
  session->getLex()->first_lists_tables_same();
472
 
 
 
448
  lex->first_lists_tables_same();
473
449
  /* should be assigned after making first tables same */
474
 
  all_tables= session->getLex()->query_tables;
475
 
 
 
450
  all_tables= lex->query_tables;
476
451
  /* 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);
 
452
  select_lex->
 
453
    context.resolve_in_table_list_only((TableList*)select_lex->
 
454
                                       table_list.first);
478
455
 
479
456
  /*
480
457
    Reset warning count for each query that uses tables
483
460
    variables, but for now this is probably good enough.
484
461
    Don't reset warnings when executing a stored routine.
485
462
  */
486
 
  if (all_tables || ! session->getLex()->is_single_level_stmt())
 
463
  if (all_tables || ! lex->is_single_level_stmt())
487
464
  {
488
465
    drizzle_reset_errors(session, 0);
489
466
  }
490
467
 
 
468
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
 
469
 
491
470
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
492
471
 
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
472
  /* now we are ready to execute the statement */
505
 
  res= session->getLex()->statement->execute();
 
473
  res= lex->statement->execute();
 
474
 
506
475
  session->set_proc_info("query end");
 
476
 
507
477
  /*
508
478
    The return value for ROW_COUNT() is "implementation dependent" if the
509
479
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
510
480
    wants. We also keep the last value in case of SQLCOM_CALL or
511
481
    SQLCOM_EXECUTE.
512
482
  */
513
 
  if (! (sql_command_flags[session->getLex()->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
483
  if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
514
484
  {
515
485
    session->row_count_func= -1;
516
486
  }
517
487
 
518
488
  return (res || session->is_error());
519
489
}
 
490
 
520
491
bool execute_sqlcom_select(Session *session, TableList *all_tables)
521
492
{
522
 
  LEX   *lex= session->getLex();
 
493
  LEX   *lex= session->lex;
523
494
  select_result *result=lex->result;
524
495
  bool res= false;
525
496
  /* assign global limit variable if limit is not given */
529
500
      param->select_limit=
530
501
        new Item_int((uint64_t) session->variables.select_limit);
531
502
  }
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
 
  if (not (res= session->openTablesLock(all_tables)))
 
503
  if (!(res= session->openTablesLock(all_tables)))
546
504
  {
547
505
    if (lex->describe)
548
506
    {
556
514
        return true;
557
515
      session->send_explain_fields(result);
558
516
      optimizer::ExplainPlan planner;
559
 
      res= planner.explainUnion(session, &session->getLex()->unit, result);
 
517
      res= planner.explainUnion(session, &session->lex->unit, result);
560
518
      if (lex->describe & DESCRIBE_EXTENDED)
561
519
      {
562
520
        char buff[1024];
563
521
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
564
522
        str.length(0);
565
 
        session->getLex()->unit.print(&str, QT_ORDINARY);
 
523
        session->lex->unit.print(&str, QT_ORDINARY);
566
524
        str.append('\0');
567
525
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
568
526
                     ER_YES, str.ptr());
571
529
        result->abort();
572
530
      else
573
531
        result->send_eof();
574
 
 
575
532
      delete result;
576
533
    }
577
534
    else
578
535
    {
579
536
      if (!result && !(result= new select_send()))
580
537
        return true;
581
 
 
582
 
      /* Init the Query Cache plugin */
583
 
      plugin::QueryCache::prepareResultset(session); 
584
538
      res= handle_select(session, lex, result, 0);
585
 
      /* Send the Resultset to the cache */
586
 
      plugin::QueryCache::setResultset(session); 
587
 
 
588
539
      if (result != lex->result)
589
540
        delete result;
590
541
    }
596
547
#define MY_YACC_INIT 1000                       // Start with big alloc
597
548
#define MY_YACC_MAX  32000                      // Because of 'short'
598
549
 
599
 
bool my_yyoverflow(short **yyss, ParserType **yyvs, ulong *yystacksize)
 
550
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
600
551
{
601
 
  LEX   *lex= current_session->getLex();
 
552
  LEX   *lex= current_session->lex;
602
553
  ulong old_info=0;
603
554
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
604
555
    return 1;
621
572
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
622
573
  }
623
574
  *yyss=(short*) lex->yacc_yyss;
624
 
  *yyvs=(ParserType*) lex->yacc_yyvs;
 
575
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
625
576
  return 0;
626
577
}
627
578
 
628
579
 
629
580
void
630
 
init_select(LEX *lex)
 
581
mysql_init_select(LEX *lex)
631
582
{
632
583
  Select_Lex *select_lex= lex->current_select;
633
584
  select_lex->init_select();
641
592
 
642
593
 
643
594
bool
644
 
new_select(LEX *lex, bool move_down)
 
595
mysql_new_select(LEX *lex, bool move_down)
645
596
{
646
597
  Select_Lex *select_lex;
647
598
  Session *session= lex->session;
648
599
 
649
600
  if (!(select_lex= new (session->mem_root) Select_Lex()))
650
 
    return true;
651
 
 
 
601
    return(1);
652
602
  select_lex->select_number= ++session->select_number;
653
603
  select_lex->parent_lex= lex; /* Used in init_query. */
654
604
  select_lex->init_query();
655
605
  select_lex->init_select();
656
606
  lex->nest_level++;
657
 
 
658
607
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
659
608
  {
660
609
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
661
610
    return(1);
662
611
  }
663
 
 
664
612
  select_lex->nest_level= lex->nest_level;
665
613
  if (move_down)
666
614
  {
688
636
    if (lex->current_select->order_list.first && !lex->current_select->braces)
689
637
    {
690
638
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
691
 
      return true;
 
639
      return(1);
692
640
    }
693
 
 
694
641
    select_lex->include_neighbour(lex->current_select);
695
642
    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
 
 
 
643
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
644
      return(1);
700
645
    select_lex->context.outer_context=
701
646
                unit->first_select()->context.outer_context;
702
647
  }
709
654
    list
710
655
  */
711
656
  select_lex->context.resolve_in_select_list= true;
712
 
 
713
 
  return false;
 
657
  return(0);
714
658
}
715
659
 
716
660
/**
723
667
  @param var_name               Variable name
724
668
*/
725
669
 
726
 
void create_select_for_variable(Session *session, const char *var_name)
 
670
void create_select_for_variable(const char *var_name)
727
671
{
 
672
  Session *session;
728
673
  LEX *lex;
729
674
  LEX_STRING tmp, null_lex_string;
730
675
  Item *var;
731
676
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
732
677
  char *end= buff;
733
678
 
734
 
  lex= session->getLex();
735
 
  init_select(lex);
 
679
  session= current_session;
 
680
  lex= session->lex;
 
681
  mysql_init_select(lex);
736
682
  lex->sql_command= SQLCOM_SELECT;
737
683
  tmp.str= (char*) var_name;
738
684
  tmp.length=strlen(var_name);
743
689
  */
744
690
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
745
691
  {
746
 
    end+= snprintf(buff, sizeof(buff), "@@session.%s", var_name);
 
692
    end+= sprintf(buff, "@@session.%s", var_name);
747
693
    var->set_name(buff, end-buff, system_charset_info);
748
694
    session->add_item_to_list(var);
749
695
  }
 
696
  return;
750
697
}
751
698
 
752
699
 
758
705
  @param       length  Length of the query text
759
706
*/
760
707
 
761
 
void parse(Session *session, const char *inBuf, uint32_t length)
 
708
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
762
709
{
763
 
  session->getLex()->start(session);
764
 
 
 
710
  lex_start(session);
765
711
  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();
 
712
 
 
713
  LEX *lex= session->lex;
 
714
 
780
715
  Lex_input_stream lip(session, inBuf, length);
 
716
 
781
717
  bool err= parse_sql(session, &lip);
 
718
 
782
719
  if (!err)
783
720
  {
784
721
    {
785
 
      if (not session->is_error())
 
722
      if (! session->is_error())
786
723
      {
787
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
 
724
        DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
788
725
                                 session->thread_id,
789
 
                                 const_cast<const char *>(session->schema()->c_str()));
790
 
        // Implement Views here --Brian
 
726
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
791
727
        /* Actually execute the query */
792
 
        try 
793
 
        {
794
 
          execute_command(session);
795
 
        }
796
 
        catch (...)
797
 
        {
798
 
          // Just try to catch any random failures that could have come
799
 
          // during execution.
800
 
          DRIZZLE_ABORT;
801
 
        }
 
728
        mysql_execute_command(session);
802
729
        DRIZZLE_QUERY_EXEC_DONE(0);
803
730
      }
804
731
    }
807
734
  {
808
735
    assert(session->is_error());
809
736
  }
 
737
 
810
738
  lex->unit.cleanup();
811
739
  session->set_proc_info("freeing items");
812
740
  session->end_statement();
813
741
  session->cleanup_after_query();
814
 
  session->set_end_timer();
 
742
 
 
743
  return;
815
744
}
816
745
 
817
746
 
833
762
                       List<String> *interval_list, const CHARSET_INFO * const cs)
834
763
{
835
764
  register CreateField *new_field;
836
 
  LEX  *lex= session->getLex();
 
765
  LEX  *lex= session->lex;
837
766
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
838
767
 
839
768
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
847
776
                      &default_key_create_info,
848
777
                      0, lex->col_list);
849
778
    statement->alter_info.key_list.push_back(key);
850
 
    lex->col_list.clear();
 
779
    lex->col_list.empty();
851
780
  }
852
781
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
853
782
  {
857
786
                 &default_key_create_info, 0,
858
787
                 lex->col_list);
859
788
    statement->alter_info.key_list.push_back(key);
860
 
    lex->col_list.clear();
 
789
    lex->col_list.empty();
861
790
  }
862
791
 
863
792
  if (default_value)
871
800
    */
872
801
    if (default_value->type() == Item::FUNC_ITEM &&
873
802
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
874
 
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
 
803
         type == DRIZZLE_TYPE_TIMESTAMP))
875
804
    {
876
805
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
877
806
      return true;
879
808
    else if (default_value->type() == Item::NULL_ITEM)
880
809
    {
881
810
      default_value= 0;
882
 
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
 
811
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
 
812
          NOT_NULL_FLAG)
883
813
      {
884
814
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
885
815
        return true;
892
822
    }
893
823
  }
894
824
 
895
 
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
 
825
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
896
826
  {
897
827
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
898
828
    return true;
911
841
}
912
842
 
913
843
 
 
844
/** Store position for column in ALTER TABLE .. ADD column. */
 
845
 
 
846
void store_position_for_column(const char *name)
 
847
{
 
848
  current_session->lex->last_field->after=const_cast<char*> (name);
 
849
}
 
850
 
914
851
/**
915
852
  Add a table to list of used tables.
916
853
 
931
868
*/
932
869
 
933
870
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)
 
871
                                             Table_ident *table,
 
872
                                             LEX_STRING *alias,
 
873
                                             uint32_t table_options,
 
874
                                             thr_lock_type lock_type,
 
875
                                             List<Index_hint> *index_hints_arg,
 
876
                                             LEX_STRING *option)
940
877
{
941
 
  TableList *ptr;
 
878
  register TableList *ptr;
942
879
  TableList *previous_table_ref; /* The table preceding the current one. */
943
880
  char *alias_str;
944
 
  LEX *lex= session->getLex();
 
881
  LEX *lex= session->lex;
945
882
 
946
883
  if (!table)
947
884
    return NULL;                                // End of memory
948
885
  alias_str= alias ? alias->str : table->table.str;
949
 
  if (! table_options.test(TL_OPTION_ALIAS) &&
 
886
  if (!test(table_options & TL_OPTION_ALIAS) &&
950
887
      check_table_name(table->table.str, table->table.length))
951
888
  {
952
889
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
953
890
    return NULL;
954
891
  }
955
892
 
956
 
  if (table->is_derived_table() == false && table->db.str)
 
893
  if (table->is_derived_table() == false && table->db.str &&
 
894
      check_db_name(&table->db))
957
895
  {
958
 
    my_casedn_str(files_charset_info, table->db.str);
959
 
 
960
 
    identifier::Schema schema_identifier(string(table->db.str));
961
 
    if (not schema::check(*session, schema_identifier))
962
 
    {
963
 
 
964
 
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
965
 
      return NULL;
966
 
    }
 
896
    my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
 
897
    return NULL;
967
898
  }
968
899
 
969
900
  if (!alias)                                   /* Alias is case sensitive */
974
905
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
975
906
      return NULL;
976
907
    }
977
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
 
908
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
978
909
      return NULL;
979
910
  }
980
911
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
981
912
    return NULL;
982
 
 
983
913
  if (table->db.str)
984
914
  {
985
 
    ptr->setIsFqtn(true);
986
 
    ptr->setSchemaName(table->db.str);
 
915
    ptr->is_fqtn= true;
 
916
    ptr->db= table->db.str;
987
917
    ptr->db_length= table->db.length;
988
918
  }
989
 
  else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
 
919
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
990
920
    return NULL;
991
921
  else
992
 
    ptr->setIsFqtn(false);
 
922
    ptr->is_fqtn= false;
993
923
 
994
924
  ptr->alias= alias_str;
995
 
  ptr->setIsAlias(alias ? true : false);
996
 
  ptr->setTableName(table->table.str);
 
925
  ptr->is_alias= alias ? true : false;
 
926
  if (table->table.length)
 
927
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
 
928
  ptr->table_name=table->table.str;
997
929
  ptr->table_name_length=table->table.length;
998
930
  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);
 
931
  ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
 
932
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
1001
933
  ptr->derived=     table->sel;
1002
934
  ptr->select_lex=  lex->current_select;
1003
935
  ptr->index_hints= index_hints_arg;
1010
942
         tables ;
1011
943
         tables=tables->next_local)
1012
944
    {
1013
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1014
 
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
 
945
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
 
946
          !strcmp(ptr->db, tables->db))
1015
947
      {
1016
948
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1017
949
        return NULL;
1075
1007
bool Select_Lex::init_nested_join(Session *session)
1076
1008
{
1077
1009
  TableList *ptr;
1078
 
  NestedJoin *nested_join;
 
1010
  nested_join_st *nested_join;
1079
1011
 
1080
1012
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1081
 
                                       sizeof(NestedJoin))))
 
1013
                                       sizeof(nested_join_st))))
1082
1014
    return true;
1083
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1084
 
  nested_join= ptr->getNestedJoin();
 
1015
  nested_join= ptr->nested_join=
 
1016
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
1017
 
1085
1018
  join_list->push_front(ptr);
1086
 
  ptr->setEmbedding(embedding);
1087
 
  ptr->setJoinList(join_list);
 
1019
  ptr->embedding= embedding;
 
1020
  ptr->join_list= join_list;
1088
1021
  ptr->alias= (char*) "(nested_join)";
1089
1022
  embedding= ptr;
1090
1023
  join_list= &nested_join->join_list;
1091
 
  join_list->clear();
 
1024
  join_list->empty();
1092
1025
  return false;
1093
1026
}
1094
1027
 
1110
1043
TableList *Select_Lex::end_nested_join(Session *)
1111
1044
{
1112
1045
  TableList *ptr;
1113
 
  NestedJoin *nested_join;
 
1046
  nested_join_st *nested_join;
1114
1047
 
1115
1048
  assert(embedding);
1116
1049
  ptr= embedding;
1117
 
  join_list= ptr->getJoinList();
1118
 
  embedding= ptr->getEmbedding();
1119
 
  nested_join= ptr->getNestedJoin();
 
1050
  join_list= ptr->join_list;
 
1051
  embedding= ptr->embedding;
 
1052
  nested_join= ptr->nested_join;
1120
1053
  if (nested_join->join_list.elements == 1)
1121
1054
  {
1122
1055
    TableList *embedded= nested_join->join_list.head();
1123
1056
    join_list->pop();
1124
 
    embedded->setJoinList(join_list);
1125
 
    embedded->setEmbedding(embedding);
 
1057
    embedded->join_list= join_list;
 
1058
    embedded->embedding= embedding;
1126
1059
    join_list->push_front(embedded);
1127
1060
    ptr= embedded;
1128
1061
  }
1151
1084
TableList *Select_Lex::nest_last_join(Session *session)
1152
1085
{
1153
1086
  TableList *ptr;
1154
 
  NestedJoin *nested_join;
 
1087
  nested_join_st *nested_join;
1155
1088
  List<TableList> *embedded_list;
1156
1089
 
1157
1090
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1158
 
                                          sizeof(NestedJoin))))
 
1091
                                       sizeof(nested_join_st))))
1159
1092
    return NULL;
1160
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1161
 
  nested_join= ptr->getNestedJoin();
1162
 
  ptr->setEmbedding(embedding);
1163
 
  ptr->setJoinList(join_list);
 
1093
  nested_join= ptr->nested_join=
 
1094
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
1095
 
 
1096
  ptr->embedding= embedding;
 
1097
  ptr->join_list= join_list;
1164
1098
  ptr->alias= (char*) "(nest_last_join)";
1165
1099
  embedded_list= &nested_join->join_list;
1166
 
  embedded_list->clear();
 
1100
  embedded_list->empty();
1167
1101
 
1168
1102
  for (uint32_t i=0; i < 2; i++)
1169
1103
  {
1170
1104
    TableList *table= join_list->pop();
1171
 
    table->setJoinList(embedded_list);
1172
 
    table->setEmbedding(ptr);
 
1105
    table->join_list= embedded_list;
 
1106
    table->embedding= ptr;
1173
1107
    embedded_list->push_back(table);
1174
1108
    if (table->natural_join)
1175
1109
    {
1205
1139
void Select_Lex::add_joined_table(TableList *table)
1206
1140
{
1207
1141
  join_list->push_front(table);
1208
 
  table->setJoinList(join_list);
1209
 
  table->setEmbedding(embedding);
 
1142
  table->join_list= join_list;
 
1143
  table->embedding= embedding;
1210
1144
}
1211
1145
 
1212
1146
 
1311
1245
  fake_select_lex->include_standalone(this,
1312
1246
                                      (Select_Lex_Node**)&fake_select_lex);
1313
1247
  fake_select_lex->select_number= INT_MAX;
1314
 
  fake_select_lex->parent_lex= session_arg->getLex(); /* Used in init_query. */
 
1248
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1315
1249
  fake_select_lex->make_empty_select();
1316
1250
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1317
1251
  fake_select_lex->select_limit= 0;
1331
1265
    */
1332
1266
    global_parameters= fake_select_lex;
1333
1267
    fake_select_lex->no_table_names_allowed= 1;
1334
 
    session_arg->getLex()->current_select= fake_select_lex;
 
1268
    session_arg->lex->current_select= fake_select_lex;
1335
1269
  }
1336
 
  session_arg->getLex()->pop_context();
 
1270
  session_arg->lex->pop_context();
1337
1271
  return(0);
1338
1272
}
1339
1273
 
1369
1303
    left_op->first_leaf_for_name_resolution();
1370
1304
  on_context->last_name_resolution_table=
1371
1305
    right_op->last_leaf_for_name_resolution();
1372
 
  return session->getLex()->push_context(on_context);
 
1306
  return session->lex->push_context(on_context);
1373
1307
}
1374
1308
 
1375
1309
 
1449
1383
 
1450
1384
 
1451
1385
/**
 
1386
  kill on thread.
 
1387
 
 
1388
  @param session                        Thread class
 
1389
  @param id                     Thread id
 
1390
  @param only_kill_query        Should it kill the query or the connection
 
1391
 
 
1392
  @note
 
1393
    This is written such that we have a short lock on LOCK_thread_count
 
1394
*/
 
1395
 
 
1396
static unsigned int
 
1397
kill_one_thread(Session *, ulong id, bool only_kill_query)
 
1398
{
 
1399
  Session *tmp= NULL;
 
1400
  uint32_t error= ER_NO_SUCH_THREAD;
 
1401
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
1402
  
 
1403
  for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
 
1404
  {
 
1405
    if ((*it)->thread_id == id)
 
1406
    {
 
1407
      tmp= *it;
 
1408
      pthread_mutex_lock(&tmp->LOCK_delete);    // Lock from delete
 
1409
      break;
 
1410
    }
 
1411
  }
 
1412
  pthread_mutex_unlock(&LOCK_thread_count);
 
1413
  if (tmp)
 
1414
  {
 
1415
 
 
1416
    if (tmp->isViewable())
 
1417
    {
 
1418
      tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
 
1419
      error= 0;
 
1420
    }
 
1421
 
 
1422
    pthread_mutex_unlock(&tmp->LOCK_delete);
 
1423
  }
 
1424
  return(error);
 
1425
}
 
1426
 
 
1427
 
 
1428
/*
 
1429
  kills a thread and sends response
 
1430
 
 
1431
  SYNOPSIS
 
1432
    sql_kill()
 
1433
    session                     Thread class
 
1434
    id                  Thread id
 
1435
    only_kill_query     Should it kill the query or the connection
 
1436
*/
 
1437
 
 
1438
void sql_kill(Session *session, ulong id, bool only_kill_query)
 
1439
{
 
1440
  uint32_t error;
 
1441
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
1442
    session->my_ok();
 
1443
  else
 
1444
    my_error(error, MYF(0), id);
 
1445
}
 
1446
 
 
1447
 
 
1448
/**
1452
1449
  Check if the select is a simple select (not an union).
1453
1450
 
1454
1451
  @retval
1457
1454
    1   error   ; In this case the error messege is sent to the client
1458
1455
*/
1459
1456
 
1460
 
bool check_simple_select(Session::pointer session)
 
1457
bool check_simple_select()
1461
1458
{
1462
 
  if (session->getLex()->current_select != &session->getLex()->select_lex)
 
1459
  Session *session= current_session;
 
1460
  LEX *lex= session->lex;
 
1461
  if (lex->current_select != &lex->select_lex)
1463
1462
  {
1464
1463
    char command[80];
1465
1464
    Lex_input_stream *lip= session->m_lip;
1519
1518
bool update_precheck(Session *session, TableList *)
1520
1519
{
1521
1520
  const char *msg= 0;
1522
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
1521
  LEX *lex= session->lex;
 
1522
  Select_Lex *select_lex= &lex->select_lex;
1523
1523
 
1524
 
  if (session->getLex()->select_lex.item_list.elements != session->getLex()->value_list.elements)
 
1524
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1525
1525
  {
1526
1526
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1527
1527
    return(true);
1528
1528
  }
1529
1529
 
1530
 
  if (session->getLex()->select_lex.table_list.elements > 1)
 
1530
  if (session->lex->select_lex.table_list.elements > 1)
1531
1531
  {
1532
1532
    if (select_lex->order_list.elements)
1533
1533
      msg= "ORDER BY";
1557
1557
 
1558
1558
bool insert_precheck(Session *session, TableList *)
1559
1559
{
 
1560
  LEX *lex= session->lex;
 
1561
 
1560
1562
  /*
1561
1563
    Check that we have modify privileges for the first table and
1562
1564
    select privileges for the rest
1563
1565
  */
1564
 
  if (session->getLex()->update_list.elements != session->getLex()->value_list.elements)
 
1566
  if (lex->update_list.elements != lex->value_list.elements)
1565
1567
  {
1566
1568
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1567
1569
    return(true);
1571
1573
 
1572
1574
 
1573
1575
/**
 
1576
  CREATE TABLE query pre-check.
 
1577
 
 
1578
  @param session                        Thread handler
 
1579
  @param tables         Global table list
 
1580
  @param create_table           Table which will be created
 
1581
 
 
1582
  @retval
 
1583
    false   OK
 
1584
  @retval
 
1585
    true   Error
 
1586
*/
 
1587
 
 
1588
bool create_table_precheck(TableIdentifier &identifier)
 
1589
{
 
1590
  if (not plugin::StorageEngine::canCreateTable(identifier))
 
1591
  {
 
1592
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName());
 
1593
    return true;
 
1594
  }
 
1595
 
 
1596
  return false;
 
1597
}
 
1598
 
 
1599
 
 
1600
/**
1574
1601
  negate given expression.
1575
1602
 
1576
1603
  @param session  thread handler
1588
1615
  {
1589
1616
    /* it is NOT(NOT( ... )) */
1590
1617
    Item *arg= ((Item_func *) expr)->arguments()[0];
1591
 
    enum_parsing_place place= session->getLex()->current_select->parsing_place;
 
1618
    enum_parsing_place place= session->lex->current_select->parsing_place;
1592
1619
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1593
1620
      return arg;
1594
1621
    /*
1637
1664
}
1638
1665
 
1639
1666
 
1640
 
bool check_identifier_name(LEX_STRING *str, error_t err_code,
 
1667
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
1641
1668
                           uint32_t max_char_length,
1642
1669
                           const char *param_for_err_msg)
1643
1670
{
1663
1690
 
1664
1691
  switch (err_code)
1665
1692
  {
1666
 
  case EE_OK:
 
1693
  case 0:
1667
1694
    break;
1668
1695
  case ER_WRONG_STRING_LENGTH:
1669
1696
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1675
1702
    assert(0);
1676
1703
    break;
1677
1704
  }
1678
 
 
1679
1705
  return true;
1680
1706
}
1681
1707
 
1696
1722
{
1697
1723
  assert(session->m_lip == NULL);
1698
1724
 
1699
 
  DRIZZLE_QUERY_PARSE_START(session->getQueryString()->c_str());
 
1725
  DRIZZLE_QUERY_PARSE_START(session->query.c_str());
1700
1726
 
1701
1727
  /* Set Lex_input_stream. */
1702
1728
 
1704
1730
 
1705
1731
  /* Parse the query. */
1706
1732
 
1707
 
  bool parse_status= base_sql_parse(session) != 0;
 
1733
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
1708
1734
 
1709
1735
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1710
1736
 
1711
 
  assert(!parse_status || session->is_error());
 
1737
  assert(!mysql_parse_status || session->is_error());
1712
1738
 
1713
1739
  /* Reset Lex_input_stream. */
1714
1740
 
1715
1741
  session->m_lip= NULL;
1716
1742
 
1717
 
  DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
 
1743
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1718
1744
 
1719
1745
  /* That's it. */
1720
1746
 
1721
 
  return parse_status || session->is_fatal_error;
 
1747
  return mysql_parse_status || session->is_fatal_error;
1722
1748
}
1723
1749
 
1724
1750
/**