~drizzle-trunk/drizzle/development

1415 by Brian Aker
Mass overhaul to use schema_identifier.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Brian Aker
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
21
22
#include <drizzled/session.h>
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
23
#include <drizzled/sql_base.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
24
#include <drizzled/charset.h>
25
#include <drizzled/transaction_services.h>
2263.3.11 by Olaf van der Spek
Open Tables
26
#include <drizzled/open_tables_state.h>
2272.1.1 by Olaf van der Spek
Refactor includes
27
#include <drizzled/table/cache.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
28
#include <drizzled/plugin/storage_engine.h>
29
#include <drizzled/plugin/authorization.h>
1415 by Brian Aker
Mass overhaul to use schema_identifier.
30
2272.1.1 by Olaf van der Spek
Refactor includes
31
namespace drizzled {
32
namespace plugin {
1415 by Brian Aker
Mass overhaul to use schema_identifier.
33
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
34
class AddSchemaNames :
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
35
  public std::unary_function<StorageEngine *, void>
1415 by Brian Aker
Mass overhaul to use schema_identifier.
36
{
2252.1.9 by Olaf van der Spek
Common fwd
37
  identifier::schema::vector &schemas;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
38
39
public:
40
2252.1.9 by Olaf van der Spek
Common fwd
41
  AddSchemaNames(identifier::schema::vector &of_names) :
1415 by Brian Aker
Mass overhaul to use schema_identifier.
42
    schemas(of_names)
43
  {
44
  }
45
46
  result_type operator() (argument_type engine)
47
  {
48
    engine->doGetSchemaIdentifiers(schemas);
49
  }
50
};
51
2252.1.9 by Olaf van der Spek
Common fwd
52
void StorageEngine::getIdentifiers(Session &session, identifier::schema::vector &schemas)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
53
{
54
  // Add hook here for engines to register schema.
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
55
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
1415 by Brian Aker
Mass overhaul to use schema_identifier.
56
           AddSchemaNames(schemas));
57
2159.2.7 by Brian Aker
Merge in shared ptr modification for auth (namely we don't take the hit for
58
  plugin::Authorization::pruneSchemaNames(*session.user(), schemas);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
59
}
60
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
61
class StorageEngineGetSchemaDefinition: public std::unary_function<StorageEngine *, bool>
1415 by Brian Aker
Mass overhaul to use schema_identifier.
62
{
2087.4.1 by Brian Aker
Merge in schema identifier.
63
  const identifier::Schema &identifier;
1966.2.1 by Brian Aker
Clean up style for shared_ptr for messages around table/schema.
64
  message::schema::shared_ptr &schema_proto;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
65
66
public:
2087.4.1 by Brian Aker
Merge in schema identifier.
67
  StorageEngineGetSchemaDefinition(const identifier::Schema &identifier_arg,
1966.2.1 by Brian Aker
Clean up style for shared_ptr for messages around table/schema.
68
                                   message::schema::shared_ptr &schema_proto_arg) :
1415 by Brian Aker
Mass overhaul to use schema_identifier.
69
    identifier(identifier_arg),
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
70
    schema_proto(schema_proto_arg)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
71
  {
72
  }
73
74
  result_type operator() (argument_type engine)
75
  {
2159.2.2 by Brian Aker
Remove additional spot where we passed in a reference of a shared pointer.
76
    schema_proto= engine->doGetSchemaDefinition(identifier);
77
    return schema_proto;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
78
  }
79
};
80
81
/*
82
  Return value is "if parsed"
83
*/
2159.2.1 by Brian Aker
Remove the pass by reference, it really wasn't a good choice.
84
message::schema::shared_ptr StorageEngine::getSchemaDefinition(const drizzled::identifier::Table &identifier)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
85
{
2187.7.5 by Brian Aker
Merge in modifications such that we check both schema and table for
86
  identifier::Schema schema_identifier= identifier;
87
  return StorageEngine::getSchemaDefinition(schema_identifier);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
88
}
89
2159.2.1 by Brian Aker
Remove the pass by reference, it really wasn't a good choice.
90
message::schema::shared_ptr StorageEngine::getSchemaDefinition(const identifier::Schema &identifier)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
91
{
2159.2.1 by Brian Aker
Remove the pass by reference, it really wasn't a good choice.
92
  message::schema::shared_ptr proto;
93
1415 by Brian Aker
Mass overhaul to use schema_identifier.
94
  EngineVector::iterator iter=
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
95
    std::find_if(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
96
                 StorageEngineGetSchemaDefinition(identifier, proto));
1415 by Brian Aker
Mass overhaul to use schema_identifier.
97
98
  if (iter != StorageEngine::getSchemaEngines().end())
99
  {
2159.2.1 by Brian Aker
Remove the pass by reference, it really wasn't a good choice.
100
    return proto;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
101
  }
102
2159.2.1 by Brian Aker
Remove the pass by reference, it really wasn't a good choice.
103
  return message::schema::shared_ptr();
1415 by Brian Aker
Mass overhaul to use schema_identifier.
104
}
105
2087.4.1 by Brian Aker
Merge in schema identifier.
106
bool StorageEngine::doesSchemaExist(const identifier::Schema &identifier)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
107
{
1966.2.1 by Brian Aker
Clean up style for shared_ptr for messages around table/schema.
108
  message::schema::shared_ptr proto;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
109
2159.2.1 by Brian Aker
Remove the pass by reference, it really wasn't a good choice.
110
  return StorageEngine::getSchemaDefinition(identifier);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
111
}
112
113
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
114
const charset_info_st *StorageEngine::getSchemaCollation(const identifier::Schema &identifier)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
115
{
2246.4.2 by Olaf van der Spek
Refactor Identifier::getSQLPath()
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();
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
120
  if (const charset_info_st* cs= get_charset_by_name(buffer.c_str()))
2246.4.2 by Olaf van der Spek
Refactor Identifier::getSQLPath()
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());
1415 by Brian Aker
Mass overhaul to use schema_identifier.
124
  return default_charset_info;
125
}
126
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
127
class CreateSchema :
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
128
  public std::unary_function<StorageEngine *, void>
1415 by Brian Aker
Mass overhaul to use schema_identifier.
129
{
130
  const drizzled::message::Schema &schema_message;
2104.1.5 by Brian Aker
This fix issue where encoded schema would be lost on restart, and also fixes
131
  uint64_t &success_count;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
132
133
public:
134
2104.1.5 by Brian Aker
This fix issue where encoded schema would be lost on restart, and also fixes
135
  CreateSchema(const drizzled::message::Schema &arg, uint64_t &success_count_arg) :
136
    schema_message(arg),
137
    success_count(success_count_arg)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
138
  {
139
  }
140
141
  result_type operator() (argument_type engine)
142
  {
143
    // @todo eomeday check that at least one engine said "true"
1979.1.1 by Joseph Daly
move trx id assignment outside of schema engine and in plugin/schema_engine/schema.cc
144
    bool success= engine->doCreateSchema(schema_message);
145
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
146
    if (success)
1979.1.1 by Joseph Daly
move trx id assignment outside of schema engine and in plugin/schema_engine/schema.cc
147
    {
2104.1.5 by Brian Aker
This fix issue where encoded schema would be lost on restart, and also fixes
148
      success_count++;
2318.6.62 by Olaf van der Spek
Refactor
149
      TransactionServices::allocateNewTransactionId();
1979.1.1 by Joseph Daly
move trx id assignment outside of schema engine and in plugin/schema_engine/schema.cc
150
    }
1415 by Brian Aker
Mass overhaul to use schema_identifier.
151
  }
152
};
153
1856.2.8 by Joseph Daly
working alter, drop, create schema
154
bool StorageEngine::createSchema(const drizzled::message::Schema &schema_message)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
155
{
156
  // Add hook here for engines to register schema.
2104.1.5 by Brian Aker
This fix issue where encoded schema would be lost on restart, and also fixes
157
  uint64_t success_count= 0;
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
158
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
2104.1.5 by Brian Aker
This fix issue where encoded schema would be lost on restart, and also fixes
159
                CreateSchema(schema_message, success_count));
160
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
161
  if (success_count)
2104.1.5 by Brian Aker
This fix issue where encoded schema would be lost on restart, and also fixes
162
  {
2318.6.62 by Olaf van der Spek
Refactor
163
    TransactionServices::allocateNewTransactionId();
2104.1.5 by Brian Aker
This fix issue where encoded schema would be lost on restart, and also fixes
164
  }
165
166
  return (bool)success_count;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
167
}
168
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
169
class DropSchema :
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
170
  public std::unary_function<StorageEngine *, void>
