~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2003 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/* create and drop of databases */
612.2.4 by Monty Taylor
Moved some defines to config.h. Stopped including config.h directly anywhere.
18
#include <drizzled/global.h>
481.1.9 by Monty Taylor
Added autoconf tests for location of cstdint and cinttypes. Use those in C++ programs now, so that we don't have to define _STDC_LIMIT_MACROS, etc by hand. Stop, in fact, defining those by hand.
19
#include CSTDINT_H
20
#include CINTTYPES_H
318 by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type.
21
#include <string>
22
#include <fstream>
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
23
#include <drizzled/message/schema.pb.h>
318 by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type.
24
using namespace std;
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
25
#include <drizzled/server_includes.h>
212.5.13 by Monty Taylor
Moved my_sys/my_pthread/my_nosys and mysys_err to mysys.
26
#include <mysys/mysys_err.h>
212.5.38 by Monty Taylor
Moved my_dir (and removed references to a lot of places)
27
#include <mysys/my_dir.h>
549 by Monty Taylor
Took gettext.h out of header files.
28
#include <drizzled/error.h>
538 by Monty Taylor
Moved gettext.h into drizzled in anticipation of the new client lib.
29
#include <drizzled/gettext.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
30
#include <mysys/hash.h>
31
#include <drizzled/session.h>
32
#include <drizzled/db.h>
33
#include <drizzled/sql_base.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
34
#include <drizzled/lock.h>
722.4.1 by Mark Atwood
integrate errmsg plugin into sql_print_* functions
35
#include <drizzled/errmsg_print.h>
1039.5.31 by Jay Pipes
This patch does a few things:
36
#include <drizzled/replication_services.h>
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
37
#include <drizzled/message/schema.pb.h>
988.1.6 by Jay Pipes
Removed old protobuf_replicator plugin, fixed up db.cc and other files to use new
38
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
39
using namespace drizzled;
40
822 by Brian Aker
Merge from Stewart.
41
#define MY_DB_OPT_FILE "db.opt"
1 by brian
clean slate
42
#define MAX_DROP_TABLE_Q_LEN      1024
43
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
44
const char *del_exts[]= {".dfe", ".BAK", ".TMD",".opt", NULL};
1 by brian
clean slate
45
static TYPELIB deletable_extentions=
46
{array_elements(del_exts)-1,"del_exts", del_exts, NULL};
47
520.1.22 by Brian Aker
Second pass of thd cleanup
48
static long mysql_rm_known_files(Session *session, MY_DIR *dirp,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
49
                                 const char *db, const char *path,
327.2.4 by Brian Aker
Refactoring table.h
50
                                 TableList **dropped_tables);
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
51
1014.3.2 by Brian Aker
Factor out need for session in many "schema" calls. Removed variable about
52
static void mysql_change_db_impl(Session *session, LEX_STRING *new_db_name);
53
            
1 by brian
clean slate
54
55
/**
56
  Return default database collation.
57
520.1.22 by Brian Aker
Second pass of thd cleanup
58
  @param session     Thread context.
1 by brian
clean slate
59
  @param db_name Database name.
60
61
  @return CHARSET_INFO object. The operation always return valid character
62
    set, even if the database does not exist.
63
*/
64
1014.3.1 by Brian Aker
Simplify the calling stack for getting schema collation. We need to extend
65
const CHARSET_INFO *get_default_db_collation(const char *db_name)
1 by brian
clean slate
66
{
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
67
  message::Schema db;
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
68
69
  get_database_metadata(db_name, &db);
70
71
  /* If for some reason the db.opt file lacks a collation,
72
     we just return the default */
73
74
  if (db.has_collation())
75
  {
76
    const string buffer= db.collation();
77
    const CHARSET_INFO* cs= get_charset_by_name(buffer.c_str());
78
79
    if (!cs)
80
    {
81
      errmsg_printf(ERRMSG_LVL_ERROR,
82
                    _("Error while loading database options: '%s':"),db_name);
83
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
84
85
      return default_charset_info;
86
    }
87
88
    return cs;
89
  }
90
91
  return default_charset_info;
1 by brian
clean slate
92
}
93
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
94
/* path is path to database, not schema file */
95
static int write_schema_file(Session *session,
96
			     const char *path, const char *name,
97
			     HA_CREATE_INFO *create)
