~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/schema_engine.cc

  • Committer: Brian Aker
  • Date: 2010-03-31 05:53:34 UTC
  • Revision ID: brian@gaz-20100331055334-yqqmzlgqb2xq1p5b
Mass overhaul to use schema_identifier.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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;
 
31
 
 
32
namespace drizzled
 
33
{
 
34
 
 
35
namespace plugin
 
36
{
 
37
 
 
38
class AddSchemaNames : 
 
39
  public unary_function<StorageEngine *, void>
 
40
{
 
41
  SchemaIdentifierList &schemas;
 
42
 
 
43
public:
 
44
 
 
45
  AddSchemaNames(SchemaIdentifierList &of_names) :
 
46
    schemas(of_names)
 
47
  {
 
48
  }
 
49
 
 
50
  result_type operator() (argument_type engine)
 
51
  {
 
52
    engine->doGetSchemaIdentifiers(schemas);
 
53
  }
 
54
};
 
55
 
 
56
void StorageEngine::getSchemaIdentifiers(Session &session, SchemaIdentifierList &schemas)
 
57
{
 
58
  // Add hook here for engines to register schema.
 
59
  for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
60
           AddSchemaNames(schemas));
 
61
 
 
62
  plugin::Authorization::pruneSchemaNames(session.getSecurityContext(), schemas);
 
63
}
 
64
 
 
65
class StorageEngineGetSchemaDefinition: public unary_function<StorageEngine *, bool>
 
66
{
 
67
  SchemaIdentifier &identifier;
 
68
  message::Schema &schema_proto;
 
69
 
 
70
public:
 
71
  StorageEngineGetSchemaDefinition(SchemaIdentifier &identifier_arg,
 
72
                                   message::Schema &schema_proto_arg) :
 
73
    identifier(identifier_arg),
 
74
    schema_proto(schema_proto_arg) 
 
75
  {
 
76
  }
 
77
 
 
78
  result_type operator() (argument_type engine)
 
79
  {
 
80
    return engine->doGetSchemaDefinition(identifier, schema_proto);
 
81
  }
 
82
};
 
83
 
 
84
/*
 
85
  Return value is "if parsed"
 
86
*/
 
87
bool StorageEngine::getSchemaDefinition(TableIdentifier &identifier, message::Schema &proto)
 
88
{
 
89
  return StorageEngine::getSchemaDefinition(identifier, proto);
 
90
}
 
91
 
 
92
bool StorageEngine::getSchemaDefinition(SchemaIdentifier &identifier, message::Schema &proto)
 
93
{
 
94
  proto.Clear();
 
95
 
 
96
  EngineVector::iterator iter=
 
97
    find_if(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
98
            StorageEngineGetSchemaDefinition(identifier, proto));
 
99
 
 
100
  if (iter != StorageEngine::getSchemaEngines().end())
 
101
  {
 
102
    return true;
 
103
  }
 
104
 
 
105
  return false;
 
106
}
 
107
 
 
108
bool StorageEngine::doesSchemaExist(SchemaIdentifier &identifier)
 
109
{
 
110
  message::Schema proto;
 
111
 
 
112
  return StorageEngine::getSchemaDefinition(identifier, proto);
 
113
}
 
114
 
 
115
 
 
116
const CHARSET_INFO *StorageEngine::getSchemaCollation(SchemaIdentifier &identifier)
 
117
{
 
118
  message::Schema schmema_proto;
 
119
  bool found;
 
120
 
 
121
  found= StorageEngine::getSchemaDefinition(identifier, schmema_proto);
 
122
 
 
123
  if (found && schmema_proto.has_collation())
 
124
  {
 
125
    const string buffer= schmema_proto.collation();
 
126
    const CHARSET_INFO* cs= get_charset_by_name(buffer.c_str());
 
127
 
 
128
    if (not cs)
 
129
    {
 
130
      errmsg_printf(ERRMSG_LVL_ERROR,
 
131
                    _("Error while loading database options: '%s':"), identifier.getSQLPath().c_str());
 
132
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
 
133
 
 
134
      return default_charset_info;
 
135
    }
 
136
 
 
137
    return cs;
 
138
  }
 
139
 
 
140
  return default_charset_info;
 
141
}
 
142
 
 
143
class CreateSchema : 
 
144
  public unary_function<StorageEngine *, void>
 
145
{
 
146
  const drizzled::message::Schema &schema_message;
 
147
 
 
148
public:
 
149
 
 
150
  CreateSchema(const drizzled::message::Schema &arg) :
 
151
    schema_message(arg)
 
152
  {
 
153
  }
 
154
 
 
155
  result_type operator() (argument_type engine)
 
156
  {
 
157
    // @todo eomeday check that at least one engine said "true"
 
158
    (void)engine->doCreateSchema(schema_message);
 
159
  }
 
160
};
 
161
 
 
162
bool StorageEngine::createSchema(const drizzled::message::Schema &schema_message)
 
163
{
 
164
  // Add hook here for engines to register schema.
 
165
  for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
166
           CreateSchema(schema_message));
 
167
 
 
168
  return true;
 
169
}
 
170
 
 
171
class DropSchema : 
 
172
  public unary_function<StorageEngine *, void>
 
173
{
 
174
  uint64_t &success_count;
 
175
  SchemaIdentifier &identifier;
 
176
 
 
177
public:
 
178
 
 
179
  DropSchema(SchemaIdentifier &arg, uint64_t &count_arg) :
 
180
    success_count(count_arg),
 
181
    identifier(arg)
 
182
  {
 
183
  }
 
184
 
 
185
  result_type operator() (argument_type engine)
 
186
  {
 
187
    // @todo someday check that at least one engine said "true"
 
188
    bool success= engine->doDropSchema(identifier);
 
189
 
 
190
    if (success)
 
191
      success_count++;
 
192
  }
 
193
};
 
194
 
 
195
bool StorageEngine::dropSchema(SchemaIdentifier &identifier)
 
196
{
 
197
  uint64_t counter= 0;
 
198
  // Add hook here for engines to register schema.
 
199
  for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
200
           DropSchema(identifier, counter));
 
201
 
 
202
  return counter ? true : false;
 
203
}
 
204
 
 
205
class AlterSchema : 
 
206
  public unary_function<StorageEngine *, void>
 
207
{
 
208
  uint64_t &success_count;
 
209
  const drizzled::message::Schema &schema_message;
 
210
 
 
211
public:
 
212
 
 
213
  AlterSchema(const drizzled::message::Schema &arg, uint64_t &count_arg) :
 
214
    success_count(count_arg),
 
215
    schema_message(arg)
 
216
  {
 
217
  }
 
218
 
 
219
  result_type operator() (argument_type engine)
 
220
  {
 
221
    // @todo eomeday check that at least one engine said "true"
 
222
    bool success= engine->doAlterSchema(schema_message);
 
223
 
 
224
    if (success)
 
225
      success_count++;
 
226
  }
 
227
};
 
228
 
 
229
bool StorageEngine::alterSchema(const drizzled::message::Schema &schema_message)
 
230
{
 
231
  uint64_t success_count= 0;
 
232
 
 
233
  for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
 
234
           AlterSchema(schema_message, success_count));
 
235
 
 
236
  return success_count ? true : false;
 
237
}
 
238
 
 
239
} /* namespace plugin */
 
240
} /* namespace drizzled */