~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.cc

  • Committer: Brian Aker
  • Date: 2011-01-29 23:08:49 UTC
  • mto: This revision was merged to the branch mainline in revision 2128.
  • Revision ID: brian@tangent.org-20110129230849-ga5zr8fq1bavtygz
Merge in changes for catalogs usage of constants for identifier.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * @file Implementation of the Session class and API
22
22
 */
23
23
 
24
 
#include <config.h>
25
 
 
26
 
#include <drizzled/copy_field.h>
27
 
#include <drizzled/data_home.h>
28
 
#include <drizzled/display.h>
29
 
#include <drizzled/drizzled.h>
30
 
#include <drizzled/error.h>
31
 
#include <drizzled/gettext.h>
32
 
#include <drizzled/identifier.h>
33
 
#include <drizzled/internal/iocache.h>
34
 
#include <drizzled/internal/thread_var.h>
35
 
#include <drizzled/internal_error_handler.h>
36
 
#include <drizzled/item/cache.h>
37
 
#include <drizzled/item/empty_string.h>
38
 
#include <drizzled/item/float.h>
39
 
#include <drizzled/item/return_int.h>
40
 
#include <drizzled/lock.h>
41
 
#include <drizzled/plugin/authentication.h>
42
 
#include <drizzled/plugin/client.h>
43
 
#include <drizzled/plugin/event_observer.h>
44
 
#include <drizzled/plugin/logging.h>
45
 
#include <drizzled/plugin/query_rewrite.h>
46
 
#include <drizzled/plugin/scheduler.h>
47
 
#include <drizzled/plugin/transactional_storage_engine.h>
48
 
#include <drizzled/probes.h>
49
 
#include <drizzled/pthread_globals.h>
50
 
#include <drizzled/query_id.h>
51
 
#include <drizzled/refresh_version.h>
52
 
#include <drizzled/select_dump.h>
53
 
#include <drizzled/select_exists_subselect.h>
54
 
#include <drizzled/select_export.h>
55
 
#include <drizzled/select_max_min_finder_subselect.h>
56
 
#include <drizzled/select_singlerow_subselect.h>
57
 
#include <drizzled/select_subselect.h>
58
 
#include <drizzled/select_to_file.h>
59
 
#include <drizzled/session.h>
60
 
#include <drizzled/session/cache.h>
61
 
#include <drizzled/show.h>
62
 
#include <drizzled/sql_base.h>
63
 
#include <drizzled/table/singular.h>
64
 
#include <drizzled/table_proto.h>
65
 
#include <drizzled/tmp_table_param.h>
66
 
#include <drizzled/transaction_services.h>
67
 
#include <drizzled/user_var_entry.h>
68
 
#include <drizzled/util/functors.h>
69
 
#include <plugin/myisam/myisam.h>
70
 
 
 
24
#include "config.h"
 
25
#include "drizzled/session.h"
 
26
#include "drizzled/session/cache.h"
 
27
#include <sys/stat.h>
 
28
#include "drizzled/error.h"
 
29
#include "drizzled/gettext.h"
 
30
#include "drizzled/query_id.h"
 
31
#include "drizzled/data_home.h"
 
32
#include "drizzled/sql_base.h"
 
33
#include "drizzled/lock.h"
 
34
#include "drizzled/item/cache.h"
 
35
#include "drizzled/item/float.h"
 
36
#include "drizzled/item/return_int.h"
 
37
#include "drizzled/item/empty_string.h"
 
38
#include "drizzled/show.h"
 
39
#include "drizzled/plugin/client.h"
 
40
#include "drizzled/plugin/scheduler.h"
 
41
#include "drizzled/plugin/authentication.h"
 
42
#include "drizzled/plugin/logging.h"
 
43
#include "drizzled/plugin/transactional_storage_engine.h"
 
44
#include "drizzled/plugin/query_rewrite.h"
 
45
#include "drizzled/probes.h"
 
46
#include "drizzled/table_proto.h"
 
47
#include "drizzled/db.h"
 
48
#include "drizzled/pthread_globals.h"
 
49
#include "drizzled/transaction_services.h"
 
50
#include "drizzled/drizzled.h"
 
51
 
 
52
#include "drizzled/identifier.h"
 
53
 
 
54
#include "drizzled/table/singular.h"
 
55
 
 
56
#include "plugin/myisam/myisam.h"
 
57
#include "drizzled/internal/iocache.h"
 
58
#include "drizzled/internal/thread_var.h"
 
59
#include "drizzled/plugin/event_observer.h"
 
60
 
 
61
#include "drizzled/util/functors.h"
 
