~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/schema_engine.cc

  • Committer: Lee Bieber
  • Date: 2011-01-10 20:12:43 UTC
  • mfrom: (2069.1.3 build)
  • Revision ID: kalebral@gmail.com-20110110201243-j7xse6v4ptaipc92
Merge Gustaf - Use constrained_check<> classes on kernel sysvars 
Merge Andrew - fix bug 645004: libuuid configure error message needs changing
Merge Stewart - fix bug 698315: innobase tests fail with --repeat=2

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
21
 
 
22
 
#include <drizzled/session.h>
23
 
 
24
 
#include <drizzled/global_charset_info.h>
25
 
#include <drizzled/charset.h>
26
 
#include <drizzled/transaction_services.h>
27
 
 
28
 
#include <drizzled/plugin/storage_engine.h>
29
 
#include <drizzled/plugin/authorization.h>
 
20
#include "config.h"
 
21
 
 
22
#include "drizzled/session.h"
 
23
 
 
24
#include "drizzled/global_charset_info.h"
 
25
#include "drizzled/charset.h"
 
26
#include "drizzled/transaction_services.h"
 
27
 
 
28
#include "drizzled/plugin/storage_engine.h"
 
29
#include "drizzled/plugin/authorization.h"
30
30
 
31
31
namespace drizzled
32
32
{
37
37
class AddSchemaNames : 
38
38
  public std::unary_function<StorageEngine *, void>
39
39
{
40
 
  identifier::Schema::vector &schemas;
 
40
  SchemaIdentifier::vector &schemas;
41
41
 
42
42
public:
43
43
 
44
 
  AddSchemaNames(identifier::Schema::vector &of_names) :
 
44
  AddSchemaNames(SchemaIdentifier::vector &of_names) :
45
45
    schemas(of_names)
46
46
  {
47
47
  }
52
52
  }
53
53
};
54
54
 
55
 
void StorageEngine::getIdentifiers(Session &session, identifier::Schema::vector &schemas)
 
55
void StorageEngine::getIdentifiers(Session &session, SchemaIdentifier::vector &schemas)
56
56
{
57
57
  // Add hook here for engines to register schema.
58
58
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
59
59
           AddSchemaNames(schemas));
60
60
 
61
 
  plugin::Authorization::pruneSchemaNames(*session.user(), schemas);
 
61
  plugin::Authorization::pruneSchemaNames(session.user(), schemas);
62
62
}
63
63
 
