~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.cc

  • Committer: Brian Aker
  • Date: 2011-01-06 05:17:09 UTC
  • Revision ID: brian@tangent.org-20110106051709-oa0se8ur02uc6i9o
Added native functions into the function table.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 */
23
23
 
24
24
#include "config.h"
25
 
 
26
 
#include <drizzled/copy_field.h>
27
25
#include "drizzled/session.h"
28
26
#include "drizzled/session/cache.h"
 
27
#include <sys/stat.h>
29
28
#include "drizzled/error.h"
30
29
#include "drizzled/gettext.h"
31
30
#include "drizzled/query_id.h"
52
51
 
53
52
#include "drizzled/identifier.h"
54
53
 
55
 
#include <drizzled/refresh_version.h>
56
 
 
57
 
#include "drizzled/table/singular.h"
 
54
#include "drizzled/table/instance.h"
58
55
 
59
56
#include "plugin/myisam/myisam.h"
60
57
#include "drizzled/internal/iocache.h"
65
62
 
66
63
#include "drizzled/display.h"
67
64
 
 
65
#include <fcntl.h>
68
66
#include <algorithm>
69
67
#include <climits>
70
 
#include <fcntl.h>
71
 
#include <sys/stat.h>
72
 
 
73
68
#include <boost/filesystem.hpp>
74
 
#include <boost/checked_delete.hpp>
75
69
 
76
70
#include "drizzled/util/backtrace.h"
77
71
 
118
112
  return fd;
119
113
}
120
114
 
 
115
int session_tablespace_op(const Session *session)
 
116
{
 
117
  return test(session->tablespace_op);
 
118
}
 
119
 
 
120
/**
 
121
   Set the process info field of the Session structure.
 
122
 
 
123
   This function is used by plug-ins. Internally, the
 
124
   Session::set_proc_info() function should be used.
 
125
 
 
126
   @see Session::set_proc_info
 
127
 */
 
128
void set_session_proc_info(Session *session, const char *info)
 
129
{
 
130
  session->set_proc_info(info);
 
131
}
 
132
 
 
133
const char *get_session_proc_info(Session *session)
 
134
{
 
135
  return session->get_proc_info();
 
136
}
 
137
 
121
138
void **Session::getEngineData(const plugin::MonitoredInTransaction *monitored)
122
139
{
123
140
  return static_cast<void **>(&ha_data[monitored->getId()].ha_ptr);
134
151
  return session->options & test_options;
135
152
}
136
153
 
137
 
Session::Session(plugin::Client *client_arg, catalog::Instance::shared_ptr catalog_arg) :
 
154
int session_sql_command(const Session *session)
 
155
{
 
156
  return (int) session->lex->sql_command;
 
157
}
 
158
 
 
159
enum_tx_isolation session_tx_isolation(const Session *session)
 
160
{
 
161
  return (enum_tx_isolation)session->variables.tx_isolation;
 
162
}
 
163
 
 
164
Session::Session(plugin::Client *client_arg) :
138
165
  Open_tables_state(refresh_version),
139
166
  mem_root(&main_mem_root),
140
167
  xa_id(0),
141
168
  lex(&main_lex),
142
169
  query(new std::string),
143
170
  _schema(new std::string("")),
 
171
  catalog("LOCAL"),
144
172
  client(client_arg),
145
173
  scheduler(NULL),
146
174
  scheduler_arg(NULL),
147
175
  lock_id(&main_lock_id),
148
176
  thread_stack(NULL),
149
177
  security_ctx(identifier::User::make_shared()),
150
 
  _where(Session::DEFAULT_WHERE),
 
178
  where(Session::DEFAULT_WHERE),
151
179
  dbug_sentry(Session_SENTRY_MAGIC),
152
180
  mysys_var(0),
153
181
  command(COM_CONNECT),
183
211
  is_fatal_error(false),
184
212
  transaction_rollback_request(false),
185
213
  is_fatal_sub_stmt_error(0),
 
214
  derived_tables_processing(false),
186
215
  tablespace_op(false),
187
 
  derived_tables_processing(false),
188
216
  m_lip(NULL),
189
217
  cached_table(0),
190
218
  transaction_message(NULL),
191
219
  statement_message(NULL),
192
220
  session_event_observers(NULL),
193
 
  _catalog(catalog_arg),
194
221
  use_usage(false)
195
222
{
196
223
  client->setSession(this);
269
296
  m_internal_handler= handler;
270
297
}
271
298
 
