~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/schema.cc

  • Committer: Brian Aker
  • Date: 2011-02-16 02:39:43 UTC
  • mfrom: (2171.1.3 drizzle-staging)
  • Revision ID: brian@tangent.org-20110216023943-z4lxkjvknnrrncr2
Merge of all fixes related to errors, schemas, etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <string>
26
26
#include <fstream>
27
27
 
28
 
#include "drizzled/error.h"
 
28
#include <drizzled/error.h>
29
29
#include <drizzled/gettext.h>
30
30
#include <drizzled/my_hash.h>
31
 
#include "drizzled/internal/m_string.h"
 
31
#include <drizzled/internal/m_string.h>
32
32
#include <drizzled/session.h>
33
 
#include <drizzled/db.h>
 
33
#include <drizzled/schema.h>
34
34
#include <drizzled/sql_base.h>
35
35
#include <drizzled/lock.h>
36
36
#include <drizzled/errmsg_print.h>
37
37
#include <drizzled/transaction_services.h>
38
38
#include <drizzled/message/schema.pb.h>
39
 
#include "drizzled/sql_table.h"
40
 
#include "drizzled/plugin/storage_engine.h"
41
 
#include "drizzled/plugin/authorization.h"
42
 
#include "drizzled/global_charset_info.h"
43
 
#include "drizzled/pthread_globals.h"
44
 
#include "drizzled/charset.h"
 
39
#include <drizzled/sql_table.h>
 
40
#include <drizzled/plugin/storage_engine.h>
 
41
#include <drizzled/plugin/authorization.h>
 
42
#include <drizzled/global_charset_info.h>
 
43
#include <drizzled/pthread_globals.h>
 
44
#include <drizzled/charset.h>
 
45
#include <drizzled/internal/my_sys.h>
45
46
 
46
47
#include <boost/thread/mutex.hpp>
47
48
 
48
 
#include "drizzled/internal/my_sys.h"
49
 
 
50
49
#define MAX_DROP_TABLE_Q_LEN      1024
51
50
 
52
51
using namespace std;
54
53
namespace drizzled
55
54
{
56
55
 
57
 
static void change_db_impl(Session *session);
58
 
static void change_db_impl(Session *session, identifier::Schema &schema_identifier);
 
56
namespace schema
 
57
{
 
58
 
 
59
static void change_db_impl(Session &session);
 
60
static void change_db_impl(Session &session, identifier::Schema &schema_identifier);
59
61
 
60
62
/*
61
63
  Create a database
78
80
 
79
81
*/
80
82
 
81
 
bool create_db(Session *session, const message::Schema &schema_message, const bool is_if_not_exists)
 
83
bool create(Session &session, const message::Schema &schema_message, const bool is_if_not_exists)
82
84
{
83
85
  TransactionServices &transaction_services= TransactionServices::singleton();
84
86
  bool error= false;
95
97
    has the global read lock and refuses the operation with
96
98
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
97
99
  */
98
 
  if (session->wait_if_global_read_lock(false, true))
 
100
  if (session.wait_if_global_read_lock(false, true))
99
101
  {
100
102
    return false;
101
103
  }
105
107
 
106
108
  // @todo push this lock down into the engine
107
109
  {
108
 
    boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock());
 
110
    boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
109
111
 
110
112
    // Check to see if it exists already.  
111
113
    identifier::Schema schema_identifier(schema_message.name());
118
120
      }
119
121
      else
120
122
      {
121
 
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
123
        push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
122
124
                            ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
123
125
                            schema_message.name().c_str());
124
 
        session->my_ok();
 
126
        session.my_ok();
125
127
      }
126
128
    }
127
129
    else if (not plugin::StorageEngine::createSchema(schema_message)) // Try to create it 
131
133
    }
132
134
    else // Created !
133
135
    {
134
 
      transaction_services.createSchema(*session, schema_message);
135
 
      session->my_ok(1);
 
136
      transaction_services.createSchema(session, schema_message);
 
137
      session.my_ok(1);
136
138
    }
137
139
  }
138
 
  session->startWaitingGlobalReadLock();
 
140
  session.startWaitingGlobalReadLock();
139
141
 
140
142
  return error;
141
143
}
143
145
 
144
146
/* db-name is already validated when we come here */
145
147
 
146
 
bool alter_db(Session *session,
147
 
              const message::Schema &schema_message,
148
 
              const message::schema::shared_ptr &original_schema)
 
148
bool alter(Session &session,
 
149
           const message::Schema &schema_message,
 
150
           const message::Schema &original_schema)
149
151
{
150
152
  TransactionServices &transaction_services= TransactionServices::singleton();
151
153
 
161
163
    has the global read lock and refuses the operation with
162
164
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
163
165
  */
164
 
  if ((session->wait_if_global_read_lock(false, true)))
 
166
  if ((session.wait_if_global_read_lock(false, true)))
165
167
    return false;
166
168
 
167
169
  bool success;
168
170
  {
169
 
    boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock());
 
171
    boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
170
172
 
171
173
    identifier::Schema schema_idenifier(schema_message.name());
172
174
    if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier))
