~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Monty Taylor
  • Date: 2010-05-12 05:00:55 UTC
  • mto: (1527.1.5 staging)
  • mto: This revision was merged to the branch mainline in revision 1529.
  • Revision ID: mordred@inaugust.com-20100512050055-i0kvg8xpr9dupz54
Wrap the libraries in if BUILD_*_PLUGIN so that they don't build when we're
disabling.

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
  lex_start(session);
765
716
  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();
 
717
 
 
718
  LEX *lex= session->lex;
 
719
 
780
720
  Lex_input_stream lip(session, inBuf, length);
 
721
 
781
722
  bool err= parse_sql(session, &lip);
 
723
 
782
724
  if (!err)
783
725
  {
784
726
    {
785
 
      if (not session->is_error())
 
727
      if (! session->is_error())
786
728
      {
787
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
 
729
        DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
788
730
                                 session->thread_id,
789
 
                                 const_cast<const char *>(session->schema()->c_str()));
790
 
        // Implement Views here --Brian
 
731
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
791
732
        /* 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
 
        }
 
733
        mysql_execute_command(session);
802
734
        DRIZZLE_QUERY_EXEC_DONE(0);
803
735
      }
804
736
    }
807
739
  {
808
740
    assert(session->is_error());
809
741
  }
 
742
 
810
743
  lex->unit.cleanup();
811
744
  session->set_proc_info("freeing items");
812
745
  session->end_statement();
813
746
  session->cleanup_after_query();
814
 
  session->set_end_timer();
 
747
 
 
748
  return;
815
749
}
816
750
 
817
751
 
833
767
                       List<String> *interval_list, const CHARSET_INFO * const cs)
834
768
{
835
769
  register CreateField *new_field;
836
 
  LEX  *lex= session->getLex();
 
770
  LEX  *lex= session->lex;
837
771
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
838
772
 
839
773
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
847
781
                      &default_key_create_info,
848
782
                      0, lex->col_list);
849
783
    statement->alter_info.key_list.push_back(key);
850
 
    lex->col_list.clear();
 
784
    lex->col_list.empty();
851
785
  }
852
786
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
853
787
  {
857
791
                 &default_key_create_info, 0,
858
792
                 lex->col_list);
859
793
    statement->alter_info.key_list.push_back(key);
860
 
    lex->col_list.clear();
 
794
    lex->col_list.empty();
861
795
  }
862
796
 
863
797
  if (default_value)
871
805
    */
872
806
    if (default_value->type() == Item::FUNC_ITEM &&
873
807
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
874
 
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
 
808
         type == DRIZZLE_TYPE_TIMESTAMP))
875
809
    {
876
810
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
877
811
      return true;
879
813
    else if (default_value->type() == Item::NULL_ITEM)
880
814
    {
881
815
      default_value= 0;
882
 
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
 
816
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
 
817
          NOT_NULL_FLAG)
883
818
      {
884
819
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
885
820
        return true;
892
827
    }
893
828
  }
894
829
 
895
 
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
 
830
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
896
831
  {
897
832
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
898
833
    return true;
911
846
}
912
847
 
913
848
 
 
849
/** Store position for column in ALTER TABLE .. ADD column. */
 
850
 
 
851
void store_position_for_column(const char *name)
 
852
{
 
853
  current_session->lex->last_field->after=const_cast<char*> (name);
 
854
}
 
855
 
914
856
/**
915
857
  Add a table to list of used tables.
916
858
 
931
873
*/
932
874
 
933
875
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)
 
876
                                             Table_ident *table,
 
877
                                             LEX_STRING *alias,
 
878
                                             uint32_t table_options,
 
879
                                             thr_lock_type lock_type,
 
880
                                             List<Index_hint> *index_hints_arg,
 
881
                                             LEX_STRING *option)
