~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Brian Aker
  • Date: 2010-04-12 19:28:43 UTC
  • mfrom: (1455.5.1 bug561739)
  • Revision ID: brian@gaz-20100412192843-mu6gouk7pq74drts
Merge Jay

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>
34
32
#include <drizzled/item/cmpfunc.h>
35
33
#include <drizzled/item/null.h>
36
34
#include <drizzled/session.h>
37
 
#include <drizzled/session/cache.h>
38
35
#include <drizzled/sql_load.h>
39
36
#include <drizzled/lock.h>
40
37
#include <drizzled/select_send.h>
42
39
#include <drizzled/statement.h>
43
40
#include <drizzled/statement/alter_table.h>
44
41
#include "drizzled/probes.h"
 
42
#include "drizzled/session_list.h"
45
43
#include "drizzled/global_charset_info.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
  {
 
198
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
216
199
    if (packet_length == 0)
217
200
    {
218
201
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
221
204
 
222
205
    string tmp(packet, packet_length);
223
206
 
224
 
    identifier::Schema identifier(tmp);
 
207
    SchemaIdentifier identifier(tmp);
225
208
 
226
 
    if (not change_db(session, identifier))
 
209
    if (not mysql_change_db(session, identifier))
227
210
    {
228
211
      session->my_ok();
229
212
    }
231
214
  }
232
215
  case COM_QUERY:
233
216
  {
234
 
    if (not session->readAndStoreQuery(packet, packet_length))
 
217
    if (! session->readAndStoreQuery(packet, packet_length))
235
218
      break;                                    // fatal error is set
236
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
 
219
    DRIZZLE_QUERY_START(session->query.c_str(),
237
220
                        session->thread_id,
238
 
                        const_cast<const char *>(session->schema()->c_str()));
 
221
                        const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
239
222
 
240
 
    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());
241
225
 
242
226
    break;
243
227
  }
248
232
    break;
249
233
  case COM_SHUTDOWN:
250
234
  {
251
 
    session->status_var.com_other++;
 
235
    status_var_increment(session->status_var.com_other);
252
236
    session->my_eof();
253
237
    session->close_thread_tables();                     // Free before kill
254
238
    kill_drizzle();
256
240
    break;
257
241
  }
258
242
  case COM_PING:
259
 
    session->status_var.com_other++;
 
243
    status_var_increment(session->status_var.com_other);
260
244
    session->my_ok();                           // Tell client we are alive
261
245
    break;
262
246
  case COM_SLEEP:
270
254
  /* If commit fails, we should be able to reset the OK status. */
271
255
  session->main_da.can_overwrite_status= true;
272
256
  TransactionServices &transaction_services= TransactionServices::singleton();
273
 
  transaction_services.autocommitOrRollback(*session, session->is_error());
 
257
  transaction_services.autocommitOrRollback(session, session->is_error());
274
258
  session->main_da.can_overwrite_status= false;
275
259
 
276
260
  session->transaction.stmt.reset();
282
266
    if (! session->main_da.is_set())
283
267
      session->send_kill_message();
284
268
  }
285
 
  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)
286
270
  {
287
 
    session->setKilled(Session::NOT_KILLED);
288
 
    session->setAbort(false);
 
271
    session->killed= Session::NOT_KILLED;
 
272
    session->mysys_var->abort= 0;
289
273
  }
290
274
 
291
275
  /* Can not be true, but do not take chances in production. */