1415 by Brian Aker
Mass overhaul to use schema_identifier.
171
{
172
  uint64_t &success_count;
2087.4.1 by Brian Aker
Merge in schema identifier.
173
  const identifier::Schema &identifier;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
174
175
public:
176
2087.4.1 by Brian Aker
Merge in schema identifier.
177
  DropSchema(const identifier::Schema &arg, uint64_t &count_arg) :
1415 by Brian Aker
Mass overhaul to use schema_identifier.
178
    success_count(count_arg),
179
    identifier(arg)
180
  {
181
  }
182
183
  result_type operator() (argument_type engine)
184
  {
185
    // @todo someday check that at least one engine said "true"
186
    bool success= engine->doDropSchema(identifier);
187
188
    if (success)
1979.1.1 by Joseph Daly
move trx id assignment outside of schema engine and in plugin/schema_engine/schema.cc
189
    {
1415 by Brian Aker
Mass overhaul to use schema_identifier.
190
      success_count++;
2318.6.62 by Olaf van der Spek
Refactor
191
      TransactionServices::allocateNewTransactionId();
1979.1.1 by Joseph Daly
move trx id assignment outside of schema engine and in plugin/schema_engine/schema.cc
192
    }
1415 by Brian Aker
Mass overhaul to use schema_identifier.
193
  }
194
};
195
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
196
static bool drop_all_tables_in_schema(Session& session,
2246.4.9 by Olaf van der Spek
Remove const_reference and reference from identifier::Schema
197
                                      const identifier::Schema& identifier,
2252.1.9 by Olaf van der Spek
Common fwd
198
                                      identifier::table::vector &dropped_tables,
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
199
                                      uint64_t &deleted)
