~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/db.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

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