295
279
  {
296
280
  case Diagnostics_area::DA_ERROR:
297
281
    /* The query failed, send error to log and abort bootstrap. */
298
 
    session->getClient()->sendError(session->main_da.sql_errno(),
 
282
    session->client->sendError(session->main_da.sql_errno(),
299
283
                               session->main_da.message());
300
284
    break;
301
285
 
302
286
  case Diagnostics_area::DA_EOF:
303
 
    session->getClient()->sendEOF();
 
287
    session->client->sendEOF();
304
288
    break;
305
289
 
306
290
  case Diagnostics_area::DA_OK:
307
 
    session->getClient()->sendOK();
 
291
    session->client->sendOK();
308
292
    break;
309
293
 
310
294
  case Diagnostics_area::DA_DISABLED:
312
296
 
313
297
  case Diagnostics_area::DA_EMPTY:
314
298
  default:
315
 
    session->getClient()->sendOK();
 
299
    session->client->sendOK();
316
300
    break;
317
301
  }
318
302
 
323
307
  session->close_thread_tables();
324
308
 
325
309
  plugin::Logging::postDo(session);
326
 
  if (unlikely(plugin::EventObserver::afterStatement(*session)))
327
 
  {
328
 
    // We should do something about an error...
329
 
  }
330
310
 
331
311
  /* Store temp state for processlist */
332
312
  session->set_proc_info("cleaning up");
333
313
  session->command= COM_SLEEP;
334
 
  session->resetQueryString();
 
314
  memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
 
315
  session->query.clear();
335
316
 
336
317
  session->set_proc_info(NULL);
337
 
  session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
 
318
  free_root(session->mem_root,MYF(memory::KEEP_PREALLOC));
338
319
 
339
320
  if (DRIZZLE_QUERY_DONE_ENABLED() || DRIZZLE_COMMAND_DONE_ENABLED())
340
321
  {
378
359
                           const string& schema_table_name)
379
360
{
380
361
  LEX_STRING db, table;
381
 
  bitset<NUM_OF_TABLE_OPTIONS> table_options;
382
362
  /*
383
363
     We have to make non const db_name & table_name
384
364
     because of lower_case_table_names
387
367
  session->make_lex_string(&table, schema_table_name, false);
388
368
 
389
369
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
390
 
                               NULL, table_options, TL_READ))
 
370
                               NULL, 0, TL_READ))
391
371
  {
392
372
    return true;
393
373
  }
442
422
    true        Error
443
423
*/
444
424
 
445
 
static int execute_command(Session *session)
 
425
static int
 
426
mysql_execute_command(Session *session)
446
427
{
447
428
  bool res= false;
448
429
  LEX  *lex= session->lex;
450
431
  Select_Lex *select_lex= &lex->select_lex;
451
432
  /* list of all tables in query */
452
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';
453
440
 
454
441
  /*
455
442
    In many cases first table of main Select_Lex have special meaning =>
486
473
    drizzle_reset_errors(session, 0);
487
474
  }
488
475
 
 
476
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
 
477
 
489
478
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
490
479
 
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
480
  /* now we are ready to execute the statement */
503
481
  res= lex->statement->execute();
 
482
 
504
483
  session->set_proc_info("query end");
 
484
 
505
485
  /*
506
486
    The return value for ROW_COUNT() is "implementation dependent" if the
507
487
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
515
495
 
516
496
  return (res || session->is_error());
517
497
}
 
498
 
518
499
bool execute_sqlcom_select(Session *session, TableList *all_tables)
519
500
{
520
501
  LEX   *lex= session->lex;
527
508
      param->select_limit=
528
509
        new Item_int((uint64_t) session->variables.select_limit);
529
510
  }
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
511
  if (not (res= session->openTablesLock(all_tables)))
544
512
  {
545
513
    if (lex->describe)
575
543
    {
576
544
      if (!result && !(result= new select_send()))
577
545
        return true;
578
 
 
579
 
      /* Init the Query Cache plugin */
580
 
      plugin::QueryCache::prepareResultset(session); 
581
546
      res= handle_select(session, lex, result, 0);
582
 
      /* Send the Resultset to the cache */
583
 
      plugin::QueryCache::setResultset(session); 
584
 
 
585
547
      if (result != lex->result)
586
548
        delete result;
587
549
    }
624
586
 
625
587
 
626
588
void
627
 
init_select(LEX *lex)
 
589
mysql_init_select(LEX *lex)
628
590
{
629
591
  Select_Lex *select_lex= lex->current_select;
630
592
  select_lex->init_select();
638
600
 
639
601
 
640
602
bool
641
 
new_select(LEX *lex, bool move_down)
 
603
mysql_new_select(LEX *lex, bool move_down)
642
604
{
643
605
  Select_Lex *select_lex;
644
606
  Session *session= lex->session;
645
607
 
646
608
  if (!(select_lex= new (session->mem_root) Select_Lex()))
647
 
    return true;
648
 
 
 
609
    return(1);
649
610
  select_lex->select_number= ++session->select_number;
650
611
  select_lex->parent_lex= lex; /* Used in init_query. */
651
612
  select_lex->init_query();
652
613
  select_lex->init_select();
653
614
  lex->nest_level++;
654
 
 
655
615
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
656
616
  {
657
617
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
658
618
    return(1);
659
619
  }
660
 
 
661
620
  select_lex->nest_level= lex->nest_level;
662
621
  if (move_down)
663
622
  {
685
644
    if (lex->current_select->order_list.first && !lex->current_select->braces)
686
645
    {
687
646
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
688
 
      return true;
 
647
      return(1);
689
648
    }
690
 
 
691
649
    select_lex->include_neighbour(lex->current_select);
692
650
    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
 
 
 
651
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
652
      return(1);
697
653
    select_lex->context.outer_context=
698
654
                unit->first_select()->context.outer_context;
699
655
  }
706
662
    list
707
663
  */
708
664
  select_lex->context.resolve_in_select_list= true;
709
 
 
710
 
  return false;
 
665
  return(0);
711
666
}
712
667
 
713
668
/**
720
675
  @param var_name               Variable name
721
676
*/
722
677
 
723
 
void create_select_for_variable(Session *session, const char *var_name)
 
678
void create_select_for_variable(const char *var_name)
724
679
{
 
680
  Session *session;
725
681
  LEX *lex;
726
682
  LEX_STRING tmp, null_lex_string;
727
683
  Item *var;
728
684
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
729
685
  char *end= buff;
730
686
 
 
687
  session= current_session;
731
688
  lex= session->lex;
732
 
  init_select(lex);
 
689
  mysql_init_select(lex);
733
690
  lex->sql_command= SQLCOM_SELECT;
734
691
  tmp.str= (char*) var_name;
735
692
  tmp.length=strlen(var_name);
744
701
    var->set_name(buff, end-buff, system_charset_info);
745
702
    session->add_item_to_list(var);
746
703
  }
 
704
  return;
747
705
}
748
706
 
749
707
 
755
713
  @param       length  Length of the query text
756
714
*/
757
715
 
758
 
void parse(Session *session, const char *inBuf, uint32_t length)
 
716
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
759
717
{
760
 
  session->lex->start(session);
761
 
 
 
718
  lex_start(session);
762
719
  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
 
  }
 
720
 
776
721
  LEX *lex= session->lex;
 
722
 
777
723
  Lex_input_stream lip(session, inBuf, length);
 
724
 
778
725
  bool err= parse_sql(session, &lip);
 
726
 
779
727
  if (!err)
780
728
  {
781
729
    {
782
 
      if (not session->is_error())
 
730
      if (! session->is_error())
783
731
      {
784
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
 
732
        DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
785
733
                                 session->thread_id,
786
 
                                 const_cast<const char *>(session->schema()->c_str()));
787
 
        // Implement Views here --Brian
 
734
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
788
735
        /* 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
 
        }
 
736
        mysql_execute_command(session);
799
737
        DRIZZLE_QUERY_EXEC_DONE(0);
800
738
      }
801
739
    }
804
742
  {
805
743
    assert(session->is_error());
806
744
  }
 
745
 
807
746
  lex->unit.cleanup();
808
747
  session->set_proc_info("freeing items");
809
748
  session->end_statement();
810
749
  session->cleanup_after_query();
811
 
  session->set_end_timer();
 
750
 
 
751
  return;
812
752
}
813
753
 
814
754
 
868
808
    */
869
809
    if (default_value->type() == Item::FUNC_ITEM &&
870
810
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
871
 
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
 
811
         type == DRIZZLE_TYPE_TIMESTAMP))
872
812
    {
873
813
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
874
814
      return true;
876
816
    else if (default_value->type() == Item::NULL_ITEM)
877
817
    {
878
818
      default_value= 0;
879
 
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
 
819
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
 
820
          NOT_NULL_FLAG)
880
821
      {
881
822
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
882
823
        return true;
889
830
    }
890
831
  }
891
832
 
892
 
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
 
833
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
893
834
  {
894
835
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
895
836
    return true;
908
849
}
909
850
 
910
851
 
 
852
/** Store position for column in ALTER TABLE .. ADD column. */
 
853
 
 
854
void store_position_for_column(const char *name)
 
855
{
 
856
  current_session->lex->last_field->after=const_cast<char*> (name);
 
857
}
 
858
 
911
859
/**
912
860
  Add a table to list of used tables.
913
861
 
928
876
*/
929
877
 
930
878
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)
 
879
                                             Table_ident *table,
 
880
                                             LEX_STRING *alias,
 
881
                                             uint32_t table_options,
 
882
                                             thr_lock_type lock_type,
 
883
                                             List<Index_hint> *index_hints_arg,
 
884
                                             LEX_STRING *option)
937
885
{
938
 
  TableList *ptr;
 
886
  register TableList *ptr;
939
887
  TableList *previous_table_ref; /* The table preceding the current one. */
940
888
  char *alias_str;
941
889
  LEX *lex= session->lex;
943
891
  if (!table)
944
892
    return NULL;                                // End of memory
945
893
  alias_str= alias ? alias->str : table->table.str;
946
 
  if (! table_options.test(TL_OPTION_ALIAS) &&
 
894
  if (!test(table_options & TL_OPTION_ALIAS) &&
947
895
      check_table_name(table->table.str, table->table.length))
948
896
  {
949
897
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
954
902
  {
955
903
    my_casedn_str(files_charset_info, table->db.str);
956
904
 
957
 
    identifier::Schema schema_identifier(string(table->db.str));
958
 
    if (not check_db_name(session, schema_identifier))
 
905
    SchemaIdentifier schema_identifier(string(table->db.str, table->db.length));
 
906
    if (not check_db_name(schema_identifier))
959
907
    {
960
908
 
961
909
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
971
919
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
972
920
      return NULL;
973
921
    }
974
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
 
922
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
975
923
      return NULL;
976
924
  }
977
925
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
978
926
    return NULL;
979
 
 
980
927
  if (table->db.str)
981
928
  {
982
 
    ptr->setIsFqtn(true);
983
 
    ptr->setSchemaName(table->db.str);
 
929
    ptr->is_fqtn= true;
 
930
    ptr->db= table->db.str;
984
931
    ptr->db_length= table->db.length;
985
932
  }
986
 
  else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
 
933
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
987
934
    return NULL;
988
935
  else
989
 
    ptr->setIsFqtn(false);
 
936
    ptr->is_fqtn= false;
990
937
 
991
938
  ptr->alias= alias_str;
992
 
  ptr->setIsAlias(alias ? true : false);
993
 
  ptr->setTableName(table->table.str);
 
939
  ptr->is_alias= alias ? true : false;
 
940
  if (table->table.length)
 
941
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
 
942
  ptr->table_name=table->table.str;
994
943
  ptr->table_name_length=table->table.length;
995
944
  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);
 
945
  ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
 
946
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
998
947
  ptr->derived=     table->sel;
999
948
  ptr->select_lex=  lex->current_select;
1000
949
  ptr->index_hints= index_hints_arg;
1007
956
         tables ;
1008
957
         tables=tables->next_local)
1009
958
    {
1010
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1011
 
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
 
959
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
 
960
          !strcasecmp(ptr->db, tables->db))
1012
961
      {
1013
962
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1014
963
        return NULL;
1072
1021
bool Select_Lex::init_nested_join(Session *session)
1073
1022
{
1074
1023
  TableList *ptr;
1075
 
  NestedJoin *nested_join;
 
1024
  nested_join_st *nested_join;
1076
1025
 
1077
1026
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1078
 
                                       sizeof(NestedJoin))))
 
1027
                                       sizeof(nested_join_st))))
