~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/schema_engine.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#include "drizzled/global_charset_info.h"
25
25
#include "drizzled/charset.h"
 
26
#include "drizzled/transaction_services.h"
26
27
 
27
28
#include "drizzled/plugin/storage_engine.h"
28
29
#include "drizzled/plugin/authorization.h"
29
30
 
30
 
using namespace std;
31
 
 
32
31
namespace drizzled
33
32
{
34
33
 
36
35
{
37
36
 
38
37
class AddSchemaNames : 
39
 
  public unary_function<StorageEngine *, void>
 
38
  public std::unary_function<StorageEngine *, void>
40
39
{
41
 
  SchemaIdentifierList &schemas;
 
40
  identifier::Schema::vector &schemas;
42
41
 
43
42
public:
44
43
 
45
 
  AddSchemaNames(SchemaIdentifierList &of_names) :
 
44
  AddSchemaNames(identifier::Schema::vector &of_names) :
46
45
    schemas(of_names)
47
46
  {
48
47
  }
53
52
  }
54
53
};
55
54
 
56
 
void StorageEngine::getSchemaIdentifiers(Session &session, SchemaIdentifierList &schemas)
 
55
void StorageEngine::getIdentifiers(Session &session, identifier::Schema::vector &schemas)
57
56
{
58
57
  // Add hook here for engines to register schema.
59
 
  for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
58
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
60
59
           AddSchemaNames(schemas));
61
60
 
62
 
  plugin::Authorization::pruneSchemaNames(session.getSecurityContext(), schemas);
 
61
  plugin::Authorization::pruneSchemaNames(session.user(), schemas);
63
62
}
64
63
 
65
 
class StorageEngineGetSchemaDefinition: public unary_function<StorageEngine *, bool>
 
