~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Brian Aker
  • Date: 2010-03-15 21:50:05 UTC
  • mto: This revision was merged to the branch mainline in revision 1343.
  • Revision ID: brian@gaz-20100315215005-oqoblpbll9n0albj
Merge of table cache/def DD.

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
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>
34
31
#include <drizzled/item/cmpfunc.h>
35
32
#include <drizzled/item/null.h>
36
33
#include <drizzled/session.h>
37
 
#include <drizzled/session/cache.h>
38
34
#include <drizzled/sql_load.h>
39
35
#include <drizzled/lock.h>
40
36
#include <drizzled/select_send.h>
42
38
#include <drizzled/statement.h>
43
39
#include <drizzled/statement/alter_table.h>
44
40
#include "drizzled/probes.h"
 
41
#include "drizzled/session_list.h"
45
42
#include "drizzled/global_charset_info.h"
 
43
#include "drizzled/transaction_services.h"
46
44
 
47
45
#include "drizzled/plugin/logging.h"
48
46
#include "drizzled/plugin/query_rewrite.h"
49
 
#include "drizzled/plugin/query_cache.h"
50
47
#include "drizzled/plugin/authorization.h"
51
48
#include "drizzled/optimizer/explain_plan.h"
52
49
#include "drizzled/pthread_globals.h"
53
 
#include "drizzled/plugin/event_observer.h"
54
 
#include "drizzled/visibility.h"
55
50
 
56
51
#include <limits.h>
57
52
 
58
53
#include <bitset>
59
54
#include <algorithm>
60
 
#include <boost/date_time.hpp>
 
55
 
61
56
#include "drizzled/internal/my_sys.h"
62
57
 
63
58
using namespace std;
70
65
/* Prototypes */
71
66
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
72
67
static bool parse_sql(Session *session, Lex_input_stream *lip);
73
 
void parse(Session *session, const char *inBuf, uint32_t length);
 
68
void mysql_parse(Session *session, const char *inBuf, uint32_t length);
74
69
 
75
70
/**
76
71
  @defgroup Runtime_Environment Runtime Environment
80
75
extern size_t my_thread_stack_size;
81
76
extern const CHARSET_INFO *character_set_filesystem;
82
77
 
83
 
namespace
84
 
{
85
 
 
86
 
static const std::string command_name[COM_END+1]={
87
 
  "Sleep",
88
 
  "Quit",
89
 
  "Init DB",
90
 
  "Query",
91
 
  "Shutdown",
92
 
  "Connect",
93
 
  "Ping",
94
 
  "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
95
87
};
96
88
 
97
 
}
98
 
 
99
89
const char *xa_state_names[]={
100
90
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
101
91
};
114
104
*/
115
105
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
116
106
 
117
 
const std::string &getCommandName(const enum_server_command& command)
118
 
{
119
 
  return command_name[command];
120
 
}
121
 
 
122
107
void init_update_queries(void)
123
108
{
124
109
  uint32_t x;
183
168
  bool error= 0;
184
169
  Query_id &query_id= Query_id::get_query_id();
185
170
 
186
 
  DRIZZLE_COMMAND_START(session->thread_id, command);
 
171
  DRIZZLE_COMMAND_START(session->thread_id,
 
172
                        command);
187
173
 
188
174
  session->command= command;
189
175
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
196
182
    break;
197
183
  /* Increase id and count all other statements. */
198
184
  default:
199
 
    session->status_var.questions++;
 
185
    statistic_increment(session->status_var.questions, &LOCK_status);
200
186
    query_id.next();
201
187
  }
202
188
 
203
 
  /* @todo set session->lex->sql_command to SQLCOM_END here */
 
189
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
204
190
 
205
191
  plugin::Logging::preDo(session);
206
 
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
207
 
  {
208
 
    // We should do something about an error...
209
 
  }
210
192
 
211
193
  session->server_status&=