62
 
 
63
#include "drizzled/display.h"
 
64
 
 
65
#include <fcntl.h>
71
66
#include <algorithm>
72
67
#include <climits>
73
 
#include <fcntl.h>
74
 
#include <sys/stat.h>
75
68
 
76
69
#include <boost/filesystem.hpp>
77
70
#include <boost/checked_delete.hpp>
78
71
 
79
 
#include <drizzled/util/backtrace.h>
80
 
 
81
 
#include <drizzled/schema.h>
 
72
#include "drizzled/util/backtrace.h"
82
73
 
83
74
using namespace std;
84
75
 
123
114
  return fd;
124
115
}
125
116
 
 
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
 
126
140
void **Session::getEngineData(const plugin::MonitoredInTransaction *monitored)
127
141
{
128
142
  return static_cast<void **>(&ha_data[monitored->getId()].ha_ptr);
139
153
  return session->options & test_options;
140
154
}
141
155
 
 
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
 
142
166
Session::Session(plugin::Client *client_arg, catalog::Instance::shared_ptr catalog_arg) :
143
167
  Open_tables_state(refresh_version),
144
168
  mem_root(&main_mem_root),
188
212
  is_fatal_error(false),
189
213
  transaction_rollback_request(false),
190
214
  is_fatal_sub_stmt_error(0),
 
215
  derived_tables_processing(false),
191
216
  tablespace_op(false),
192
 
  derived_tables_processing(false),
193
217
  m_lip(NULL),
194
218
  cached_table(0),
195
219
  transaction_message(NULL),
237
261
  open_options=ha_open_options;
238
262
  update_lock_default= TL_WRITE;
239
263
  session_tx_isolation= (enum_tx_isolation) variables.tx_isolation;
240
 
  warn_list.clear();
 
264
  warn_list.empty();
241
265
  memset(warn_count, 0, sizeof(warn_count));
242
266
  memset(&status_var, 0, sizeof(status_var));
243
267
 
311
335
  m_internal_handler= NULL;
312
336
}
313
337
 
314
 
void Session::get_xid(DrizzleXid *xid)
 
338
void Session::get_xid(DRIZZLE_XID *xid)
315
339
{
316
 
  *xid = *(DrizzleXid *) &transaction.xid_state.xid;
 
340
  *xid = *(DRIZZLE_XID *) &transaction.xid_state.xid;
317
341
}
318
342
 
319
343
/* Do operations that may take a long time */
364
388
    assert(security_ctx);
365
389
    if (global_system_variables.log_warnings)
366
390
    {
367
 
      errmsg_printf(error::WARN, ER(ER_FORCING_CLOSE),
 
391
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_FORCING_CLOSE),
368
392
                    internal::my_progname,
369
393
                    thread_id,
370
394
                    security_ctx->username().c_str());
397
421
 
398
422
  plugin::Logging::postEndDo(this);
399
423
  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();
400
430
}
401
431
 
402
432
void Session::setClient(plugin::Client *client_arg)
592
622
*/
593
623
bool Session::isViewable(identifier::User::const_reference user_arg) const
594
624
{
595
 
  return plugin::Authorization::isAuthorized(user_arg, *this, false);
 
625
  return plugin::Authorization::isAuthorized(user_arg, this, false);
596
626
}
597
627
 
598
628
 
635
665
                        const std::string &in_db)
636
666
{
637
667
  bool is_authenticated=
638
 
    plugin::Authentication::isAuthenticated(*user(), passwd_str);
 
668
    plugin::Authentication::isAuthenticated(user(), passwd_str);
639
669
 
640
670
  if (is_authenticated != true)
641
671
  {
648
678
  if (not in_db.empty())
649
679
  {
650
680
    identifier::Schema identifier(in_db);
651
 
    if (schema::change(*this, identifier))
 
681
    if (change_db(this, identifier))
652
682
    {
653
683
      /* change_db() has pushed the error message. */
654
684
      return false;
718
748
    plugin::QueryRewriter::rewriteQuery(*_schema, *new_query);
719
749
  }
720
750
  query.reset(new_query);
721
 
  _state.reset(new session::State(in_packet, in_packet_length));
 
751
  _state.reset(new State(in_packet, in_packet_length));
722
752
 
723
753
  return true;
724
754
}
808
838
{
809
839
  bool result= true;
810
840
 
811
 
  assert(! inTransaction());
812
 
 
813
 
  options|= OPTION_BEGIN;
814
 
  server_status|= SERVER_STATUS_IN_TRANS;
815
 
 
816
 
  if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
 
841
  if (! endActiveTransaction())
817
842
  {
818
843
    result= false;
819
844
  }
 
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
  }
820
855
 
821
856
  return result;
822
857
}
876
911
                                     bool allocate_lex_string)
