~drizzle-trunk/drizzle/development

575.1.5 by Monty Taylor
Moved stuff to handlerton.cc
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
575.1.5 by Monty Taylor
Moved stuff to handlerton.cc
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
21
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
22
#include <fcntl.h>
23
#include <unistd.h>
24
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
25
#include <string>
26
#include <vector>
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
27
#include <set>
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
28
#include <fstream>
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
29
#include <algorithm>
30
#include <functional>
31
32
#include <google/protobuf/io/zero_copy_stream.h>
33
#include <google/protobuf/io/zero_copy_stream_impl.h>
34
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
35
#include <drizzled/cached_directory.h>
575.1.5 by Monty Taylor
Moved stuff to handlerton.cc
36
#include <drizzled/definitions.h>
37
#include <drizzled/base.h>
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
38
#include <drizzled/cursor.h>
960.2.23 by Monty Taylor
Moved handlerton to plugin/storage_engine.h.
39
#include <drizzled/plugin/storage_engine.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
40
#include <drizzled/session.h>
575.1.5 by Monty Taylor
Moved stuff to handlerton.cc
41
#include <drizzled/error.h>
42
#include <drizzled/gettext.h>
971.1.37 by Monty Taylor
Another one bites the dust.
43
#include <drizzled/unireg.h>
971.1.38 by Monty Taylor
Moved delete table.
44
#include <drizzled/data_home.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
45
#include <drizzled/errmsg_print.h>
46
#include <drizzled/xid.h>
47
#include <drizzled/sql_table.h>
48
#include <drizzled/global_charset_info.h>
49
#include <drizzled/charset.h>
50
#include <drizzled/internal/my_sys.h>
1095.3.3 by Stewart Smith
move drizzle_proto_exists and drizzle_read_table_proto out of unireg.h and into table_proto.h
51
#include <drizzled/table_proto.h>
1502.5.7 by Barry.Leslie at PrimeBase
Renamed the 'Event' plugin to 'EventObserver' plugin along with some internal class renames to make things clearer.
52
#include <drizzled/plugin/event_observer.h>
1859.2.9 by Brian Aker
We add a "shell" table type, used only during field creation.
53
#include <drizzled/table/shell.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
54
#include <drizzled/message/cache.h>
2198.1.1 by Olaf van der Spek
Remove unnecessary alter* includes
55
#include <drizzled/key.h>
2241.3.4 by Olaf van der Spek
Refactor Session::transaction
56
#include <drizzled/session/transactions.h>
2263.3.11 by Olaf van der Spek
Open Tables
57
#include <drizzled/open_tables_state.h>
1910.2.14 by Brian Aker
Check simple cache performance hit.
58
1685.2.10 by Brian Aker
Optimize the string comparison a bit.
59
#include <boost/algorithm/string/compare.hpp>
60
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
61
static bool shutdown_has_begun= false; // Once we put in the container for the vector/etc for engines this will go away.
62
2281.4.3 by Olaf van der Spek
Prune
63
namespace drizzled {
64
namespace plugin {
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
65
66
static EngineVector vector_of_engines;
1273.19.30 by Brian Aker
Performance patch... we now only cycle through engines that actually have
67
static EngineVector vector_of_schema_engines;
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
68
1685.2.10 by Brian Aker
Optimize the string comparison a bit.
69
const std::string DEFAULT_STRING("default");
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
70
const std::string UNKNOWN_STRING("UNKNOWN");
71
const std::string DEFAULT_DEFINITION_FILE_EXT(".dfe");
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
72
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
73
static std::set<std::string> set_of_table_definition_ext;
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
74
1415 by Brian Aker
Mass overhaul to use schema_identifier.
75
EngineVector &StorageEngine::getSchemaEngines()
76
{
77
  return vector_of_schema_engines;
78
}
79
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
80
StorageEngine::StorageEngine(const std::string name_arg,
81
                             const std::bitset<HTON_BIT_SIZE> &flags_arg) :
1372.1.4 by Brian Aker
Update to remove cache in enginges for per session (which also means... no
82
  Plugin(name_arg, "StorageEngine"),
83
  MonitoredInTransaction(), /* This gives the storage engine a "slot" or ID */
84
  flags(flags_arg)
964.1.6 by Monty Taylor
Moved savepoint stuff to protected virtual methods, moved the offset to private, and moved the logic about adding the offset to the passed-in pointer and adding the offset to the global offset inside of the class.
85
{
86
}
87
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
88
StorageEngine::~StorageEngine()
964.1.6 by Monty Taylor
Moved savepoint stuff to protected virtual methods, moved the offset to private, and moved the logic about adding the offset to the passed-in pointer and adding the offset to the global offset inside of the class.
89
{
90
}
91
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
92
void StorageEngine::setTransactionReadWrite(Session& session)
1039.3.8 by Stewart Smith
Re-introduce marking transaction as read-write for engine on create_table:
93
{
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
94
  TransactionContext &statement_ctx= session.transaction.stmt;
95
  statement_ctx.markModifiedNonTransData();
1039.3.8 by Stewart Smith
Re-introduce marking transaction as read-write for engine on create_table:
96
}
97
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
98
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
99
int StorageEngine::renameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
1039.3.5 by Stewart Smith
move handler::rename_table to StorageEngine
100
{
1502.5.2 by Barry.Leslie at PrimeBase
Changes made to drizzle source when building in the events plugin.
101
  int error;
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
102
  setTransactionReadWrite(session);
103
1502.5.8 by Barry.Leslie at PrimeBase
- Changed names to match the drizzle naming convention.
104
  if (unlikely(plugin::EventObserver::beforeRenameTable(session, from, to)))
1502.5.7 by Barry.Leslie at PrimeBase
Renamed the 'Event' plugin to 'EventObserver' plugin along with some internal class renames to make things clearer.
105
  {
106
    error= ER_EVENT_OBSERVER_PLUGIN;
107
  }
1502.5.2 by Barry.Leslie at PrimeBase
Changes made to drizzle source when building in the events plugin.
108
  else
109
  {
110
    error =  doRenameTable(session, from, to);
1502.5.8 by Barry.Leslie at PrimeBase
- Changed names to match the drizzle naming convention.
111
    if (unlikely(plugin::EventObserver::afterRenameTable(session, from, to, error)))
1502.5.7 by Barry.Leslie at PrimeBase
Renamed the 'Event' plugin to 'EventObserver' plugin along with some internal class renames to make things clearer.
112
    {
113
      error= ER_EVENT_OBSERVER_PLUGIN;
114
    }
1502.5.2 by Barry.Leslie at PrimeBase
Changes made to drizzle source when building in the events plugin.
115
  }
116
  
117
  return error;
1039.3.5 by Stewart Smith
move handler::rename_table to StorageEngine
118
}
119
1039.3.2 by Stewart Smith
move delete_table out of handler and up into StorageEngine where it belongs.
120
/**
121
  Delete all files with extension from bas_ext().
122
123
  @param name		Base name of table
124
125
  @note
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
126
    We assume that the Cursor may return more extensions than
1039.3.2 by Stewart Smith
move delete_table out of handler and up into StorageEngine where it belongs.
127
    was actually used for the file.
128
129
  @retval
130
    0   If we successfully deleted at least one file from base_ext and
131
    didn't get any other errors than ENOENT
132
  @retval
133
    !0  Error
134
*/
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
135
int StorageEngine::doDropTable(Session&, const identifier::Table &identifier)
1358.1.3 by Brian Aker
doDropTable() now only uses identifier.
136
                               
1039.3.2 by Stewart Smith
move delete_table out of handler and up into StorageEngine where it belongs.
137
{
138
  int error= 0;
139
  int enoent_or_zero= ENOENT;                   // Error if no file was deleted
140
  char buff[FN_REFLEN];
141
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
142
  for (const char **ext= bas_ext(); *ext ; ext++)
1039.3.2 by Stewart Smith
move delete_table out of handler and up into StorageEngine where it belongs.
143
  {
1358.1.9 by Brian Aker
Update for std::string
144
    internal::fn_format(buff, identifier.getPath().c_str(), "", *ext,
145
                        MY_UNPACK_FILENAME|MY_APPEND_EXT);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
146
    if (internal::my_delete_with_symlink(buff, MYF(0)))
1039.3.2 by Stewart Smith
move delete_table out of handler and up into StorageEngine where it belongs.
147
    {
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
148
      if ((error= errno) != ENOENT)
1373 by Brian Aker
Simple style cleanup.
149
        break;
1039.3.2 by Stewart Smith
move delete_table out of handler and up into StorageEngine where it belongs.
150
    }
151
    else
1395.1.12 by Brian Aker
Fixes failure related to Heap's hack on deletion. Also cleans up error
152
    {
1039.3.2 by Stewart Smith
move delete_table out of handler and up into StorageEngine where it belongs.
153
      enoent_or_zero= 0;                        // No error for ENOENT
1395.1.12 by Brian Aker
Fixes failure related to Heap's hack on deletion. Also cleans up error
154
    }
155
1039.3.2 by Stewart Smith
move delete_table out of handler and up into StorageEngine where it belongs.
156
    error= enoent_or_zero;
157
  }
158
  return error;
159
}
971.1.38 by Monty Taylor
Moved delete table.
160
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
161
bool StorageEngine::addPlugin(StorageEngine *engine)
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
162
{
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
163
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
164
  vector_of_engines.push_back(engine);
165
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
166
  if (engine->getTableDefinitionFileExtension().length())
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
167
  {
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
168
    assert(engine->getTableDefinitionFileExtension().length() == DEFAULT_DEFINITION_FILE_EXT.length());
169
    set_of_table_definition_ext.insert(engine->getTableDefinitionFileExtension());
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
170
  }
171
1273.19.30 by Brian Aker
Performance patch... we now only cycle through engines that actually have
172
  if (engine->check_flag(HTON_BIT_SCHEMA_DICTIONARY))
173
    vector_of_schema_engines.push_back(engine);
174
1130.1.19 by Monty Taylor
Added error reporting to plugin registration.
175
  return false;
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
176
}
177
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
178
void StorageEngine::removePlugin(StorageEngine *)
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
179
{
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
180
  if (shutdown_has_begun == false)
181
  {
182
    vector_of_engines.clear();
1273.19.30 by Brian Aker
Performance patch... we now only cycle through engines that actually have
183
    vector_of_schema_engines.clear();
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
184
185
    shutdown_has_begun= true;
186
  }
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
187
}
188
1228.3.2 by Monty Taylor
Removed engine_map - just use vector_of_engines.
189
class FindEngineByName
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
190
  : public std::unary_function<StorageEngine *, bool>
1228.3.2 by Monty Taylor
Removed engine_map - just use vector_of_engines.
191
{
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
192
  const std::string &predicate;
1384 by Brian Aker
Merge
193
1228.3.2 by Monty Taylor
Removed engine_map - just use vector_of_engines.
194
public:
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
195
  explicit FindEngineByName(const std::string &target_arg) :
1685.2.10 by Brian Aker
Optimize the string comparison a bit.
196
    predicate(target_arg)
1384 by Brian Aker
Merge
197
  {
198
  }
1685.2.10 by Brian Aker
Optimize the string comparison a bit.
199
1228.3.2 by Monty Taylor
Removed engine_map - just use vector_of_engines.
200
  result_type operator() (argument_type engine)
201
  {
1685.2.10 by Brian Aker
Optimize the string comparison a bit.
202
    return boost::iequals(engine->getName(), predicate);
1228.3.2 by Monty Taylor
Removed engine_map - just use vector_of_engines.
203
  }
204
};
205
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
206
StorageEngine *StorageEngine::findByName(const std::string &predicate)
1183.1.29 by Brian Aker
Clean up interface so that Truncate sets the propper engine when
207
{
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
208
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
209
                                            vector_of_engines.end(),
210
                                            FindEngineByName(predicate));
1228.3.2 by Monty Taylor
Removed engine_map - just use vector_of_engines.
211
  if (iter != vector_of_engines.end())
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
212
  {
1228.3.2 by Monty Taylor
Removed engine_map - just use vector_of_engines.
213
    StorageEngine *engine= *iter;
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
214
    if (engine->is_user_selectable())
215
      return engine;
216
  }
1183.1.29 by Brian Aker
Clean up interface so that Truncate sets the propper engine when
217
218
  return NULL;
219
}
220
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
221
StorageEngine *StorageEngine::findByName(Session& session, const std::string &predicate)
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
222
{
1685.2.10 by Brian Aker
Optimize the string comparison a bit.
223
  if (boost::iequals(predicate, DEFAULT_STRING))
1183.1.29 by Brian Aker
Clean up interface so that Truncate sets the propper engine when
224
    return session.getDefaultStorageEngine();
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
225
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
226
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
227
                                            vector_of_engines.end(),
228
                                            FindEngineByName(predicate));
1228.3.2 by Monty Taylor
Removed engine_map - just use vector_of_engines.
229
  if (iter != vector_of_engines.end())
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
230
  {
1228.3.2 by Monty Taylor
Removed engine_map - just use vector_of_engines.
231
    StorageEngine *engine= *iter;
1217.1.3 by Brian Aker
Remove NameMap from Storage Engine. Modified the cleanup code to just do a
232
    if (engine->is_user_selectable())
233
      return engine;
234
  }
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
235
236
  return NULL;
237
}
238
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
239
class StorageEngineCloseConnection : public std::unary_function<StorageEngine *, void>
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
240
{
241
  Session *session;
242
public:
243
  StorageEngineCloseConnection(Session *session_arg) : session(session_arg) {}
244
  /*
245
    there's no need to rollback here as all transactions must
246
    be rolled back already
247
  */
248
  inline result_type operator() (argument_type engine)
249
  {
1273.1.20 by Jay Pipes
Remove StorageEngine::start_consitent_snapshot() and StorageEngine::enable(), disable() and is_enabled(). Unused.
250
    if (*session->getEngineData(engine))
1240.9.6 by Monty Taylor
Removed some casts- also removed a few c-interface functions and made them actual methods on session. Also made the ha_data private. (fancy that)
251
      engine->close_connection(session);
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
252
  }
253
};
254
255
/**
256
  @note
257
    don't bother to rollback here, it's done already
258
*/
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
259
void StorageEngine::closeConnection(Session* session)
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
260
{
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
261
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
262
                StorageEngineCloseConnection(session));
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
263
}
264
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
265
bool StorageEngine::flushLogs(StorageEngine *engine)
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
266
{
267
  if (engine == NULL)
268
  {
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
269
    if (std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
270
                     std::mem_fun(&StorageEngine::flush_logs))
1309.1.3 by Brian Aker
Cache for Schema.
271
        != vector_of_engines.begin())
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
272
      return true;
273
  }