212
194
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
213
195
  switch (command) {
214
196
  case COM_INIT_DB:
215
197
  {
216
 
    if (packet_length == 0)
217
 
    {
218
 
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
219
 
      break;
220
 
    }
221
 
 
 
198
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
222
199
    string tmp(packet, packet_length);
223
200
 
224
 
    identifier::Schema identifier(tmp);
225
 
 
226
 
    if (not change_db(session, identifier))
 
201
    if (not mysql_change_db(session, tmp))
227
202
    {
228
203
      session->my_ok();
229
204
    }
231
206
  }
232
207
  case COM_QUERY:
233
208
  {
234
 
    if (not session->readAndStoreQuery(packet, packet_length))
 
209
    if (! session->readAndStoreQuery(packet, packet_length))
235
210
      break;                                    // fatal error is set
236
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
 
211
    DRIZZLE_QUERY_START(session->query.c_str(),
237
212
                        session->thread_id,
238
 
                        const_cast<const char *>(session->schema()->c_str()));
 
213
                        const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
239
214
 
240
 
    parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
215
    plugin::QueryRewriter::rewriteQuery(session->db, session->query);
 
216
    mysql_parse(session, session->query.c_str(), session->query.length());
241
217
 
242
218
    break;
243
219
  }
248
224
    break;
249
225
  case COM_SHUTDOWN:
250
226
  {
251
 
    session->status_var.com_other++;
 
227
    status_var_increment(session->status_var.com_other);
252
228
    session->my_eof();
253
229
    session->close_thread_tables();                     // Free before kill
254
230
    kill_drizzle();
256
232
    break;
257
233
  }
258
234
  case COM_PING:
259
 
    session->status_var.com_other++;
 
235
    status_var_increment(session->status_var.com_other);
260
236
    session->my_ok();                           // Tell client we are alive
261
237
    break;
262
238
  case COM_SLEEP:
270
246
  /* If commit fails, we should be able to reset the OK status. */
271
247
  session->main_da.can_overwrite_status= true;
272
248
  TransactionServices &transaction_services= TransactionServices::singleton();
273
 
  transaction_services.autocommitOrRollback(*session, session->is_error());
 
249
  transaction_services.ha_autocommit_or_rollback(session, session->is_error());
274
250
  session->main_da.can_overwrite_status= false;
275
251
 
276
252
  session->transaction.stmt.reset();
282
258
    if (! session->main_da.is_set())
283
259
      session->send_kill_message();
284
260
  }
285
 
  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)
286
262
  {
287
 
    session->setKilled(Session::NOT_KILLED);
288
 
    session->setAbort(false);
 
263
    session->killed= Session::NOT_KILLED;
 
264
    session->mysys_var->abort= 0;
289
265
  }
290
266
 
291
267
  /* Can not be true, but do not take chances in production. */
