~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.cc

  • Committer: Brian Aker
  • Date: 2011-02-12 10:32:50 UTC
  • Revision ID: brian@tangent.org-20110212103250-q8y7zqan2948uban
Merge in a possible fix for Solaris

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>
25
27
#include "drizzled/session.h"
26
28
#include "drizzled/session/cache.h"
27
 
#include <sys/stat.h>
28
29
#include "drizzled/error.h"
29
30
#include "drizzled/gettext.h"
30
31
#include "drizzled/query_id.h"
51
52
 
52
53
#include "drizzled/identifier.h"
53
54
 
 
55
#include <drizzled/refresh_version.h>
 
56
 
54
57
#include "drizzled/table/singular.h"
55
58
 
56
59
#include "plugin/myisam/myisam.h"
62
65
 
63
66
#include "drizzled/display.h"
64
67
 
65
 
#include <fcntl.h>
66
68
#include <algorithm>
67
69
#include <climits>
 
70
#include <fcntl.h>
 
71
#include <sys/stat.h>
68
72
 
69
73
#include <boost/filesystem.hpp>
70
74
#include <boost/checked_delete.hpp>
114
118
  return fd;
115
119
}
116
120
 
117
 
int session_tablespace_op(const Session *session)
118
 
{
119
 
  return test(session->tablespace_op);
120
 
}
121
 
 
122
 
/**
123
 
   Set the process info field of the Session structure.
124
 
 
125
 
   This function is used by plug-ins. Internally, the
126
 
   Session::set_proc_info() function should be used.
127
 
 
128
 
   @see Session::set_proc_info
129
 
 */
130
 
void set_session_proc_info(Session *session, const char *info)
131
 
{
132
 
  session->set_proc_info(info);
133
 
}
134
 
 
135
 
const char *get_session_proc_info(Session *session)
136
 
{
137
 
  return session->get_proc_info();
138
 
}
139
 
 
140
121
void **Session::getEngineData(const plugin::MonitoredInTransaction *monitored)
141
122
{
142
123
  return static_cast<void **>(&ha_data[monitored->getId()].ha_ptr);
153
134
  return session->options & test_options;
154
135
}
155
136
 
156
 
int session_sql_command(const Session *session)
157
 
{
158
 
  return (int) session->lex->sql_command;
159
 
}
160
 
 
161
 
enum_tx_isolation session_tx_isolation(const Session *session)
162
 
{
163
 
  return (enum_tx_isolation)session->variables.tx_isolation;
164
 
}
165
 
 
166
137
Session::Session(plugin::Client *client_arg, catalog::Instance::shared_ptr catalog_arg) :
167
138
  Open_tables_state(refresh_version),
168
139
  mem_root(&main_mem_root),
176
147
  lock_id(&main_lock_id),
177
148
  thread_stack(NULL),
178
149
  security_ctx(identifier::User::make_shared()),
179
 
  where(Session::DEFAULT_WHERE),
 
150
  _where(Session::DEFAULT_WHERE),
180
151
  dbug_sentry(Session_SENTRY_MAGIC),
181
152
  mysys_var(0),
182
153
  command(COM_CONNECT),
212
183
  is_fatal_error(false),
213
184
  transaction_rollback_request(false),
214
185
  is_fatal_sub_stmt_error(0),
 
186
  tablespace_op(false),
215
187
  derived_tables_processing(false),
216
 
  tablespace_op(false),
217
188
  m_lip(NULL),
218
189
  cached_table(0),
219
190
  transaction_message(NULL),
355
326
#endif
356
327
  {
357
328
    TransactionServices &transaction_services= TransactionServices::singleton();
358
 
    transaction_services.rollbackTransaction(this, true);
 
329
    transaction_services.rollbackTransaction(*this, true);
359
330
    xid_cache_delete(&transaction.xid_state);
360
331
  }
361
332
 
388
359
    assert(security_ctx);
389
360
    if (global_system_variables.log_warnings)
390
361
    {
391
 
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_FORCING_CLOSE),
 
362
      errmsg_printf(error::WARN, ER(ER_FORCING_CLOSE),
392
363
                    internal::my_progname,
393
364
                    thread_id,
394
365
                    security_ctx->username().c_str());
421
392
 
422
393
  plugin::Logging::postEndDo(this);
423
394
  plugin::EventObserver::deregisterSessionEvents(*this); 
424
 
 
425
 
  for (PropertyMap::iterator iter= life_properties.begin(); iter != life_properties.end(); iter++)
426
 
  {
427
 
    boost::checked_delete((*iter).second);
428
 
  }
429
 
  life_properties.clear();
430
395
}
431
396
 
432
397
void Session::setClient(plugin::Client *client_arg)
748
713
    plugin::QueryRewriter::rewriteQuery(*_schema, *new_query);
749
714
  }
750
715
  query.reset(new_query);
751
 
  _state.reset(new State(in_packet, in_packet_length));
 