274
  else
275
  {
1273.1.20 by Jay Pipes
Remove StorageEngine::start_consitent_snapshot() and StorageEngine::enable(), disable() and is_enabled(). Unused.
276
    if (engine->flush_logs())
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
277
      return true;
278
  }
279
  return false;
280
}
281
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
282
class StorageEngineGetTableDefinition: public std::unary_function<StorageEngine *,bool>
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
283
{
1183.1.29 by Brian Aker
Clean up interface so that Truncate sets the propper engine when
284
  Session& session;
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
285
  const identifier::Table &identifier;
1354.1.1 by Brian Aker
Modify ptr to reference.
286
  message::Table &table_message;
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
287
  drizzled::error_t &err;
1183.5.1 by Brian Aker
Extended definition interface (filename building should now be moved
288
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
289
public:
1200 by Brian Aker
Clean up some mispellings
290
  StorageEngineGetTableDefinition(Session& session_arg,
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
291
                                  const identifier::Table &identifier_arg,
1354.1.1 by Brian Aker
Modify ptr to reference.
292
                                  message::Table &table_message_arg,
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
293
                                  drizzled::error_t &err_arg) :
1183.1.29 by Brian Aker
Clean up interface so that Truncate sets the propper engine when
294
    session(session_arg), 
1358.1.2 by Brian Aker
Long pass through the system to use more of TableIdentifiers.
295
    identifier(identifier_arg),
1320.1.1 by Brian Aker
Light cleanup for references.
296
    table_message(table_message_arg), 
1183.5.1 by Brian Aker
Extended definition interface (filename building should now be moved
297
    err(err_arg) {}
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
298
299
  result_type operator() (argument_type engine)
300
  {
1395.1.12 by Brian Aker
Fixes failure related to Heap's hack on deletion. Also cleans up error
301
    int ret= engine->doGetTableDefinition(session, identifier, table_message);
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
302
303
    if (ret != ENOENT)
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
304
      err= static_cast<drizzled::error_t>(ret);
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
305
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
306
    return err == static_cast<drizzled::error_t>(EEXIST) or err != static_cast<drizzled::error_t>(ENOENT);
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
307
  }
308
};
309
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
310
class StorageEngineDoesTableExist: public std::unary_function<StorageEngine *, bool>
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
311
{
312
  Session& session;
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
313
  const identifier::Table &identifier;
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
314
315
public:
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
316
  StorageEngineDoesTableExist(Session& session_arg, const identifier::Table &identifier_arg) :
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
317
    session(session_arg), 
318
    identifier(identifier_arg) 
319
  { }
320
321
  result_type operator() (argument_type engine)
322
  {
323
    return engine->doDoesTableExist(session, identifier);
324
  }
325
};
326
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
327
/**
1273.19.11 by Brian Aker
This restores temporary tables back to being viewable via show/select.
328
  Utility method which hides some of the details of getTableDefinition()
329
*/
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
330
bool plugin::StorageEngine::doesTableExist(Session &session,
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
331
                                           const identifier::Table &identifier,
1273.19.11 by Brian Aker
This restores temporary tables back to being viewable via show/select.
332
                                           bool include_temporary_tables)
333
{
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
334
  if (include_temporary_tables)
335
  {
2263.3.2 by Olaf van der Spek
Use open_tables
336
    if (session.open_tables.doDoesTableExist(identifier))
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
337
      return true;
338
  }
339
340
  EngineVector::iterator iter=
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
341
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
342
                 StorageEngineDoesTableExist(session, identifier));
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
343
1414 by Brian Aker
Remove dead type.
344
  if (iter == vector_of_engines.end())
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
345
  {
346
    return false;
347
  }
348
349
  return true;
350
}
351
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
352
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::identifier::Table&)
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
353
{
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
354
  std::cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
355
  assert(0);
356
  return false;
1273.19.11 by Brian Aker
This restores temporary tables back to being viewable via show/select.
357
}
358
2068.7.8 by Brian Aker
Merge in changes for table message API.
359
message::table::shared_ptr StorageEngine::getTableMessage(Session& session,
2246.4.10 by Olaf van der Spek
Remove const_reference and reference from identifier::Table
360
                                                          const identifier::Table& identifier,
2068.7.8 by Brian Aker
Merge in changes for table message API.
361
                                                          bool include_temporary_tables)
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
362
{
2159.2.4 by Brian Aker
Merge in error wasteful removal.
363
  drizzled::error_t error;
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
364
  error= static_cast<drizzled::error_t>(ENOENT);
365
366
  if (include_temporary_tables)
367
  {
2263.3.2 by Olaf van der Spek
Use open_tables
368
    Table *table= session.open_tables.find_temporary_table(identifier);
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
369
    if (table)
370
    {
2134.1.6 by Brian Aker
Merge in encapsulation of the table message from the main class.
371
      return message::table::shared_ptr(new message::Table(*table->getShare()->getTableMessage()));
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
372
    }
373
  }
374
375
  drizzled::message::table::shared_ptr table_ptr;
376
  if ((table_ptr= drizzled::message::Cache::singleton().find(identifier)))
377
  {
2068.7.8 by Brian Aker
Merge in changes for table message API.
378
    (void)table_ptr;
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
379
  }
380
381
  message::Table message;
382
  EngineVector::iterator iter=
383
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
384
                 StorageEngineGetTableDefinition(session, identifier, message, error));