64
64
class StorageEngineGetSchemaDefinition: public std::unary_function<StorageEngine *, bool>
65
65
{
66
 
  const identifier::Schema &identifier;
 
66
  const SchemaIdentifier &identifier;
67
67
  message::schema::shared_ptr &schema_proto;
68
68
 
69
69
public:
70
 
  StorageEngineGetSchemaDefinition(const identifier::Schema &identifier_arg,
 
70
  StorageEngineGetSchemaDefinition(const SchemaIdentifier &identifier_arg,
71
71
                                   message::schema::shared_ptr &schema_proto_arg) :
72
72
    identifier(identifier_arg),
73
73
    schema_proto(schema_proto_arg) 
76
76
 
77
77
  result_type operator() (argument_type engine)
78
78
  {
79
 
    schema_proto= engine->doGetSchemaDefinition(identifier);
80
 
    return schema_proto;
 
79
    return engine->doGetSchemaDefinition(identifier, schema_proto);
81
80
  }
82
81
};
83
82
 
84
83
/*
85
84
  Return value is "if parsed"
86
85
*/
87
 
message::schema::shared_ptr StorageEngine::getSchemaDefinition(const drizzled::identifier::Table &identifier)
 
86
bool StorageEngine::getSchemaDefinition(const drizzled::TableIdentifier &identifier, message::schema::shared_ptr &proto)
88
87
{
89
 
  return StorageEngine::getSchemaDefinition(identifier);
 
88
  return StorageEngine::getSchemaDefinition(identifier, proto);
90
89
}
91
90
 
92
 
message::schema::shared_ptr StorageEngine::getSchemaDefinition(const identifier::Schema &identifier)
 
91
bool StorageEngine::getSchemaDefinition(const SchemaIdentifier &identifier, message::schema::shared_ptr &proto)
93
92
{
94
 
  message::schema::shared_ptr proto;
95
 
 
96
93
  EngineVector::iterator iter=
97
94
    std::find_if(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
98
95
                 StorageEngineGetSchemaDefinition(identifier, proto));
99
96
 
100
97
  if (iter != StorageEngine::getSchemaEngines().end())
101
98
  {
102
 
    return proto;
 
99
    return true;
103
100
  }
104
101
 
105
 
  return message::schema::shared_ptr();
 
102
  return false;
106
103
}
107
104
 
108
 
bool StorageEngine::doesSchemaExist(const identifier::Schema &identifier)
 
105
bool StorageEngine::doesSchemaExist(const SchemaIdentifier &identifier)
109
106
{
110
107
  message::schema::shared_ptr proto;
111
108
 
112
 
  return StorageEngine::getSchemaDefinition(identifier);
 
109
  return StorageEngine::getSchemaDefinition(identifier, proto);
113
110
}
114
111
 
115
112
 
116
 
const CHARSET_INFO *StorageEngine::getSchemaCollation(const identifier::Schema &identifier)
 
113
const CHARSET_INFO *StorageEngine::getSchemaCollation(const SchemaIdentifier &identifier)
117
114
{
118
115
  message::schema::shared_ptr schmema_proto;
119
 
 
120
 
  schmema_proto= StorageEngine::getSchemaDefinition(identifier);
121
 
 
122
 
  if (schmema_proto && schmema_proto->has_collation())
 
116
  bool found;
 
117
 
 
118
  found= StorageEngine::getSchemaDefinition(identifier, schmema_proto);
 
119
 
 
120
  if (found && schmema_proto->has_collation())
123
121
  {
124
122
    const std::string buffer= schmema_proto->collation();
125
123
    const CHARSET_INFO* cs= get_charset_by_name(buffer.c_str());
129
127
      std::string path;
130
128
      identifier.getSQLPath(path);
131
129
 
132
 
      errmsg_printf(error::ERROR,
 
130
      errmsg_printf(ERRMSG_LVL_ERROR,
133
131
                    _("Error while loading database options: '%s':"), path.c_str());
134
 
      errmsg_printf(error::ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
 
132
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
135
133
 
136
134
      return default_charset_info;
137
135
    }
146
144
  public std::unary_function<StorageEngine *, void>
147
145
{
148
146
  const drizzled::message::Schema &schema_message;
149
 
  uint64_t &success_count;
150
147
 
151
148
public:
152
149
 
153
 
  CreateSchema(const drizzled::message::Schema &arg, uint64_t &success_count_arg) :
154
 
    schema_message(arg),
155
 
    success_count(success_count_arg)
 
150
  CreateSchema(const drizzled::message::Schema &arg) :
 
151
    schema_message(arg)
156
152
  {
157
153
  }
158
154
 
163
159
 
164
160
    if (success) 
165
161
    {
166
 
      success_count++;
167
162
      TransactionServices &transaction_services= TransactionServices::singleton();
168
163
      transaction_services.allocateNewTransactionId();
169
164
    }
173
168
bool StorageEngine::createSchema(const drizzled::message::Schema &schema_message)
174
169
{
175
170
  // Add hook here for engines to register schema.
176
 
  uint64_t success_count= 0;
177
171
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
178
 
                CreateSchema(schema_message, success_count));
179
 
 
180
 
  if (success_count) 
181
 
  {
182
 
    TransactionServices &transaction_services= TransactionServices::singleton();
183
 
    transaction_services.allocateNewTransactionId();
184
 
  }
185
 
 
186
 
  return (bool)success_count;
 
172
                CreateSchema(schema_message));
 
173
 
 
174
  return true;
187
175
}
188
176
 
189
177
class DropSchema : 
190
178
  public std::unary_function<StorageEngine *, void>
191
179
{
192
180
  uint64_t &success_count;
193
 
  const identifier::Schema &identifier;
 
181
  const SchemaIdentifier &identifier;
194
182
 
195
183
public:
196
184
 
197
 
  DropSchema(const identifier::Schema &arg, uint64_t &count_arg) :
 
185
  DropSchema(const SchemaIdentifier &arg, uint64_t &count_arg) :
198
186
    success_count(count_arg),
199
187
    identifier(arg)
200
188
  {
215
203
};
216
204
 
217
205
static bool drop_all_tables_in_schema(Session& session,
218
 
                                      identifier::Schema::const_reference identifier,
219
 
                                      identifier::Table::vector &dropped_tables,
 
206
                                      SchemaIdentifier::const_reference identifier,
 
207
                                      TableIdentifier::vector &dropped_tables,
220
208
                                      uint64_t &deleted)
221
209
{
222
210
  TransactionServices &transaction_services= TransactionServices::singleton();
223
211
 
224
212
  plugin::StorageEngine::getIdentifiers(session, identifier, dropped_tables);
225
213
 
226
 
  for (identifier::Table::vector::iterator it= dropped_tables.begin();
 
214
  for (TableIdentifier::vector::iterator it= dropped_tables.begin();
227
215
       it != dropped_tables.end();
228
216
       it++)
229
217
  {
230
218
    boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
231
 
 
232
 
    message::table::shared_ptr message= StorageEngine::getTableMessage(session, *it, false);
233
 
    if (not message)
234
 
    {
235
 
      my_error(ER_TABLE_DROP, *it);
236
 
      return false;
237
 
    }
238
 
 
239
219
    table::Cache::singleton().removeTable(&session, *it,
240
220
                                          RTFC_WAIT_OTHER_THREAD_FLAG |
241
221
                                          RTFC_CHECK_KILLED_FLAG);
242
 
    if (not plugin::StorageEngine::dropTable(session, *it))
 
222
    if (plugin::StorageEngine::dropTable(session, *it))
243
223
    {
244
224
      my_error(ER_TABLE_DROP, *it);
245
225
      return false;
246
226
    }
247
 
    transaction_services.dropTable(session, *it, *message, true);
 
227
    transaction_services.dropTable(&session, *it, true);
248
228
    deleted++;
249
229
  }
250
230
 
251
231
  return true;
252
232
}
253
233
 
254
 
bool StorageEngine::dropSchema(Session::reference session,
255
 
                               identifier::Schema::const_reference identifier,
256
 
                               message::schema::const_reference schema_message)
 
234
bool StorageEngine::dropSchema(Session::reference session, SchemaIdentifier::const_reference identifier)
257
235
{
258
236
  uint64_t deleted= 0;
259
237
  bool error= false;
260
 
  identifier::Table::vector dropped_tables;
 
238
  TableIdentifier::vector dropped_tables;
 
239
  message::Schema schema_proto;
261
240
 
262
241
  do
263
242
  {
265
244
    // shadowing (ie temp over standard table)
266
245
    {
267
246
      // Lets delete the temporary tables first outside of locks.  
268
 
      identifier::Table::vector set_of_identifiers;
 
247
      TableIdentifier::vector set_of_identifiers;
269
248
      session.doGetTableIdentifiers(identifier, set_of_identifiers);
270
249
 
271
 
      for (identifier::Table::vector::iterator iter= set_of_identifiers.begin(); iter != set_of_identifiers.end(); iter++)
 
250
      for (TableIdentifier::vector::iterator iter= set_of_identifiers.begin(); iter != set_of_identifiers.end(); iter++)
272
251
      {
273
252
        if (session.drop_temporary_table(*iter))
274
253
        {
305
284
    {
306
285
      /* We've already verified that the schema does exist, so safe to log it */
307
286
      TransactionServices &transaction_services= TransactionServices::singleton();
308
 
      transaction_services.dropSchema(session, identifier, schema_message);
 
287
      transaction_services.dropSchema(&session, identifier);
309
288
    }
310
289
  } while (0);
311
290
 
344
323
    if (success)
345
324
    {
346
325
      success_count++;
 
326
      TransactionServices &transaction_services= TransactionServices::singleton();
 
327
      transaction_services.allocateNewTransactionId();
347
328
    }
348
329
  }
349
330
};
355
336
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
356
337
                AlterSchema(schema_message, success_count));
357
338
 
358
 
  if (success_count)
359
 
  {
360
 
    TransactionServices &transaction_services= TransactionServices::singleton();
361
 
    transaction_services.allocateNewTransactionId();
362
 
  }
363
 
 
364
339
  return success_count ? true : false;
365
340
}
366
341