716
  _state.reset(new session::State(in_packet, in_packet_length));
752
717
 
753
718
  return true;
754
719
}
773
738
       * (Which of course should never happen...)
774
739
       */
775
740
      server_status&= ~SERVER_STATUS_IN_TRANS;
776
 
      if (transaction_services.commitTransaction(this, true))
 
741
      if (transaction_services.commitTransaction(*this, true))
777
742
        result= false;
778
743
      options&= ~(OPTION_BEGIN);
779
744
      break;
790
755
    case ROLLBACK_AND_CHAIN:
791
756
    {
792
757
      server_status&= ~SERVER_STATUS_IN_TRANS;
793
 
      if (transaction_services.rollbackTransaction(this, true))
 
758
      if (transaction_services.rollbackTransaction(*this, true))
794
759
        result= false;
795
760
      options&= ~(OPTION_BEGIN);
796
761
      if (result == true && (completion == ROLLBACK_AND_CHAIN))
827
792
  if (options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
828
793
  {
829
794
    server_status&= ~SERVER_STATUS_IN_TRANS;
830
 
    if (transaction_services.commitTransaction(this, true))
 
795
    if (transaction_services.commitTransaction(*this, true))
831
796
      result= false;
832
797
  }
833
798
  options&= ~(OPTION_BEGIN);
838
803
{
839
804
  bool result= true;
840
805
 
841
 
  if (! endActiveTransaction())
 
806
  assert(! inTransaction());
 
807
 
 
808
  options|= OPTION_BEGIN;
 
809
  server_status|= SERVER_STATUS_IN_TRANS;
 
810
 
 
811
  if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
842
812
  {
843
813
    result= false;
844
814
  }
845
 
  else
846
 
  {
847
 
    options|= OPTION_BEGIN;
848
 
    server_status|= SERVER_STATUS_IN_TRANS;
849
 
 
850
 
    if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
851
 
    {
852
 
      result= false;
853
 
    }
854
 
  }
855
815
 
856
816
  return result;
857
817
}
873
833
    first_successful_insert_id_in_cur_stmt= 0;
874
834
    substitute_null_with_insert_id= true;
875
835
  }
 
836
 
876
837
  arg_of_last_insert_id_function= false;
 
838
 
877
839
  /* Free Items that were created during this execution */
878
840
  free_items();
879
 
  /* Reset where. */
880
 
  where= Session::DEFAULT_WHERE;
 
841
 
 
842
  /* Reset _where. */
 
843
  _where= Session::DEFAULT_WHERE;
881
844
 
882
845
  /* Reset the temporary shares we built */
883
846
  for_each(temporary_shares.begin(),
908
871
                                     bool allocate_lex_string)
909
872
{
910
873
  if (allocate_lex_string)
911
 
    if (!(lex_str= (LEX_STRING *)alloc(sizeof(LEX_STRING))))
 
874
    if (!(lex_str= (LEX_STRING *)getMemRoot()->allocate(sizeof(LEX_STRING))))
912
875
      return 0;
913
876
  if (!(lex_str->str= mem_root->strmake_root(str, length)))
914
877
    return 0;
1649
1612
  @param  session   Thread handle
1650
1613
  @param  all   true <=> rollback main transaction.
1651
1614
*/
1652
 
void mark_transaction_to_rollback(Session *session, bool all)
 
1615
void Session::markTransactionForRollback(bool all)
1653
1616
{
1654
 
  if (session)
1655
 
  {
1656
 
    session->is_fatal_sub_stmt_error= true;
1657
 
    session->transaction_rollback_request= all;
1658
 
  }
 
1617
  is_fatal_sub_stmt_error= true;
 
1618
  transaction_rollback_request= all;
1659
1619
}
1660
1620
 
1661
1621
void Session::disconnect(enum error_t errcode)
1673
1633
  {
1674
1634
    if (not getKilled() && variables.log_warnings > 1)
1675
1635
    {
1676
 
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_NEW_ABORTING_CONNECTION)
 
1636
      errmsg_printf(error::WARN, ER(ER_NEW_ABORTING_CONNECTION)
1677
1637
                  , thread_id
1678
1638
                  , (_schema->empty() ? "unconnected" : _schema->c_str())
1679
1639
                  , security_ctx->username().empty() == false ? security_ctx->username().c_str() : "unauthenticated"
1811
1771
 
1812
1772
user_var_entry *Session::getVariable(const std::string  &name, bool create_if_not_exists)
1813
1773
{
1814
 
  UserVarsRange ppp= user_vars.equal_range(name);
 
1774
  if (cleanup_done)
 
1775
    return NULL;
1815
1776
 
1816
 
  for (UserVars::iterator iter= ppp.first;
1817
 
       iter != ppp.second; ++iter)
1818
 
  {
 
1777
  UserVars::iterator iter= user_vars.find(name);
 
1778
  if (iter != user_vars.end())
1819
1779
    return (*iter).second;
1820
 
  }
1821
1780
 
1822
1781
  if (not create_if_not_exists)
1823
1782
    return NULL;
1841
1800
void Session::setVariable(const std::string &name, const std::string &value)
1842
1801
{
1843
1802
  user_var_entry *updateable_var= getVariable(name.c_str(), true);
1844
 
 
1845
 
  updateable_var->update_hash(false,
1846
 
                              (void*)value.c_str(),
1847
 
                              static_cast<uint32_t>(value.length()), STRING_RESULT,
1848
 
                              &my_charset_bin,
1849
 
                              DERIVATION_IMPLICIT, false);
 
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
  }
1850
1811
}
1851
1812
 
1852
1813
void Open_tables_state::mark_temp_tables_as_free_for_reuse()
1901
1862
  {
1902
1863
    TransactionServices &transaction_services= TransactionServices::singleton();
1903
1864
    main_da.can_overwrite_status= true;
1904
 
    transaction_services.autocommitOrRollback(this, is_error());
 
1865
    transaction_services.autocommitOrRollback(*this, is_error());
1905
1866
    main_da.can_overwrite_status= false;
1906
1867
    transaction.stmt.reset();
1907
1868
  }
1984
1945
    {
1985
1946
      std::string path;
1986
1947
      identifier.getSQLPath(path);
1987
 
      errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
 
1948
      errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
1988
1949
                    path.c_str(), errno);
1989
1950
    }
1990
1951
 
2003
1964
  {
2004
1965
    std::string path;
2005
1966
    identifier.getSQLPath(path);
2006
 
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
 
1967
    errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
2007
1968
                  path.c_str(), error);
2008
1969
 
2009
1970
    return true;
2028
1989
  {
2029
1990
    bool have_proto= false;
2030
1991
 
2031
 
    message::Table *proto= table->getShare()->getTableProto();
2032
 
    if (table->getShare()->getTableProto())
 
1992
    message::Table *proto= table->getShare()->getTableMessage();
 
1993
    if (table->getShare()->getTableMessage())
2033
1994
      have_proto= true;
2034
1995
 
2035
1996
    const char *answer= have_proto ? "true" : "false";
2046
2007
  }
2047
2008
}
2048
2009
 
2049
 
bool Session::TableMessages::storeTableMessage(const identifier::Table &identifier, message::Table &table_message)
2050
 
{
2051
 
  table_message_cache.insert(make_pair(identifier.getPath(), table_message));
2052
 
 
2053
 
  return true;
2054
 
}
2055
 
 
2056
 
bool Session::TableMessages::removeTableMessage(const identifier::Table &identifier)
2057
 
{
2058
 
  TableMessageCache::iterator iter;
2059
 
 
2060
 
  iter= table_message_cache.find(identifier.getPath());
2061
 
 
2062
 
  if (iter == table_message_cache.end())
2063
 
    return false;
2064
 
 
2065
 
  table_message_cache.erase(iter);
2066
 
 
2067
 
  return true;
2068
 
}
2069
 
 
2070
 
bool Session::TableMessages::getTableMessage(const identifier::Table &identifier, message::Table &table_message)
2071
 
{
2072
 
  TableMessageCache::iterator iter;
2073
 
 
2074
 
  iter= table_message_cache.find(identifier.getPath());
2075
 
 
2076
 
  if (iter == table_message_cache.end())
2077
 
    return false;
2078
 
 
2079
 
  table_message.CopyFrom(((*iter).second));
2080
 
 
2081
 
  return true;
2082
 
}
2083
 
 
2084
 
bool Session::TableMessages::doesTableMessageExist(const identifier::Table &identifier)
2085
 
{
2086
 
  TableMessageCache::iterator iter;
2087
 
 
2088
 
  iter= table_message_cache.find(identifier.getPath());
2089
 
 
2090
 
  if (iter == table_message_cache.end())
2091
 
  {
2092
 
    return false;
2093
 
  }
2094
 
 
2095
 
  return true;
2096
 
}
2097
 
 
2098
 
bool Session::TableMessages::renameTableMessage(const identifier::Table &from, const identifier::Table &to)
2099
 
{
2100
 
  TableMessageCache::iterator iter;
2101
 
 
2102
 
  table_message_cache[to.getPath()]= table_message_cache[from.getPath()];
2103
 
 
2104
 
  iter= table_message_cache.find(to.getPath());
2105
 
 
2106
 
  if (iter == table_message_cache.end())
2107
 
  {
2108
 
    return false;
2109
 
  }
2110
 
 
2111
 
  (*iter).second.set_schema(to.getSchemaName());
2112
 
  (*iter).second.set_name(to.getTableName());
2113
 
 
2114
 
  return true;
2115
 
}
2116
 
 
2117
2010
table::Singular *Session::getInstanceTable()
2118
2011
{
2119
2012
  temporary_shares.push_back(new table::Singular()); // This will not go into the tableshare cache, so no key is used.