385
386
  if (iter == vector_of_engines.end())
387
  {
2068.7.8 by Brian Aker
Merge in changes for table message API.
388
    return message::table::shared_ptr();
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
389
  }
2068.7.8 by Brian Aker
Merge in changes for table message API.
390
  message::table::shared_ptr table_message(new message::Table(message));
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
391
392
  drizzled::message::Cache::singleton().insert(identifier, table_message);
393
2068.7.8 by Brian Aker
Merge in changes for table message API.
394
  return table_message;
2068.7.7 by Brian Aker
Add additional getTableMessage() method which will have a good return type.
395
}
396
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
397
class DropTableByIdentifier: public std::unary_function<EngineVector::value_type, bool>
398
{
2246.4.11 by Olaf van der Spek
Remove const_reference and reference from identifier::User
399
  Session& session;
2246.4.10 by Olaf van der Spek
Remove const_reference and reference from identifier::Table
400
  const identifier::Table& identifier;
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
401
  drizzled::error_t &error;
402
403
public:
404
2246.4.11 by Olaf van der Spek
Remove const_reference and reference from identifier::User
405
  DropTableByIdentifier(Session& session_arg,
2246.4.10 by Olaf van der Spek
Remove const_reference and reference from identifier::Table
406
                        const identifier::Table& identifier_arg,
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
407
                        drizzled::error_t &error_arg) :
408
    session(session_arg),
409
    identifier(identifier_arg),
410
    error(error_arg)
411
  { }
412
413
  result_type operator() (argument_type engine)
414
  {
415
    if (not engine->doDoesTableExist(session, identifier))
416
      return false;
417
418
    int local_error= engine->doDropTable(session, identifier);
419
420
421
    if (not local_error)
422
      return true;
423
424
    switch (local_error)
425
    {
426
    case HA_ERR_NO_SUCH_TABLE:
427
    case ENOENT:
428
      error= static_cast<drizzled::error_t>(HA_ERR_NO_SUCH_TABLE);
429
      return false;
430
431
    default:
432
      error= static_cast<drizzled::error_t>(local_error);
433
      return true;
434
    }
435
  } 
436
};
437
438
2246.4.11 by Olaf van der Spek
Remove const_reference and reference from identifier::User
439
bool StorageEngine::dropTable(Session& session,
2246.4.10 by Olaf van der Spek
Remove const_reference and reference from identifier::Table
440
                              const identifier::Table& identifier,
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
441
                              drizzled::error_t &error)