877
912
{
878
913
  if (allocate_lex_string)
879
 
    if (!(lex_str= (LEX_STRING *)getMemRoot()->allocate(sizeof(LEX_STRING))))
 
914
    if (!(lex_str= (LEX_STRING *)alloc(sizeof(LEX_STRING))))
880
915
      return 0;
881
916
  if (!(lex_str->str= mem_root->strmake_root(str, length)))
882
917
    return 0;
1090
1125
 
1091
1126
  /* Check if there is any blobs in data */
1092
1127
  {
1093
 
    List<Item>::iterator li(list.begin());
 
1128
    List_iterator_fast<Item> li(list);
1094
1129
    Item *item;
1095
1130
    while ((item=li++))
1096
1131
    {
1155
1190
  row_count++;
1156
1191
  Item *item;
1157
1192
  uint32_t used_length=0,items_left=items.elements;
1158
 
  List<Item>::iterator li(items.begin());
 
1193
  List_iterator_fast<Item> li(items);
1159
1194
 
1160
1195
  if (my_b_write(cache,(unsigned char*) exchange->line_start->ptr(),
1161
1196
                 exchange->line_start->length()))
1344
1379
 
1345
1380
bool select_dump::send_data(List<Item> &items)
1346
1381
{
1347
 
  List<Item>::iterator li(items.begin());
 
1382
  List_iterator_fast<Item> li(items);
1348
1383
  char buff[MAX_FIELD_WIDTH];
1349
1384
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1350
1385
  tmp.length(0);
1397
1432
    unit->offset_limit_cnt--;
1398
1433
    return(0);
1399
1434
  }
1400
 
  List<Item>::iterator li(items.begin());
 
1435
  List_iterator_fast<Item> li(items);
1401
1436
  Item *val_item;
1402
1437
  for (uint32_t i= 0; (val_item= li++); i++)
1403
1438
    it->store(i, val_item);
1415
1450
bool select_max_min_finder_subselect::send_data(List<Item> &items)
1416
1451
{
1417
1452
  Item_maxmin_subselect *it= (Item_maxmin_subselect *)item;
1418
 
  List<Item>::iterator li(items.begin());
 
1453
  List_iterator_fast<Item> li(items);
1419
1454
  Item *val_item= li++;
1420
1455
  it->register_value();
1421
1456
  if (it->assigned())
1617
1652
  @param  session   Thread handle
1618
1653
  @param  all   true <=> rollback main transaction.
1619
1654
*/
1620
 
void Session::markTransactionForRollback(bool all)
 
1655
void mark_transaction_to_rollback(Session *session, bool all)
1621
1656
{
1622
 
  is_fatal_sub_stmt_error= true;
1623
 
  transaction_rollback_request= all;
 
1657
  if (session)
 
1658
  {
 
1659
    session->is_fatal_sub_stmt_error= true;
 
1660
    session->transaction_rollback_request= all;
 
1661
  }
1624
1662
}
1625
1663
 
1626
1664
void Session::disconnect(enum error_t errcode)
1638
1676
  {
1639
1677
    if (not getKilled() && variables.log_warnings > 1)
1640
1678
    {
1641
 
      errmsg_printf(error::WARN, ER(ER_NEW_ABORTING_CONNECTION)
 
1679
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_NEW_ABORTING_CONNECTION)
1642
1680
                  , thread_id
1643
1681
                  , (_schema->empty() ? "unconnected" : _schema->c_str())
1644
1682
                  , security_ctx->username().empty() == false ? security_ctx->username().c_str() : "unauthenticated"
1776
1814
 
1777
1815
user_var_entry *Session::getVariable(const std::string  &name, bool create_if_not_exists)
1778
1816
{
1779
 
  if (cleanup_done)
1780
 
    return NULL;
 
1817
  UserVarsRange ppp= user_vars.equal_range(name);
1781
1818
 
1782
 
  UserVars::iterator iter= user_vars.find(name);
1783
 
  if (iter != user_vars.end())
 
1819
  for (UserVars::iterator iter= ppp.first;
 
1820
       iter != ppp.second; ++iter)
 
1821
  {
1784
1822
    return (*iter).second;
 
1823
  }
1785
1824
 
1786
1825
  if (not create_if_not_exists)
1787
1826
    return NULL;
1805
1844
void Session::setVariable(const std::string &name, const std::string &value)
1806
1845
{
1807
1846
  user_var_entry *updateable_var= getVariable(name.c_str(), true);
1808
 
  if (updateable_var)
1809
 
  {
1810
 
    updateable_var->update_hash(false,
1811
 
                                (void*)value.c_str(),
1812
 
                                static_cast<uint32_t>(value.length()), STRING_RESULT,
1813
 
                                &my_charset_bin,
1814
 
                                DERIVATION_IMPLICIT, false);
1815
 
  }
 
1847
 
 
1848
  updateable_var->update_hash(false,
 
1849
                              (void*)value.c_str(),
 
1850
                              static_cast<uint32_t>(value.length()), STRING_RESULT,
 
1851
                              &my_charset_bin,
 
1852
                              DERIVATION_IMPLICIT, false);
1816
1853
}
1817
1854
 
1818
1855
void Open_tables_state::mark_temp_tables_as_free_for_reuse()
1950
1987
    {
1951
1988
      std::string path;
1952
1989
      identifier.getSQLPath(path);
1953
 
      errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
 
1990
      errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
1954
1991
                    path.c_str(), errno);
1955
1992
    }
1956
1993
 
1969
2006
  {
1970
2007
    std::string path;
1971
2008
    identifier.getSQLPath(path);
1972
 
    errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
 
2009
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
1973
2010
                  path.c_str(), error);
1974
2011
 
1975
2012
    return true;
1994
2031
  {
1995
2032
    bool have_proto= false;
1996
2033
 
1997
 
    message::Table *proto= table->getShare()->getTableMessage();
1998
 
    if (table->getShare()->getTableMessage())
 
2034
    message::Table *proto= table->getShare()->getTableProto();
 
2035
    if (table->getShare()->getTableProto())
1999
2036
      have_proto= true;
2000
2037
 
2001
2038
    const char *answer= have_proto ? "true" : "false";
2012
2049
  }
2013
2050
}
2014
2051
 
 
2052
bool Session::TableMessages::storeTableMessage(const identifier::Table &identifier, message::Table &table_message)
 
2053
{
 
2054
  table_message_cache.insert(make_pair(identifier.getPath(), table_message));
 
2055
 
 
2056
  return true;
 
2057
}
 
2058
 
 
2059
bool Session::TableMessages::removeTableMessage(const identifier::Table &identifier)
 
2060
{
 
2061
  TableMessageCache::iterator iter;
 
2062
 
 
2063
  iter= table_message_cache.find(identifier.getPath());
 
2064
 
 
2065
  if (iter == table_message_cache.end())
 
2066
    return false;
 
2067
 
 
2068
  table_message_cache.erase(iter);
 
2069
 
 
2070
  return true;
 
2071
}
 
2072
 
 
2073
bool Session::TableMessages::getTableMessage(const identifier::Table &identifier, message::Table &table_message)
 
2074
{
 
2075
  TableMessageCache::iterator iter;
 
2076
 
 
2077
  iter= table_message_cache.find(identifier.getPath());
 
2078
 
 
2079
  if (iter == table_message_cache.end())
 
2080
    return false;
 
2081
 
 
2082
  table_message.CopyFrom(((*iter).second));
 
2083
 
 
2084
  return true;
 
2085
}
 
2086
 
 
2087
bool Session::TableMessages::doesTableMessageExist(const identifier::Table &identifier)
 
2088
{
 
2089
  TableMessageCache::iterator iter;
 
2090
 
 
2091
  iter= table_message_cache.find(identifier.getPath());
 
2092
 
 
2093
  if (iter == table_message_cache.end())
 
2094
  {
 
2095
    return false;
 
2096
  }
 
2097
 
 
2098
  return true;
 
2099
}
 
2100
 
 
2101
bool Session::TableMessages::renameTableMessage(const identifier::Table &from, const identifier::Table &to)
 
2102
{
 
2103
  TableMessageCache::iterator iter;
 
2104
 
 
2105
  table_message_cache[to.getPath()]= table_message_cache[from.getPath()];
 
2106
 
 
2107
  iter= table_message_cache.find(to.getPath());
 
2108
 
 
2109
  if (iter == table_message_cache.end())
 
2110
  {
 
2111
    return false;
 
2112
  }
 
2113
 
 
2114
  (*iter).second.set_schema(to.getSchemaName());
 
2115
  (*iter).second.set_name(to.getTableName());
 
2116
 
 
2117
  return true;
 
2118
}
 
2119
 
2015
2120
table::Singular *Session::getInstanceTable()
2016
2121
{
2017
2122
  temporary_shares.push_back(new table::Singular()); // This will not go into the tableshare cache, so no key is used.