64
class StorageEngineGetSchemaDefinition: public std::unary_function<StorageEngine *, bool>
66
65
{
67
 
  SchemaIdentifier &identifier;
68
 
  message::Schema &schema_proto;
 
66
  const identifier::Schema &identifier;
 
67
  message::schema::shared_ptr &schema_proto;
69
68
 
70
69
public:
71
 
  StorageEngineGetSchemaDefinition(SchemaIdentifier &identifier_arg,
72
 
                                   message::Schema &schema_proto_arg) :
 
70
  StorageEngineGetSchemaDefinition(const identifier::Schema &identifier_arg,
 
71
                                   message::schema::shared_ptr &schema_proto_arg) :
73
72
    identifier(identifier_arg),
74
73
    schema_proto(schema_proto_arg) 
75
74
  {
84
83
/*
85
84
  Return value is "if parsed"
86
85
*/
87
 
bool StorageEngine::getSchemaDefinition(TableIdentifier &identifier, message::Schema &proto)
 
86
bool StorageEngine::getSchemaDefinition(const drizzled::identifier::Table &identifier, message::schema::shared_ptr &proto)
88
87
{
89
88
  return StorageEngine::getSchemaDefinition(identifier, proto);
90
89
}
91
90
 
92
 
bool StorageEngine::getSchemaDefinition(SchemaIdentifier &identifier, message::Schema &proto)
 
91
bool StorageEngine::getSchemaDefinition(const identifier::Schema &identifier, message::schema::shared_ptr &proto)
93
92
{
94
 
  proto.Clear();
95
 
 
96
93
  EngineVector::iterator iter=
97
 
    find_if(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
98
 
            StorageEngineGetSchemaDefinition(identifier, proto));
 
94
    std::find_if(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
95
                 StorageEngineGetSchemaDefinition(identifier, proto));
99
96
 
100
97
  if (iter != StorageEngine::getSchemaEngines().end())
101
98
  {
105
102
  return false;
106
103
}
107
104
 
108
 
bool StorageEngine::doesSchemaExist(SchemaIdentifier &identifier)
 
105
bool StorageEngine::doesSchemaExist(const identifier::Schema &identifier)
109
106
{
110
 
  message::Schema proto;
 
107
  message::schema::shared_ptr proto;
111
108
 
112
109
  return StorageEngine::getSchemaDefinition(identifier, proto);
113
110
}
114
111
 
115
112
 
116
 
const CHARSET_INFO *StorageEngine::getSchemaCollation(SchemaIdentifier &identifier)
 
113
const CHARSET_INFO *StorageEngine::getSchemaCollation(const identifier::Schema &identifier)
117
114
{
118
 
  message::Schema schmema_proto;
 
115
  message::schema::shared_ptr schmema_proto;
119
116
  bool found;
120
117
 
121
118
  found= StorageEngine::getSchemaDefinition(identifier, schmema_proto);
122
119
 
123
 
  if (found && schmema_proto.has_collation())
 
120
  if (found && schmema_proto->has_collation())
124
121
  {
125
 
    const string buffer= schmema_proto.collation();
 
122
    const std::string buffer= schmema_proto->collation();
126
123
    const CHARSET_INFO* cs= get_charset_by_name(buffer.c_str());
127
124
 
128
125
    if (not cs)
129
126
    {
130
 
      errmsg_printf(ERRMSG_LVL_ERROR,
131
 
                    _("Error while loading database options: '%s':"), identifier.getSQLPath().c_str());
132
 
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
 
127
      std::string path;
 
128
      identifier.getSQLPath(path);
 
129
 
 
130
      errmsg_printf(error::ERROR,
 
131
                    _("Error while loading database options: '%s':"), path.c_str());
 
132
      errmsg_printf(error::ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
133
133
 
134
134
      return default_charset_info;
135
135
    }
141
141
}
142
142
 
143
143
class CreateSchema : 
144
 
  public unary_function<StorageEngine *, void>
 
144
  public std::unary_function<StorageEngine *, void>
145
145
{
146
146
  const drizzled::message::Schema &schema_message;
 
147
  uint64_t &success_count;
147
148
 
148
149
public:
149
150
 
150
 
  CreateSchema(const drizzled::message::Schema &arg) :
151
 
    schema_message(arg)
 
151
  CreateSchema(const drizzled::message::Schema &arg, uint64_t &success_count_arg) :
 
152
    schema_message(arg),
 
153
    success_count(success_count_arg)
152
154
  {
153
155
  }
154
156
 
155
157
  result_type operator() (argument_type engine)
156
158
  {
157
159
    // @todo eomeday check that at least one engine said "true"
158
 
    (void)engine->doCreateSchema(schema_message);
 
160
    bool success= engine->doCreateSchema(schema_message);
 
161
 
 
162
    if (success) 
 
163
    {
 
164
      success_count++;
 
165
      TransactionServices &transaction_services= TransactionServices::singleton();
 
166
      transaction_services.allocateNewTransactionId();
 
167
    }
159
168
  }
160
169
};
161
170
 
162
171
bool StorageEngine::createSchema(const drizzled::message::Schema &schema_message)
163
172
{
164
173
  // Add hook here for engines to register schema.
165
 
  for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
166
 
           CreateSchema(schema_message));
167
 
 
168
 
  return true;
 
174
  uint64_t success_count= 0;
 
175
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
176
                CreateSchema(schema_message, success_count));
 
177
 
 
178
  if (success_count) 
 
179
  {
 
180
    TransactionServices &transaction_services= TransactionServices::singleton();
 
181
    transaction_services.allocateNewTransactionId();
 
182
  }
 
183
 
 
184
  return (bool)success_count;
169
185
}
170
186
 
171
187
class DropSchema : 
172
 
  public unary_function<StorageEngine *, void>
 
188
  public std::unary_function<StorageEngine *, void>
173
189
{
174
190
  uint64_t &success_count;
175
 
  SchemaIdentifier &identifier;
 
191
  const identifier::Schema &identifier;
176
192
 
177
193
public:
178
194
 
179
 
  DropSchema(SchemaIdentifier &arg, uint64_t &count_arg) :
 
195
  DropSchema(const identifier::Schema &arg, uint64_t &count_arg) :
180
196
    success_count(count_arg),
181
197
    identifier(arg)
182
198
  {
188
204
    bool success= engine->doDropSchema(identifier);
189
205
 
190
206
    if (success)
 
207
    {
191
208
      success_count++;
 
209
      TransactionServices &transaction_services= TransactionServices::singleton();
 
210
      transaction_services.allocateNewTransactionId();
 
211
    }
192
212
  }
193
213
};
194
214
 
195
 
bool StorageEngine::dropSchema(SchemaIdentifier &identifier)
196
 
{
197
 
  uint64_t counter= 0;
198
 
  // Add hook here for engines to register schema.
199
 
  for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
200
 
           DropSchema(identifier, counter));
201
 
 
202
 
  return counter ? true : false;
 
215
static bool drop_all_tables_in_schema(Session& session,
 
216
                                      identifier::Schema::const_reference identifier,
 
217
                                      identifier::Table::vector &dropped_tables,
 
218
                                      uint64_t &deleted)
 
219
{
 
220
  TransactionServices &transaction_services= TransactionServices::singleton();
 
221
 
 
222
  plugin::StorageEngine::getIdentifiers(session, identifier, dropped_tables);
 
223
 
 
224
  for (identifier::Table::vector::iterator it= dropped_tables.begin();
 
225
       it != dropped_tables.end();
 
226
       it++)
 
227
  {
 
228
    boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
 
229
    table::Cache::singleton().removeTable(&session, *it,
 
230
                                          RTFC_WAIT_OTHER_THREAD_FLAG |
 
231
                                          RTFC_CHECK_KILLED_FLAG);
 
232
    if (not plugin::StorageEngine::dropTable(session, *it))
 
233
    {
 
234
      my_error(ER_TABLE_DROP, *it);
 
235
      return false;
 
236
    }
 
237
    transaction_services.dropTable(session, *it, true);
 
238
    deleted++;
 
239
  }
 
240
 
 
241
  return true;
 
242
}
 
243
 
 
244
bool StorageEngine::dropSchema(Session::reference session, identifier::Schema::const_reference identifier)
 
245
{
 
246
  uint64_t deleted= 0;
 
247
  bool error= false;
 
248
  identifier::Table::vector dropped_tables;
 
249
  message::Schema schema_proto;
 
250
 
 
251
  do
 
252
  {
 
253
    // Remove all temp tables first, this prevents loss of table from
 
254
    // shadowing (ie temp over standard table)
 
255
    {
 
256
      // Lets delete the temporary tables first outside of locks.  
 
257
      identifier::Table::vector set_of_identifiers;
 
258
      session.doGetTableIdentifiers(identifier, set_of_identifiers);
 
259
 
 
260
      for (identifier::Table::vector::iterator iter= set_of_identifiers.begin(); iter != set_of_identifiers.end(); iter++)
 
261
      {
 
262
        if (session.drop_temporary_table(*iter))
 
263
        {
 
264
          my_error(ER_TABLE_DROP, *iter);
 
265
          error= true;
 
266
          break;
 
267
        }
 
268
      }
 
269
    }
 
270
 
 
271
    /* After deleting database, remove all cache entries related to schema */
 
272
    table::Cache::singleton().removeSchema(identifier);
 
273
 
 
274
    if (not drop_all_tables_in_schema(session, identifier, dropped_tables, deleted))
 
275
    {
 
276
      error= true;
 
277
      my_error(ER_DROP_SCHEMA, identifier);
 
278
      break;
 
279
    }
 
280
 
 
281
    uint64_t counter= 0;
 
282
    // Add hook here for engines to register schema.
 
283
    std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
284
                  DropSchema(identifier, counter));
 
285
 
 
286
    if (not counter)
 
287
    {
 
288
      my_error(ER_DROP_SCHEMA, identifier);
 
289
      error= true;
 
290
 
 
291
      break;
 
292
    }
 
293
    else
 
294
    {
 
295
      /* We've already verified that the schema does exist, so safe to log it */
 
296
      TransactionServices &transaction_services= TransactionServices::singleton();
 
297
      transaction_services.dropSchema(session, identifier);
 
298
    }
 
299
  } while (0);
 
300
 
 
301
  if (deleted > 0)
 
302
  {
 
303
    session.clear_error();
 
304
    session.server_status|= SERVER_STATUS_DB_DROPPED;
 
305
    session.my_ok((uint32_t) deleted);
 
306
    session.server_status&= ~SERVER_STATUS_DB_DROPPED;
 
307
  }
 
308
 
 
309
 
 
310
  return error;
203
311
}
204
312
 
205
313
class AlterSchema : 
206
 
  public unary_function<StorageEngine *, void>
 
314
  public std::unary_function<StorageEngine *, void>
207
315
{
208
316
  uint64_t &success_count;
209
317
  const drizzled::message::Schema &schema_message;
221
329
    // @todo eomeday check that at least one engine said "true"
222
330
    bool success= engine->doAlterSchema(schema_message);
223
331
 
 
332
 
224
333
    if (success)
 
334
    {
225
335
      success_count++;
 
336
    }
226
337
  }
227
338
};
228
339
 
230
341
{
231
342
  uint64_t success_count= 0;
232
343
 
233
 
  for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
234
 
           AlterSchema(schema_message, success_count));
 
344
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
345
                AlterSchema(schema_message, success_count));
 
346
 
 
347
  if (success_count)
 
348
  {
 
349
    TransactionServices &transaction_services= TransactionServices::singleton();
 
350
    transaction_services.allocateNewTransactionId();
 
351
  }
235
352
 
236
353
  return success_count ? true : false;
237
354
}