442
{
443
  error= EE_OK;
444
445
  EngineVector::const_iterator iter= std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
446
                                                  DropTableByIdentifier(session, identifier, error));
447
448
  if (error)
449
  {
450
    return false;
451
  }
452
  else if (iter == vector_of_engines.end())
453
  {
454
    error= ER_BAD_TABLE_ERROR;
455
    return false;
456
  }
457
458
  drizzled::message::Cache::singleton().erase(identifier);
459
460
  return true;
461
}
462
463
bool StorageEngine::dropTable(Session& session,
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
464
                              const identifier::Table &identifier)
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
465
{
466
  drizzled::error_t error;
467
468
  if (not dropTable(session, identifier, error))
469
  {
470
    return false;
471
  }
472
473
  return true;
474
}
475
2246.4.11 by Olaf van der Spek
Remove const_reference and reference from identifier::User
476
bool StorageEngine::dropTable(Session& session,
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
477
                              StorageEngine &engine,
2246.4.10 by Olaf van der Spek
Remove const_reference and reference from identifier::Table
478
                              const identifier::Table& identifier,
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
479
                              drizzled::error_t &error)
480
{
481
  error= EE_OK;
1395.1.12 by Brian Aker
Fixes failure related to Heap's hack on deletion. Also cleans up error
482
  engine.setTransactionReadWrite(session);
2068.7.2 by Brian Aker
Add assert to make sure we only use this method on temp tables.
483
484
  assert(identifier.isTmp());
1502.5.2 by Barry.Leslie at PrimeBase
Changes made to drizzle source when building in the events plugin.
485
  
1502.5.8 by Barry.Leslie at PrimeBase
- Changed names to match the drizzle naming convention.
486
  if (unlikely(plugin::EventObserver::beforeDropTable(session, identifier)))
1502.5.7 by Barry.Leslie at PrimeBase
Renamed the 'Event' plugin to 'EventObserver' plugin along with some internal class renames to make things clearer.
487
  {
488
    error= ER_EVENT_OBSERVER_PLUGIN;
489
  }
490
  else
491
  {
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
492
    error= static_cast<drizzled::error_t>(engine.doDropTable(session, identifier));
493
1502.5.8 by Barry.Leslie at PrimeBase
- Changed names to match the drizzle naming convention.
494
    if (unlikely(plugin::EventObserver::afterDropTable(session, identifier, error)))
1502.5.7 by Barry.Leslie at PrimeBase
Renamed the 'Event' plugin to 'EventObserver' plugin along with some internal class renames to make things clearer.
495
    {
496
      error= ER_EVENT_OBSERVER_PLUGIN;
497
    }
498
  }
499
1910.2.14 by Brian Aker
Check simple cache performance hit.
500
  drizzled::message::Cache::singleton().erase(identifier);
1395.1.12 by Brian Aker
Fixes failure related to Heap's hack on deletion. Also cleans up error
501
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
502
  if (error)
503
  {
504
    return false;
505
  }
506
507
  return true;
1395.1.12 by Brian Aker
Fixes failure related to Heap's hack on deletion. Also cleans up error
508
}
509
1412 by Brian Aker
Innodb is now in the house (aka... it handls its own DFE).
510
971.1.38 by Monty Taylor
Moved delete table.
511
/**
512
  Initiates table-file and calls appropriate database-creator.
513
514
  @retval
515
   0  ok
516
  @retval
517
   1  error
518
*/
2068.7.6 by Brian Aker
Fix interface for create table such that it issues error and returns state
519
bool StorageEngine::createTable(Session &session,
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
520
                                const identifier::Table &identifier,
2068.7.6 by Brian Aker
Fix interface for create table such that it issues error and returns state
521
                                message::Table& table_message)