98
{
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
99
  message::Schema db;
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
100
  char schema_file_tmp[FN_REFLEN];
101
  string schema_file(path);
102
103
  assert(path);
104
  assert(name);
105
  assert(create);
106
822 by Brian Aker
Merge from Stewart.
107
  snprintf(schema_file_tmp, FN_REFLEN, "%s%c%s.tmpXXXXXX", path, FN_LIBCHAR, MY_DB_OPT_FILE);
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
108
109
  schema_file.append(1, FN_LIBCHAR);
822 by Brian Aker
Merge from Stewart.
110
  schema_file.append(MY_DB_OPT_FILE);
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
111
112
  int fd= mkstemp(schema_file_tmp);
113
114
  if (fd==-1)
115
    return errno;
116
117
  if (!create->default_table_charset)
118
    create->default_table_charset= session->variables.collation_server;
119
120
  db.set_name(name);
121
  db.set_collation(create->default_table_charset->name);
122
123
  if (!db.SerializeToFileDescriptor(fd))
124
  {
125
    close(fd);
126
    unlink(schema_file_tmp);
127
    return -1;
128
  }
129
130
  if (rename(schema_file_tmp, schema_file.c_str()) == -1)
131
  {
132
    close(fd);
133
    return errno;
134
  }
135
136
  close(fd);
137
  return 0;
138
}
139
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
140
int get_database_metadata(const char *dbname, message::Schema *db)
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
141
{
142
  char db_opt_path[FN_REFLEN];
1039.1.6 by Brian Aker
Refactor for build_table_filename()
143
  size_t length;
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
144
145
  /*
146
    Pass an empty file name, and the database options file name as extension
147
    to avoid table name to file name encoding.
148
  */
1039.1.6 by Brian Aker
Refactor for build_table_filename()
149
  length= build_table_filename(db_opt_path, sizeof(db_opt_path),
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
150
                              dbname, "", false);
1039.1.6 by Brian Aker
Refactor for build_table_filename()
151
  strcpy(db_opt_path + length, MY_DB_OPT_FILE);
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
152
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
153
  int fd= open(db_opt_path, O_RDONLY);
154
155
  if (fd == -1)
156
    return errno;
157
158
  if (!db->ParseFromFileDescriptor(fd))
159
  {
160
    close(fd);
161
    return -1;
162
  }
163
  close(fd);
164
165
  return 0;
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
166
}
167
1 by brian
clean slate
168
/*
169
  Create a database
170
171
  SYNOPSIS
172
  mysql_create_db()
520.1.22 by Brian Aker
Second pass of thd cleanup
173
  session		Thread handler
1 by brian
clean slate
174
  db		Name of database to create
175
		Function assumes that this is already validated.
176
  create_info	Database create options (like character set)
177
178
  SIDE-EFFECTS
179
   1. Report back to client that command succeeded (my_ok)
180
   2. Report errors to client
181
   3. Log event to binary log
182
183
  RETURN VALUES
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
184
  false ok
185
  true  Error
1 by brian
clean slate
186
187
*/
188
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
189
bool mysql_create_db(Session *session, const char *db, HA_CREATE_INFO *create_info)
1 by brian
clean slate
190
{
1130.1.2 by Monty Taylor
Re-org'd the replication stuff into slots.
191
  ReplicationServices &replication_services= ReplicationServices::singleton();
1 by brian
clean slate
192
  char	 path[FN_REFLEN+16];
193
  long result= 1;
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
194
  int error_erno;
195
  bool error= false;
482 by Brian Aker
Remove uint.
196
  uint32_t create_options= create_info ? create_info->options : 0;
197
  uint32_t path_len;
1 by brian
clean slate
198
199
  /* do not create 'information_schema' db */
575.4.7 by Monty Taylor
More header cleanup.
200
  if (!my_strcasecmp(system_charset_info, db, INFORMATION_SCHEMA_NAME.c_str()))
1 by brian
clean slate
201
  {
202
    my_error(ER_DB_CREATE_EXISTS, MYF(0), db);
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
203
    return(-1);
1 by brian
clean slate
204
  }
205
206
  /*
207
    Do not create database if another thread is holding read lock.
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
208
    Wait for global read lock before acquiring LOCK_create_db.
1 by brian
clean slate
209
    After wait_if_global_read_lock() we have protection against another
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
210
    global read lock. If we would acquire LOCK_create_db first,
1 by brian
clean slate
211
    another thread could step in and get the global read lock before we
212
    reach wait_if_global_read_lock(). If this thread tries the same as we
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
213
    (admin a db), it would then go and wait on LOCK_create_db...
1 by brian
clean slate
214
    Furthermore wait_if_global_read_lock() checks if the current thread
215
    has the global read lock and refuses the operation with
216
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
217
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
218
  if (wait_if_global_read_lock(session, 0, 1))
1 by brian
clean slate
219
  {
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
220
    error= true;
1 by brian
clean slate
221
    goto exit2;
222
  }
223
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
224
  pthread_mutex_lock(&LOCK_create_db);
1 by brian
clean slate
225
226
  /* Check directory */
1039.1.6 by Brian Aker
Refactor for build_table_filename()
227
  path_len= build_table_filename(path, sizeof(path), db, "", false);
1 by brian
clean slate
228
  path[path_len-1]= 0;                    // Remove last '/' from path
229
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
230
  if (mkdir(path,0777) == -1)
1 by brian
clean slate
231
  {
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
232
    if (errno == EEXIST)
1 by brian
clean slate
233
    {
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
234
      if (!(create_options & HA_LEX_CREATE_IF_NOT_EXISTS))
235
      {
236
	my_error(ER_DB_CREATE_EXISTS, MYF(0), db);
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
237
	error= true;
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
238
	goto exit;
239
      }
240
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
241
			  ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS), db);
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
242
      session->my_ok();
243
      error= false;
1 by brian
clean slate
244
      goto exit;
245
    }
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
246
247
    my_error(ER_CANT_CREATE_DB, MYF(0), db, my_errno);
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
248
    error= true;
