~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <drizzled/error.h>
22
22
#include <drizzled/nested_join.h>
23
23
#include <drizzled/query_id.h>
 
24
#include "drizzled/transaction_services.h"
24
25
#include <drizzled/sql_parse.h>
25
26
#include <drizzled/data_home.h>
26
27
#include <drizzled/sql_base.h>
40
41
#include "drizzled/probes.h"
41
42
#include "drizzled/session_list.h"
42
43
#include "drizzled/global_charset_info.h"
43
 
#include "drizzled/transaction_services.h"
44
44
 
45
45
#include "drizzled/plugin/logging.h"
46
46
#include "drizzled/plugin/query_rewrite.h"
 
47
#include "drizzled/plugin/query_cache.h"
47
48
#include "drizzled/plugin/authorization.h"
48
49
#include "drizzled/optimizer/explain_plan.h"
49
50
#include "drizzled/pthread_globals.h"
182
183
    break;
183
184
  /* Increase id and count all other statements. */
184
185
  default:
185
 
    statistic_increment(session->status_var.questions, &LOCK_status);
 
186
    session->status_var.questions++;
186
187
    query_id.next();
187
188
  }
188
189
 
195
196
  switch (command) {
196
197
  case COM_INIT_DB:
197
198
  {
198
 
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
 
199
    if (packet_length == 0)
 
200
    {
 
201
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
 
202
      break;
 
203
    }
 
204
 
199
205
    string tmp(packet, packet_length);
200
206
 
201
 
    if (not mysql_change_db(session, tmp))
 
207
    SchemaIdentifier identifier(tmp);
 
208
 
 
209
    if (not mysql_change_db(session, identifier))
202
210
    {
203
211
      session->my_ok();
204
212
    }
224
232
    break;
225
233
  case COM_SHUTDOWN:
226
234
  {
227
 
    status_var_increment(session->status_var.com_other);
 
235
    session->status_var.com_other++;
228
236
    session->my_eof();
229
237
    session->close_thread_tables();                     // Free before kill
230
238
    kill_drizzle();
232
240
    break;
233
241
  }
234
242
  case COM_PING:
235
 
    status_var_increment(session->status_var.com_other);
 
243
    session->status_var.com_other++;
236
244
    session->my_ok();                           // Tell client we are alive
237
245
    break;
238
246
  case COM_SLEEP:
246
254
  /* If commit fails, we should be able to reset the OK status. */
247
255
  session->main_da.can_overwrite_status= true;
248
256
  TransactionServices &transaction_services= TransactionServices::singleton();
249
 
  transaction_services.ha_autocommit_or_rollback(session, session->is_error());
 
257
  transaction_services.autocommitOrRollback(session, session->is_error());
250
258
  session->main_da.can_overwrite_status= false;
251
259
 
252
260
  session->transaction.stmt.reset();
261
269
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
262
270
  {
263
271
    session->killed= Session::NOT_KILLED;
264
 
    session->mysys_var->abort= 0;
 
272
    session->setAbort(false);
265
273
  }
266
274
 
267
275
  /* Can not be true, but do not take chances in production. */
307
315
  session->query.clear();
308
316
 
309
317
  session->set_proc_info(NULL);
310
 
  free_root(session->mem_root,MYF(memory::KEEP_PREALLOC));
 
318
  session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
311
319
 
312
320
  if (DRIZZLE_QUERY_DONE_ENABLED() || DRIZZLE_COMMAND_DONE_ENABLED())
313
321
  {
465
473
    drizzle_reset_errors(session, 0);
466
474
  }
467
475
 
468
 
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
469
 
 
470
476
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
471
477
 
472
478
  /* now we are ready to execute the statement */
473
479
  res= lex->statement->execute();
474
 
 
475
480
  session->set_proc_info("query end");
476
 
 
477
481
  /*
478
482
    The return value for ROW_COUNT() is "implementation dependent" if the
479
483
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
487
491
 
488
492
  return (res || session->is_error());
489
493
}
490
 
 
491
494
bool execute_sqlcom_select(Session *session, TableList *all_tables)
492
495
{
493
496
  LEX   *lex= session->lex;
500
503
      param->select_limit=
501
504
        new Item_int((uint64_t) session->variables.select_limit);
502
505
  }
503
 
  if (!(res= session->openTablesLock(all_tables)))
 
506
  if (not (res= session->openTablesLock(all_tables)))
504
507
  {
505
508
    if (lex->describe)
506
509
    {
535
538
    {
536
539
      if (!result && !(result= new select_send()))
537
540
        return true;
 
541
 
 
542
      /* Init the Query Cache plugin */
 
543
      plugin::QueryCache::prepareResultset(session); 
538
544
      res= handle_select(session, lex, result, 0);
 
545
      /* Send the Resultset to the cache */
 
546
      plugin::QueryCache::setResultset(session); 
 
547
 
539
548
      if (result != lex->result)
540
549
        delete result;
541
550
    }
689
698
  */
690
699
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
691
700
  {
692
 
    end+= sprintf(buff, "@@session.%s", var_name);
 
701
    end+= snprintf(buff, sizeof(buff), "@@session.%s", var_name);
693
702
    var->set_name(buff, end-buff, system_charset_info);
694
703
    session->add_item_to_list(var);
695
704
  }
707
716
 
708
717
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
709
718
{
 
719
  uint64_t start_time= my_getsystime();
710
720
  lex_start(session);
711
721
  session->reset_for_next_command();
712
 
 
 
722
  /* Check if the Query is Cached if and return true if yes
 
723
   * TODO the plugin has to make sure that the query is cacheble
 
724
   * by setting the query_safe_cache param to TRUE
 
725
   */
 
726
  bool res= true;
 
727
  if (plugin::QueryCache::isCached(session))
 
728
  {
 
729
    res= plugin::QueryCache::sendCachedResultset(session);
 
730
  }
 
731
  if (not res)
 
732
  {
 
733
    return;
 
734
  }
713
735
  LEX *lex= session->lex;
714
 
 
715
736
  Lex_input_stream lip(session, inBuf, length);
716
 
 
717
737
  bool err= parse_sql(session, &lip);
718
 
 
719
738
  if (!err)
720
739
  {
721
740
    {
724
743
        DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
725
744
                                 session->thread_id,
726
745
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
 
746
        // Implement Views here --Brian
727
747
        /* Actually execute the query */
728
 
        mysql_execute_command(session);
 
748
        try 
 
749
        {
 
750
          mysql_execute_command(session);
 
751
        }
 
752
        catch (...)
 
753
        {
 
754
          // Just try to catch any random failures that could have come
 
755
          // during execution.
 
756
        }
729
757
        DRIZZLE_QUERY_EXEC_DONE(0);
730
758
      }
731
759
    }
734
762
  {
735
763
    assert(session->is_error());
736
764
  }
737
 
 
738
765
  lex->unit.cleanup();
739
766
  session->set_proc_info("freeing items");
740
767
  session->end_statement();
741
768
  session->cleanup_after_query();
742
 
 
743
 
  return;
 
769
  session->status_var.execution_time_nsec+= my_getsystime() - start_time;
744
770
}
745
771
 
746
772
 
890
916
    return NULL;
891
917
  }
