~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

retrieve data from the local cache

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