940
882
{
941
 
  TableList *ptr;
 
883
  register TableList *ptr;
942
884
  TableList *previous_table_ref; /* The table preceding the current one. */
943
885
  char *alias_str;
944
 
  LEX *lex= session->getLex();
 
886
  LEX *lex= session->lex;
945
887
 
946
888
  if (!table)
947
889
    return NULL;                                // End of memory
948
890
  alias_str= alias ? alias->str : table->table.str;
949
 
  if (! table_options.test(TL_OPTION_ALIAS) &&
 
891
  if (!test(table_options & TL_OPTION_ALIAS) &&
950
892
      check_table_name(table->table.str, table->table.length))
951
893
  {
952
894
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
957
899
  {
958
900
    my_casedn_str(files_charset_info, table->db.str);
959
901
 
960
 
    identifier::Schema schema_identifier(string(table->db.str));
961
 
    if (not schema::check(*session, schema_identifier))
 
902
    SchemaIdentifier schema_identifier(string(table->db.str));
 
903
    if (not check_db_name(schema_identifier))
962
904
    {
963
905
 
964
906
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
974
916
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
975
917
      return NULL;
976
918
    }
977
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
 
919
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
978
920
      return NULL;
979
921
  }
980
922
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
981
923
    return NULL;
982
 
 
983
924
  if (table->db.str)
984
925
  {
985
 
    ptr->setIsFqtn(true);
986
 
    ptr->setSchemaName(table->db.str);
 
926
    ptr->is_fqtn= true;
 
927
    ptr->db= table->db.str;
987
928
    ptr->db_length= table->db.length;
988
929
  }
989
 
  else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
 
930
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
990
931
    return NULL;
991
932
  else
992
 
    ptr->setIsFqtn(false);
 
933
    ptr->is_fqtn= false;
993
934
 
994
935
  ptr->alias= alias_str;
995
 
  ptr->setIsAlias(alias ? true : false);
996
 
  ptr->setTableName(table->table.str);
 
936
  ptr->is_alias= alias ? true : false;
 
937
  if (table->table.length)
 
938
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
 
939
  ptr->table_name=table->table.str;
997
940
  ptr->table_name_length=table->table.length;
998
941
  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);
 
942
  ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
 
943
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
1001
944
  ptr->derived=     table->sel;
1002
945
  ptr->select_lex=  lex->current_select;
1003
946
  ptr->index_hints= index_hints_arg;
1010
953
         tables ;
1011
954
         tables=tables->next_local)
1012
955
    {
1013
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1014
 
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
 
956
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
 
957
          !strcasecmp(ptr->db, tables->db))
1015
958
      {
1016
959
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1017
960
        return NULL;
1075
1018
bool Select_Lex::init_nested_join(Session *session)
1076
1019
{
1077
1020
  TableList *ptr;
1078
 
  NestedJoin *nested_join;
 
1021
  nested_join_st *nested_join;
1079
1022
 
1080
1023
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1081
 
                                       sizeof(NestedJoin))))
 
1024
                                       sizeof(nested_join_st))))
1082
1025
    return true;
1083
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1084
 
  nested_join= ptr->getNestedJoin();
 
1026
  nested_join= ptr->nested_join=
 
1027
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
1028
 
1085
1029
  join_list->push_front(ptr);
1086
 
  ptr->setEmbedding(embedding);
1087
 
  ptr->setJoinList(join_list);
 
1030
  ptr->embedding= embedding;
 
1031
  ptr->join_list= join_list;
1088
1032
  ptr->alias= (char*) "(nested_join)";
1089
1033
  embedding= ptr;
1090
1034
  join_list= &nested_join->join_list;
1091
 
  join_list->clear();
 
1035
  join_list->empty();
1092
1036
  return false;
1093
1037
}
1094
1038
 
1110
1054
TableList *Select_Lex::end_nested_join(Session *)
1111
1055
{
1112
1056
  TableList *ptr;
1113
 
  NestedJoin *nested_join;
 
1057
  nested_join_st *nested_join;
1114
1058
 
1115
1059
  assert(embedding);
1116
1060
  ptr= embedding;
1117
 
  join_list= ptr->getJoinList();
1118
 
  embedding= ptr->getEmbedding();
1119
 
  nested_join= ptr->getNestedJoin();
 
1061
  join_list= ptr->join_list;
 
1062
  embedding= ptr->embedding;
 
1063
  nested_join= ptr->nested_join;
1120
1064
  if (nested_join->join_list.elements == 1)
1121
1065
  {
1122
1066
    TableList *embedded= nested_join->join_list.head();
1123
1067
    join_list->pop();
1124
 
    embedded->setJoinList(join_list);
1125
 
    embedded->setEmbedding(embedding);
 
1068
    embedded->join_list= join_list;
 
1069
    embedded->embedding= embedding;
1126
1070
    join_list->push_front(embedded);
1127
1071
    ptr= embedded;
1128
1072
  }
1151
1095
TableList *Select_Lex::nest_last_join(Session *session)
1152
1096
{
1153
1097
  TableList *ptr;
1154
 
  NestedJoin *nested_join;
 
1098
  nested_join_st *nested_join;
1155
1099
  List<TableList> *embedded_list;
1156
1100
 
1157
1101
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1158
 
                                          sizeof(NestedJoin))))
 
1102
                                       sizeof(nested_join_st))))
1159
1103
    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);
 
1104
  nested_join= ptr->nested_join=
 
1105
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
1106
 
 
1107
  ptr->embedding= embedding;
 
1108
  ptr->join_list= join_list;
1164
1109
  ptr->alias= (char*) "(nest_last_join)";
1165
1110
  embedded_list= &nested_join->join_list;
1166
 
  embedded_list->clear();
 
1111
  embedded_list->empty();
1167
1112
 