892
918
 
893
 
  if (table->is_derived_table() == false && table->db.str &&
894
 
      check_db_name(&table->db))
 
919
  if (table->is_derived_table() == false && table->db.str)
895
920
  {
896
 
    my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
897
 
    return NULL;
 
921
    my_casedn_str(files_charset_info, table->db.str);
 
922
 
 
923
    SchemaIdentifier schema_identifier(string(table->db.str));
 
924
    if (not check_db_name(session, schema_identifier))
 
925
    {
 
926
 
 
927
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
 
928
      return NULL;
 
929
    }
898
930
  }
899
931
 
900
932
  if (!alias)                                   /* Alias is case sensitive */
910
942
  }
911
943
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
912
944
    return NULL;
 
945
 
913
946
  if (table->db.str)
914
947
  {
915
 
    ptr->is_fqtn= true;
 
948
    ptr->setIsFqtn(true);
916
949
    ptr->db= table->db.str;
917
950
    ptr->db_length= table->db.length;
918
951
  }
919
952
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
920
953
    return NULL;
921
954
  else
922
 
    ptr->is_fqtn= false;
 
955
    ptr->setIsFqtn(false);
923
956
 
924
957
  ptr->alias= alias_str;
925
 
  ptr->is_alias= alias ? true : false;
 
958
  ptr->setIsAlias(alias ? true : false);
926
959
  if (table->table.length)
927
960
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
928
961
  ptr->table_name=table->table.str;
943
976
         tables=tables->next_local)
944
977
    {
945
978
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
946
 
          !strcmp(ptr->db, tables->db))
 
979
          !strcasecmp(ptr->db, tables->db))