295
271
  {
296
272
  case Diagnostics_area::DA_ERROR:
297
273
    /* The query failed, send error to log and abort bootstrap. */
298
 
    session->getClient()->sendError(session->main_da.sql_errno(),
 
274
    session->client->sendError(session->main_da.sql_errno(),
299
275
                               session->main_da.message());
300
276
    break;
301
277
 
302
278
  case Diagnostics_area::DA_EOF:
303
 
    session->getClient()->sendEOF();
 
279
    session->client->sendEOF();
304
280
    break;
305
281
 
306
282
  case Diagnostics_area::DA_OK:
307
 
    session->getClient()->sendOK();
 
283
    session->client->sendOK();
308
284
    break;
309
285
 
310
286
  case Diagnostics_area::DA_DISABLED:
312
288
 
313
289
  case Diagnostics_area::DA_EMPTY:
314
290
  default:
315
 
    session->getClient()->sendOK();
 
291
    session->client->sendOK();
316
292
    break;
317
293
  }
318
294
 
323
299
  session->close_thread_tables();
324
300
 
325
301
  plugin::Logging::postDo(session);
326
 
  if (unlikely(plugin::EventObserver::afterStatement(*session)))
327
 
  {
328
 
    // We should do something about an error...
329
 
  }
330
302
 
331
303
  /* Store temp state for processlist */
332
304
  session->set_proc_info("cleaning up");
333
305
  session->command= COM_SLEEP;
334
 
  session->resetQueryString();
 
306
  memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
 
307
  session->query.clear();
335
308
 
336
309
  session->set_proc_info(NULL);
337
 
  session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
 
310
  free_root(session->mem_root,MYF(memory::KEEP_PREALLOC));
338
311
 
339
312
  if (DRIZZLE_QUERY_DONE_ENABLED() || DRIZZLE_COMMAND_DONE_ENABLED())
340
313
  {
378
351
                           const string& schema_table_name)
379
352
{
380
353
  LEX_STRING db, table;
381
 
  bitset<NUM_OF_TABLE_OPTIONS> table_options;
382
354
  /*
383
355
     We have to make non const db_name & table_name
384
356
     because of lower_case_table_names
387
359
  session->make_lex_string(&table, schema_table_name, false);
388
360
 
389
361
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
390
 
                               NULL, table_options, TL_READ))
 
362
                               NULL, 0, TL_READ))
391
363
  {
392
364
    return true;
393
365
  }
442
414
    true        Error
443
415
*/
444
416
 
445
 
static int execute_command(Session *session)
 
417
static int
 
418
mysql_execute_command(Session *session)
446
419
{
447
420
  bool res= false;
448
421
  LEX  *lex= session->lex;
450
423
  Select_Lex *select_lex= &lex->select_lex;
451
424
  /* list of all tables in query */
452
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';
453
432
 
454
433
  /*
455
434
    In many cases first table of main Select_Lex have special meaning =>
486
465
    drizzle_reset_errors(session, 0);
487
466
  }
488
467
 
 
468
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
 
469
 
489
470
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
490
471
 
491
 
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
492
 
      && ! session->inTransaction()
493
 
      && lex->statement->isTransactional())
494
 
  {
495
 
    if (session->startTransaction() == false)
496
 
    {
497
 
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
498
 
      return true;
499
 
    }
500
 
  }
501
 
 
502
472
  /* now we are ready to execute the statement */
503
473
  res= lex->statement->execute();
 
474
 
504
475
  session->set_proc_info("query end");
 
476
 
505
477
  /*
506
478
    The return value for ROW_COUNT() is "implementation dependent" if the
507
479
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
515
487
 
516
488
  return (res || session->is_error());
517
489
}
 
490
 
518
491
bool execute_sqlcom_select(Session *session, TableList *all_tables)
519
492
{
520
493
  LEX   *lex= session->lex;
527
500
      param->select_limit=
528
501
        new Item_int((uint64_t) session->variables.select_limit);
529
502
  }
530
 
 
531
 
  if (all_tables
532
 
      && ! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
533
 
      && ! session->inTransaction()
534
 
      && ! lex->statement->isShow())
535
 
  {
536
 
    if (session->startTransaction() == false)
537
 
    {
538
 
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
539
 
      return true;
540
 
    }
541
 
  }
542
 
 
543
 
  if (not (res= session->openTablesLock(all_tables)))
 
503
  if (!(res= session->openTablesLock(all_tables)))
544
504
  {
545
505
    if (lex->describe)
546
506
    {
575
535
    {
576
536
      if (!result && !(result= new select_send()))
577
537
        return true;
578
 
 
579
 
      /* Init the Query Cache plugin */
580
 
      plugin::QueryCache::prepareResultset(session); 
581
538
      res= handle_select(session, lex, result, 0);
582
 
      /* Send the Resultset to the cache */
583
 
      plugin::QueryCache::setResultset(session); 
584
 
 
585
539
      if (result != lex->result)
586
540
        delete result;
587
541
    }
624
578
 
625
579
 
626
580
void
627
 
init_select(LEX *lex)
 
581
mysql_init_select(LEX *lex)
628
582
{
629
583
  Select_Lex *select_lex= lex->current_select;
630
584
  select_lex->init_select();
638
592
 
639
593
 
640
594
bool
641
 
new_select(LEX *lex, bool move_down)
 
595
mysql_new_select(LEX *lex, bool move_down)
642
596
{
643
597
  Select_Lex *select_lex;
644
598
  Session *session= lex->session;
645
599
 
646
600
  if (!(select_lex= new (session->mem_root) Select_Lex()))
647
 
    return true;
648
 
 
 
601
    return(1);
649
602
  select_lex->select_number= ++session->select_number;
650
603
  select_lex->parent_lex= lex; /* Used in init_query. */
651
604
  select_lex->init_query();
652
605
  select_lex->init_select();
653
606
  lex->nest_level++;
654
 
 
655
607
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
656
608
  {
657
609
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
658
610
    return(1);
659
611
  }
660
 
 
661
612
  select_lex->nest_level= lex->nest_level;
662
613
  if (move_down)
663
614
  {
685
636
    if (lex->current_select->order_list.first && !lex->current_select->braces)
686
637
    {
687
638
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
688
 
      return true;
 
639
      return(1);
689
640
    }
690
 
 
691
641
    select_lex->include_neighbour(lex->current_select);
692
642
    Select_Lex_Unit *unit= select_lex->master_unit();
693
 
 
694
 
    if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
695
 
      return true;
696
 
 
 
643
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
644
      return(1);
697
645
    select_lex->context.outer_context=
698
646
                unit->first_select()->context.outer_context;
699
647
  }
706
654
    list
707
655
  */
708
656
  select_lex->context.resolve_in_select_list= true;
709
 
 
710
 
  return false;
 
657
  return(0);
711
658
}
712
659
 
713
660
/**
720
667
  @param var_name               Variable name
721
668
*/
722
669
 
723
 
void create_select_for_variable(Session *session, const char *var_name)
 
670
void create_select_for_variable(const char *var_name)
724
671
{
 
672
  Session *session;
725
673
  LEX *lex;
726
674
  LEX_STRING tmp, null_lex_string;
727
675
  Item *var;
728
676
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
729
677
  char *end= buff;
730
678
 
 
679
  session= current_session;
731
680
  lex= session->lex;
732
 
  init_select(lex);
 
681
  mysql_init_select(lex);
733
682
  lex->sql_command= SQLCOM_SELECT;
734
683
  tmp.str= (char*) var_name;
735
684
  tmp.length=strlen(var_name);
740
689
  */
741
690
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
742
691
  {
743
 
    end+= snprintf(buff, sizeof(buff), "@@session.%s", var_name);
 
692
    end+= sprintf(buff, "@@session.%s", var_name);
744
693
    var->set_name(buff, end-buff, system_charset_info);
745
694
    session->add_item_to_list(var);
746
695
  }
 
696
  return;
747
697
}
748
698
 
749
699
 
755
705
  @param       length  Length of the query text
756
706
*/
757
707
 
758
 
void parse(Session *session, const char *inBuf, uint32_t length)
 
708
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
759
709
{
760
 
  session->lex->start(session);
761
 
 
 
710
  lex_start(session);
762
711
  session->reset_for_next_command();
763
 
  /* Check if the Query is Cached if and return true if yes
764
 
   * TODO the plugin has to make sure that the query is cacheble
765
 
   * by setting the query_safe_cache param to TRUE
766
 
   */
767
 
  bool res= true;
768
 
  if (plugin::QueryCache::isCached(session))
769
 
  {
770
 
    res= plugin::QueryCache::sendCachedResultset(session);
771
 
  }
772
 
  if (not res)
773
 
  {
774
 
    return;
775
 
  }
 
712
 
776
713
  LEX *lex= session->lex;
 
714
 
777
715
  Lex_input_stream lip(session, inBuf, length);
 
716
 
778
717
  bool err= parse_sql(session, &lip);
 
718
 
779
719
  if (!err)
780
720
  {
781
721
    {
782
 
      if (not session->is_error())
 
722
      if (! session->is_error())
783
723
      {
784
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
 
724
        DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
785
725
                                 session->thread_id,
786
 
                                 const_cast<const char *>(session->schema()->c_str()));
787
 
        // Implement Views here --Brian
 
726
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
788
727
        /* Actually execute the query */
789
 
        try 
790
 
        {
791
 
          execute_command(session);
792
 
        }
793
 
        catch (...)
794
 
        {
795
 
          // Just try to catch any random failures that could have come
796
 
          // during execution.
797
 
          DRIZZLE_ABORT;
798
 
        }
 
728
        mysql_execute_command(session);
799
729
        DRIZZLE_QUERY_EXEC_DONE(0);
800
730
      }
801
731
    }
804
734
  {
805
735
    assert(session->is_error());
806
736
  }
 
737
 
807
738
  lex->unit.cleanup();
808
739
  session->set_proc_info("freeing items");
809
740
  session->end_statement();
810
741
  session->cleanup_after_query();
811
 
  session->set_end_timer();
 
742
 
 
743
  return;
812
744
}
813
745
 
814
746
 
868
800
    */
869
801
    if (default_value->type() == Item::FUNC_ITEM &&
870
802
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
871
 
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
 
803
         type == DRIZZLE_TYPE_TIMESTAMP))
872
804
    {
873
805
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
874
806
      return true;
876
808
    else if (default_value->type() == Item::NULL_ITEM)
877
809
    {
878
810
      default_value= 0;
879
 
      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)
880
813
      {
881
814
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
882
815
        return true;
889
822
    }
890
823
  }
891
824
 
892
 
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
 
825
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
893
826
  {
894
827
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
895
828
    return true;
908
841
}
909
842
 
910
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
 
911
851
/**
912
852
  Add a table to list of used tables.
913
853
 
928
868
*/
929
869
 
930
870
TableList *Select_Lex::add_table_to_list(Session *session,
931
 
                                         Table_ident *table,
932
 
                                         LEX_STRING *alias,
933
 
                                         const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
934
 
                                         thr_lock_type lock_type,
935
 
                                         List<Index_hint> *index_hints_arg,
936
 
                                         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)
937
877
{
938
 
  TableList *ptr;
 
878
  register TableList *ptr;
939
879
  TableList *previous_table_ref; /* The table preceding the current one. */
940
880
  char *alias_str;
941
881
  LEX *lex= session->lex;
943
883
  if (!table)
944
884
    return NULL;                                // End of memory
945
885
  alias_str= alias ? alias->str : table->table.str;
946
 
  if (! table_options.test(TL_OPTION_ALIAS) &&
 
886
  if (!test(table_options & TL_OPTION_ALIAS) &&
947
887
      check_table_name(table->table.str, table->table.length))
948
888
  {
949
889
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
950
890
    return NULL;
951
891
  }
952
892
 
953
 
  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))
954
895
  {
955
 
    my_casedn_str(files_charset_info, table->db.str);
956
 
 
957
 
    identifier::Schema schema_identifier(string(table->db.str));
958
 
    if (not check_db_name(session, schema_identifier))
959
 
    {
960
 
 
961
 
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
962
 
      return NULL;
963
 
    }
 
896
    my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
 
897
    return NULL;
964
898
  }
965
899
 
966
900
  if (!alias)                                   /* Alias is case sensitive */
971
905
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
972
906
      return NULL;
973
907
    }
974
 
    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)))