200
{
201
  plugin::StorageEngine::getIdentifiers(session, identifier, dropped_tables);
202
2318.6.62 by Olaf van der Spek
Refactor
203
  for (identifier::table::vector::iterator it= dropped_tables.begin(); it != dropped_tables.end(); it++)
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
204
  {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
205
    boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
2168.3.2 by Brian Aker
CREATE TABLE/SCHEMA now allow you to add REPLICATION to the list of items
206
2172.3.1 by Brian Aker
Merge in my patch for having some schema/table not replicated.
207
    message::table::shared_ptr message= StorageEngine::getTableMessage(session, *it, false);
2168.3.2 by Brian Aker
CREATE TABLE/SCHEMA now allow you to add REPLICATION to the list of items
208
    if (not message)
209
    {
210
      my_error(ER_TABLE_DROP, *it);
211
      return false;
212
    }
213
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
214
    table::Cache::removeTable(session, *it, RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG);
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
215
    if (not plugin::StorageEngine::dropTable(session, *it))
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
216
    {
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
217
      my_error(ER_TABLE_DROP, *it);
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
218
      return false;
219
    }
2318.6.62 by Olaf van der Spek
Refactor
220
    TransactionServices::dropTable(session, *it, *message, true);
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
221
    deleted++;
222
  }
223
224
  return true;
225
}
226
2246.4.11 by Olaf van der Spek
Remove const_reference and reference from identifier::User
227
bool StorageEngine::dropSchema(Session& session,
2246.4.9 by Olaf van der Spek
Remove const_reference and reference from identifier::Schema
228
                               const identifier::Schema& identifier,
2172.3.1 by Brian Aker
Merge in my patch for having some schema/table not replicated.
229
                               message::schema::const_reference schema_message)
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
230
{
231
  uint64_t deleted= 0;
232
  bool error= false;
2252.1.9 by Olaf van der Spek
Common fwd
233
  identifier::table::vector dropped_tables;
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
234
235
  do
236
  {
237
    // Remove all temp tables first, this prevents loss of table from
238
    // shadowing (ie temp over standard table)
239
    {
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
240
      // Lets delete the temporary tables first outside of locks.
2252.1.9 by Olaf van der Spek
Common fwd
241
      identifier::table::vector set_of_identifiers;
2263.3.2 by Olaf van der Spek
Use open_tables
242
      session.open_tables.doGetTableIdentifiers(identifier, set_of_identifiers);
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
243
2252.1.9 by Olaf van der Spek
Common fwd
244
      for (identifier::table::vector::iterator iter= set_of_identifiers.begin(); iter != set_of_identifiers.end(); iter++)
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
245
      {
2263.3.7 by Olaf van der Spek
Keep functions in Open_tables_state
246
        if (session.open_tables.drop_temporary_table(*iter))
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
247
        {
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
248
          my_error(ER_TABLE_DROP, *iter);
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
249
          error= true;
250
          break;
251
        }
252
      }
253
    }
254
255
    /* After deleting database, remove all cache entries related to schema */
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
256
    table::Cache::removeSchema(identifier);
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
257
258
    if (not drop_all_tables_in_schema(session, identifier, dropped_tables, deleted))
259
    {
260
      error= true;
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
261
      my_error(ER_DROP_SCHEMA, identifier);
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
262
      break;
263
    }
264
2041.3.12 by Brian Aker
Move to private.
265
    uint64_t counter= 0;
266
    // Add hook here for engines to register schema.
267
    std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
268
                  DropSchema(identifier, counter));
269
270
    if (not counter)
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
271
    {
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
272
      my_error(ER_DROP_SCHEMA, identifier);
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
273
      error= true;
274
275
      break;
276
    }
277
    else
278
    {
279
      /* We've already verified that the schema does exist, so safe to log it */
2318.6.62 by Olaf van der Spek
Refactor
280
      TransactionServices::dropSchema(session, identifier, schema_message);
2041.3.10 by Brian Aker
Put more of drop schema behind interface.
281
    }
282
  } while (0);
