~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/db.cc

  • Committer: Brian Aker
  • Date: 2010-02-24 23:10:30 UTC
  • mto: (1273.13.101 build)
  • mto: This revision was merged to the branch mainline in revision 1309.
  • Revision ID: brian@gaz-20100224231030-4dwc2iwcih5cootv
createSchema() now works via SE interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
static void mysql_change_db_impl(Session *session, LEX_STRING *new_db_name);
63
63
 
64
64
/* path is path to database, not schema file */
65
 
static int write_schema_file(const char *path, const message::Schema &db)
 
65
int write_schema_file(const char *path, const message::Schema &db)
66
66
{
67
67
  char schema_file_tmp[FN_REFLEN];
68
68
  string schema_file(path);
116
116
 
117
117
*/
118
118
 
119
 
bool mysql_create_db(Session *session, const char *db, message::Schema *schema_message, bool is_if_not_exists)
 
119
bool mysql_create_db(Session *session, message::Schema &schema_message, bool is_if_not_exists)
120
120
{
121
121
  ReplicationServices &replication_services= ReplicationServices::singleton();
122
 
  long result= 1;
123
 
  int error_erno;
124
122
  bool error= false;
125
123
 
126
 
  schema_message->set_name(db);
127
 
 
128
124
  /*
129
125
    Do not create database if another thread is holding read lock.
130
126
    Wait for global read lock before acquiring LOCK_create_db.
139
135
  */
140
136
  if (wait_if_global_read_lock(session, 0, 1))
141
137
  {
142
 
    error= true;
143
 
    goto exit2;
 
138
    return false;
144
139
  }
145
140
 
 
141
  // @todo push this lock down into the engine
146
142
  pthread_mutex_lock(&LOCK_create_db);
147
143
 
148
 
  /* check directory */
149
 
  char   path[FN_REFLEN+16];
150
 
  uint32_t path_len;
151
 
  path_len= build_table_filename(path, sizeof(path), db, "", false);
152
 
  path[path_len-1]= 0;                    // remove last '/' from path
153
 
 
154
 
  if (mkdir(path, 0777) == -1)
 
144
  // Check to see if it exists already.
 
145
  if (plugin::StorageEngine::doesSchemaExist(schema_message.name()))
155
146
  {
156
 
    if (errno == EEXIST)
157
 
    {
158
 
      if (! is_if_not_exists)
159
 
      {
160
 
        my_error(ER_DB_CREATE_EXISTS, MYF(0), path);
161
 
        error= true;
162
 
        goto exit;
163
 
      }
 
147
    if (not is_if_not_exists)
 
148
    {
 
149
      my_error(ER_DB_CREATE_EXISTS, MYF(0), schema_message.name().c_str());
 
150
      error= true;
 
151
    }
 
152
    else
 
153
    {
164
154
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
165
 
                          ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
166
 
                          path);
 
155
                          ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
 
156
                          schema_message.name().c_str());
167
157
      session->my_ok();
168
 
      error= false;
169
 
      goto exit;
170
 
    }
171
 
 
172
 
    my_error(ER_CANT_CREATE_DB, MYF(0), path, errno);
173
 
    error= true;
174
 
    goto exit;
175
 
  }
176
 
 
177
 
  error_erno= write_schema_file(path, *schema_message);
178
 
  if (error_erno && error_erno != EEXIST)
179
 
  {
180
 
    if (rmdir(path) >= 0)
181
 
    {
182
 
      error= true;
183
 
      goto exit;
184
 
    }
185
 
  }
186
 
  else if (error_erno)
187
 
    error= true;
188
 
 
189
 
  replication_services.rawStatement(session, session->query);
190
 
  session->my_ok(result);
191
 
 
192
 
exit:
 
158
    }
 
159
  }
 
160
  else if (not plugin::StorageEngine::createSchema(schema_message)) // Try to create it 
 
161
  {
 
162
    my_error(ER_CANT_CREATE_DB, MYF(0), schema_message.name().c_str(), errno);
 
163
    error= true;
 
164
  }
 
165
  else // Created !
 
166
  {
 
167
    replication_services.rawStatement(session, session->query);
 
168
    session->my_ok(1);
 
169
  }
 
170
 
193
171
  pthread_mutex_unlock(&LOCK_create_db);
194
172
  start_waiting_global_read_lock(session);
195
 
exit2:
 
173
 
196
174
  return error;
197
175
}
198
176