975
909
      return NULL;
976
910
  }
977
911
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
978
912
    return NULL;
979
 
 
980
913
  if (table->db.str)
981
914
  {
982
 
    ptr->setIsFqtn(true);
983
 
    ptr->setSchemaName(table->db.str);
 
915
    ptr->is_fqtn= true;
 
916
    ptr->db= table->db.str;
984
917
    ptr->db_length= table->db.length;
985
918
  }
986
 
  else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
 
919
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
987
920
    return NULL;
988
921
  else
989
 
    ptr->setIsFqtn(false);
 
922
    ptr->is_fqtn= false;
990
923
 
991
924
  ptr->alias= alias_str;
992
 
  ptr->setIsAlias(alias ? true : false);
993
 
  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;
994
929
  ptr->table_name_length=table->table.length;
995
930
  ptr->lock_type=   lock_type;
996
 
  ptr->force_index= table_options.test(TL_OPTION_FORCE_INDEX);
997
 
  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);
998
933
  ptr->derived=     table->sel;
999
934
  ptr->select_lex=  lex->current_select;
1000
935
  ptr->index_hints= index_hints_arg;
1007
942
         tables ;
1008
943
         tables=tables->next_local)
1009
944
    {
1010
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1011
 
          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))
1012
947
      {
1013
948
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1014
949
        return NULL;
1072
1007
bool Select_Lex::init_nested_join(Session *session)
1073
1008
{
1074
1009
  TableList *ptr;
1075
 
  NestedJoin *nested_join;
 
1010
  nested_join_st *nested_join;
1076
1011
 
1077
1012
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1078
 
                                       sizeof(NestedJoin))))
 