1 by brian
clean slate
249
    goto exit;
250
  }
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
251
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
252
  error_erno= write_schema_file(session, path, db, create_info);
253
  if (error_erno && error_erno != EEXIST)
1 by brian
clean slate
254
  {
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
255
    if (rmdir(path) >= 0)
256
    {
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
257
      error= true;
1 by brian
clean slate
258
      goto exit;
259
    }
260
  }
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
261
  else if (error_erno)
262
    error= true;
263
1039.5.31 by Jay Pipes
This patch does a few things:
264
  replication_services.rawStatement(session, session->query, session->query_length);
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
265
  session->my_ok(result);
1 by brian
clean slate
266
267
exit:
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
268
  pthread_mutex_unlock(&LOCK_create_db);
520.1.22 by Brian Aker
Second pass of thd cleanup
269
  start_waiting_global_read_lock(session);
1 by brian
clean slate
270
exit2:
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
271
  return error;
1 by brian
clean slate
272
}
273
274
275
/* db-name is already validated when we come here */
276
520.1.22 by Brian Aker
Second pass of thd cleanup
277
bool mysql_alter_db(Session *session, const char *db, HA_CREATE_INFO *create_info)
1 by brian
clean slate
278
{
1130.1.2 by Monty Taylor
Re-org'd the replication stuff into slots.
279
  ReplicationServices &replication_services= ReplicationServices::singleton();
1 by brian
clean slate
280
  long result=1;
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
281
  int error= 0;
282
  char	 path[FN_REFLEN+16];
283
  uint32_t path_len;
1 by brian
clean slate
284
285
  /*
286
    Do not alter database if another thread is holding read lock.
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
287
    Wait for global read lock before acquiring LOCK_create_db.
1 by brian
clean slate
288
    After wait_if_global_read_lock() we have protection against another
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
289
    global read lock. If we would acquire LOCK_create_db first,
1 by brian
clean slate
290
    another thread could step in and get the global read lock before we
291
    reach wait_if_global_read_lock(). If this thread tries the same as we
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
292
    (admin a db), it would then go and wait on LOCK_create_db...
1 by brian
clean slate
293
    Furthermore wait_if_global_read_lock() checks if the current thread
294
    has the global read lock and refuses the operation with
295
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
296
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
297
  if ((error=wait_if_global_read_lock(session,0,1)))
811.1.2 by Stewart Smith
remove existing db.opt implementation.
298
    goto exit;
1 by brian
clean slate
299
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
300
  pthread_mutex_lock(&LOCK_create_db);
1 by brian
clean slate
301
302
  /* Change options if current database is being altered. */
1039.1.6 by Brian Aker
Refactor for build_table_filename()
303
  path_len= build_table_filename(path, sizeof(path), db, "", false);
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
304
  path[path_len-1]= 0;                    // Remove last '/' from path
305
306
  error= write_schema_file(session, path, db, create_info);
307
  if (error && error != EEXIST)
308
  {
309
    /* TODO: find some witty way of getting back an error message */
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
310
    pthread_mutex_unlock(&LOCK_create_db);
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
311
    goto exit;
312
  }
1 by brian
clean slate
313
1039.5.31 by Jay Pipes
This patch does a few things:
314
  replication_services.rawStatement(session, session->getQueryString(), session->getQueryLength());
836 by Brian Aker
Fixed session call from function to method.
315
  session->my_ok(result);
1 by brian
clean slate
316
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
317
  pthread_mutex_unlock(&LOCK_create_db);
520.1.22 by Brian Aker
Second pass of thd cleanup
318
  start_waiting_global_read_lock(session);
811.1.2 by Stewart Smith
remove existing db.opt implementation.
319
exit:
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
320
  return error ? true : false;
1 by brian
clean slate
321
}
322
323
324
/*
325
  Drop all tables in a database and the database itself
326
327
  SYNOPSIS
328
    mysql_rm_db()
520.1.22 by Brian Aker
Second pass of thd cleanup
329
    session			Thread handle
1 by brian
clean slate
330
    db			Database name in the case given by user
331
		        It's already validated and set to lower case
332
                        (if needed) when we come here
333
    if_exists		Don't give error if database doesn't exists
334
    silent		Don't generate errors
335
336
  RETURN
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
337
    false ok (Database dropped)
1 by brian
clean slate
338
    ERROR Error
339
*/
340
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
341
bool mysql_rm_db(Session *session,char *db,bool if_exists)
1 by brian
clean slate
342
{
343
  long deleted=0;
253 by Brian Aker
Removed final my_bool from sql_db.
344
  int error= false;
1 by brian
clean slate
345
  char	path[FN_REFLEN+16];
346
  MY_DIR *dirp;
482 by Brian Aker
Remove uint.
347
  uint32_t length;
1034.1.5 by Brian Aker
Small refactor of db.cc
348
  TableList *dropped_tables= NULL;
1 by brian
clean slate
349
350
  if (db && (strcmp(db, "information_schema") == 0))
351
  {
575.4.7 by Monty Taylor
More header cleanup.
352
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
1014.3.3 by Brian Aker
Formating fix.
353
    return true;
1 by brian
clean slate
354
  }
355
356
  /*
357
    Do not drop database if another thread is holding read lock.
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
358
    Wait for global read lock before acquiring LOCK_create_db.
1 by brian
clean slate
359
    After wait_if_global_read_lock() we have protection against another
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
360
    global read lock. If we would acquire LOCK_create_db first,
1 by brian
clean slate
361
    another thread could step in and get the global read lock before we
362
    reach wait_if_global_read_lock(). If this thread tries the same as we
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
363
    (admin a db), it would then go and wait on LOCK_create_db...
1 by brian
clean slate
364
    Furthermore wait_if_global_read_lock() checks if the current thread
365
    has the global read lock and refuses the operation with
366
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
367
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
368
  if (wait_if_global_read_lock(session, 0, 1))
1 by brian
clean slate
369
  {
370
    error= -1;
371
    goto exit2;
372
  }
373
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
374
  pthread_mutex_lock(&LOCK_create_db);
1 by brian
clean slate
375
1039.1.6 by Brian Aker
Refactor for build_table_filename()
376
  length= build_table_filename(path, sizeof(path), db, "", false);
822 by Brian Aker
Merge from Stewart.
377
  strcpy(path+length, MY_DB_OPT_FILE);         // Append db option file name
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
378
  unlink(path);
1 by brian
clean slate
379
  path[length]= '\0';				// Remove file name
380
381
  /* See if the directory exists */
382
  if (!(dirp= my_dir(path,MYF(MY_DONT_SORT))))
383
  {
384
    if (!if_exists)
385
    {
386
      error= -1;
387
      my_error(ER_DB_DROP_EXISTS, MYF(0), db);
388
      goto exit;
389
    }
390
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
391
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
392
			  ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS), db);
