~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-07-04 19:11:47 UTC
  • mto: This revision was merged to the branch mainline in revision 2367.
  • Revision ID: olafvdspek@gmail.com-20110704191147-s99ojek811zi1fzj
RemoveĀ unusedĀ Name_resolution_context::error_reporter

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