1013
                                       sizeof(nested_join_st))))
1079
1014
    return true;
1080
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1081
 
  nested_join= ptr->getNestedJoin();
 
1015
  nested_join= ptr->nested_join=
 
1016
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
1017
 
1082
1018
  join_list->push_front(ptr);
1083
 
  ptr->setEmbedding(embedding);
1084
 
  ptr->setJoinList(join_list);
 
1019
  ptr->embedding= embedding;
 
1020
  ptr->join_list= join_list;
1085
1021
  ptr->alias= (char*) "(nested_join)";
1086
1022
  embedding= ptr;
1087
1023
  join_list= &nested_join->join_list;
1107
1043
TableList *Select_Lex::end_nested_join(Session *)
1108
1044
{
1109
1045
  TableList *ptr;
1110
 
  NestedJoin *nested_join;
 
1046
  nested_join_st *nested_join;
1111
1047
 
1112
1048
  assert(embedding);
1113
1049
  ptr= embedding;
1114
 
  join_list= ptr->getJoinList();
1115
 
  embedding= ptr->getEmbedding();
1116
 
  nested_join= ptr->getNestedJoin();
 
1050
  join_list= ptr->join_list;
 
1051
  embedding= ptr->embedding;
 
1052
  nested_join= ptr->nested_join;
1117
1053
  if (nested_join->join_list.elements == 1)
1118
1054
  {
1119
1055
    TableList *embedded= nested_join->join_list.head();
1120
1056
    join_list->pop();
1121
 
    embedded->setJoinList(join_list);
1122
 
    embedded->setEmbedding(embedding);
 
1057
    embedded->join_list= join_list;
 
1058
    embedded->embedding= embedding;
1123
1059
    join_list->push_front(embedded);
1124
1060
    ptr= embedded;
1125
1061
  }
1148
1084
TableList *Select_Lex::nest_last_join(Session *session)
1149
1085
{
1150
1086
  TableList *ptr;
1151
 
  NestedJoin *nested_join;
 
1087
  nested_join_st *nested_join;
1152
1088
  List<TableList> *embedded_list;
1153
1089
 
1154
1090
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1155
 
                                          sizeof(NestedJoin))))
 