393
  }
394
  else
395
  {
1046.1.2 by Brian Aker
Comments on LOCK_open
396
    pthread_mutex_lock(&LOCK_open); /* After deleting database, remove all cache entries related to schema */
1 by brian
clean slate
397
    remove_db_from_cache(db);
398
    pthread_mutex_unlock(&LOCK_open);
399
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
400
1 by brian
clean slate
401
    error= -1;
1034.1.5 by Brian Aker
Small refactor of db.cc
402
    if ((deleted= mysql_rm_known_files(session, dirp, db, path, &dropped_tables)) >= 0)
1 by brian
clean slate
403
    {
1152.1.4 by Brian Aker
Remove wrappers from SE
404
      plugin::StorageEngine::dropDatabase(path);
1 by brian
clean slate
405
      error = 0;
406
    }
407
  }
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
408
  if (deleted >= 0)
1 by brian
clean slate
409
  {
410
    const char *query;
308 by Brian Aker
ulong conversion
411
    uint32_t query_length;
520.1.22 by Brian Aker
Second pass of thd cleanup
412
    if (!session->query)
1 by brian
clean slate
413
    {
414
      /* The client used the old obsolete mysql_drop_db() call */
415
      query= path;
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
416
      query_length= sprintf(path, "drop database `%s`", db);
1 by brian
clean slate
417
    }
418
    else
419
    {
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
420
      query= session->query;
520.1.22 by Brian Aker
Second pass of thd cleanup
421
      query_length= session->query_length;
1 by brian
clean slate
422
    }
1130.1.2 by Monty Taylor
Re-org'd the replication stuff into slots.
423
    ReplicationServices &replication_services= ReplicationServices::singleton();
1039.5.31 by Jay Pipes
This patch does a few things:
424
    replication_services.rawStatement(session, session->getQueryString(), session->getQueryLength());
520.1.22 by Brian Aker
Second pass of thd cleanup
425
    session->clear_error();
426
    session->server_status|= SERVER_STATUS_DB_DROPPED;
836 by Brian Aker
Fixed session call from function to method.
427
    session->my_ok((uint32_t) deleted);
520.1.22 by Brian Aker
Second pass of thd cleanup
428
    session->server_status&= ~SERVER_STATUS_DB_DROPPED;
1 by brian
clean slate
429
  }
798.2.19 by Brian Aker
Converted db.cc away from using binlog
430
  else