272
 
bool Session::handle_error(drizzled::error_t sql_errno, const char *message,
273
 
                           DRIZZLE_ERROR::enum_warning_level level)
 
299
bool Session::handle_error(uint32_t sql_errno, const char *message,
 
300
                       DRIZZLE_ERROR::enum_warning_level level)
274
301
{
275
302
  if (m_internal_handler)
276
303
  {
326
353
#endif
327
354
  {
328
355
    TransactionServices &transaction_services= TransactionServices::singleton();
329
 
    transaction_services.rollbackTransaction(*this, true);
 
356
    transaction_services.rollbackTransaction(this, true);
330
357
    xid_cache_delete(&transaction.xid_state);
331
358
  }
332
359
 
335
362
       iter++)
336
363
  {
337
364
    user_var_entry *entry= (*iter).second;
338
 
    boost::checked_delete(entry);
 
365
    delete entry;
339
366
  }
340
367
  user_vars.clear();
341
368
 
359
386
    assert(security_ctx);
360
387
    if (global_system_variables.log_warnings)
361
388
    {
362
 
      errmsg_printf(error::WARN, ER(ER_FORCING_CLOSE),
 
389
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_FORCING_CLOSE),
363
390
                    internal::my_progname,
364
391
                    thread_id,
365
392
                    security_ctx->username().c_str());
372
399
  if (client)
373
400
  {
374
401
    client->close();
375
 
    boost::checked_delete(client);
376
 
    client= NULL;
 
402
    delete client;
377
403
  }
378
404
 
379
405
  if (cleanup_done == false)
392
418
 
393
419
  plugin::Logging::postEndDo(this);
394
420
  plugin::EventObserver::deregisterSessionEvents(*this); 
 
421
 
 
422
  for (PropertyMap::iterator iter= life_properties.begin(); iter != life_properties.end(); iter++)
 
423
  {
 
424
    delete (*iter).second;
 
425
  }
 
426
  life_properties.clear();
395
427
}
396
428
 
397
429
void Session::setClient(plugin::Client *client_arg)
642
674
  /* Change database if necessary */
643
675
  if (not in_db.empty())
644
676
  {
645
 
    identifier::Schema identifier(in_db);
 
677
    SchemaIdentifier identifier(in_db);
646
678
    if (change_db(this, identifier))
647
679
    {
648
680
      /* change_db() has pushed the error message. */
713
745
    plugin::QueryRewriter::rewriteQuery(*_schema, *new_query);
714
746
  }
715
747
  query.reset(new_query);
716
 
  _state.reset(new session::State(in_packet, in_packet_length));
 
748
  _state.reset(new State(in_packet, in_packet_length));
717
749
 
718
750
  return true;
719
751
}
738
770
       * (Which of course should never happen...)
739
771
       */
740
772
      server_status&= ~SERVER_STATUS_IN_TRANS;
741
 
      if (transaction_services.commitTransaction(*this, true))
 
773
      if (transaction_services.commitTransaction(this, true))
742
774
        result= false;
743
775
      options&= ~(OPTION_BEGIN);
744
776
      break;
755
787
    case ROLLBACK_AND_CHAIN:
756
788
    {
757
789
      server_status&= ~SERVER_STATUS_IN_TRANS;
758
 
      if (transaction_services.rollbackTransaction(*this, true))
 
790
      if (transaction_services.rollbackTransaction(this, true))
759
791
        result= false;
760
792
      options&= ~(OPTION_BEGIN);
761
793
      if (result == true && (completion == ROLLBACK_AND_CHAIN))
792
824
  if (options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
793
825
  {
794
826
    server_status&= ~SERVER_STATUS_IN_TRANS;
795
 
    if (transaction_services.commitTransaction(*this, true))
 
827
    if (transaction_services.commitTransaction(this, true))
796
828
      result= false;
797
829
  }
798
830
  options&= ~(OPTION_BEGIN);
803
835
{
804
836
  bool result= true;
805
837
 
806
 
  assert(! inTransaction());
807
 
 
808
 
  options|= OPTION_BEGIN;
809
 
  server_status|= SERVER_STATUS_IN_TRANS;
810
 
 
811
 
  if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
 
838
  if (! endActiveTransaction())
812
839
  {
813
840
    result= false;
814
841
  }
 
842
  else
 
843
  {
 
844
    options|= OPTION_BEGIN;
 
845
    server_status|= SERVER_STATUS_IN_TRANS;
 
846
 
 
847
    if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
 
848
    {
 
849
      result= false;
 
850
    }
 
851
  }
815
852
 
816
853
  return result;
817
854
}
833
870
    first_successful_insert_id_in_cur_stmt= 0;
834
871
    substitute_null_with_insert_id= true;
835
872
  }
836
 
 
837
873
  arg_of_last_insert_id_function= false;
838
 
 
839
874
  /* Free Items that were created during this execution */
840
875
  free_items();
841
 
 
842
 
  /* Reset _where. */
843
 
  _where= Session::DEFAULT_WHERE;
 
876
  /* Reset where. */
 
877
  where= Session::DEFAULT_WHERE;
844
878
 
845
879
  /* Reset the temporary shares we built */
846
880
  for_each(temporary_shares.begin(),
871
905
                                     bool allocate_lex_string)
872
906
{
873
907
  if (allocate_lex_string)
874
 
    if (!(lex_str= (LEX_STRING *)getMemRoot()->allocate(sizeof(LEX_STRING))))
 
908
    if (!(lex_str= (LEX_STRING *)alloc(sizeof(LEX_STRING))))
875
909
      return 0;
876
910
  if (!(lex_str->str= mem_root->strmake_root(str, length)))
877
911
    return 0;
917
951
  return (result->send_fields(field_list));
918
952
}
919
953
 
920
 
void select_result::send_error(drizzled::error_t errcode, const char *err)
 
954
void select_result::send_error(uint32_t errcode, const char *err)
921
955
{
922
956
  my_message(errcode, err, MYF(0));
923
957
}
926
960
  Handling writing to file
927
961
************************************************************************/
928
962
 
929
 
void select_to_file::send_error(drizzled::error_t errcode,const char *err)
 
963
void select_to_file::send_error(uint32_t errcode,const char *err)
930
964
{
931
965
  my_message(errcode, err, MYF(0));
932
966
  if (file > 0)
1574
1608
  /* Fix for Intel compiler */
1575
1609
  if (copy_field)
1576
1610
  {
1577
 
    boost::checked_array_delete(copy_field);
 
1611
    delete [] copy_field;
1578
1612
    save_copy_field= save_copy_field_end= copy_field= copy_field_end= 0;
1579
1613
  }
1580
1614
}
1581
1615
 
1582
1616
void Session::send_kill_message() const
1583
1617
{
1584
 
  drizzled::error_t err= static_cast<drizzled::error_t>(killed_errno());
1585
 
  if (err != EE_OK)
 
1618
  int err= killed_errno();
 
1619
  if (err)
1586
1620
    my_message(err, ER(err), MYF(0));
1587
1621
}
1588
1622
 
1612
1646
  @param  session   Thread handle
1613
1647
  @param  all   true <=> rollback main transaction.
1614
1648
*/
1615
 
void Session::markTransactionForRollback(bool all)
 
1649
void mark_transaction_to_rollback(Session *session, bool all)
1616
1650
{
1617
 
  is_fatal_sub_stmt_error= true;
1618
 
  transaction_rollback_request= all;
 
1651
  if (session)
 
1652
  {
 
1653
    session->is_fatal_sub_stmt_error= true;
 
1654
    session->transaction_rollback_request= all;
 
1655
  }
1619
1656
}
1620
1657
 
1621
1658
void Session::disconnect(enum error_t errcode)
1633
1670
  {
1634
1671
    if (not getKilled() && variables.log_warnings > 1)
1635
1672
    {
1636
 
      errmsg_printf(error::WARN, ER(ER_NEW_ABORTING_CONNECTION)
 
1673
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_NEW_ABORTING_CONNECTION)
1637
1674
                  , thread_id
1638
1675
                  , (_schema->empty() ? "unconnected" : _schema->c_str())
1639
1676
                  , security_ctx->username().empty() == false ? security_ctx->username().c_str() : "unauthenticated"
1743
1780
  table->free_io_cache();
1744
1781
  table->delete_table();
1745
1782
 
1746
 
  identifier::Table identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
 
1783
  TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
1747
1784
  rm_temporary_table(table_type, identifier);
1748
1785
 
1749
 
  boost::checked_delete(table->getMutableShare());
 
1786
  delete table->getMutableShare();
1750
1787
 
1751
 
  boost::checked_delete(table);
 
1788
  delete table;
1752
1789
}
1753
1790
 
1754
1791
/** Clear most status variables. */
1771
1808
 
1772
1809
user_var_entry *Session::getVariable(const std::string  &name, bool create_if_not_exists)
1773
1810
{
1774
 
  if (cleanup_done)
1775
 
    return NULL;
 
1811
  UserVarsRange ppp= user_vars.equal_range(name);
1776
1812
 
1777
 
  UserVars::iterator iter= user_vars.find(name);
1778
 
  if (iter != user_vars.end())
 
1813
  for (UserVars::iterator iter= ppp.first;
 
1814
       iter != ppp.second; ++iter)
 
1815
  {
1779
1816
    return (*iter).second;
 
1817
  }
1780
1818
 
1781
1819
  if (not create_if_not_exists)
1782
1820
    return NULL;
1791
1829
 
1792
1830
  if (not returnable.second)
1793
1831
  {
1794
 
    boost::checked_delete(entry);
 
1832
    delete entry;
1795
1833
  }
1796
1834
 
1797
1835
  return entry;
1800
1838
void Session::setVariable(const std::string &name, const std::string &value)
1801
1839
{
1802
1840
  user_var_entry *updateable_var= getVariable(name.c_str(), true);
1803
 
  if (updateable_var)
1804
 
  {
1805
 
    updateable_var->update_hash(false,
1806
 
                                (void*)value.c_str(),
1807
 
                                static_cast<uint32_t>(value.length()), STRING_RESULT,
1808
 
                                &my_charset_bin,
1809
 
                                DERIVATION_IMPLICIT, false);
1810
 
  }
 
1841
 
 
1842
  updateable_var->update_hash(false,
 
1843
                              (void*)value.c_str(),
 
1844
                              static_cast<uint32_t>(value.length()), STRING_RESULT,
 
1845
                              &my_charset_bin,
 
1846
                              DERIVATION_IMPLICIT, false);
1811
1847
}
1812
1848
 
1813
1849
void Open_tables_state::mark_temp_tables_as_free_for_reuse()
1862
1898
  {
1863
1899
    TransactionServices &transaction_services= TransactionServices::singleton();
1864
1900
    main_da.can_overwrite_status= true;
1865
 
    transaction_services.autocommitOrRollback(*this, is_error());
 
1901
    transaction_services.autocommitOrRollback(this, is_error());
1866
1902
    main_da.can_overwrite_status= false;
1867
1903
    transaction.stmt.reset();
1868
1904
  }
1918
1954
 
1919
1955
    if (not lock_tables(tables, counter, &need_reopen))
1920
1956
      break;
1921
 
 
1922
1957
    if (not need_reopen)
1923
1958
      return true;
1924
 
 
1925
1959
    close_tables_for_reopen(&tables);
1926
1960
  }
1927
 
 
1928
 
  if ((handle_derived(lex, &derived_prepare) || (handle_derived(lex, &derived_filling))))
 
1961
  if ((handle_derived(lex, &derived_prepare) ||
 
1962
       (
 
1963
        handle_derived(lex, &derived_filling))))
1929
1964
    return true;
1930
1965
 
1931
1966
  return false;
1937
1972
  might be an issue (lame engines).
1938
1973
*/
1939
1974
 
1940
 
bool Open_tables_state::rm_temporary_table(const identifier::Table &identifier, bool best_effort)
 
1975
bool Open_tables_state::rm_temporary_table(const TableIdentifier &identifier, bool best_effort)
1941
1976
{
1942
 
  if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
 
1977
  if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
1943
1978
  {
1944
1979
    if (not best_effort)
1945
1980
    {
1946
1981
      std::string path;
1947
1982
      identifier.getSQLPath(path);
1948
 
      errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
 
1983
      errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
1949
1984
                    path.c_str(), errno);
1950
1985
    }
1951
1986
 
1955
1990
  return false;
1956
1991
}
1957
1992
 
1958
 
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const identifier::Table &identifier)
 
1993
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const TableIdentifier &identifier)
1959
1994
{
1960
 
  drizzled::error_t error;
1961
1995
  assert(base);
1962
1996
 
1963
 
  if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier, error))
 