971.1.38 by Monty Taylor
Moved delete table.
522
{
2068.7.6 by Brian Aker
Fix interface for create table such that it issues error and returns state
523
  drizzled::error_t error= EE_OK;
524
1608.2.4 by Brian Aker
Update for having share declared type.
525
  TableShare share(identifier);
1859.2.11 by Brian Aker
Merge in so that shell requires a share to construct.
526
  table::Shell table(share);
1130.3.16 by Monty Taylor
Moved the last three methods from plugin/storage_engine.cc to be static methods on StorageEngine.
527
  message::Table tmp_proto;
971.1.38 by Monty Taylor
Moved delete table.
528
1626.3.2 by Brian Aker
Cleanup table_share to pass in identifier.
529
  if (share.parse_table_proto(session, table_message) || share.open_table_from_share(&session, identifier, "", 0, 0, table))
1372.1.4 by Brian Aker
Update to remove cache in enginges for per session (which also means... no
530
  { 
531
    // @note Error occured, we should probably do a little more here.
2068.7.6 by Brian Aker
Fix interface for create table such that it issues error and returns state
532
    // ER_CORRUPT_TABLE_DEFINITION,ER_CORRUPT_TABLE_DEFINITION_ENUM 
533
    
534
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
535
536
    return false;
1372.1.4 by Brian Aker
Update to remove cache in enginges for per session (which also means... no
537
  }
538
  else
539
  {
540
    /* Check for legal operations against the Engine using the proto (if used) */
541
    if (table_message.type() == message::Table::TEMPORARY &&
542
        share.storage_engine->check_flag(HTON_BIT_TEMPORARY_NOT_SUPPORTED) == true)
543
    {
544
      error= HA_ERR_UNSUPPORTED;
545
    }
546
    else if (table_message.type() != message::Table::TEMPORARY &&
547
             share.storage_engine->check_flag(HTON_BIT_TEMPORARY_ONLY) == true)
548
    {
549
      error= HA_ERR_UNSUPPORTED;
550
    }
551
    else
552
    {
1412 by Brian Aker
Innodb is now in the house (aka... it handls its own DFE).
553
      share.storage_engine->setTransactionReadWrite(session);
554
2068.7.6 by Brian Aker
Fix interface for create table such that it issues error and returns state
555
      error= static_cast<drizzled::error_t>(share.storage_engine->doCreateTable(session,
556
                                                                                table,
557
                                                                                identifier,
558
                                                                                table_message));
1372.1.4 by Brian Aker
Update to remove cache in enginges for per session (which also means... no
559
    }
560
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
561
    if (error == ER_TABLE_PERMISSION_DENIED)
562
      my_error(ER_TABLE_PERMISSION_DENIED, identifier);
563
    else if (error)
2246.4.2 by Olaf van der Spek
Refactor Identifier::getSQLPath()
564
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), identifier.getSQLPath().c_str(), error);
1672.3.2 by Brian Aker
Tiny little cleanup around Table.
565
    table.delete_table();
1372.1.4 by Brian Aker
Update to remove cache in enginges for per session (which also means... no
566
  }
2068.7.6 by Brian Aker
Fix interface for create table such that it issues error and returns state
567
  return(error == EE_OK);
971.1.38 by Monty Taylor
Moved delete table.
568
}
569
1869.1.4 by Brian Aker
TableShare is no longer in the house (i.e. we no longer directly have a copy
570
Cursor *StorageEngine::getCursor(Table &arg)
1183.1.1 by Brian Aker
Rework interface pieces on SE (sort of... dumb ones...)
571
{
1869.1.4 by Brian Aker
TableShare is no longer in the house (i.e. we no longer directly have a copy
572
  return create(arg);
1183.1.1 by Brian Aker
Rework interface pieces on SE (sort of... dumb ones...)
573
}
1160.1.1 by Brian Aker
Refactor SE createTable back to engine class.
574
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
575
class AddTableIdentifier : 
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
576
  public std::unary_function<StorageEngine *, void>
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
577
{
578
  CachedDirectory &directory;
2087.4.1 by Brian Aker
Merge in schema identifier.
579
  const identifier::Schema &identifier;
2252.1.9 by Olaf van der Spek
Common fwd
580
  identifier::table::vector &set_of_identifiers;
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
581
582
public:
583
2252.1.9 by Olaf van der Spek
Common fwd
584
  AddTableIdentifier(CachedDirectory &directory_arg, const identifier::Schema &identifier_arg, identifier::table::vector &of_names) :
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
585
    directory(directory_arg),
586
    identifier(identifier_arg),
587
    set_of_identifiers(of_names)
588
  {
589
  }
590
591
  result_type operator() (argument_type engine)
592
  {
593
    engine->doGetTableIdentifiers(directory, identifier, set_of_identifiers);
594
  }
595
};
596
1415 by Brian Aker
Mass overhaul to use schema_identifier.
597
2252.1.9 by Olaf van der Spek
Common fwd
598
void StorageEngine::getIdentifiers(Session &session, const identifier::Schema &schema_identifier, identifier::table::vector &set_of_identifiers)
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
599
{
600
  CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
601
602
  if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
2246.4.2 by Olaf van der Spek
Refactor Identifier::getSQLPath()
603
  { 
604
	}
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
605
  else if (schema_identifier == DATA_DICTIONARY_IDENTIFIER)
2246.4.2 by Olaf van der Spek
Refactor Identifier::getSQLPath()
606
  { 
607
	}
608
  else if (directory.fail())
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
609
  {
2246.4.2 by Olaf van der Spek
Refactor Identifier::getSQLPath()
610
    errno= directory.getError();
611
    if (errno == ENOENT)
612
      my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), schema_identifier.getSQLPath().c_str());
613
    else
614
      my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
615
    return;
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
616
  }
617
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
618
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
619
                AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
620
2263.3.2 by Olaf van der Spek
Use open_tables
621
  session.open_tables.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
622
}
623
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
624
class DropTable: public std::unary_function<identifier::Table&, bool>
1510.3.2 by mordred
Fix error found by cppcheck - modifying a container invalidates its iterators,
625
{
626
  Session &session;
627
  StorageEngine *engine;
628
629
public:
630
631
  DropTable(Session &session_arg, StorageEngine *engine_arg) :
632
    session(session_arg),
633
    engine(engine_arg)
634
  { }
635
636
  result_type operator() (argument_type identifier)
637
  {
638
    return engine->doDropTable(session, identifier) == 0;
639
  } 
640
};
641
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
642
/* This will later be converted to identifier::Tables */
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
643
class DropTables: public std::unary_function<StorageEngine *, void>
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
644
{
645
  Session &session;
2252.1.9 by Olaf van der Spek
Common fwd
646
  identifier::table::vector &table_identifiers;
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
647
648
public:
649
2252.1.9 by Olaf van der Spek
Common fwd
650
  DropTables(Session &session_arg, identifier::table::vector &table_identifiers_arg) :
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
651
    session(session_arg),
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
652
    table_identifiers(table_identifiers_arg)
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
653
  { }
654
655
  result_type operator() (argument_type engine)
656
  {
1510.3.2 by mordred
Fix error found by cppcheck - modifying a container invalidates its iterators,
657
    // True returning from DropTable means the table has been successfully
658
    // deleted, so it should be removed from the list of tables to drop
1966.2.15 by Brian Aker
Did a grep for _if(
659
    table_identifiers.erase(std::remove_if(table_identifiers.begin(),
660
                                           table_identifiers.end(),
661
                                           DropTable(session, engine)),
1510.3.2 by mordred
Fix error found by cppcheck - modifying a container invalidates its iterators,
662
                            table_identifiers.end());
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
663
  }
664
};
665
666
/*
667
  This only works for engines which use file based DFE.
668
669
  Note-> Unlike MySQL, we do not, on purpose, delete files that do not match any engines. 
670
*/
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
671
void StorageEngine::removeLostTemporaryTables(Session &session, const char *directory)
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
672
{
673
  CachedDirectory dir(directory, set_of_table_definition_ext);
2252.1.9 by Olaf van der Spek
Common fwd
674
  identifier::table::vector table_identifiers;
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
675
676
  if (dir.fail())
677
  {
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
678
    errno= dir.getError();
679
    my_error(ER_CANT_READ_DIR, MYF(0), directory, errno);
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
680
681
    return;
682
  }
683
684
  CachedDirectory::Entries files= dir.getEntries();
685
686
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
687
       fileIter != files.end(); fileIter++)
688
  {
689
    size_t length;
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
690
    std::string path;
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
691
    CachedDirectory::Entry *entry= *fileIter;
692
693
    /* We remove the file extension. */
694
    length= entry->filename.length();
695
    entry->filename.resize(length - DEFAULT_DEFINITION_FILE_EXT.length());
696
697
    path+= directory;
698
    path+= FN_LIBCHAR;
699
    path+= entry->filename;
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
700
    message::Table definition;
701
    if (StorageEngine::readTableFile(path, definition))
702
    {
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
703
      identifier::Table identifier(definition.schema(), definition.name(), path);
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
704
      table_identifiers.push_back(identifier);
705
    }
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
706
  }
707
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
708
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
709
                DropTables(session, table_identifiers));
710
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
711
  /*
712
    Now we just clean up anything that might left over.
713
714
    We rescan because some of what might have been there should
715
    now be all nice and cleaned up.
716
  */
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
717
  std::set<std::string> all_exts= set_of_table_definition_ext;
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
718
1273.19.30 by Brian Aker
Performance patch... we now only cycle through engines that actually have
719
  for (EngineVector::iterator iter= vector_of_engines.begin();
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
720
       iter != vector_of_engines.end() ; iter++)
721
  {
722
    for (const char **ext= (*iter)->bas_ext(); *ext ; ext++)
723
      all_exts.insert(*ext);
724
  }
725
726
  CachedDirectory rescan(directory, all_exts);
727
728
  files= rescan.getEntries();
729
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
730
       fileIter != files.end(); fileIter++)
731
  {
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
732
    std::string path;
1213 by Brian Aker
Fixes startup failures when temporary tables were left behind in a crash.
733
    CachedDirectory::Entry *entry= *fileIter;
734
735
    path+= directory;
736
    path+= FN_LIBCHAR;
737
    path+= entry->filename;
738
739
    unlink(path.c_str());
740
  }
741
}
742
1160.1.1 by Brian Aker
Refactor SE createTable back to engine class.
743
1216.1.1 by Brian Aker
Move print_error up to Engine.
744
/**
745
  Print error that we got from Cursor function.
746
747
  @note
748
    In case of delete table it's only safe to use the following parts of
749
    the 'table' structure:
1532.1.15 by Brian Aker
Partial encapsulation of TableShare from Table.
750
    - table->getShare()->path
1216.1.1 by Brian Aker
Move print_error up to Engine.
751
    - table->alias
752
*/
2148.7.12 by Brian Aker
Merge in header fixes.
753
void StorageEngine::print_error(int error, myf errflag, const Table &table) const
1216.1.1 by Brian Aker
Move print_error up to Engine.
754
{
2054.1.2 by Brian Aker
Rename of the Loooongggggg error type over to simply drizzled::error_t
755
  drizzled::error_t textno= ER_GET_ERRNO;
1216.1.1 by Brian Aker
Move print_error up to Engine.
756
  switch (error) {
757
  case EACCES:
758
    textno=ER_OPEN_AS_READONLY;
759
    break;
760
  case EAGAIN:
761
    textno=ER_FILE_USED;
762
    break;
763
  case ENOENT:
764
    textno=ER_FILE_NOT_FOUND;
765
    break;
766
  case HA_ERR_KEY_NOT_FOUND:
767
  case HA_ERR_NO_ACTIVE_RECORD:
768
  case HA_ERR_END_OF_FILE:
769
    textno=ER_KEY_NOT_FOUND;
770
    break;
771
  case HA_ERR_WRONG_MRG_TABLE_DEF:
772
    textno=ER_WRONG_MRG_TABLE;
773
    break;
774
  case HA_ERR_FOUND_DUPP_KEY:
775
  {
2068.7.5 by Brian Aker
Remove dead call to print error that didn't use a table reference.
776
    uint32_t key_nr= table.get_dup_key(error);
1216.1.1 by Brian Aker
Move print_error up to Engine.
777
    if ((int) key_nr >= 0)
778
    {
779
      const char *err_msg= ER(ER_DUP_ENTRY_WITH_KEY_NAME);
780
2068.7.5 by Brian Aker
Remove dead call to print error that didn't use a table reference.
781
      print_keydup_error(key_nr, err_msg, table);
1578.6.5 by Brian Aker
Remove error message for less effective one, but remove current_session().
782
1216.1.1 by Brian Aker
Move print_error up to Engine.
783
      return;
784
    }
785
    textno=ER_DUP_KEY;
786
    break;
787
  }
788
  case HA_ERR_FOREIGN_DUPLICATE_KEY:
789
  {
2068.7.5 by Brian Aker
Remove dead call to print error that didn't use a table reference.
790
    uint32_t key_nr= table.get_dup_key(error);
1216.1.1 by Brian Aker
Move print_error up to Engine.
791
    if ((int) key_nr >= 0)
792
    {
793
      uint32_t max_length;
794
795
      /* Write the key in the error message */
796
      char key[MAX_KEY_LENGTH];
797
      String str(key,sizeof(key),system_charset_info);
798
799
      /* Table is opened and defined at this point */
2068.7.5 by Brian Aker
Remove dead call to print error that didn't use a table reference.
800
      key_unpack(&str, &table,(uint32_t) key_nr);
1216.1.1 by Brian Aker
Move print_error up to Engine.
801
      max_length= (DRIZZLE_ERRMSG_SIZE-
802
                   (uint32_t) strlen(ER(ER_FOREIGN_DUPLICATE_KEY)));
803
      if (str.length() >= max_length)
804
      {
805
        str.length(max_length-4);
806
        str.append(STRING_WITH_LEN("..."));
807
      }
2068.7.5 by Brian Aker
Remove dead call to print error that didn't use a table reference.
808
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table.getShare()->getTableName(),
1216.1.1 by Brian Aker
Move print_error up to Engine.
809
        str.c_ptr(), key_nr+1);
810
      return;
811
    }
812
    textno= ER_DUP_KEY;
813
    break;
814
  }
815
  case HA_ERR_FOUND_DUPP_UNIQUE:
816
    textno=ER_DUP_UNIQUE;
817
    break;
818
  case HA_ERR_RECORD_CHANGED:
819
    textno=ER_CHECKREAD;
820
    break;
821
  case HA_ERR_CRASHED:
822
    textno=ER_NOT_KEYFILE;
823
    break;
824
  case HA_ERR_WRONG_IN_RECORD:
825
    textno= ER_CRASHED_ON_USAGE;
826
    break;
827
  case HA_ERR_CRASHED_ON_USAGE:
828
    textno=ER_CRASHED_ON_USAGE;
829
    break;
830
  case HA_ERR_NOT_A_TABLE:
2054.1.2 by Brian Aker
Rename of the Loooongggggg error type over to simply drizzled::error_t
831
    textno= static_cast<drizzled::error_t>(error);
1216.1.1 by Brian Aker
Move print_error up to Engine.
832
    break;
833
  case HA_ERR_CRASHED_ON_REPAIR:
834
    textno=ER_CRASHED_ON_REPAIR;
835
    break;
836
  case HA_ERR_OUT_OF_MEM:
837
    textno=ER_OUT_OF_RESOURCES;
838
    break;
839
  case HA_ERR_WRONG_COMMAND:
840
    textno=ER_ILLEGAL_HA;
841
    break;
842
  case HA_ERR_OLD_FILE:
843
    textno=ER_OLD_KEYFILE;
844
    break;
845
  case HA_ERR_UNSUPPORTED:
846
    textno=ER_UNSUPPORTED_EXTENSION;
847
    break;
848
  case HA_ERR_RECORD_FILE_FULL:
849
  case HA_ERR_INDEX_FILE_FULL:
850
    textno=ER_RECORD_FILE_FULL;
851
    break;
852
  case HA_ERR_LOCK_WAIT_TIMEOUT:
853
    textno=ER_LOCK_WAIT_TIMEOUT;
854
    break;
855
  case HA_ERR_LOCK_TABLE_FULL:
856
    textno=ER_LOCK_TABLE_FULL;
857
    break;
858
  case HA_ERR_LOCK_DEADLOCK:
859
    textno=ER_LOCK_DEADLOCK;
860
    break;
861
  case HA_ERR_READ_ONLY_TRANSACTION:
862
    textno=ER_READ_ONLY_TRANSACTION;
863
    break;
864
  case HA_ERR_CANNOT_ADD_FOREIGN:
865
    textno=ER_CANNOT_ADD_FOREIGN;
866
    break;
867
  case HA_ERR_ROW_IS_REFERENCED:
868
  {
869
    String str;
870
    get_error_message(error, &str);
871
    my_error(ER_ROW_IS_REFERENCED_2, MYF(0), str.c_ptr_safe());
872
    return;
873
  }
874
  case HA_ERR_NO_REFERENCED_ROW:
875
  {
876
    String str;
877
    get_error_message(error, &str);
878
    my_error(ER_NO_REFERENCED_ROW_2, MYF(0), str.c_ptr_safe());
879
    return;
880
  }
881
  case HA_ERR_TABLE_DEF_CHANGED:
882
    textno=ER_TABLE_DEF_CHANGED;
883
    break;
884
  case HA_ERR_NO_SUCH_TABLE:
2140.1.3 by Brian Aker
Merge in error message fix for just one type of error for unknown table.
885
    {
886
      identifier::Table identifier(table.getShare()->getSchemaName(), table.getShare()->getTableName());
887
      my_error(ER_TABLE_UNKNOWN, identifier);
888
      return;
889
    }
1216.1.1 by Brian Aker
Move print_error up to Engine.
890
  case HA_ERR_RBR_LOGGING_FAILED:
891
    textno= ER_BINLOG_ROW_LOGGING_FAILED;
892
    break;
893
  case HA_ERR_DROP_INDEX_FK:
894
  {
895
    const char *ptr= "???";
2068.7.5 by Brian Aker
Remove dead call to print error that didn't use a table reference.
896
    uint32_t key_nr= table.get_dup_key(error);
1216.1.1 by Brian Aker
Move print_error up to Engine.
897
    if ((int) key_nr >= 0)
2068.7.5 by Brian Aker
Remove dead call to print error that didn't use a table reference.
898
      ptr= table.key_info[key_nr].name;
1216.1.1 by Brian Aker
Move print_error up to Engine.
899
    my_error(ER_DROP_INDEX_FK, MYF(0), ptr);
900
    return;
901
  }
902
  case HA_ERR_TABLE_NEEDS_UPGRADE:
903
    textno=ER_TABLE_NEEDS_UPGRADE;
904
    break;
905
  case HA_ERR_TABLE_READONLY:
906
    textno= ER_OPEN_AS_READONLY;
907
    break;
908
  case HA_ERR_AUTOINC_READ_FAILED:
909
    textno= ER_AUTOINC_READ_FAILED;
910
    break;
911
  case HA_ERR_AUTOINC_ERANGE:
912
    textno= ER_WARN_DATA_OUT_OF_RANGE;
913
    break;
914
  case HA_ERR_LOCK_OR_ACTIVE_TRANSACTION:
915
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
916
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
917
    return;
918
  default:
919
    {
920
      /* 
921
        The error was "unknown" to this function.
922
        Ask Cursor if it has got a message for this error 
923
      */
924
      bool temporary= false;
925
      String str;
926
      temporary= get_error_message(error, &str);
927
      if (!str.is_empty())
928
      {
929
        const char* engine_name= getName().c_str();
930
        if (temporary)
931
          my_error(ER_GET_TEMPORARY_ERRMSG, MYF(0), error, str.ptr(),
932
                   engine_name);
933
        else
934
          my_error(ER_GET_ERRMSG, MYF(0), error, str.ptr(), engine_name);
935
      }
936
      else
937
      {
938
	      my_error(ER_GET_ERRNO,errflag,error);
939
      }
940
      return;
941
    }
942
  }