283
284
  if (deleted > 0)
285
  {
286
    session.clear_error();
287
    session.server_status|= SERVER_STATUS_DB_DROPPED;
288
    session.my_ok((uint32_t) deleted);
289
    session.server_status&= ~SERVER_STATUS_DB_DROPPED;
290
  }
291
292
293
  return error;
294
}
295
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
296
class AlterSchema :
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
297
  public std::unary_function<StorageEngine *, void>
1415 by Brian Aker
Mass overhaul to use schema_identifier.
298
{
299
  uint64_t &success_count;
300
  const drizzled::message::Schema &schema_message;
301
302
public:
303
304
  AlterSchema(const drizzled::message::Schema &arg, uint64_t &count_arg) :
305
    success_count(count_arg),
306
    schema_message(arg)
307
  {
308
  }
309
310
  result_type operator() (argument_type engine)
311
  {
312
    // @todo eomeday check that at least one engine said "true"
313
    bool success= engine->doAlterSchema(schema_message);
314
1802.12.1 by Brian Aker
This solves bug lp:654905
315
1415 by Brian Aker
Mass overhaul to use schema_identifier.
316
    if (success)
1979.1.1 by Joseph Daly
move trx id assignment outside of schema engine and in plugin/schema_engine/schema.cc
317
    {
1415 by Brian Aker
Mass overhaul to use schema_identifier.
318
      success_count++;
1979.1.1 by Joseph Daly
move trx id assignment outside of schema engine and in plugin/schema_engine/schema.cc
319
    }
1415 by Brian Aker
Mass overhaul to use schema_identifier.
320
  }
321
};
322
323
bool StorageEngine::alterSchema(const drizzled::message::Schema &schema_message)
324
{
325
  uint64_t success_count= 0;
326
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
327
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
328
                AlterSchema(schema_message, success_count));
1415 by Brian Aker
Mass overhaul to use schema_identifier.
329
2104.1.5 by Brian Aker
This fix issue where encoded schema would be lost on restart, and also fixes
330
  if (success_count)
331
  {
2318.6.62 by Olaf van der Spek
Refactor
332
    TransactionServices::allocateNewTransactionId();
2104.1.5 by Brian Aker
This fix issue where encoded schema would be lost on restart, and also fixes
333
  }
334
1415 by Brian Aker
Mass overhaul to use schema_identifier.
335
  return success_count ? true : false;
336
}
337
338
} /* namespace plugin */
339
} /* namespace drizzled */