1168
1113
  for (uint32_t i=0; i < 2; i++)
1169
1114
  {
1170
1115
    TableList *table= join_list->pop();
1171
 
    table->setJoinList(embedded_list);
1172
 
    table->setEmbedding(ptr);
 
1116
    table->join_list= embedded_list;
 
1117
    table->embedding= ptr;
1173
1118
    embedded_list->push_back(table);
1174
1119
    if (table->natural_join)
1175
1120
    {
1205
1150
void Select_Lex::add_joined_table(TableList *table)
1206
1151
{
1207
1152
  join_list->push_front(table);
1208
 
  table->setJoinList(join_list);
1209
 
  table->setEmbedding(embedding);
 
1153
  table->join_list= join_list;
 
1154
  table->embedding= embedding;
1210
1155
}
1211
1156
 
1212
1157
 
1311
1256
  fake_select_lex->include_standalone(this,
1312
1257
                                      (Select_Lex_Node**)&fake_select_lex);
1313
1258
  fake_select_lex->select_number= INT_MAX;
1314
 
  fake_select_lex->parent_lex= session_arg->getLex(); /* Used in init_query. */
 
1259
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1315
1260
  fake_select_lex->make_empty_select();
1316
1261
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1317
1262
  fake_select_lex->select_limit= 0;
1331
1276
    */
1332
1277
    global_parameters= fake_select_lex;
1333
1278
    fake_select_lex->no_table_names_allowed= 1;
1334
 
    session_arg->getLex()->current_select= fake_select_lex;
 
1279
    session_arg->lex->current_select= fake_select_lex;
1335
1280
  }
1336
 
  session_arg->getLex()->pop_context();
 
1281
  session_arg->lex->pop_context();
1337
1282
  return(0);
1338
1283
}
1339
1284
 
1369
1314
    left_op->first_leaf_for_name_resolution();
1370
1315
  on_context->last_name_resolution_table=
1371
1316
    right_op->last_leaf_for_name_resolution();
1372
 
  return session->getLex()->push_context(on_context);
 
1317
  return session->lex->push_context(on_context);
1373
1318
}
1374
1319
 
1375
1320
 
1449
1394
 
1450
1395
 
1451
1396
/**
 
1397
  kill on thread.
 
1398
 
 
1399
  @param session                        Thread class
 
1400
  @param id                     Thread id
 
1401
  @param only_kill_query        Should it kill the query or the connection
 
1402
 
 
1403
  @note
 
1404
    This is written such that we have a short lock on LOCK_thread_count
 
1405
*/
 
1406
 
 
1407
static unsigned int
 
1408
kill_one_thread(Session *, ulong id, bool only_kill_query)
 
1409
{
 
1410
  Session *tmp= NULL;
 
1411
  uint32_t error= ER_NO_SUCH_THREAD;
 
1412
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
1413
  
 
1414
  for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
 
1415
  {
 
1416
    if ((*it)->thread_id == id)
 
1417
    {
 
1418
      tmp= *it;
 
1419
      pthread_mutex_lock(&tmp->LOCK_delete);    // Lock from delete
 
1420
      break;
 
1421
    }
 
1422
  }
 
1423
  pthread_mutex_unlock(&LOCK_thread_count);
 
1424
  if (tmp)
 
1425
  {
 
1426
 
 
1427
    if (tmp->isViewable())
 
1428
    {
 
1429
      tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
 
1430
      error= 0;
 
1431
    }
 
1432
 
 
1433
    pthread_mutex_unlock(&tmp->LOCK_delete);
 
1434
  }
 
1435
  return(error);
 
1436
}
 
1437
 
 
1438
 
 
1439
/*
 
1440
  kills a thread and sends response
 
1441
 
 
1442
  SYNOPSIS
 
1443
    sql_kill()
 
1444
    session                     Thread class
 
1445
    id                  Thread id
 
1446
    only_kill_query     Should it kill the query or the connection
 
1447
*/
 
1448
 
 
1449
void sql_kill(Session *session, ulong id, bool only_kill_query)
 
1450
{
 
1451
  uint32_t error;
 
1452
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
1453
    session->my_ok();
 
1454
  else
 
1455
    my_error(error, MYF(0), id);
 
1456
}
 
1457
 
 
1458
 
 
1459
/**
1452
1460
  Check if the select is a simple select (not an union).
1453
1461
 
1454
1462
  @retval
1457
1465
    1   error   ; In this case the error messege is sent to the client
1458
1466
*/
1459
1467
 
1460
 
bool check_simple_select(Session::pointer session)
 
