~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/schema_engine.cc

  • Committer: Monty Taylor
  • Date: 2010-10-21 23:10:12 UTC
  • mto: (1879.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1880.
  • Revision ID: mordred@inaugust.com-20101021231012-uhsebiqo23xi0ygy
Updated AUTHORS list with everyone from bzr logs.

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