1997
  if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier))
1964
1998
  {
1965
1999
    std::string path;
1966
2000
    identifier.getSQLPath(path);
1967
 
    errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
1968
 
                  path.c_str(), error);
 
2001
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
 
2002
                  path.c_str(), errno);
1969
2003
 
1970
2004
    return true;
1971
2005
  }
1989
2023
  {
1990
2024
    bool have_proto= false;
1991
2025
 
1992
 
    message::Table *proto= table->getShare()->getTableMessage();
1993
 
    if (table->getShare()->getTableMessage())
 
2026
    message::Table *proto= table->getShare()->getTableProto();
 
2027
    if (table->getShare()->getTableProto())
1994
2028
      have_proto= true;
1995
2029
 
1996
2030
    const char *answer= have_proto ? "true" : "false";
2007
2041
  }
2008
2042
}
2009
2043
 
2010
 
table::Singular *Session::getInstanceTable()
2011
 
{
2012
 
  temporary_shares.push_back(new table::Singular()); // This will not go into the tableshare cache, so no key is used.
2013
 
 
2014
 
  table::Singular *tmp_share= temporary_shares.back();
 
2044
bool Session::TableMessages::storeTableMessage(const TableIdentifier &identifier, message::Table &table_message)
 
2045
{
 
2046
  table_message_cache.insert(make_pair(identifier.getPath(), table_message));
 
2047
 
 
2048
  return true;
 
2049
}
 
2050
 
 
2051
bool Session::TableMessages::removeTableMessage(const TableIdentifier &identifier)
 
2052
{
 
2053
  TableMessageCache::iterator iter;
 
2054
 
 
2055
  iter= table_message_cache.find(identifier.getPath());
 
2056
 
 
2057
  if (iter == table_message_cache.end())
 
2058
    return false;
 
2059
 
 
2060
  table_message_cache.erase(iter);
 
2061
 
 
2062
  return true;
 
2063
}
 
2064
 
 
2065
bool Session::TableMessages::getTableMessage(const TableIdentifier &identifier, message::Table &table_message)
 
2066
{
 
2067
  TableMessageCache::iterator iter;
 
2068
 
 
2069
  iter= table_message_cache.find(identifier.getPath());
 
2070
 
 
2071
  if (iter == table_message_cache.end())
 
2072
    return false;
 
2073
 
 
2074
  table_message.CopyFrom(((*iter).second));
 
2075
 
 
2076
  return true;
 
2077
}
 
2078
 
 
2079
bool Session::TableMessages::doesTableMessageExist(const TableIdentifier &identifier)
 
2080
{
 
2081
  TableMessageCache::iterator iter;
 
2082
 
 
2083
  iter= table_message_cache.find(identifier.getPath());
 
2084
 
 
2085
  if (iter == table_message_cache.end())
 
2086
  {
 
2087
    return false;
 
2088
  }
 
2089
 
 
2090
  return true;
 
2091
}
 
2092
 
 
2093
bool Session::TableMessages::renameTableMessage(const TableIdentifier &from, const TableIdentifier &to)
 
2094
{
 
2095
  TableMessageCache::iterator iter;
 
2096
 
 
2097
  table_message_cache[to.getPath()]= table_message_cache[from.getPath()];
 
2098
 
 
2099
  iter= table_message_cache.find(to.getPath());
 
2100
 
 
2101
  if (iter == table_message_cache.end())
 
2102
  {
 
2103
    return false;
 
2104
  }
 
2105
 
 
2106
  (*iter).second.set_schema(to.getSchemaName());
 
2107
  (*iter).second.set_name(to.getTableName());
 
2108
 
 
2109
  return true;
 
2110
}
 
2111
 
 
2112
table::Instance *Session::getInstanceTable()
 
2113
{
 
2114
  temporary_shares.push_back(new table::Instance()); // This will not go into the tableshare cache, so no key is used.
 
2115
 
 
2116
  table::Instance *tmp_share= temporary_shares.back();
2015
2117
 
2016
2118
  assert(tmp_share);
2017
2119
 
2037
2139
  @return
2038
2140
    0 if out of memory, Table object in case of success
2039
2141
*/
2040
 
table::Singular *Session::getInstanceTable(List<CreateField> &field_list)
 
2142
table::Instance *Session::getInstanceTable(List<CreateField> &field_list)
2041
2143
{
2042
 
  temporary_shares.push_back(new table::Singular(this, field_list)); // This will not go into the tableshare cache, so no key is used.
 
2144
  temporary_shares.push_back(new table::Instance(this, field_list)); // This will not go into the tableshare cache, so no key is used.
2043
2145
 
2044
 
  table::Singular *tmp_share= temporary_shares.back();
 
2146
  table::Instance *tmp_share= temporary_shares.back();
2045
2147
 
2046
2148
  assert(tmp_share);
2047
2149