1468
bool check_simple_select()
1461
1469
{
1462
 
  if (session->getLex()->current_select != &session->getLex()->select_lex)
 
1470
  Session *session= current_session;
 
1471
  LEX *lex= session->lex;
 
1472
  if (lex->current_select != &lex->select_lex)
1463
1473
  {
1464
1474
    char command[80];
1465
1475
    Lex_input_stream *lip= session->m_lip;
1519
1529
bool update_precheck(Session *session, TableList *)
1520
1530
{
1521
1531
  const char *msg= 0;
1522
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
1532
  LEX *lex= session->lex;
 
1533
  Select_Lex *select_lex= &lex->select_lex;
1523
1534
 
1524
 
  if (session->getLex()->select_lex.item_list.elements != session->getLex()->value_list.elements)
 
1535
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1525
1536
  {
1526
1537
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1527
1538
    return(true);
1528
1539
  }
1529
1540
 
1530
 
  if (session->getLex()->select_lex.table_list.elements > 1)
 
1541
  if (session->lex->select_lex.table_list.elements > 1)
1531
1542
  {
1532
1543
    if (select_lex->order_list.elements)
1533
1544
      msg= "ORDER BY";
1557
1568
 
1558
1569
bool insert_precheck(Session *session, TableList *)
1559
1570
{
 
1571
  LEX *lex= session->lex;
 
1572
 
1560
1573
  /*
1561
1574
    Check that we have modify privileges for the first table and
1562
1575
    select privileges for the rest
1563
1576
  */
1564
 
  if (session->getLex()->update_list.elements != session->getLex()->value_list.elements)
 
1577
  if (lex->update_list.elements != lex->value_list.elements)
1565
1578
  {
1566
1579
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1567
1580
    return(true);
1571
1584
 
1572
1585
 
1573
1586
/**
 
1587
  CREATE TABLE query pre-check.
 
1588
 
 
1589
  @param session                        Thread handler
 
1590
  @param tables         Global table list
 
1591
  @param create_table           Table which will be created
 
1592
 
 
1593
  @retval
 
1594
    false   OK
 
1595
  @retval
 
1596
    true   Error
 
1597
*/
 
1598
 
 
1599
bool create_table_precheck(TableIdentifier &identifier)
 
1600
{
 
1601
  if (not plugin::StorageEngine::canCreateTable(identifier))
 
1602
  {
 
1603
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
 
1604
    return true;
 
1605
  }
 
1606
 
 
1607
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
1608
  {
 
1609
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
 
1610
    return true;
 
1611
  }
 
1612
 
 
1613
  return false;
 
1614
}
 
1615
 
 
1616
 
 
1617
/**
1574
1618
  negate given expression.
1575
1619
 
1576
1620
  @param session  thread handler
1588
1632
  {
1589
1633
    /* it is NOT(NOT( ... )) */
1590
1634
    Item *arg= ((Item_func *) expr)->arguments()[0];
1591
 
    enum_parsing_place place= session->getLex()->current_select->parsing_place;
 
1635
    enum_parsing_place place= session->lex->current_select->parsing_place;
1592
1636
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1593
1637
      return arg;
1594
1638
    /*
1637
1681
}
1638
1682
 
1639
1683
 
1640
 
bool check_identifier_name(LEX_STRING *str, error_t err_code,
 
1684
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
1641
1685
                           uint32_t max_char_length,
1642
1686
                           const char *param_for_err_msg)
1643
1687
{
1663
1707
 
1664
1708
  switch (err_code)
1665
1709
  {
1666
 
  case EE_OK:
 
1710
  case 0:
1667
1711
    break;
1668
1712
  case ER_WRONG_STRING_LENGTH:
1669
1713
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1675
1719
    assert(0);
1676
1720
    break;
1677
1721
  }
1678
 
 
1679
1722
  return true;
1680
1723
}
1681
1724
 
1696
1739
{
1697
1740
  assert(session->m_lip == NULL);
1698
1741
 
1699
 
  DRIZZLE_QUERY_PARSE_START(session->getQueryString()->c_str());
 
1742
  DRIZZLE_QUERY_PARSE_START(session->query.c_str());
1700
1743
 
1701
1744
  /* Set Lex_input_stream. */
1702
1745
 
1704
1747
 
1705
1748
  /* Parse the query. */
1706
1749
 
1707
 
  bool parse_status= base_sql_parse(session) != 0;
 
1750
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
1708
1751
 
1709
1752
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1710
1753
 
1711
 
  assert(!parse_status || session->is_error());
 
1754
  assert(!mysql_parse_status || session->is_error());
1712
1755
 
1713
1756
  /* Reset Lex_input_stream. */
1714
1757
 
1715
1758
  session->m_lip= NULL;
1716
1759
 
1717
 
  DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
 
1760
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1718
1761
 
1719
1762
  /* That's it. */
1720
1763
 
1721
 
  return parse_status || session->is_fatal_error;
 
1764
  return mysql_parse_status || session->is_fatal_error;
1722
1765
}
1723
1766
 
1724
1767
/**