2068.7.5 by Brian Aker
Remove dead call to print error that didn't use a table reference.
943
944
  my_error(textno, errflag, table.getShare()->getTableName(), error);
1216.1.1 by Brian Aker
Move print_error up to Engine.
945
}
946
947
948
/**
949
  Return an error message specific to this Cursor.
950
951
  @param error  error code previously returned by Cursor
952
  @param buf    pointer to String where to add error message
953
954
  @return
955
    Returns true if this is a temporary error
956
*/
2148.7.12 by Brian Aker
Merge in header fixes.
957
bool StorageEngine::get_error_message(int , String* ) const
1216.1.1 by Brian Aker
Move print_error up to Engine.
958
{
959
  return false;
960
}
961
962
2148.7.12 by Brian Aker
Merge in header fixes.
963
void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, const Table &table) const
1216.1.1 by Brian Aker
Move print_error up to Engine.
964
{
965
  /* Write the duplicated key in the error message */
966
  char key[MAX_KEY_LENGTH];
967
  String str(key,sizeof(key),system_charset_info);
968
969
  if (key_nr == MAX_KEY)
970
  {
971
    /* Key is unknown */
972
    str.copy("", 0, system_charset_info);
973
    my_printf_error(ER_DUP_ENTRY, msg, MYF(0), str.c_ptr(), "*UNKNOWN*");
974
  }
975
  else
976
  {
977
    /* Table is opened and defined at this point */
978
    key_unpack(&str, &table, (uint32_t) key_nr);
979
    uint32_t max_length=DRIZZLE_ERRMSG_SIZE-(uint32_t) strlen(msg);
980
    if (str.length() >= max_length)
981
    {
982
      str.length(max_length-4);
983
      str.append(STRING_WITH_LEN("..."));
984
    }
985
    my_printf_error(ER_DUP_ENTRY, msg,
986
		    MYF(0), str.c_ptr(), table.key_info[key_nr].name);
987
  }
988
}
989
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
990
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
991
int StorageEngine::deleteDefinitionFromPath(const identifier::Table &identifier)
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
992
{
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
993
  std::string path(identifier.getPath());
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
994
995
  path.append(DEFAULT_DEFINITION_FILE_EXT);
996
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
997
  return internal::my_delete(path.c_str(), MYF(0));
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
998
}
999
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1000
int StorageEngine::renameDefinitionFromPath(const identifier::Table &dest, const identifier::Table &src)
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1001
{
1395.1.18 by Brian Aker
Small update.
1002
  message::Table table_message;
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
1003
  std::string src_path(src.getPath());
1004
  std::string dest_path(dest.getPath());
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1005
1006
  src_path.append(DEFAULT_DEFINITION_FILE_EXT);
1007
  dest_path.append(DEFAULT_DEFINITION_FILE_EXT);
1008
1395.1.18 by Brian Aker
Small update.
1009
  bool was_read= StorageEngine::readTableFile(src_path.c_str(), table_message);
1010
1011
  if (not was_read)
1012
  {
1013
    return ENOENT;
1014
  }
1395.1.1 by Brian Aker
Fixes for alter table to make sure that the proto on disk is valid.
1015
1016
  dest.copyToTableMessage(table_message);
1017
1395.1.18 by Brian Aker
Small update.
1018
  int error= StorageEngine::writeDefinitionFromPath(dest, table_message);
1019
1020
  if (not error)
1395.1.1 by Brian Aker
Fixes for alter table to make sure that the proto on disk is valid.
1021
  {
1395.1.18 by Brian Aker
Small update.
1022
    if (unlink(src_path.c_str()))
1023
      perror(src_path.c_str());
1395.1.1 by Brian Aker
Fixes for alter table to make sure that the proto on disk is valid.
1024
  }
1025
1395.1.18 by Brian Aker
Small update.
1026
  return error;
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1027
}
1028
2224.4.2 by Brian Aker
Merge in work around making all passing of table as const.
1029
int StorageEngine::writeDefinitionFromPath(const identifier::Table &identifier, const message::Table &table_message)
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1030
{
1395.1.18 by Brian Aker
Small update.
1031
  char definition_file_tmp[FN_REFLEN];
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
1032
  std::string file_name(identifier.getPath());
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1033
1034
  file_name.append(DEFAULT_DEFINITION_FILE_EXT);
1035
1415 by Brian Aker
Mass overhaul to use schema_identifier.
1036
  snprintf(definition_file_tmp, sizeof(definition_file_tmp), "%sXXXXXX", file_name.c_str());
1395.1.18 by Brian Aker
Small update.
1037
1038
  int fd= mkstemp(definition_file_tmp);
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1039
1040
  if (fd == -1)
1395.1.1 by Brian Aker
Fixes for alter table to make sure that the proto on disk is valid.
1041
  {
1395.1.18 by Brian Aker
Small update.
1042
    perror(definition_file_tmp);
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1043
    return errno;
1395.1.1 by Brian Aker
Fixes for alter table to make sure that the proto on disk is valid.
1044
  }
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1045
1046
  google::protobuf::io::ZeroCopyOutputStream* output=
1047
    new google::protobuf::io::FileOutputStream(fd);
1048
1608.1.2 by Brian Aker
Merge enum test
1049
  bool success;
1050
1966.3.1 by Monty Taylor
Use std::exception instead of catch(...)
1051
  try
1052
  {
1608.1.2 by Brian Aker
Merge enum test
1053
    success= table_message.SerializeToZeroCopyStream(output);
1054
  }
1055
  catch (...)
1056
  {
1057
    success= false;
1058
  }
1059
1060
  if (not success)
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1061
  {
2246.4.2 by Olaf van der Spek
Refactor Identifier::getSQLPath()
1062
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), identifier.getSQLPath().c_str(), table_message.InitializationErrorString().c_str());
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1063
    delete output;