1 by brian
clean slate
431
  {
432
    char *query, *query_pos, *query_end, *query_data_start;
327.2.4 by Brian Aker
Refactoring table.h
433
    TableList *tbl;
482 by Brian Aker
Remove uint.
434
    uint32_t db_len;
1 by brian
clean slate
435
520.1.22 by Brian Aker
Second pass of thd cleanup
436
    if (!(query= (char*) session->alloc(MAX_DROP_TABLE_Q_LEN)))
1 by brian
clean slate
437
      goto exit; /* not much else we can do */
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
438
    query_pos= query_data_start= strcpy(query,"drop table ")+11;
1 by brian
clean slate
439
    query_end= query + MAX_DROP_TABLE_Q_LEN;
440
    db_len= strlen(db);
441
1130.1.2 by Monty Taylor
Re-org'd the replication stuff into slots.
442
    ReplicationServices &replication_services= ReplicationServices::singleton();
1 by brian
clean slate
443
    for (tbl= dropped_tables; tbl; tbl= tbl->next_local)
444
    {
482 by Brian Aker
Remove uint.
445
      uint32_t tbl_name_len;
1 by brian
clean slate
446
447
      /* 3 for the quotes and the comma*/
448
      tbl_name_len= strlen(tbl->table_name) + 3;
449
      if (query_pos + tbl_name_len + 1 >= query_end)
450
      {
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
451
        /* These DDL methods and logging protected with LOCK_create_db */
1039.5.31 by Jay Pipes
This patch does a few things:
452
        replication_services.rawStatement(session, query, (size_t) (query_pos -1 - query));
1 by brian
clean slate
453
        query_pos= query_data_start;
454
      }
455
456
      *query_pos++ = '`';
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
457
      query_pos= strcpy(query_pos,tbl->table_name) + (tbl_name_len-3);
1 by brian
clean slate
458
      *query_pos++ = '`';
459
      *query_pos++ = ',';
460
    }
461
462
    if (query_pos != query_data_start)
463
    {
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
464
      /* These DDL methods and logging protected with LOCK_create_db */
1039.5.31 by Jay Pipes
This patch does a few things:
465
      replication_services.rawStatement(session, query, (size_t) (query_pos -1 - query));
1 by brian
clean slate
466
    }
467
  }
468
469
exit:
470
  /*
471
    If this database was the client's selected database, we silently
472
    change the client's selected database to nothing (to have an empty
520.1.22 by Brian Aker
Second pass of thd cleanup
473
    SELECT DATABASE() in the future). For this we free() session->db and set
1 by brian
clean slate
474
    it to 0.
475
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
476
  if (session->db && !strcmp(session->db, db))
1014.3.2 by Brian Aker
Factor out need for session in many "schema" calls. Removed variable about
477
    mysql_change_db_impl(session, NULL);
971.3.2 by Eric Day
Renamed LOCK_drizzleclient_create_db to LOCK_create_db since this has nothing to do with drizzleclient. I think Monty accidently got this during the drizzleclient rename.
478
  pthread_mutex_unlock(&LOCK_create_db);
520.1.22 by Brian Aker
Second pass of thd cleanup
479
  start_waiting_global_read_lock(session);
1 by brian
clean slate
480
exit2:
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
481
  return(error);
1 by brian
clean slate
482
}
483
484
/*
255 by Brian Aker
Removed RAID table delete point.
485
  Removes files with known extensions plus.
520.1.22 by Brian Aker
Second pass of thd cleanup
486
  session MUST be set when calling this function!
1 by brian
clean slate
487
*/
488
520.1.22 by Brian Aker
Second pass of thd cleanup
489
static long mysql_rm_known_files(Session *session, MY_DIR *dirp, const char *db,
1034.1.5 by Brian Aker
Small refactor of db.cc
490
				 const char *org_path,
327.2.4 by Brian Aker
Refactoring table.h
491
                                 TableList **dropped_tables)
