~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/create_schema.cc

  • Committer: Olaf van der Spek
  • Date: 2011-10-10 09:27:50 UTC
  • mto: (2430.1.6 rf)
  • mto: This revision was merged to the branch mainline in revision 2436.
  • Revision ID: olafvdspek@gmail.com-20111010092750-ryxgmn7zj5yvxfkf
Refactor

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
 
21
#include <config.h>
 
22
 
22
23
#include <drizzled/show.h>
23
24
#include <drizzled/session.h>
24
25
#include <drizzled/statement/create_schema.h>
25
 
#include <drizzled/db.h>
 
26
#include <drizzled/schema.h>
26
27
#include <drizzled/plugin/event_observer.h>
27
28
#include <drizzled/message.h>
 
29
#include <drizzled/plugin/storage_engine.h>
 
30
#include <drizzled/sql_lex.h>
 
31
#include <drizzled/plugin/authorization.h>
28
32
 
29
33
#include <string>
30
34
 
38
42
  if (not validateSchemaOptions())
39
43
    return true;
40
44
 
41
 
  if (not session->endActiveTransaction())
 
45
  if (session().inTransaction())
42
46
  {
 
47
    my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
43
48
    return true;
44
49
  }
45
50
 
46
 
  SchemaIdentifier schema_identifier(string(session->lex->name.str, session->lex->name.length));
 
51
  identifier::Schema schema_identifier(string(lex().name.str, lex().name.length));
47
52
  if (not check(schema_identifier))
48
53
    return false;
49
54
 
50
 
  drizzled::message::init(schema_message, session->lex->name.str);
 
55
  drizzled::message::schema::init(schema_message, lex().name.str);
 
56
 
 
57
  message::set_definer(schema_message, *session().user());
51
58
 
52
59
  bool res = false;
53
 
  std::string path;
54
 
  schema_identifier.getSQLPath(path);
 
60
  std::string path = schema_identifier.getSQLPath();
55
61
 
56
 
  if (unlikely(plugin::EventObserver::beforeCreateDatabase(*session, path)))
 
62
  if (unlikely(plugin::EventObserver::beforeCreateDatabase(session(), path)))
57
63
  {
58
64
    my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
59
65
  }
60
66
  else
61
67
  {
62
 
    res= mysql_create_db(session, schema_message, is_if_not_exists);
63
 
    if (unlikely(plugin::EventObserver::afterCreateDatabase(*session, path, res)))
 
68
    res= schema::create(session(), schema_message, lex().exists());
 
69
    if (unlikely(plugin::EventObserver::afterCreateDatabase(session(), path, res)))
64
70
    {
65
 
      my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
 
71
      my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier);
66
72
      res = false;
67
73
    }
68
74
 
71
77
  return not res;
72
78
}
73
79
 
74
 
bool statement::CreateSchema::check(const SchemaIdentifier &identifier)
 
80
bool statement::CreateSchema::check(const identifier::Schema &identifier)
75
81
{
76
82
  if (not identifier.isValid())
77
83
    return false;
78
84
 
79
 
  if (not plugin::Authorization::isAuthorized(getSession()->user(), identifier))
 
85
  if (not plugin::Authorization::isAuthorized(*session().user(), identifier))
80
86
    return false;
81
87
 
82
 
  if (not is_if_not_exists)
 
88
  if (not lex().exists())
83
89
  {
84
90
    if (plugin::StorageEngine::doesSchemaExist(identifier))
85
91
    {
86
 
      std::string name;
87
 
 
88
 
      identifier.getSQLPath(name);
89
 
      my_error(ER_DB_CREATE_EXISTS, MYF(0), name.c_str());
 
92
      my_error(ER_DB_CREATE_EXISTS, identifier);
90
93
 
91
94
      return false;
92
95
    }