1395.1.18 by Brian Aker
Small update.
1064
1065
    if (close(fd) == -1)
1066
      perror(definition_file_tmp);
1067
1068
    if (unlink(definition_file_tmp) == -1)
1069
      perror(definition_file_tmp);
1070
1071
    return ER_CORRUPT_TABLE_DEFINITION;
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1072
  }
1073
1074
  delete output;
1395.1.18 by Brian Aker
Small update.
1075
1076
  if (close(fd) == -1)
1077
  {
1078
    int error= errno;
1079
    perror(definition_file_tmp);
1080
1081
    if (unlink(definition_file_tmp))
1082
      perror(definition_file_tmp);
1083
1084
    return error;
1085
  }
1086
1087
  if (rename(definition_file_tmp, file_name.c_str()) == -1)
1088
  {
1089
    int error= errno;
1090
    perror(definition_file_tmp);
1091
1092
    if (unlink(definition_file_tmp))
1093
      perror(definition_file_tmp);
1094
1095
    return error;
1096
  }
1395.1.1 by Brian Aker
Fixes for alter table to make sure that the proto on disk is valid.
1097
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
1098
  return 0;
1099
}
1100
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
1101
class CanCreateTable: public std::unary_function<StorageEngine *, bool>
1320.1.8 by Brian Aker
Temporary fix for allowing engines to say "don't do this".
1102
{
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1103
  const identifier::Table &identifier;
1320.1.8 by Brian Aker
Temporary fix for allowing engines to say "don't do this".
1104
1105
public:
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1106
  CanCreateTable(const identifier::Table &identifier_arg) :
1320.1.8 by Brian Aker
Temporary fix for allowing engines to say "don't do this".
1107
    identifier(identifier_arg)
1108
  { }
1109
1110
  result_type operator() (argument_type engine)
1111
  {
1112
    return not engine->doCanCreateTable(identifier);
1113
  }
1114
};
1115
1116
1117
/**
1118
  @note on success table can be created.
1119
*/
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1120
bool StorageEngine::canCreateTable(const identifier::Table &identifier)
1320.1.8 by Brian Aker
Temporary fix for allowing engines to say "don't do this".
1121
{
1122
  EngineVector::iterator iter=
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
1123
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
1124
                 CanCreateTable(identifier));