1 by brian
clean slate
492
{
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
493
  long deleted= 0;
1 by brian
clean slate
494
  char filePath[FN_REFLEN];
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
495
  TableList *tot_list= NULL, **tot_list_next;
1 by brian
clean slate
496
497
  tot_list_next= &tot_list;
498
1034.1.5 by Brian Aker
Small refactor of db.cc
499
  for (uint32_t idx= 0;
895 by Brian Aker
Completion (?) of uint conversion.
500
       idx < (uint32_t) dirp->number_off_files && !session->killed ;
1 by brian
clean slate
501
       idx++)
502
  {
503
    FILEINFO *file=dirp->dir_entry+idx;
504
    char *extension;
505
506
    /* skiping . and .. */
507
    if (file->name[0] == '.' && (!file->name[1] ||
508
       (file->name[1] == '.' &&  !file->name[2])))
509
      continue;
510
511
    if (!(extension= strrchr(file->name, '.')))
376 by Brian Aker
strend remove
512
      extension= strchr(file->name, '\0');
1 by brian
clean slate
513
    if (find_type(extension, &deletable_extentions,1+2) <= 0)
514
    {
584.2.7 by Stewart Smith
rename and delete tabledefinition protobuf file
515
      /*
516
        ass ass ass.
517
518
        strange checking for magic extensions that are then deleted if
519
        not reg_ext (i.e. .frm).
520
521
        and (previously) we'd err out on drop database if files not matching
522
        engine ha_known_exts() or deletable_extensions were present.
523
524
        presumably this was to avoid deleting other user data... except if that
525
        data happened to be in files ending in .BAK, .opt or .TMD. *fun*
526
       */
1 by brian
clean slate
527
      continue;
528
    }
529
    /* just for safety we use files_charset_info */
530
    if (db && !my_strcasecmp(files_charset_info,
896.4.9 by Stewart Smith
No longer write the FRM. just use proto.
531
                             extension, ".dfe"))
1 by brian
clean slate
532
    {
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
533
      uint32_t db_len= strlen(db);
534
1 by brian
clean slate
535
      /* Drop the table nicely */
1039.1.14 by Brian Aker
Fix for gcc 4.1
536
      *extension= 0;			// Remove extension
327.2.4 by Brian Aker
Refactoring table.h
537
      TableList *table_list=(TableList*)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
538
                              session->calloc(sizeof(*table_list) +
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
539
                                          db_len + 1 +
1 by brian
clean slate
540
                                          strlen(file->name) + 1);
541
542
      if (!table_list)
543
        goto err;
544
      table_list->db= (char*) (table_list+1);
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
545
      table_list->table_name= strcpy(table_list->db, db) + db_len + 1;
398.1.10 by Monty Taylor
Actually removed VOID() this time.
546
      filename_to_tablename(file->name, table_list->table_name,
547
                            strlen(file->name) + 1);
1 by brian
clean slate
548
      table_list->alias= table_list->table_name;	// If lower_case_table_names=2
584.2.1 by Stewart Smith
convert tmp_file_prefix to TMP_FILE_PREFIX to indicate it's a macro
549
      table_list->internal_tmp_table= is_prefix(file->name, TMP_FILE_PREFIX);
1 by brian
clean slate
550
      /* Link into list */
551
      (*tot_list_next)= table_list;
552
      tot_list_next= &table_list->next_local;
553
      deleted++;
554
    }
555
    else
556
    {
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
557
      sprintf(filePath, "%s/%s", org_path, file->name);
1 by brian
clean slate
558
      if (my_delete_with_symlink(filePath,MYF(MY_WME)))
559
      {
560
	goto err;
561
      }
562
    }
563
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
564
  if (session->killed ||
590.1.7 by Stewart Smith
remove mysql_frm_type
565
      (tot_list && mysql_rm_table_part2(session, tot_list, 1, 0, 1)))
1 by brian
clean slate
566
    goto err;
567
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
568
  my_dirend(dirp);
569
1 by brian
clean slate
570
  if (dropped_tables)
571
    *dropped_tables= tot_list;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
572
1060.1.1 by Eric Lambert
-removed rm_dir_w_symlink method call with rmdir since dir should not be a sym link in the first place.
573
  if (rmdir(org_path))
574
  {
575
    my_error(ER_DB_DROP_RMDIR, MYF(0), org_path, errno);
1034.1.5 by Brian Aker
Small refactor of db.cc
576
    return -1;
1060.1.1 by Eric Lambert
-removed rm_dir_w_symlink method call with rmdir since dir should not be a sym link in the first place.
577
  }
1 by brian
clean slate
578
1039.1.3 by Brian Aker
Remove dead bits from db create/rm
579
  return deleted;
1 by brian
clean slate
580
581
err:
582
  my_dirend(dirp);
1034.1.5 by Brian Aker
Small refactor of db.cc
583
  return -1;
1 by brian
clean slate
584
}
585
586
/**
587
  @brief Internal implementation: switch current database to a valid one.
588
520.1.22 by Brian Aker
Second pass of thd cleanup
589
  @param session            Thread context.
1 by brian
clean slate
590
  @param new_db_name    Name of the database to switch to. The function will
591
                        take ownership of the name (the caller must not free
592
                        the allocated memory). If the name is NULL, we're
593
                        going to switch to NULL db.
594
  @param new_db_charset Character set of the new database.
595
*/
596
1014.3.2 by Brian Aker
Factor out need for session in many "schema" calls. Removed variable about
597
static void mysql_change_db_impl(Session *session, LEX_STRING *new_db_name)
1 by brian
clean slate
598
{
520.1.21 by Brian Aker
THD -> Session rename
599
  /* 1. Change current database in Session. */
1 by brian
clean slate
600
601
  if (new_db_name == NULL)
602
  {
603
    /*
520.1.21 by Brian Aker
THD -> Session rename
604
      Session::set_db() does all the job -- it frees previous database name and
1 by brian
clean slate
605
      sets the new one.
606
    */
607
520.1.22 by Brian Aker
Second pass of thd cleanup
608
    session->set_db(NULL, 0);
1 by brian
clean slate
609
  }
575.4.7 by Monty Taylor
More header cleanup.
610
  else if (my_strcasecmp(system_charset_info, new_db_name->str,
611
                         INFORMATION_SCHEMA_NAME.c_str()) == 0)
1 by brian
clean slate
612
  {
613
    /*
520.1.21 by Brian Aker
THD -> Session rename
614
      Here we must use Session::set_db(), because we want to copy
1 by brian
clean slate
615
      INFORMATION_SCHEMA_NAME constant.
616
    */
617
575.4.7 by Monty Taylor
More header cleanup.
618
    session->set_db(INFORMATION_SCHEMA_NAME.c_str(),
619
                    INFORMATION_SCHEMA_NAME.length());
1 by brian
clean slate
620
  }
621
  else
622
  {
623
    /*
520.1.21 by Brian Aker
THD -> Session rename
624
      Here we already have a copy of database name to be used in Session. So,
625
      we just call Session::reset_db(). Since Session::reset_db() does not releases
1 by brian
clean slate
626
      the previous database name, we should do it explicitly.
627
    */
628
520.1.22 by Brian Aker
Second pass of thd cleanup
629
    if (session->db)
630
      free(session->db);
1 by brian
clean slate
631
520.1.22 by Brian Aker
Second pass of thd cleanup
632
    session->reset_db(new_db_name->str, new_db_name->length);
1 by brian
clean slate
633
  }
634
}
635
636
/**
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
637
  Return true if db1_name is equal to db2_name, false otherwise.
1 by brian
clean slate
638
639
  The function allows to compare database names according to the MySQL
640
  rules. The database names db1 and db2 are equal if:
641
     - db1 is NULL and db2 is NULL;
642
     or
643
     - db1 is not-NULL, db2 is not-NULL, db1 is equal (ignoring case) to
644
       db2 in system character set (UTF8).
645
*/
646
647
static inline bool
648
cmp_db_names(const char *db1_name,
649
             const char *db2_name)
650
{
651
  return
652
         /* db1 is NULL and db2 is NULL */
653
         (!db1_name && !db2_name) ||
654
655
         /* db1 is not-NULL, db2 is not-NULL, db1 == db2. */
656
         (db1_name && db2_name && my_strcasecmp(system_charset_info, db1_name, db2_name) == 0);
657
}
658
659
660
/**
661
  @brief Change the current database and its attributes unconditionally.
662
520.1.22 by Brian Aker
Second pass of thd cleanup
663
  @param session          thread handle
1 by brian
clean slate
664
  @param new_db_name  database name
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
665
  @param force_switch if force_switch is false, then the operation will fail if
1 by brian
clean slate
666
667
                        - new_db_name is NULL or empty;
668
669
                        - OR new database name is invalid
670
                          (check_db_name() failed);
671
672
                        - OR user has no privilege on the new database;
673
674
                        - OR new database does not exist;
675
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
676
                      if force_switch is true, then
1 by brian
clean slate
677
678
                        - if new_db_name is NULL or empty, the current
679
                          database will be NULL, @@collation_database will
680
                          be set to @@collation_server, the operation will
681
                          succeed.
682
683
                        - if new database name is invalid
684
                          (check_db_name() failed), the current database
685
                          will be NULL, @@collation_database will be set to
686
                          @@collation_server, but the operation will fail;
687
688
                        - user privileges will not be checked
520.1.21 by Brian Aker
THD -> Session rename
689
                          (Session::db_access however is updated);
1 by brian
clean slate
690
691
                          TODO: is this really the intention?
692
                                (see sp-security.test).
693
694
                        - if new database does not exist,the current database
695
                          will be NULL, @@collation_database will be set to
696
                          @@collation_server, a warning will be thrown, the
697
                          operation will succeed.
698
699
  @details The function checks that the database name corresponds to a
700
  valid and existent database, checks access rights and changes the current
701
  database with database attributes (@@collation_database session variable,
520.1.21 by Brian Aker
THD -> Session rename
702
  Session::db_access).
1 by brian
clean slate
703
704
  This function is not the only way to switch the database that is
705
  currently employed. When the replication slave thread switches the
520.1.22 by Brian Aker
Second pass of thd cleanup
706
  database before executing a query, it calls session->set_db directly.
1 by brian
clean slate
707
  However, if the query, in turn, uses a stored routine, the stored routine
708
  will use this function, even if it's run on the slave.
709
710
  This function allocates the name of the database on the system heap: this
711
  is necessary to be able to uniformly change the database from any module
712
  of the server. Up to 5.0 different modules were using different memory to
713
  store the name of the database, and this led to memory corruption:
714
  a stack pointer set by Stored Procedures was used by replication after
715
  the stack address was long gone.
716
717
  @return Operation status
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
718
    @retval false Success
719
    @retval true  Error
1 by brian
clean slate
720
*/
721
520.1.22 by Brian Aker
Second pass of thd cleanup
722
bool mysql_change_db(Session *session, const LEX_STRING *new_db_name, bool force_switch)
1 by brian
clean slate
723
{
724
  LEX_STRING new_db_file_name;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
725
  const CHARSET_INFO *db_default_cl;
1 by brian
clean slate
726
1039.3.14 by Stewart Smith
remove old code for changing databases only called from stored procedures
727
  assert(new_db_name);
728
  assert(new_db_name->length);
1 by brian
clean slate
729
730
  if (my_strcasecmp(system_charset_info, new_db_name->str,
575.4.7 by Monty Taylor
More header cleanup.
731
                    INFORMATION_SCHEMA_NAME.c_str()) == 0)
1 by brian
clean slate
732
  {
733
    /* Switch the current database to INFORMATION_SCHEMA. */
575.4.7 by Monty Taylor
More header cleanup.
734
    /* const_cast<> is safe here: mysql_change_db_impl does a copy */
735
    LEX_STRING is_name= { const_cast<char *>(INFORMATION_SCHEMA_NAME.c_str()),
736
                          INFORMATION_SCHEMA_NAME.length() };
1014.3.2 by Brian Aker
Factor out need for session in many "schema" calls. Removed variable about
737
    mysql_change_db_impl(session, &is_name);
1 by brian
clean slate
738
1014.3.3 by Brian Aker
Formating fix.
739
    return false;
1 by brian
clean slate
740
  }
741
742
  /*
743
    Now we need to make a copy because check_db_name requires a
744
    non-constant argument. Actually, it takes database file name.
745
746
    TODO: fix check_db_name().
747
  */
748
656.1.34 by Monty Taylor
Got closer...
749
  new_db_file_name.length= new_db_name->length;
750
  new_db_file_name.str= (char *)malloc(new_db_name->length + 1);
656.1.44 by Monty Taylor
Added some return checking.
751
  if (new_db_file_name.str == NULL)
1014.3.3 by Brian Aker
Formating fix.
752
    return true;                             /* the error is set */
656.1.34 by Monty Taylor
Got closer...
753
  memcpy(new_db_file_name.str, new_db_name->str, new_db_name->length);
754
  new_db_file_name.str[new_db_name->length]= 0;
755
1 by brian
clean slate
756
757
  /*
758
    NOTE: if check_db_name() fails, we should throw an error in any case,
759
    even if we are called from sp_head::execute().
760
761
    It's next to impossible however to get this error when we are called
762
    from sp_head::execute(). But let's switch the current database to NULL
763
    in this case to be sure.
764
  */
765
766
  if (check_db_name(&new_db_file_name))
767
  {
768
    my_error(ER_WRONG_DB_NAME, MYF(0), new_db_file_name.str);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
769
    free(new_db_file_name.str);
1 by brian
clean slate
770
771
    if (force_switch)
1014.3.2 by Brian Aker
Factor out need for session in many "schema" calls. Removed variable about
772
      mysql_change_db_impl(session, NULL);
1 by brian
clean slate
773
1014.3.3 by Brian Aker
Formating fix.
774
    return true;
1 by brian
clean slate
775
  }
776
777
  if (check_db_dir_existence(new_db_file_name.str))
778
  {
779
    if (force_switch)
780
    {
781
      /* Throw a warning and free new_db_file_name. */
782
520.1.22 by Brian Aker
Second pass of thd cleanup
783
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
784
                          ER_BAD_DB_ERROR, ER(ER_BAD_DB_ERROR),
785
                          new_db_file_name.str);
786
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
787
      free(new_db_file_name.str);
1 by brian
clean slate
788
789
      /* Change db to NULL. */
790
1014.3.2 by Brian Aker
Factor out need for session in many "schema" calls. Removed variable about
791
      mysql_change_db_impl(session, NULL);
1 by brian
clean slate
792
793
      /* The operation succeed. */
794
1014.3.3 by Brian Aker
Formating fix.
795
      return false;
1 by brian
clean slate
796
    }
797
    else
798
    {
799
      /* Report an error and free new_db_file_name. */
800
801
      my_error(ER_BAD_DB_ERROR, MYF(0), new_db_file_name.str);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
802
      free(new_db_file_name.str);
1 by brian
clean slate
803
804
      /* The operation failed. */
805
1014.3.3 by Brian Aker
Formating fix.
806
      return true;
1 by brian
clean slate
807
    }
808
  }
809
810
  /*
520.1.21 by Brian Aker
THD -> Session rename
811
    NOTE: in mysql_change_db_impl() new_db_file_name is assigned to Session
812
    attributes and will be freed in Session::~Session().
1 by brian
clean slate
813
  */
814
1014.3.1 by Brian Aker
Simplify the calling stack for getting schema collation. We need to extend
815
  db_default_cl= get_default_db_collation(new_db_file_name.str);
1 by brian
clean slate
816
1014.3.2 by Brian Aker
Factor out need for session in many "schema" calls. Removed variable about
817
  mysql_change_db_impl(session, &new_db_file_name);
1 by brian
clean slate
818
1014.3.3 by Brian Aker
Formating fix.
819
  return false;
1 by brian
clean slate
820
}
821
822
/*
823
  Check if there is directory for the database name.
824
825
  SYNOPSIS
826
    check_db_dir_existence()
827
    db_name   database name
828
829
  RETURN VALUES
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
830
    false   There is directory for the specified database name.
831
    true    The directory does not exist.
1 by brian
clean slate
832
*/
833
834
bool check_db_dir_existence(const char *db_name)
835
{
836
  char db_dir_path[FN_REFLEN];
482 by Brian Aker
Remove uint.
837
  uint32_t db_dir_path_len;
1 by brian
clean slate
838
839
  db_dir_path_len= build_table_filename(db_dir_path, sizeof(db_dir_path),
1039.1.6 by Brian Aker
Refactor for build_table_filename()
840
                                        db_name, "", false);
1 by brian
clean slate
841
842
  if (db_dir_path_len && db_dir_path[db_dir_path_len - 1] == FN_LIBCHAR)
843
    db_dir_path[db_dir_path_len - 1]= 0;
844
845
  /* Check access. */
846
847
  return my_access(db_dir_path, F_OK);
848
}