1091
                                       sizeof(nested_join_st))))
1156
1092
    return NULL;
1157
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1158
 
  nested_join= ptr->getNestedJoin();
1159
 
  ptr->setEmbedding(embedding);
1160
 
  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;
1161
1098
  ptr->alias= (char*) "(nest_last_join)";
1162
1099
  embedded_list= &nested_join->join_list;
1163
1100
  embedded_list->empty();
1165
1102
  for (uint32_t i=0; i < 2; i++)
1166
1103
  {
1167
1104
    TableList *table= join_list->pop();
1168
 
    table->setJoinList(embedded_list);
1169
 
    table->setEmbedding(ptr);
 
1105
    table->join_list= embedded_list;
 
1106
    table->embedding= ptr;
1170
1107
    embedded_list->push_back(table);
1171
1108
    if (table->natural_join)
1172
1109
    {
1202
1139
void Select_Lex::add_joined_table(TableList *table)
1203
1140
{
1204
1141
  join_list->push_front(table);
1205
 
  table->setJoinList(join_list);
1206
 
  table->setEmbedding(embedding);
 
1142
  table->join_list= join_list;
 
1143
  table->embedding= embedding;
1207
1144
}
1208
1145
 
1209
1146
 
1446
1383
 
1447
1384
 
1448
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
/**
1449
1449
  Check if the select is a simple select (not an union).
1450
1450
 
1451
1451
  @retval
1454
1454
    1   error   ; In this case the error messege is sent to the client
1455
1455
*/
1456
1456
 
1457
 
bool check_simple_select(Session::pointer session)
 
1457
bool check_simple_select()
1458
1458
{
 
1459
  Session *session= current_session;
1459
1460
  LEX *lex= session->lex;
1460
1461
  if (lex->current_select != &lex->select_lex)
1461
1462
  {
1572
1573
 
1573
1574
 
1574
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
/**
1575
1601
  negate given expression.
1576
1602
 
1577
1603
  @param session  thread handler
1638
1664
}
1639
1665
 
1640
1666
 
1641
 
bool check_identifier_name(LEX_STRING *str, error_t err_code,
 
1667
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
1642
1668
                           uint32_t max_char_length,
1643
1669
                           const char *param_for_err_msg)
1644
1670
{
1664
1690
 
1665
1691
  switch (err_code)
1666
1692
  {
1667
 
  case EE_OK:
 
1693
  case 0:
1668
1694
    break;
1669
1695
  case ER_WRONG_STRING_LENGTH:
1670
1696
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1676
1702
    assert(0);
1677
1703
    break;
1678
1704
  }
1679
 
 
1680
1705
  return true;
1681
1706
}
1682
1707
 
1697
1722
{
1698
1723
  assert(session->m_lip == NULL);
1699
1724
 
1700
 
  DRIZZLE_QUERY_PARSE_START(session->getQueryString()->c_str());
 
1725
  DRIZZLE_QUERY_PARSE_START(session->query.c_str());
1701
1726
 
1702
1727
  /* Set Lex_input_stream. */
1703
1728
 
1705
1730
 
1706
1731
  /* Parse the query. */
1707
1732
 
1708
 
  bool parse_status= DRIZZLEparse(session) != 0;
 
1733
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
1709
1734
 
1710
1735
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1711
1736
 
1712
 
  assert(!parse_status || session->is_error());
 
1737
  assert(!mysql_parse_status || session->is_error());
1713
1738
 
1714
1739
  /* Reset Lex_input_stream. */
1715
1740
 
1716
1741
  session->m_lip= NULL;
1717
1742
 
1718
 
  DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
 
1743
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1719
1744
 
1720
1745
  /* That's it. */
1721
1746
 
1722
 
  return parse_status || session->is_fatal_error;
 
1747
  return mysql_parse_status || session->is_fatal_error;
1723
1748
}
1724
1749
 
1725
1750
/**