1320.1.8 by Brian Aker
Temporary fix for allowing engines to say "don't do this".
1125
1126
  if (iter == vector_of_engines.end())
1127
  {
1128
    return true;
1129
  }
1130
1131
  return false;
1132
}
1133
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
1134
bool StorageEngine::readTableFile(const std::string &path, message::Table &table_message)
1135
{
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
1136
  std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
1137
1138
  if (input.good())
1139
  {
1608.1.2 by Brian Aker
Merge enum test
1140
    try {
1141
      if (table_message.ParseFromIstream(&input))
1142
      {
1143
        return true;
1144
      }
1145
    }
1146
    catch (...)
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
1147
    {
1608.1.2 by Brian Aker
Merge enum test
1148
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
2017.2.1 by Brian Aker
Cleanup error messages around bad table definitions.
1149
               table_message.name().empty() ? path.c_str() : table_message.name().c_str(),
1608.1.2 by Brian Aker
Merge enum test
1150
               table_message.InitializationErrorString().empty() ? "": table_message.InitializationErrorString().c_str());
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
1151
    }
1152
  }
1153
  else
1154
  {
1155
    perror(path.c_str());
1156
  }
1157
1158
  return false;
1159
}
1160
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
1161
std::ostream& operator<<(std::ostream& output, const StorageEngine &engine)
1162
{
1163
  output << "StorageEngine:(";
1164
  output <<  engine.getName();
1165
  output << ")";
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
1166
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
1167
  return output;
1168
}
1395.1.8 by Brian Aker
Cleanup usage of TI so that the NULL ones no longer exist.
1169
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
1170
} /* namespace plugin */
1160.1.1 by Brian Aker
Refactor SE createTable back to engine class.
1171
} /* namespace drizzled */