947
980
      {
948
981
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
949
982
        return NULL;
1012
1045
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1013
1046
                                       sizeof(nested_join_st))))
1014
1047
    return true;
1015
 
  nested_join= ptr->nested_join=
1016
 
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1017
 
 
 
1048
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1049
  nested_join= ptr->getNestedJoin();
1018
1050
  join_list->push_front(ptr);
1019
 
  ptr->embedding= embedding;
1020
 
  ptr->join_list= join_list;
 
1051
  ptr->setEmbedding(embedding);
 
1052
  ptr->setJoinList(join_list);
1021
1053
  ptr->alias= (char*) "(nested_join)";
1022
1054
  embedding= ptr;
1023
1055
  join_list= &nested_join->join_list;
1047
1079
 
1048
1080
  assert(embedding);
1049
1081
  ptr= embedding;
1050
 
  join_list= ptr->join_list;
1051
 
  embedding= ptr->embedding;
1052
 
  nested_join= ptr->nested_join;
 
1082
  join_list= ptr->getJoinList();
 
1083
  embedding= ptr->getEmbedding();
 
1084
  nested_join= ptr->getNestedJoin();
1053
1085
  if (nested_join->join_list.elements == 1)
1054
1086
  {
1055
1087
    TableList *embedded= nested_join->join_list.head();
1056
1088
    join_list->pop();
1057
 
    embedded->join_list= join_list;
1058
 
    embedded->embedding= embedding;
 
1089
    embedded->setJoinList(join_list);
 
1090
    embedded->setEmbedding(embedding);
1059
1091
    join_list->push_front(embedded);
1060
1092
    ptr= embedded;
1061
1093
  }
1088
1120
  List<TableList> *embedded_list;
1089
1121
 
1090
1122
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1091
 
                                       sizeof(nested_join_st))))
 
1123
                                          sizeof(nested_join_st))))
1092
1124
    return NULL;
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;
 
1125
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1126
  nested_join= ptr->getNestedJoin();
 
1127
  ptr->setEmbedding(embedding);
 
1128
  ptr->setJoinList(join_list);
1098
1129
  ptr->alias= (char*) "(nest_last_join)";
1099
1130
  embedded_list= &nested_join->join_list;
1100
1131
  embedded_list->empty();
1102
1133
  for (uint32_t i=0; i < 2; i++)
1103
1134
  {
1104
1135
    TableList *table= join_list->pop();
1105
 
    table->join_list= embedded_list;
1106
 
    table->embedding= ptr;
 
1136
    table->setJoinList(embedded_list);
 
1137
    table->setEmbedding(ptr);
1107
1138
    embedded_list->push_back(table);
1108
1139
    if (table->natural_join)
1109
1140
    {
1139
1170
void Select_Lex::add_joined_table(TableList *table)
1140
1171
{
1141
1172
  join_list->push_front(table);
1142
 
  table->join_list= join_list;
1143
 
  table->embedding= embedding;
 
1173
  table->setJoinList(join_list);
 
1174
  table->setEmbedding(embedding);
1144
1175
}
1145
1176
 
1146
1177
 
1398
1429
{
1399
1430
  Session *tmp= NULL;
1400
1431
  uint32_t error= ER_NO_SUCH_THREAD;
1401
 
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
1432
  LOCK_thread_count.lock(); // For unlink from list
1402
1433
  
1403
1434
  for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
1404
1435
  {
1405
1436
    if ((*it)->thread_id == id)
1406
1437
    {
1407
1438
      tmp= *it;
1408
 
      pthread_mutex_lock(&tmp->LOCK_delete);    // Lock from delete
 
1439
      tmp->lockForDelete();
1409
1440
      break;
1410
1441
    }
1411
1442
  }
1412
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
1443
  LOCK_thread_count.unlock();
1413
1444
  if (tmp)
1414
1445
  {
1415
1446
 
1419
1450
      error= 0;
1420
1451
    }
1421
1452
 
1422
 
    pthread_mutex_unlock(&tmp->LOCK_delete);
 
1453
    tmp->unlockForDelete();
1423
1454
  }
1424
1455
  return(error);
1425
1456
}
1589
1620
{
1590
1621
  if (not plugin::StorageEngine::canCreateTable(identifier))
1591
1622
  {
1592
 
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName());
 
1623
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
 
1624
    return true;
 
1625
  }
 
1626
 
 
1627
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
1628
  {
 
1629
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
1593
1630
    return true;
1594
1631
  }
1595
1632