1079
1028
    return true;
1080
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1081
 
  nested_join= ptr->getNestedJoin();
 
1029
  nested_join= ptr->nested_join=
 
1030
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
1031
 
1082
1032
  join_list->push_front(ptr);
1083
 
  ptr->setEmbedding(embedding);
1084
 
  ptr->setJoinList(join_list);
 
1033
  ptr->embedding= embedding;
 
1034
  ptr->join_list= join_list;
1085
1035
  ptr->alias= (char*) "(nested_join)";
1086
1036
  embedding= ptr;
1087
1037
  join_list= &nested_join->join_list;
1107
1057
TableList *Select_Lex::end_nested_join(Session *)
1108
1058
{
1109
1059
  TableList *ptr;
1110
 
  NestedJoin *nested_join;
 
1060
  nested_join_st *nested_join;
1111
1061
 
1112
1062
  assert(embedding);
1113
1063
  ptr= embedding;
1114
 
  join_list= ptr->getJoinList();
1115
 
  embedding= ptr->getEmbedding();
1116
 
  nested_join= ptr->getNestedJoin();
 
1064
  join_list= ptr->join_list;
 
1065
  embedding= ptr->embedding;
 
1066
  nested_join= ptr->nested_join;
1117
1067
  if (nested_join->join_list.elements == 1)
1118
1068
  {
1119
1069
    TableList *embedded= nested_join->join_list.head();
1120
1070
    join_list->pop();
1121
 
    embedded->setJoinList(join_list);
1122
 
    embedded->setEmbedding(embedding);
 
1071
    embedded->join_list= join_list;
 
1072
    embedded->embedding= embedding;
1123
1073
    join_list->push_front(embedded);
1124
1074
    ptr= embedded;
1125
1075
  }
1148
1098
TableList *Select_Lex::nest_last_join(Session *session)
1149
1099
{
1150
1100
  TableList *ptr;
1151
 
  NestedJoin *nested_join;
 
1101
  nested_join_st *nested_join;
1152
1102
  List<TableList> *embedded_list;
1153
1103
 
1154
1104
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1155
 
                                          sizeof(NestedJoin))))
 
