~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/schema_engine.cc

edit

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
{
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>
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::identifier::Table &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 identifier::Schema &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
105
bool StorageEngine::doesSchemaExist(const identifier::Schema &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
113
const CHARSET_INFO *StorageEngine::getSchemaCollation(const identifier::Schema &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 : 
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);
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, identifier::Schema::const_reference identifier)
257
235
{
258
236
  uint64_t deleted= 0;
259
237
  bool error= false;
260
238
  identifier::Table::vector dropped_tables;
 
239
  message::Schema schema_proto;
261
240
 
262
241
  do
263
242
  {
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