180
182
 
181
183
    if (success)
182
184
    {
183
 
      transaction_services.alterSchema(*session, original_schema, schema_message);
184
 
      session->my_ok(1);
 
185
      transaction_services.alterSchema(session, original_schema, schema_message);
 
186
      session.my_ok(1);
185
187
    }
186
188
    else
187
189
    {
188
190
      my_error(ER_ALTER_SCHEMA, schema_idenifier);
189
191
    }
190
192
  }
191
 
  session->startWaitingGlobalReadLock();
 
193
  session.startWaitingGlobalReadLock();
192
194
 
193
195
  return success;
194
196
}
211
213
    ERROR Error
212
214
*/
213
215
 
214
 
bool rm_db(Session *session, identifier::Schema &schema_identifier, const bool if_exists)
 
216
bool drop(Session &session, identifier::Schema &schema_identifier, const bool if_exists)
215
217
{
216
218
  bool error= false;
217
219
 
227
229
    has the global read lock and refuses the operation with
228
230
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
229
231
  */
230
 
  if (session->wait_if_global_read_lock(false, true))
 
232
  if (session.wait_if_global_read_lock(false, true))
231
233
  {
232
234
    return true;
233
235
  }
234
236
 
235
237
  do
236
238
  {
237
 
    boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock());
 
239
    boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
238
240
 
239
241
    /* See if the schema exists */
240
242
    if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
244
246
        std::string path;
245
247
        schema_identifier.getSQLPath(path);
246
248
 
247
 
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
249
        push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
248
250
                            ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
249
251
                            path.c_str());
250
252
      }
257
259
    }
258
260
    else
259
261
    {
260
 
      error= plugin::StorageEngine::dropSchema(*session, schema_identifier);
 
262
      error= plugin::StorageEngine::dropSchema(session, schema_identifier);
261
263
    }
262
264
 
263
265
  } while (0);
268
270
    SELECT DATABASE() in the future). For this we free() session->db and set
269
271
    it to 0.
270
272
  */
271
 
  if (not error and schema_identifier.compare(*session->schema()))
 
273
  if (not error and schema_identifier.compare(*session.schema()))
272
274
    change_db_impl(session);
273
275
 
274
 
  session->startWaitingGlobalReadLock();
 
276
  session.startWaitingGlobalReadLock();
275
277
 
276
278
  return error;
277
279
}
338
340
    @retval true  Error
339
341
*/
340
342
 
341
 
bool change_db(Session *session, identifier::Schema &schema_identifier)
 
343
bool change(Session &session, identifier::Schema &schema_identifier)
342
344
{
343
345
 
344
 
  if (not plugin::Authorization::isAuthorized(session->user(), schema_identifier))
 
346
  if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
345
347
  {
346
348
    /* Error message is set in isAuthorized */
347
349
    return true;
348
350
  }
349
351
 
350
 
  if (not check_db_name(session, schema_identifier))
 
352
  if (not check(session, schema_identifier))
351
353
  {
352
354
    my_error(ER_WRONG_DB_NAME, schema_identifier);
353
355
 
379
381
  @param new_db_charset Character set of the new database.
380
382
*/
381
383
 
382
 
static void change_db_impl(Session *session, identifier::Schema &schema_identifier)
 
384
static void change_db_impl(Session &session, identifier::Schema &schema_identifier)
383
385
{
384
386
  /* 1. Change current database in Session. */
385
387
 
402
404
      the previous database name, we should do it explicitly.
403
405
    */
404
406
 
405
 
    session->set_db(schema_identifier.getSchemaName());
406
 
  }
407
 
}
408
 
 
409
 
static void change_db_impl(Session *session)
410
 
{
411
 
  session->set_db(string());
412
 
}
 
407
    session.set_db(schema_identifier.getSchemaName());
 
408
  }
 
409
}
 
410
 
 
411
static void change_db_impl(Session &session)
 
412
{
 
413
  session.set_db(string());
 
414
}
 
415
 
 
416
/*
 
417
  Check if database name is valid
 
418
 
 
419
  SYNPOSIS
 
420
    check()
 
421
    org_name            Name of database and length
 
422
 
 
423
  RETURN
 
424
    false error
 
425
    true ok
 
426
*/
 
427
 
 
428
bool check(Session &session, identifier::Schema &schema_identifier)
 
429
{
 
430
  if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
 
431
  {
 
432
    return false;
 
433
  }
 
434
 
 
435
  return schema_identifier.isValid();
 
436
}
 
437
 
 
438
} /* namespace schema */
413
439
 
414
440
} /* namespace drizzled */