1105
                                       sizeof(nested_join_st))))
1156
1106
    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);
 
1107
  nested_join= ptr->nested_join=
 
1108
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
1109
 
 
1110
  ptr->embedding= embedding;
 
1111
  ptr->join_list= join_list;
1161
1112
  ptr->alias= (char*) "(nest_last_join)";
1162
1113
  embedded_list= &nested_join->join_list;
1163
1114
  embedded_list->empty();
1165
1116
  for (uint32_t i=0; i < 2; i++)
1166
1117
  {
1167
1118
    TableList *table= join_list->pop();
1168
 
    table->setJoinList(embedded_list);
1169
 
    table->setEmbedding(ptr);
 
1119
    table->join_list= embedded_list;
 
1120
    table->embedding= ptr;
1170
1121
    embedded_list->push_back(table);
1171
1122
    if (table->natural_join)
1172
1123
    {
1202
1153
void Select_Lex::add_joined_table(TableList *table)
1203
1154
{
1204
1155
  join_list->push_front(table);
1205
 
  table->setJoinList(join_list);
1206
 
  table->setEmbedding(embedding);
 
1156
  table->join_list= join_list;
 
1157
  table->embedding= embedding;
1207
1158
}
1208
1159
 
1209
1160
 
1446
1397
 
1447
1398
 
1448
1399
/**
 
1400
  kill on thread.
 
1401
 
 
1402
  @param session                        Thread class
 
1403
  @param id                     Thread id
 
1404
  @param only_kill_query        Should it kill the query or the connection
 
1405
 
 
1406
  @note
 
1407
    This is written such that we have a short lock on LOCK_thread_count
 
1408
*/
 
1409
 
 
1410
static unsigned int
 
1411
kill_one_thread(Session *, ulong id, bool only_kill_query)
 
1412
{
 
1413
  Session *tmp= NULL;
 
1414
  uint32_t error= ER_NO_SUCH_THREAD;
 
1415
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
1416
  
 
1417
  for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
 
1418
  {
 
1419
    if ((*it)->thread_id == id)
 
1420
    {
 
1421
      tmp= *it;
 
1422
      pthread_mutex_lock(&tmp->LOCK_delete);    // Lock from delete
 
1423
      break;
 
1424
    }
 
1425
  }
 
1426
  pthread_mutex_unlock(&LOCK_thread_count);
 
1427
  if (tmp)
 
1428
  {
 
1429
 
 
1430
    if (tmp->isViewable())
 
1431
    {
 
1432
      tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
 
1433
      error= 0;
 
1434
    }
 
1435
 
 
1436
    pthread_mutex_unlock(&tmp->LOCK_delete);
 
1437
  }
 
1438
  return(error);
 
1439
}
 
1440
 
 
1441
 
 
1442
/*
 
1443
  kills a thread and sends response
 
1444
 
 
1445
  SYNOPSIS
 
1446
    sql_kill()
 
1447
    session                     Thread class
 
1448
    id                  Thread id
 
1449
    only_kill_query     Should it kill the query or the connection
 
1450
*/
 
1451
 
 
1452
void sql_kill(Session *session, ulong id, bool only_kill_query)
 
1453
{
 
1454
  uint32_t error;
 
1455
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
1456
    session->my_ok();
 
1457
  else
 
1458
    my_error(error, MYF(0), id);
 
1459
}
 
1460
 
 
1461
 
 
1462
/**
1449
1463
  Check if the select is a simple select (not an union).
1450
1464
 
1451
1465
  @retval
1454
1468
    1   error   ; In this case the error messege is sent to the client
1455
1469
*/
1456
1470
 
1457
 
bool check_simple_select(Session::pointer session)
 
1471
bool check_simple_select()
1458
1472
{
 
1473
  Session *session= current_session;
1459
1474
  LEX *lex= session->lex;
1460
1475
  if (lex->current_select != &lex->select_lex)
1461
1476
  {
1572
1587
 
1573
1588
 
1574
1589
/**
 
1590
  CREATE TABLE query pre-check.
 
1591
 
 
1592
  @param session                        Thread handler
 
1593
  @param tables         Global table list
 
1594
  @param create_table           Table which will be created
 
1595
 
 
1596
  @retval
 
1597
    false   OK
 
1598
  @retval
 
1599
    true   Error
 
1600
*/
 
1601
 
 
1602
bool create_table_precheck(TableIdentifier &identifier)
 
1603
{
 
1604
  if (not plugin::StorageEngine::canCreateTable(identifier))
 
1605
  {
 
1606
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
 
1607
    return true;
 
1608
  }
 
1609
 
 
1610
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
1611
  {
 
1612
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
 
1613
    return true;
 
1614
  }
 
1615
 
 
1616
  return false;
 
1617
}
 
1618
 
 
1619
 
 
1620
/**
1575
1621
  negate given expression.
1576
1622
 
1577
1623
  @param session  thread handler
1638
1684
}
1639
1685
 
1640
1686
 
1641
 
bool check_identifier_name(LEX_STRING *str, error_t err_code,
 
1687
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
1642
1688
                           uint32_t max_char_length,
1643
1689
                           const char *param_for_err_msg)
1644
1690
{
1664
1710
 
1665
1711
  switch (err_code)
1666
1712
  {
1667
 
  case EE_OK:
 
1713
  case 0:
1668
1714
    break;
1669
1715
  case ER_WRONG_STRING_LENGTH:
1670
1716
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1676
1722
    assert(0);
1677
1723
    break;
1678
1724
  }
1679
 
 
1680
1725
  return true;
1681
1726
}
1682
1727
 
1697
1742
{
1698
1743
  assert(session->m_lip == NULL);
1699
1744
 
1700
 
  DRIZZLE_QUERY_PARSE_START(session->getQueryString()->c_str());
 
1745
  DRIZZLE_QUERY_PARSE_START(session->query.c_str());
1701
1746
 
1702
1747
  /* Set Lex_input_stream. */
1703
1748
 
1705
1750
 
1706
1751
  /* Parse the query. */
1707
1752
 
1708
 
  bool parse_status= DRIZZLEparse(session) != 0;
 
1753
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
1709
1754
 
1710
1755
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1711
1756
 
1712
 
  assert(!parse_status || session->is_error());
 
1757
  assert(!mysql_parse_status || session->is_error());
1713
1758
 
1714
1759
  /* Reset Lex_input_stream. */
1715
1760
 
1716
1761
  session->m_lip= NULL;
1717
1762
 
1718
 
  DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
 
1763
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1719
1764
 
1720
1765
  /* That's it. */
1721
1766
 
1722
 
  return parse_status || session->is_fatal_error;
 
1767
  return mysql_parse_status || session->is_fatal_error;
1723
1768
}
1724
1769
 
1725
1770
/**