~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/schema_engine.cc

  • Committer: Stewart Smith
  • Date: 2010-03-02 06:41:59 UTC
  • mto: (1309.2.13 build)
  • mto: This revision was merged to the branch mainline in revision 1318.
  • Revision ID: stewart@flamingspork.com-20100302064159-gktw6hcbs3u0fflm
move Item_result out to its own header file and out of common.h

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
 
#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 : 
38
 
  public std::unary_function<StorageEngine *, void>
39
 
{
40
 
  SchemaIdentifier::vector &schemas;
41
 
 
42
 
public:
43
 
 
44
 
  AddSchemaNames(SchemaIdentifier::vector &of_names) :
45
 
    schemas(of_names)
46
 
  {
47
 
  }
48
 
 
49
 
  result_type operator() (argument_type engine)
50
 
  {
51
 
    engine->doGetSchemaIdentifiers(schemas);
52
 
  }
53
 
};
54
 
 
55
 
void StorageEngine::getIdentifiers(Session &session, SchemaIdentifier::vector &schemas)
56
 
{
57
 
  // Add hook here for engines to register schema.
58
 
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
59
 
           AddSchemaNames(schemas));
60
 
 
61
 
  plugin::Authorization::pruneSchemaNames(session.getSecurityContext(), schemas);
62
 
}
63
 
 
64
 
class StorageEngineGetSchemaDefinition: public std::unary_function<StorageEngine *, bool>
65
 
{
66
 
  const SchemaIdentifier &identifier;
67
 
  message::schema::shared_ptr &schema_proto;
68
 
 
69
 
public:
70
 
  StorageEngineGetSchemaDefinition(const SchemaIdentifier &identifier_arg,
71
 
                                   message::schema::shared_ptr &schema_proto_arg) :
72
 
    identifier(identifier_arg),
73
 
    schema_proto(schema_proto_arg) 
74
 
  {
75
 
  }
76
 
 
77
 
  result_type operator() (argument_type engine)
78
 
  {
79
 
    return engine->doGetSchemaDefinition(identifier, schema_proto);
80
 
  }
81
 
};
82
 
 
83
 
/*
84
 
  Return value is "if parsed"
85
 
*/
86
 
bool StorageEngine::getSchemaDefinition(const drizzled::TableIdentifier &identifier, message::schema::shared_ptr &proto)
87
 
{
88
 
  return StorageEngine::getSchemaDefinition(identifier, proto);
89
 
}
90
 
 
91
 
bool StorageEngine::getSchemaDefinition(const SchemaIdentifier &identifier, message::schema::shared_ptr &proto)
92
 
{
93
 
  EngineVector::iterator iter=
94
 
    std::find_if(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
95
 
                 StorageEngineGetSchemaDefinition(identifier, proto));
96
 
 
97
 
  if (iter != StorageEngine::getSchemaEngines().end())
98
 
  {
99
 
    return true;
100
 
  }
101
 
 
102
 
  return false;
103
 
}
104
 
 
105
 
bool StorageEngine::doesSchemaExist(const SchemaIdentifier &identifier)
106
 
{
107
 
  message::schema::shared_ptr proto;
108
 
 
109
 
  return StorageEngine::getSchemaDefinition(identifier, proto);
110
 
}
111
 
 
112
 
 
113
 
const CHARSET_INFO *StorageEngine::getSchemaCollation(const SchemaIdentifier &identifier)
114
 
{
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(ERRMSG_LVL_ERROR,
131
 
                    _("Error while loading database options: '%s':"), path.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 std::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
 
    bool success= engine->doCreateSchema(schema_message);
159
 
 
160
 
    if (success) 
161
 
    {
162
 
      TransactionServices &transaction_services= TransactionServices::singleton();
163
 
      transaction_services.allocateNewTransactionId();
164
 
    }
165
 
  }
166
 
};
167
 
 
168
 
bool StorageEngine::createSchema(const drizzled::message::Schema &schema_message)
169
 
{
170
 
  // Add hook here for engines to register schema.
171
 
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
172
 
                CreateSchema(schema_message));
173
 
 
174
 
  return true;
175
 
}
176
 
 
177
 
class DropSchema : 
178
 
  public std::unary_function<StorageEngine *, void>
179
 
{
180
 
  uint64_t &success_count;
181
 
  const SchemaIdentifier &identifier;
182
 
 
183
 
public:
184
 
 
185
 
  DropSchema(const SchemaIdentifier &arg, uint64_t &count_arg) :
186
 
    success_count(count_arg),
187
 
    identifier(arg)
188
 
  {
189
 
  }
190
 
 
191
 
  result_type operator() (argument_type engine)
192
 
  {
193
 
    // @todo someday check that at least one engine said "true"
194
 
    bool success= engine->doDropSchema(identifier);
195
 
 
196
 
    if (success)
197
 
    {
198
 
      success_count++;
199
 
      TransactionServices &transaction_services= TransactionServices::singleton();
200
 
      transaction_services.allocateNewTransactionId();
201
 
    }
202
 
  }
203
 
};
204
 
 
205
 
bool StorageEngine::dropSchema(const SchemaIdentifier &identifier)
206
 
{
207
 
  uint64_t counter= 0;
208
 
  // Add hook here for engines to register schema.
209
 
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
210
 
                DropSchema(identifier, counter));
211
 
 
212
 
  return counter ? true : false;
213
 
}
214
 
 
215
 
class AlterSchema : 
216
 
  public std::unary_function<StorageEngine *, void>
217
 
{
218
 
  uint64_t &success_count;
219
 
  const drizzled::message::Schema &schema_message;
220
 
 
221
 
public:
222
 
 
223
 
  AlterSchema(const drizzled::message::Schema &arg, uint64_t &count_arg) :
224
 
    success_count(count_arg),
225
 
    schema_message(arg)
226
 
  {
227
 
  }
228
 
 
229
 
  result_type operator() (argument_type engine)
230
 
  {
231
 
    // @todo eomeday check that at least one engine said "true"
232
 
    bool success= engine->doAlterSchema(schema_message);
233
 
 
234
 
 
235
 
    if (success)
236
 
    {
237
 
      success_count++;
238
 
      TransactionServices &transaction_services= TransactionServices::singleton();
239
 
      transaction_services.allocateNewTransactionId();
240
 
    }
241
 
  }
242
 
};
243
 
 
244
 
bool StorageEngine::alterSchema(const drizzled::message::Schema &schema_message)
245
 
{
246
 
  uint64_t success_count= 0;
247
 
 
248
 
  std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
249
 
                AlterSchema(schema_message, success_count));
250
 
 
251
 
  return success_count ? true : false;
252
 
}
253
 
 
254
 
} /* namespace plugin */
255
 
} /* namespace drizzled */