~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/schema_engine.cc

  • Committer: Brian Aker
  • Date: 2011-02-14 05:47:07 UTC
  • mto: This revision was merged to the branch mainline in revision 2167.
  • Revision ID: brian@tangent.org-20110214054707-61nsqgg1g4w1zhx1
Merge in all changes for current_session, etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
class AddSchemaNames : 
38
38
  public std::unary_function<StorageEngine *, void>
39
39
{
40
 
  SchemaIdentifier::vector &schemas;
 
40
  identifier::Schema::vector &schemas;
41
41
 
42
42
public:
43
43
 
44
 
  AddSchemaNames(SchemaIdentifier::vector &of_names) :
 
44
  AddSchemaNames(identifier::Schema::vector &of_names) :
45
45
    schemas(of_names)
46
46
  {
47
47
  }
52
52
  }
53
53
};
54
54
 
55
 
void StorageEngine::getIdentifiers(Session &session, SchemaIdentifier::vector &schemas)
 
55
void StorageEngine::getIdentifiers(Session &session, identifier::Schema::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.getSecurityContext(), 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 SchemaIdentifier &identifier;
 
66
  const identifier::Schema &identifier;
67
67
  message::schema::shared_ptr &schema_proto;
68
68
 
69
69
public:
70
 
  StorageEngineGetSchemaDefinition(const SchemaIdentifier &identifier_arg,
 
70
  StorageEngineGetSchemaDefinition(const identifier::Schema &identifier_arg,
71
71
                                   message::schema::shared_ptr &schema_proto_arg) :
72
72
    identifier(identifier_arg),
73
73
    schema_proto(schema_proto_arg) 
83
83
/*
84
84
  Return value is "if parsed"
85
85
*/
86
 
bool StorageEngine::getSchemaDefinition(const drizzled::TableIdentifier &identifier, message::schema::shared_ptr &proto)
 
86
bool StorageEngine::getSchemaDefinition(const drizzled::identifier::Table &identifier, message::schema::shared_ptr &proto)
87
87
{
88
88
  return StorageEngine::getSchemaDefinition(identifier, proto);
89
89
}
90
90
 
91
 
bool StorageEngine::getSchemaDefinition(const SchemaIdentifier &identifier, message::schema::shared_ptr &proto)
 
91
bool StorageEngine::getSchemaDefinition(const identifier::Schema &identifier, message::schema::shared_ptr &proto)
92
92
{
93
93
  EngineVector::iterator iter=
94
94
    std::find_if(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
102
102
  return false;
103
103
}
104
104
 
105
 
bool StorageEngine::doesSchemaExist(const SchemaIdentifier &identifier)
 
105
bool StorageEngine::doesSchemaExist(const identifier::Schema &identifier)
106
106
{
107
107
  message::schema::shared_ptr proto;
108
108
 
110
110
}
111
111
 
112
112
 
113
 
const CHARSET_INFO *StorageEngine::getSchemaCollation(const SchemaIdentifier &identifier)
 
113
const CHARSET_INFO *StorageEngine::getSchemaCollation(const identifier::Schema &identifier)
114
114
{
115
115
  message::schema::shared_ptr schmema_proto;
116
116
  bool found;
127
127
      std::string path;
128
128
      identifier.getSQLPath(path);
129
129
 
130
 
      errmsg_printf(ERRMSG_LVL_ERROR,
 
130
      errmsg_printf(error::ERROR,
131
131
                    _("Error while loading database options: '%s':"), path.c_str());
132
 
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
 
132
      errmsg_printf(error::ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
133
133
 
134
134
      return default_charset_info;
135
135
    }
144
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
 
159
161
 
160
162
    if (success) 
161
163
    {
 
164
      success_count++;
162
165
      TransactionServices &transaction_services= TransactionServices::singleton();
163
166
      transaction_services.allocateNewTransactionId();
164
167
    }
168
171
bool StorageEngine::createSchema(const drizzled::message::Schema &schema_message)
169
172
{
170
173
  // Add hook here for engines to register schema.
 
174
  uint64_t success_count= 0;
171
175
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
172
 
                CreateSchema(schema_message));
173
 
 
174
 
  return true;
 
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;
175
185
}
176
186
 
177
187
class DropSchema : 
178
188
  public std::unary_function<StorageEngine *, void>
179
189
{
180
190
  uint64_t &success_count;
181
 
  const SchemaIdentifier &identifier;
 
191
  const identifier::Schema &identifier;
182
192
 
183
193
public:
184
194
 
185
 
  DropSchema(const SchemaIdentifier &arg, uint64_t &count_arg) :
 
195
  DropSchema(const identifier::Schema &arg, uint64_t &count_arg) :
186
196
    success_count(count_arg),
187
197
    identifier(arg)
188
198
  {
202
212
  }
203
213
};
204
214
 
205
 
bool StorageEngine::dropSchema(const SchemaIdentifier &identifier)
206
 
{
207
 
  uint64_t counter= 0;
208
 
  // Add hook here for engines to register schema.
209
 
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
210
 
                DropSchema(identifier, counter));
211
 
 
212
 
  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;
213
311
}
214
312
 
215
313
class AlterSchema : 
235
333
    if (success)
236
334
    {
237
335
      success_count++;
238
 
      TransactionServices &transaction_services= TransactionServices::singleton();
239
 
      transaction_services.allocateNewTransactionId();
240
336
    }
241
337
  }
242
338
};
248
344
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
249
345
                AlterSchema(schema_message, success_count));
250
346
 
 
347
  if (success_count)
 
348
  {
 
349
    TransactionServices &transaction_services= TransactionServices::singleton();
 
350
    transaction_services.allocateNewTransactionId();
 
351
  }
 
352
 
251
353
  return success_count ? true : false;
252
354
}
253
355