~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>
316 by Brian Aker
First pass of new sql_db.cc work
23
#include <drizzled/serialize/serialize.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>
1 by brian
clean slate
28
#include "log.h"
549 by Monty Taylor
Took gettext.h out of header files.
29
#include <drizzled/error.h>
538 by Monty Taylor
Moved gettext.h into drizzled in anticipation of the new client lib.
30
#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.
31
#include <mysys/hash.h>
32
#include <drizzled/session.h>
33
#include <drizzled/db.h>
34
#include <drizzled/sql_base.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
35
#include <drizzled/lock.h>
316 by Brian Aker
First pass of new sql_db.cc work
36
1 by brian
clean slate
37
#define MAX_DROP_TABLE_Q_LEN      1024
38
461 by Monty Taylor
Removed NullS. bu-bye.
39
const char *del_exts[]= {".frm", ".BAK", ".TMD",".opt", NULL};
1 by brian
clean slate
40
static TYPELIB deletable_extentions=
41
{array_elements(del_exts)-1,"del_exts", del_exts, NULL};
42
520.1.22 by Brian Aker
Second pass of thd cleanup
43
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.
44
                                 const char *db, const char *path,
45
                                 uint32_t level,
327.2.4 by Brian Aker
Refactoring table.h
46
                                 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.
47
253 by Brian Aker
Removed final my_bool from sql_db.
48
static bool rm_dir_w_symlink(const char *org_path, bool send_error);
520.1.22 by Brian Aker
Second pass of thd cleanup
49
static void mysql_change_db_impl(Session *session,
1 by brian
clean slate
50
                                 LEX_STRING *new_db_name,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
51
                                 const CHARSET_INFO * const new_db_charset);
1 by brian
clean slate
52
53
54
/* Database lock hash */
55
HASH lock_db_cache;
56
pthread_mutex_t LOCK_lock_db;
57
int creating_database= 0;  // how many database locks are made
58
59
60
/* Structure for database lock */
61
typedef struct my_dblock_st
62
{
63
  char *name;        /* Database name        */
482 by Brian Aker
Remove uint.
64
  uint32_t name_length;  /* Database length name */
1 by brian
clean slate
65
} my_dblock_t;
66
67
68
/*
69
  lock_db key.
70
*/
71
481 by Brian Aker
Remove all of uchar.
72
extern "C" unsigned char* lock_db_get_key(my_dblock_t *, size_t *, bool not_used);
1 by brian
clean slate
73
481 by Brian Aker
Remove all of uchar.
74
unsigned char* lock_db_get_key(my_dblock_t *ptr, size_t *length,
253 by Brian Aker
Removed final my_bool from sql_db.
75
                       bool not_used __attribute__((unused)))
1 by brian
clean slate
76
{
77
  *length= ptr->name_length;
481 by Brian Aker
Remove all of uchar.
78
  return (unsigned char*) ptr->name;
1 by brian
clean slate
79
}
80
81
82
/*
83
  Free lock_db hash element.
84
*/
85
86
extern "C" void lock_db_free_element(void *ptr);
87
88
void lock_db_free_element(void *ptr)
89
{
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.
90
  free(ptr);
1 by brian
clean slate
91
}
92
93
94
/*
95
  Delete a database lock entry from hash.
96
*/
97
482 by Brian Aker
Remove uint.
98
void lock_db_delete(const char *name, uint32_t length)
1 by brian
clean slate
99
{
100
  my_dblock_t *opt;
101
  safe_mutex_assert_owner(&LOCK_lock_db);
102
  if ((opt= (my_dblock_t *)hash_search(&lock_db_cache,
481 by Brian Aker
Remove all of uchar.
103
                                       (const unsigned char*) name, length)))
104
    hash_delete(&lock_db_cache, (unsigned char*) opt);
1 by brian
clean slate
105
}
106
107
108
/* Database options hash */
109
static HASH dboptions;
253 by Brian Aker
Removed final my_bool from sql_db.
110
static bool dboptions_init= 0;
658 by Brian Aker
Part removal of my_pthread.h
111
static pthread_rwlock_t LOCK_dboptions;
1 by brian
clean slate
112
113
/* Structure for database options */
114
typedef struct my_dbopt_st
115
{
116
  char *name;			/* Database name                  */
482 by Brian Aker
Remove uint.
117
  uint32_t name_length;		/* Database length name           */
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
118
  const CHARSET_INFO *charset;	/* Database default character set */
1 by brian
clean slate
119
} my_dbopt_t;
120
121
122
/*
123
  Function we use in the creation of our hash to get key.
124
*/
125
481 by Brian Aker
Remove all of uchar.
126
extern "C" unsigned char* dboptions_get_key(my_dbopt_t *opt, size_t *length,
253 by Brian Aker
Removed final my_bool from sql_db.
127
                                    bool not_used);
1 by brian
clean slate
128
481 by Brian Aker
Remove all of uchar.
129
unsigned char* dboptions_get_key(my_dbopt_t *opt, size_t *length,
253 by Brian Aker
Removed final my_bool from sql_db.
130
                         bool not_used __attribute__((unused)))
1 by brian
clean slate
131
{
132
  *length= opt->name_length;
481 by Brian Aker
Remove all of uchar.
133
  return (unsigned char*) opt->name;
1 by brian
clean slate
134
}
135
136
137
/*
138
  Helper function to write a query to binlog used by mysql_rm_db()
139
*/
140
520.1.22 by Brian Aker
Second pass of thd cleanup
141
static inline void write_to_binlog(Session *session, char *query, uint32_t q_len,
482 by Brian Aker
Remove uint.
142
                                   char *db, uint32_t db_len)
1 by brian
clean slate
143
{
520.1.22 by Brian Aker
Second pass of thd cleanup
144
  Query_log_event qinfo(session, query, q_len, 0, 0);
1 by brian
clean slate
145
  qinfo.error_code= 0;
146
  qinfo.db= db;
147
  qinfo.db_len= db_len;
586.1.1 by Yoshinori Sano
Rename mysql to drizzle, specifically mysql_bin_log to drizzle_bin_log.
148
  drizzle_bin_log.write(&qinfo);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
149
}
1 by brian
clean slate
150
151
152
/*
153
  Function to free dboptions hash element
154
*/
155
156
extern "C" void free_dbopt(void *dbopt);
157
158
void free_dbopt(void *dbopt)
159
{
481 by Brian Aker
Remove all of uchar.
160
  free((unsigned char*) dbopt);
1 by brian
clean slate
161
}
162
163
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
164
/*
1 by brian
clean slate
165
  Initialize database option hash and locked database hash.
166
167
  SYNOPSIS
168
    my_database_names()
169
170
  NOTES
171
    Must be called before any other database function is called.
172
173
  RETURN
174
    0	ok
175
    1	Fatal error
176
*/
177
178
bool my_database_names_init(void)
179
{
253 by Brian Aker
Removed final my_bool from sql_db.
180
  bool error= false;
658 by Brian Aker
Part removal of my_pthread.h
181
  (void) pthread_rwlock_init(&LOCK_dboptions, NULL);
1 by brian
clean slate
182
  if (!dboptions_init)
183
  {
184
    dboptions_init= 1;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
185
    error= hash_init(&dboptions, lower_case_table_names ?
1 by brian
clean slate
186
                     &my_charset_bin : system_charset_info,
187
                     32, 0, 0, (hash_get_key) dboptions_get_key,
188
                     free_dbopt,0) ||
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
189
           hash_init(&lock_db_cache, lower_case_table_names ?
1 by brian
clean slate
190
                     &my_charset_bin : system_charset_info,
191
                     32, 0, 0, (hash_get_key) lock_db_get_key,
192
                     lock_db_free_element,0);
193
194
  }
195
  return error;
196
}
197
198
199
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
200
/*
1 by brian
clean slate
201
  Free database option hash and locked databases hash.
202
*/
203
204
void my_database_names_free(void)
205
{
206
  if (dboptions_init)
207
  {
208
    dboptions_init= 0;
209
    hash_free(&dboptions);
658 by Brian Aker
Part removal of my_pthread.h
210
    (void) pthread_rwlock_destroy(&LOCK_dboptions);
1 by brian
clean slate
211
    hash_free(&lock_db_cache);
212
  }
213
}
214
215
216
/*
217
  Cleanup cached options
218
*/
219
220
void my_dbopt_cleanup(void)
221
{
658 by Brian Aker
Part removal of my_pthread.h
222
  pthread_rwlock_wrlock(&LOCK_dboptions);
1 by brian
clean slate
223
  hash_free(&dboptions);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
224
  hash_init(&dboptions, lower_case_table_names ?
1 by brian
clean slate
225
            &my_charset_bin : system_charset_info,
226
            32, 0, 0, (hash_get_key) dboptions_get_key,
227
            free_dbopt,0);
658 by Brian Aker
Part removal of my_pthread.h
228
  pthread_rwlock_unlock(&LOCK_dboptions);
1 by brian
clean slate
229
}
230
231
232
/*
233
  Find database options in the hash.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
234
1 by brian
clean slate
235
  DESCRIPTION
236
    Search a database options in the hash, usings its path.
237
    Fills "create" on success.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
238
1 by brian
clean slate
239
  RETURN VALUES
240
    0 on success.
241
    1 on error.
242
*/
243
253 by Brian Aker
Removed final my_bool from sql_db.
244
static bool get_dbopt(const char *dbname, HA_CREATE_INFO *create)
1 by brian
clean slate
245
{
246
  my_dbopt_t *opt;
482 by Brian Aker
Remove uint.
247
  uint32_t length;
253 by Brian Aker
Removed final my_bool from sql_db.
248
  bool error= true;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
249
1 by brian
clean slate
250
  length= (uint) strlen(dbname);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
251
658 by Brian Aker
Part removal of my_pthread.h
252
  pthread_rwlock_rdlock(&LOCK_dboptions);
481 by Brian Aker
Remove all of uchar.
253
  if ((opt= (my_dbopt_t*) hash_search(&dboptions, (unsigned char*) dbname, length)))
1 by brian
clean slate
254
  {
255
    create->default_table_charset= opt->charset;
253 by Brian Aker
Removed final my_bool from sql_db.
256
    error= true;
1 by brian
clean slate
257
  }
658 by Brian Aker
Part removal of my_pthread.h
258
  pthread_rwlock_unlock(&LOCK_dboptions);
1 by brian
clean slate
259
  return error;
260
}
261
262
263
/*
264
  Writes database options into the hash.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
265
1 by brian
clean slate
266
  DESCRIPTION
267
    Inserts database options into the hash, or updates
268
    options if they are already in the hash.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
269
1 by brian
clean slate
270
  RETURN VALUES
271
    0 on success.
272
    1 on error.
273
*/
274
253 by Brian Aker
Removed final my_bool from sql_db.
275
static bool put_dbopt(const char *dbname, HA_CREATE_INFO *create)
1 by brian
clean slate
276
{
277
  my_dbopt_t *opt;
482 by Brian Aker
Remove uint.
278
  uint32_t length;
253 by Brian Aker
Removed final my_bool from sql_db.
279
  bool error= false;
1 by brian
clean slate
280
281
  length= (uint) strlen(dbname);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
282
658 by Brian Aker
Part removal of my_pthread.h
283
  pthread_rwlock_wrlock(&LOCK_dboptions);
481 by Brian Aker
Remove all of uchar.
284
  if (!(opt= (my_dbopt_t*) hash_search(&dboptions, (unsigned char*) dbname, length)))
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
285
  {
1 by brian
clean slate
286
    /* Options are not in the hash, insert them */
287
    char *tmp_name;
288
    if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
289
                         &opt, (uint) sizeof(*opt), &tmp_name, (uint) length+1,
461 by Monty Taylor
Removed NullS. bu-bye.
290
                         NULL))
1 by brian
clean slate
291
    {
253 by Brian Aker
Removed final my_bool from sql_db.
292
      error= true;
1 by brian
clean slate
293
      goto end;
294
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
295
1 by brian
clean slate
296
    opt->name= tmp_name;
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
297
    strcpy(opt->name, dbname);
1 by brian
clean slate
298
    opt->name_length= length;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
299
481 by Brian Aker
Remove all of uchar.
300
    if ((error= my_hash_insert(&dboptions, (unsigned char*) opt)))
1 by brian
clean slate
301
    {
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.
302
      free(opt);
1 by brian
clean slate
303
      goto end;
304
    }
305
  }
306
307
  /* Update / write options in hash */
308
  opt->charset= create->default_table_charset;
309
310
end:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
311
  pthread_rwlock_unlock(&LOCK_dboptions);
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
312
  return(error);
1 by brian
clean slate
313
}
314
315
316
/*
317
  Deletes database options from the hash.
318
*/
319
320
void del_dbopt(const char *path)
321
{
322
  my_dbopt_t *opt;
658 by Brian Aker
Part removal of my_pthread.h
323
  pthread_rwlock_wrlock(&LOCK_dboptions);
481 by Brian Aker
Remove all of uchar.
324
  if ((opt= (my_dbopt_t *)hash_search(&dboptions, (const unsigned char*) path,
1 by brian
clean slate
325
                                      strlen(path))))
481 by Brian Aker
Remove all of uchar.
326
    hash_delete(&dboptions, (unsigned char*) opt);
658 by Brian Aker
Part removal of my_pthread.h
327
  pthread_rwlock_unlock(&LOCK_dboptions);
1 by brian
clean slate
328
}
329
330
331
/*
332
  Create database options file:
333
334
  DESCRIPTION
335
    Currently database default charset is only stored there.
336
337
  RETURN VALUES
338
  0	ok
339
  1	Could not create file or write to it.  Error sent through my_error()
340
*/
341
520.1.22 by Brian Aker
Second pass of thd cleanup
342
static bool write_db_opt(Session *session, const char *path, const char *name, HA_CREATE_INFO *create)
1 by brian
clean slate
343
{
253 by Brian Aker
Removed final my_bool from sql_db.
344
  bool error= true;
323 by Brian Aker
Updated proto file for table (not FRM work).
345
  drizzle::Schema db;
318 by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type.
346
347
  assert(name);
1 by brian
clean slate
348
349
  if (!create->default_table_charset)
520.1.22 by Brian Aker
Second pass of thd cleanup
350
    create->default_table_charset= session->variables.collation_server;
1 by brian
clean slate
351
352
  if (put_dbopt(path, create))
353
    return 1;
354
318 by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type.
355
  db.set_name(name);
356
  db.set_characterset(create->default_table_charset->csname);
357
  db.set_collation(create->default_table_charset->name);
358
359
  fstream output(path, ios::out | ios::trunc | ios::binary);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
360
  if (!db.SerializeToOstream(&output))
318 by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type.
361
    error= false;
362
1 by brian
clean slate
363
  return error;
364
}
365
366
367
/*
368
  Load database options file
369
370
  load_db_opt()
371
  path		Path for option file
372
  create	Where to store the read options
373
374
  DESCRIPTION
375
376
  RETURN VALUES
377
  0	File found
378
  1	No database file or could not open it
379
380
*/
381
520.1.22 by Brian Aker
Second pass of thd cleanup
382
bool load_db_opt(Session *session, const char *path, HA_CREATE_INFO *create)
1 by brian
clean slate
383
{
384
  bool error=1;
323 by Brian Aker
Updated proto file for table (not FRM work).
385
  drizzle::Schema db;
318 by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type.
386
  string buffer;
1 by brian
clean slate
387
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
388
  memset(create, 0, sizeof(*create));
520.1.22 by Brian Aker
Second pass of thd cleanup
389
  create->default_table_charset= session->variables.collation_server;
1 by brian
clean slate
390
391
  /* Check if options for this database are already in the hash */
392
  if (!get_dbopt(path, create))
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
393
    return(0);
1 by brian
clean slate
394
318 by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type.
395
  fstream input(path, ios::in | ios::binary);
396
  if (!input)
397
    goto err1;
398
  else if (!db.ParseFromIstream(&input))
399
    goto err1;
400
401
  buffer= db.characterset();
402
  if (!(create->default_table_charset= get_charset_by_csname(buffer.c_str(), MY_CS_PRIMARY, MYF(0))))
403
  {
338 by Monty Taylor
Tagged more strings.
404
    sql_print_error(_("Error while loading database options: '%s':"),path);
318 by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type.
405
    sql_print_error(ER(ER_UNKNOWN_COLLATION), buffer.c_str());
406
    create->default_table_charset= default_charset_info;
407
  }
408
409
  buffer= db.collation();
410
  if (!(create->default_table_charset= get_charset_by_name(buffer.c_str(), MYF(0))))
411
  {
338 by Monty Taylor
Tagged more strings.
412
    sql_print_error(_("Error while loading database options: '%s':"),path);
318 by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type.
413
    sql_print_error(ER(ER_UNKNOWN_COLLATION), buffer.c_str());
414
    create->default_table_charset= default_charset_info;
415
  }
416
1 by brian
clean slate
417
  /*
418
    Put the loaded value into the hash.
419
    Note that another thread could've added the same
420
    entry to the hash after we called get_dbopt(),
421
    but it's not an error, as put_dbopt() takes this
422
    possibility into account.
423
  */
424
  error= put_dbopt(path, create);
425
426
err1:
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
427
  return(error);
1 by brian
clean slate
428
}
429
430
431
/*
432
  Retrieve database options by name. Load database options file or fetch from
433
  cache.
434
435
  SYNOPSIS
436
    load_db_opt_by_name()
437
    db_name         Database name
438
    db_create_info  Where to store the database options
439
440
  DESCRIPTION
441
    load_db_opt_by_name() is a shortcut for load_db_opt().
442
443
  NOTE
444
    Although load_db_opt_by_name() (and load_db_opt()) returns status of
445
    the operation, it is useless usually and should be ignored. The problem
446
    is that there are 1) system databases ("mysql") and 2) virtual
447
    databases ("information_schema"), which do not contain options file.
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
448
    So, load_db_opt[_by_name]() returns false for these databases, but this
1 by brian
clean slate
449
    is not an error.
450
451
    load_db_opt[_by_name]() clears db_create_info structure in any case, so
452
    even on failure it contains valid data. So, common use case is just
453
    call load_db_opt[_by_name]() without checking return value and use
454
    db_create_info right after that.
455
456
  RETURN VALUES (read NOTE!)
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
457
    false   Success
458
    true    Failed to retrieve options
1 by brian
clean slate
459
*/
460
520.1.22 by Brian Aker
Second pass of thd cleanup
461
bool load_db_opt_by_name(Session *session, const char *db_name,
1 by brian
clean slate
462
                         HA_CREATE_INFO *db_create_info)
463
{
464
  char db_opt_path[FN_REFLEN];
465
466
  /*
467
    Pass an empty file name, and the database options file name as extension
468
    to avoid table name to file name encoding.
469
  */
470
  (void) build_table_filename(db_opt_path, sizeof(db_opt_path),
471
                              db_name, "", MY_DB_OPT_FILE, 0);
472
520.1.22 by Brian Aker
Second pass of thd cleanup
473
  return load_db_opt(session, db_opt_path, db_create_info);
1 by brian
clean slate
474
}
475
476
477
/**
478
  Return default database collation.
479
520.1.22 by Brian Aker
Second pass of thd cleanup
480
  @param session     Thread context.
1 by brian
clean slate
481
  @param db_name Database name.
482
483
  @return CHARSET_INFO object. The operation always return valid character
484
    set, even if the database does not exist.
485
*/
486
520.1.22 by Brian Aker
Second pass of thd cleanup
487
const CHARSET_INFO *get_default_db_collation(Session *session, const char *db_name)
1 by brian
clean slate
488
{
489
  HA_CREATE_INFO db_info;
490
520.1.22 by Brian Aker
Second pass of thd cleanup
491
  if (session->db != NULL && strcmp(db_name, session->db) == 0)
492
    return session->db_charset;
1 by brian
clean slate
493
520.1.22 by Brian Aker
Second pass of thd cleanup
494
  load_db_opt_by_name(session, db_name, &db_info);
1 by brian
clean slate
495
496
  /*
497
    NOTE: even if load_db_opt_by_name() fails,
498
    db_info.default_table_charset contains valid character set
499
    (collation_server). We should not fail if load_db_opt_by_name() fails,
500
    because it is valid case. If a database has been created just by
501
    "mkdir", it does not contain db.opt file, but it is valid database.
502
  */
503
504
  return db_info.default_table_charset;
505
}
506
507
508
/*
509
  Create a database
510
511
  SYNOPSIS
512
  mysql_create_db()
520.1.22 by Brian Aker
Second pass of thd cleanup
513
  session		Thread handler
1 by brian
clean slate
514
  db		Name of database to create
515
		Function assumes that this is already validated.
516
  create_info	Database create options (like character set)
517
  silent	Used by replication when internally creating a database.
518
		In this case the entry should not be logged.
519
520
  SIDE-EFFECTS
521
   1. Report back to client that command succeeded (my_ok)
522
   2. Report errors to client
523
   3. Log event to binary log
524
   (The 'silent' flags turns off 1 and 3.)
525
526
  RETURN VALUES
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
527
  false ok
528
  true  Error
1 by brian
clean slate
529
530
*/
531
520.1.22 by Brian Aker
Second pass of thd cleanup
532
int mysql_create_db(Session *session, char *db, HA_CREATE_INFO *create_info, bool silent)
1 by brian
clean slate
533
{
534
  char	 path[FN_REFLEN+16];
535
  char	 tmp_query[FN_REFLEN+16];
536
  long result= 1;
537
  int error= 0;
15 by brian
Fix for stat, NETWARE removal
538
  struct stat stat_info;
482 by Brian Aker
Remove uint.
539
  uint32_t create_options= create_info ? create_info->options : 0;
540
  uint32_t path_len;
1 by brian
clean slate
541
542
  /* do not create 'information_schema' db */
575.4.7 by Monty Taylor
More header cleanup.
543
  if (!my_strcasecmp(system_charset_info, db, INFORMATION_SCHEMA_NAME.c_str()))
1 by brian
clean slate
544
  {
545
    my_error(ER_DB_CREATE_EXISTS, MYF(0), db);
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
546
    return(-1);
1 by brian
clean slate
547
  }
548
549
  /*
550
    Do not create database if another thread is holding read lock.
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
551
    Wait for global read lock before acquiring LOCK_drizzle_create_db.
1 by brian
clean slate
552
    After wait_if_global_read_lock() we have protection against another
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
553
    global read lock. If we would acquire LOCK_drizzle_create_db first,
1 by brian
clean slate
554
    another thread could step in and get the global read lock before we
555
    reach wait_if_global_read_lock(). If this thread tries the same as we
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
556
    (admin a db), it would then go and wait on LOCK_drizzle_create_db...
1 by brian
clean slate
557
    Furthermore wait_if_global_read_lock() checks if the current thread
558
    has the global read lock and refuses the operation with
559
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
560
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
561
  if (wait_if_global_read_lock(session, 0, 1))
1 by brian
clean slate
562
  {
563
    error= -1;
564
    goto exit2;
565
  }
566
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
567
  pthread_mutex_lock(&LOCK_drizzle_create_db);
1 by brian
clean slate
568
569
  /* Check directory */
570
  path_len= build_table_filename(path, sizeof(path), db, "", "", 0);
571
  path[path_len-1]= 0;                    // Remove last '/' from path
572
15 by brian
Fix for stat, NETWARE removal
573
  if (!stat(path,&stat_info))
1 by brian
clean slate
574
  {
575
    if (!(create_options & HA_LEX_CREATE_IF_NOT_EXISTS))
576
    {
577
      my_error(ER_DB_CREATE_EXISTS, MYF(0), db);
578
      error= -1;
579
      goto exit;
580
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
581
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
582
			ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS), db);
583
    if (!silent)
520.1.22 by Brian Aker
Second pass of thd cleanup
584
      my_ok(session);
1 by brian
clean slate
585
    error= 0;
586
    goto exit;
587
  }
588
  else
589
  {
15 by brian
Fix for stat, NETWARE removal
590
    if (errno != ENOENT)
1 by brian
clean slate
591
    {
15 by brian
Fix for stat, NETWARE removal
592
      my_error(EE_STAT, MYF(0), path, errno);
1 by brian
clean slate
593
      goto exit;
594
    }
595
    if (my_mkdir(path,0777,MYF(0)) < 0)
596
    {
597
      my_error(ER_CANT_CREATE_DB, MYF(0), db, my_errno);
598
      error= -1;
599
      goto exit;
600
    }
601
  }
602
603
  path[path_len-1]= FN_LIBCHAR;
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
604
  strncpy(path+path_len, MY_DB_OPT_FILE, sizeof(path)-path_len-1);
520.1.22 by Brian Aker
Second pass of thd cleanup
605
  if (write_db_opt(session, path, db, create_info))
1 by brian
clean slate
606
  {
607
    /*
608
      Could not create options file.
609
      Restore things to beginning.
610
    */
611
    path[path_len]= 0;
612
    if (rmdir(path) >= 0)
613
    {
614
      error= -1;
615
      goto exit;
616
    }
617
    /*
618
      We come here when we managed to create the database, but not the option
619
      file.  In this case it's best to just continue as if nothing has
620
      happened.  (This is a very unlikely senario)
621
    */
622
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
623
1 by brian
clean slate
624
  if (!silent)
625
  {
626
    char *query;
482 by Brian Aker
Remove uint.
627
    uint32_t query_length;
1 by brian
clean slate
628
520.1.22 by Brian Aker
Second pass of thd cleanup
629
    if (!session->query)				// Only in replication
1 by brian
clean slate
630
    {
631
      query= 	     tmp_query;
632
      query_length= (uint) (strxmov(tmp_query,"create database `",
461 by Monty Taylor
Removed NullS. bu-bye.
633
                                    db, "`", NULL) - tmp_query);
1 by brian
clean slate
634
    }
635
    else
636
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
637
      query= 	    session->query;
638
      query_length= session->query_length;
1 by brian
clean slate
639
    }
640
586.1.1 by Yoshinori Sano
Rename mysql to drizzle, specifically mysql_bin_log to drizzle_bin_log.
641
    if (drizzle_bin_log.is_open())
1 by brian
clean slate
642
    {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
643
      Query_log_event qinfo(session, query, query_length, 0,
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
644
			    /* suppress_use */ true);
1 by brian
clean slate
645
646
      /*
647
	Write should use the database being created as the "current
648
        database" and not the threads current database, which is the
649
        default. If we do not change the "current database" to the
650
        database being created, the CREATE statement will not be
651
        replicated when using --binlog-do-db to select databases to be
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
652
        replicated.
1 by brian
clean slate
653
654
	An example (--binlog-do-db=sisyfos):
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
655
1 by brian
clean slate
656
          CREATE DATABASE bob;        # Not replicated
657
          USE bob;                    # 'bob' is the current database
658
          CREATE DATABASE sisyfos;    # Not replicated since 'bob' is
659
                                      # current database.
660
          USE sisyfos;                # Will give error on slave since
661
                                      # database does not exist.
662
      */
663
      qinfo.db     = db;
664
      qinfo.db_len = strlen(db);
665
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
666
      /* These DDL methods and logging protected with LOCK_drizzle_create_db */
586.1.1 by Yoshinori Sano
Rename mysql to drizzle, specifically mysql_bin_log to drizzle_bin_log.
667
      drizzle_bin_log.write(&qinfo);
1 by brian
clean slate
668
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
669
    my_ok(session, result);
1 by brian
clean slate
670
  }
671
672
exit:
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
673
  pthread_mutex_unlock(&LOCK_drizzle_create_db);
520.1.22 by Brian Aker
Second pass of thd cleanup
674
  start_waiting_global_read_lock(session);
1 by brian
clean slate
675
exit2:
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
676
  return(error);
1 by brian
clean slate
677
}
678
679
680
/* db-name is already validated when we come here */
681
520.1.22 by Brian Aker
Second pass of thd cleanup
682
bool mysql_alter_db(Session *session, const char *db, HA_CREATE_INFO *create_info)
1 by brian
clean slate
683
{
684
  char path[FN_REFLEN+16];
685
  long result=1;
253 by Brian Aker
Removed final my_bool from sql_db.
686
  int error= false;
1 by brian
clean slate
687
688
  /*
689
    Do not alter database if another thread is holding read lock.
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
690
    Wait for global read lock before acquiring LOCK_drizzle_create_db.
1 by brian
clean slate
691
    After wait_if_global_read_lock() we have protection against another
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
692
    global read lock. If we would acquire LOCK_drizzle_create_db first,
1 by brian
clean slate
693
    another thread could step in and get the global read lock before we
694
    reach wait_if_global_read_lock(). If this thread tries the same as we
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
695
    (admin a db), it would then go and wait on LOCK_drizzle_create_db...
1 by brian
clean slate
696
    Furthermore wait_if_global_read_lock() checks if the current thread
697
    has the global read lock and refuses the operation with
698
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
699
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
700
  if ((error=wait_if_global_read_lock(session,0,1)))
1 by brian
clean slate
701
    goto exit2;
702
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
703
  pthread_mutex_lock(&LOCK_drizzle_create_db);
1 by brian
clean slate
704
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
705
  /*
1 by brian
clean slate
706
     Recreate db options file: /dbpath/.db.opt
707
     We pass MY_DB_OPT_FILE as "extension" to avoid
708
     "table name to file name" encoding.
709
  */
710
  build_table_filename(path, sizeof(path), db, "", MY_DB_OPT_FILE, 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
711
  if ((error=write_db_opt(session, path, db, create_info)))
1 by brian
clean slate
712
    goto exit;
713
714
  /* Change options if current database is being altered. */
715
520.1.22 by Brian Aker
Second pass of thd cleanup
716
  if (session->db && !strcmp(session->db,db))
1 by brian
clean slate
717
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
718
    session->db_charset= create_info->default_table_charset ?
1 by brian
clean slate
719
		     create_info->default_table_charset :
520.1.22 by Brian Aker
Second pass of thd cleanup
720
		     session->variables.collation_server;
721
    session->variables.collation_database= session->db_charset;
1 by brian
clean slate
722
  }
723
586.1.1 by Yoshinori Sano
Rename mysql to drizzle, specifically mysql_bin_log to drizzle_bin_log.
724
  if (drizzle_bin_log.is_open())
1 by brian
clean slate
725
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
726
    Query_log_event qinfo(session, session->query, session->query_length, 0,
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
727
			  /* suppress_use */ true);
1 by brian
clean slate
728
729
    /*
730
      Write should use the database being created as the "current
731
      database" and not the threads current database, which is the
732
      default.
733
    */
734
    qinfo.db     = db;
735
    qinfo.db_len = strlen(db);
736
520.1.22 by Brian Aker
Second pass of thd cleanup
737
    session->clear_error();
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
738
    /* These DDL methods and logging protected with LOCK_drizzle_create_db */
586.1.1 by Yoshinori Sano
Rename mysql to drizzle, specifically mysql_bin_log to drizzle_bin_log.
739
    drizzle_bin_log.write(&qinfo);
1 by brian
clean slate
740
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
741
  my_ok(session, result);
1 by brian
clean slate
742
743
exit:
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
744
  pthread_mutex_unlock(&LOCK_drizzle_create_db);
520.1.22 by Brian Aker
Second pass of thd cleanup
745
  start_waiting_global_read_lock(session);
1 by brian
clean slate
746
exit2:
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
747
  return(error);
1 by brian
clean slate
748
}
749
750
751
/*
752
  Drop all tables in a database and the database itself
753
754
  SYNOPSIS
755
    mysql_rm_db()
520.1.22 by Brian Aker
Second pass of thd cleanup
756
    session			Thread handle
1 by brian
clean slate
757
    db			Database name in the case given by user
758
		        It's already validated and set to lower case
759
                        (if needed) when we come here
760
    if_exists		Don't give error if database doesn't exists
761
    silent		Don't generate errors
762
763
  RETURN
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
764
    false ok (Database dropped)
1 by brian
clean slate
765
    ERROR Error
766
*/
767
520.1.22 by Brian Aker
Second pass of thd cleanup
768
bool mysql_rm_db(Session *session,char *db,bool if_exists, bool silent)
1 by brian
clean slate
769
{
770
  long deleted=0;
253 by Brian Aker
Removed final my_bool from sql_db.
771
  int error= false;
1 by brian
clean slate
772
  char	path[FN_REFLEN+16];
773
  MY_DIR *dirp;
482 by Brian Aker
Remove uint.
774
  uint32_t length;
327.2.4 by Brian Aker
Refactoring table.h
775
  TableList* dropped_tables= 0;
1 by brian
clean slate
776
777
  if (db && (strcmp(db, "information_schema") == 0))
778
  {
575.4.7 by Monty Taylor
More header cleanup.
779
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
780
    return(true);
1 by brian
clean slate
781
  }
782
783
  /*
784
    Do not drop database if another thread is holding read lock.
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
785
    Wait for global read lock before acquiring LOCK_drizzle_create_db.
1 by brian
clean slate
786
    After wait_if_global_read_lock() we have protection against another
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
787
    global read lock. If we would acquire LOCK_drizzle_create_db first,
1 by brian
clean slate
788
    another thread could step in and get the global read lock before we
789
    reach wait_if_global_read_lock(). If this thread tries the same as we
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
790
    (admin a db), it would then go and wait on LOCK_drizzle_create_db...
1 by brian
clean slate
791
    Furthermore wait_if_global_read_lock() checks if the current thread
792
    has the global read lock and refuses the operation with
793
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
794
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
795
  if (wait_if_global_read_lock(session, 0, 1))
1 by brian
clean slate
796
  {
797
    error= -1;
798
    goto exit2;
799
  }
800
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
801
  pthread_mutex_lock(&LOCK_drizzle_create_db);
1 by brian
clean slate
802
803
  length= build_table_filename(path, sizeof(path), db, "", "", 0);
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
804
  strcpy(path+length, MY_DB_OPT_FILE);		// Append db option file name
1 by brian
clean slate
805
  del_dbopt(path);				// Remove dboption hash entry
806
  path[length]= '\0';				// Remove file name
807
808
  /* See if the directory exists */
809
  if (!(dirp= my_dir(path,MYF(MY_DONT_SORT))))
810
  {
811
    if (!if_exists)
812
    {
813
      error= -1;
814
      my_error(ER_DB_DROP_EXISTS, MYF(0), db);
815
      goto exit;
816
    }
817
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
818
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
819
			  ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS), db);
820
  }
821
  else
822
  {
823
    pthread_mutex_lock(&LOCK_open);
824
    remove_db_from_cache(db);
825
    pthread_mutex_unlock(&LOCK_open);
826
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
827
1 by brian
clean slate
828
    error= -1;
520.1.22 by Brian Aker
Second pass of thd cleanup
829
    if ((deleted= mysql_rm_known_files(session, dirp, db, path, 0,
1 by brian
clean slate
830
                                       &dropped_tables)) >= 0)
831
    {
832
      ha_drop_database(path);
833
      error = 0;
834
    }
835
  }
836
  if (!silent && deleted>=0)
837
  {
838
    const char *query;
308 by Brian Aker
ulong conversion
839
    uint32_t query_length;
520.1.22 by Brian Aker
Second pass of thd cleanup
840
    if (!session->query)
1 by brian
clean slate
841
    {
842
      /* The client used the old obsolete mysql_drop_db() call */
843
      query= path;
844
      query_length= (uint) (strxmov(path, "drop database `", db, "`",
461 by Monty Taylor
Removed NullS. bu-bye.
845
                                     NULL) - path);
1 by brian
clean slate
846
    }
847
    else
848
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
849
      query =session->query;
850
      query_length= session->query_length;
1 by brian
clean slate
851
    }
586.1.1 by Yoshinori Sano
Rename mysql to drizzle, specifically mysql_bin_log to drizzle_bin_log.
852
    if (drizzle_bin_log.is_open())
1 by brian
clean slate
853
    {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
854
      Query_log_event qinfo(session, query, query_length, 0,
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
855
			    /* suppress_use */ true);
1 by brian
clean slate
856
      /*
857
        Write should use the database being created as the "current
858
        database" and not the threads current database, which is the
859
        default.
860
      */
861
      qinfo.db     = db;
862
      qinfo.db_len = strlen(db);
863
520.1.22 by Brian Aker
Second pass of thd cleanup
864
      session->clear_error();
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
865
      /* These DDL methods and logging protected with LOCK_drizzle_create_db */
586.1.1 by Yoshinori Sano
Rename mysql to drizzle, specifically mysql_bin_log to drizzle_bin_log.
866
      drizzle_bin_log.write(&qinfo);
1 by brian
clean slate
867
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
868
    session->clear_error();
869
    session->server_status|= SERVER_STATUS_DB_DROPPED;
870
    my_ok(session, (uint32_t) deleted);
871
    session->server_status&= ~SERVER_STATUS_DB_DROPPED;
1 by brian
clean slate
872
  }
586.1.1 by Yoshinori Sano
Rename mysql to drizzle, specifically mysql_bin_log to drizzle_bin_log.
873
  else if (drizzle_bin_log.is_open())
1 by brian
clean slate
874
  {
875
    char *query, *query_pos, *query_end, *query_data_start;
327.2.4 by Brian Aker
Refactoring table.h
876
    TableList *tbl;
482 by Brian Aker
Remove uint.
877
    uint32_t db_len;
1 by brian
clean slate
878
520.1.22 by Brian Aker
Second pass of thd cleanup
879
    if (!(query= (char*) session->alloc(MAX_DROP_TABLE_Q_LEN)))
1 by brian
clean slate
880
      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
881
    query_pos= query_data_start= strcpy(query,"drop table ")+11;
1 by brian
clean slate
882
    query_end= query + MAX_DROP_TABLE_Q_LEN;
883
    db_len= strlen(db);
884
885
    for (tbl= dropped_tables; tbl; tbl= tbl->next_local)
886
    {
482 by Brian Aker
Remove uint.
887
      uint32_t tbl_name_len;
1 by brian
clean slate
888
889
      /* 3 for the quotes and the comma*/
890
      tbl_name_len= strlen(tbl->table_name) + 3;
891
      if (query_pos + tbl_name_len + 1 >= query_end)
892
      {
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
893
        /* These DDL methods and logging protected with LOCK_drizzle_create_db */
520.1.22 by Brian Aker
Second pass of thd cleanup
894
        write_to_binlog(session, query, query_pos -1 - query, db, db_len);
1 by brian
clean slate
895
        query_pos= query_data_start;
896
      }
897
898
      *query_pos++ = '`';
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
899
      query_pos= strcpy(query_pos,tbl->table_name) + (tbl_name_len-3);
1 by brian
clean slate
900
      *query_pos++ = '`';
901
      *query_pos++ = ',';
902
    }
903
904
    if (query_pos != query_data_start)
905
    {
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
906
      /* These DDL methods and logging protected with LOCK_drizzle_create_db */
520.1.22 by Brian Aker
Second pass of thd cleanup
907
      write_to_binlog(session, query, query_pos -1 - query, db, db_len);
1 by brian
clean slate
908
    }
909
  }
910
911
exit:
912
  /*
913
    If this database was the client's selected database, we silently
914
    change the client's selected database to nothing (to have an empty
520.1.22 by Brian Aker
Second pass of thd cleanup
915
    SELECT DATABASE() in the future). For this we free() session->db and set
1 by brian
clean slate
916
    it to 0.
917
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
918
  if (session->db && !strcmp(session->db, db))
919
    mysql_change_db_impl(session, NULL, session->variables.collation_server);
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
920
  pthread_mutex_unlock(&LOCK_drizzle_create_db);
520.1.22 by Brian Aker
Second pass of thd cleanup
921
  start_waiting_global_read_lock(session);
1 by brian
clean slate
922
exit2:
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
923
  return(error);
1 by brian
clean slate
924
}
925
926
/*
255 by Brian Aker
Removed RAID table delete point.
927
  Removes files with known extensions plus.
520.1.22 by Brian Aker
Second pass of thd cleanup
928
  session MUST be set when calling this function!
1 by brian
clean slate
929
*/
930
520.1.22 by Brian Aker
Second pass of thd cleanup
931
static long mysql_rm_known_files(Session *session, MY_DIR *dirp, const char *db,
482 by Brian Aker
Remove uint.
932
				 const char *org_path, uint32_t level,
327.2.4 by Brian Aker
Refactoring table.h
933
                                 TableList **dropped_tables)
1 by brian
clean slate
934
{
935
  long deleted=0;
308 by Brian Aker
ulong conversion
936
  uint32_t found_other_files=0;
1 by brian
clean slate
937
  char filePath[FN_REFLEN];
327.2.4 by Brian Aker
Refactoring table.h
938
  TableList *tot_list=0, **tot_list_next;
1 by brian
clean slate
939
940
  tot_list_next= &tot_list;
941
482 by Brian Aker
Remove uint.
942
  for (uint32_t idx=0 ;
520.1.22 by Brian Aker
Second pass of thd cleanup
943
       idx < (uint) dirp->number_off_files && !session->killed ;
1 by brian
clean slate
944
       idx++)
945
  {
946
    FILEINFO *file=dirp->dir_entry+idx;
947
    char *extension;
948
949
    /* skiping . and .. */
950
    if (file->name[0] == '.' && (!file->name[1] ||
951
       (file->name[1] == '.' &&  !file->name[2])))
952
      continue;
953
954
    if (!(extension= strrchr(file->name, '.')))
376 by Brian Aker
strend remove
955
      extension= strchr(file->name, '\0');
1 by brian
clean slate
956
    if (find_type(extension, &deletable_extentions,1+2) <= 0)
957
    {
584.2.7 by Stewart Smith
rename and delete tabledefinition protobuf file
958
      /*
959
        ass ass ass.
960
961
        strange checking for magic extensions that are then deleted if
962
        not reg_ext (i.e. .frm).
963
964
        and (previously) we'd err out on drop database if files not matching
965
        engine ha_known_exts() or deletable_extensions were present.
966
967
        presumably this was to avoid deleting other user data... except if that
968
        data happened to be in files ending in .BAK, .opt or .TMD. *fun*
969
       */
970
      find_type(extension, ha_known_exts(),1+2);
1 by brian
clean slate
971
      continue;
972
    }
973
    /* just for safety we use files_charset_info */
974
    if (db && !my_strcasecmp(files_charset_info,
975
                             extension, reg_ext))
976
    {
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
977
      uint32_t db_len= strlen(db);
978
1 by brian
clean slate
979
      /* Drop the table nicely */
980
      *extension= 0;			// Remove extension
327.2.4 by Brian Aker
Refactoring table.h
981
      TableList *table_list=(TableList*)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
982
                              session->calloc(sizeof(*table_list) +
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
983
                                          db_len + 1 +
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
984
                                          MYSQL50_TABLE_NAME_PREFIX_LENGTH +
1 by brian
clean slate
985
                                          strlen(file->name) + 1);
986
987
      if (!table_list)
988
        goto err;
989
      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
990
      table_list->table_name= strcpy(table_list->db, db) + db_len + 1;
398.1.10 by Monty Taylor
Actually removed VOID() this time.
991
      filename_to_tablename(file->name, table_list->table_name,
992
                            MYSQL50_TABLE_NAME_PREFIX_LENGTH +
993
                            strlen(file->name) + 1);
1 by brian
clean slate
994
      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
995
      table_list->internal_tmp_table= is_prefix(file->name, TMP_FILE_PREFIX);
1 by brian
clean slate
996
      /* Link into list */
997
      (*tot_list_next)= table_list;
998
      tot_list_next= &table_list->next_local;
999
      deleted++;
1000
    }
1001
    else
1002
    {
461 by Monty Taylor
Removed NullS. bu-bye.
1003
      strxmov(filePath, org_path, "/", file->name, NULL);
1 by brian
clean slate
1004
      if (my_delete_with_symlink(filePath,MYF(MY_WME)))
1005
      {
1006
	goto err;
1007
      }
1008
    }
1009
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1010
  if (session->killed ||
590.1.7 by Stewart Smith
remove mysql_frm_type
1011
      (tot_list && mysql_rm_table_part2(session, tot_list, 1, 0, 1)))
1 by brian
clean slate
1012
    goto err;
1013
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1014
  my_dirend(dirp);
1015
1 by brian
clean slate
1016
  if (dropped_tables)
1017
    *dropped_tables= tot_list;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1018
1 by brian
clean slate
1019
  /*
1020
    If the directory is a symbolic link, remove the link first, then
1021
    remove the directory the symbolic link pointed at
1022
  */
1023
  if (found_other_files)
1024
  {
1025
    my_error(ER_DB_DROP_RMDIR, MYF(0), org_path, EEXIST);
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1026
    return(-1);
1 by brian
clean slate
1027
  }
1028
  else
1029
  {
1030
    /* Don't give errors if we can't delete 'RAID' directory */
1031
    if (rm_dir_w_symlink(org_path, level == 0))
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1032
      return(-1);
1 by brian
clean slate
1033
  }
1034
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1035
  return(deleted);
1 by brian
clean slate
1036
1037
err:
1038
  my_dirend(dirp);
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1039
  return(-1);
1 by brian
clean slate
1040
}
1041
1042
1043
/*
1044
  Remove directory with symlink
1045
1046
  SYNOPSIS
1047
    rm_dir_w_symlink()
1048
    org_path    path of derictory
1049
    send_error  send errors
1050
  RETURN
1051
    0 OK
1052
    1 ERROR
1053
*/
1054
253 by Brian Aker
Removed final my_bool from sql_db.
1055
static bool rm_dir_w_symlink(const char *org_path, bool send_error)
1 by brian
clean slate
1056
{
1057
  char tmp_path[FN_REFLEN], *pos;
1058
  char *path= tmp_path;
1059
  unpack_filename(tmp_path, org_path);
1060
#ifdef HAVE_READLINK
1061
  int error;
1062
  char tmp2_path[FN_REFLEN];
1063
1064
  /* Remove end FN_LIBCHAR as this causes problem on Linux in readlink */
376 by Brian Aker
strend remove
1065
  pos= strchr(path, '\0');
1 by brian
clean slate
1066
  if (pos > path && pos[-1] == FN_LIBCHAR)
1067
    *--pos=0;
1068
1069
  if ((error= my_readlink(tmp2_path, path, MYF(MY_WME))) < 0)
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1070
    return(1);
1 by brian
clean slate
1071
  if (!error)
1072
  {
1073
    if (my_delete(path, MYF(send_error ? MY_WME : 0)))
1074
    {
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1075
      return(send_error);
1 by brian
clean slate
1076
    }
1077
    /* Delete directory symbolic link pointed at */
1078
    path= tmp2_path;
1079
  }
1080
#endif
1081
  /* Remove last FN_LIBCHAR to not cause a problem on OS/2 */
376 by Brian Aker
strend remove
1082
  pos= strchr(path, '\0');
1 by brian
clean slate
1083
1084
  if (pos > path && pos[-1] == FN_LIBCHAR)
1085
    *--pos=0;
1086
  if (rmdir(path) < 0 && send_error)
1087
  {
1088
    my_error(ER_DB_DROP_RMDIR, MYF(0), path, errno);
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1089
    return(1);
1 by brian
clean slate
1090
  }
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1091
  return(0);
1 by brian
clean slate
1092
}
1093
1094
1095
/**
1096
  @brief Internal implementation: switch current database to a valid one.
1097
520.1.22 by Brian Aker
Second pass of thd cleanup
1098
  @param session            Thread context.
1 by brian
clean slate
1099
  @param new_db_name    Name of the database to switch to. The function will
1100
                        take ownership of the name (the caller must not free
1101
                        the allocated memory). If the name is NULL, we're
1102
                        going to switch to NULL db.
1103
  @param new_db_charset Character set of the new database.
1104
*/
1105
520.1.22 by Brian Aker
Second pass of thd cleanup
1106
static void mysql_change_db_impl(Session *session,
1 by brian
clean slate
1107
                                 LEX_STRING *new_db_name,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1108
                                 const CHARSET_INFO * const new_db_charset)
1 by brian
clean slate
1109
{
520.1.21 by Brian Aker
THD -> Session rename
1110
  /* 1. Change current database in Session. */
1 by brian
clean slate
1111
1112
  if (new_db_name == NULL)
1113
  {
1114
    /*
520.1.21 by Brian Aker
THD -> Session rename
1115
      Session::set_db() does all the job -- it frees previous database name and
1 by brian
clean slate
1116
      sets the new one.
1117
    */
1118
520.1.22 by Brian Aker
Second pass of thd cleanup
1119
    session->set_db(NULL, 0);
1 by brian
clean slate
1120
  }
575.4.7 by Monty Taylor
More header cleanup.
1121
  else if (my_strcasecmp(system_charset_info, new_db_name->str,
1122
                         INFORMATION_SCHEMA_NAME.c_str()) == 0)
1 by brian
clean slate
1123
  {
1124
    /*
520.1.21 by Brian Aker
THD -> Session rename
1125
      Here we must use Session::set_db(), because we want to copy
1 by brian
clean slate
1126
      INFORMATION_SCHEMA_NAME constant.
1127
    */
1128
575.4.7 by Monty Taylor
More header cleanup.
1129
    session->set_db(INFORMATION_SCHEMA_NAME.c_str(),
1130
                    INFORMATION_SCHEMA_NAME.length());
1 by brian
clean slate
1131
  }
1132
  else
1133
  {
1134
    /*
520.1.21 by Brian Aker
THD -> Session rename
1135
      Here we already have a copy of database name to be used in Session. So,
1136
      we just call Session::reset_db(). Since Session::reset_db() does not releases
1 by brian
clean slate
1137
      the previous database name, we should do it explicitly.
1138
    */
1139
520.1.22 by Brian Aker
Second pass of thd cleanup
1140
    if (session->db)
1141
      free(session->db);
1 by brian
clean slate
1142
520.1.22 by Brian Aker
Second pass of thd cleanup
1143
    session->reset_db(new_db_name->str, new_db_name->length);
1 by brian
clean slate
1144
  }
1145
1146
  /* 3. Update db-charset environment variables. */
1147
520.1.22 by Brian Aker
Second pass of thd cleanup
1148
  session->db_charset= new_db_charset;
1149
  session->variables.collation_database= new_db_charset;
1 by brian
clean slate
1150
}
1151
1152
1153
1154
/**
1155
  Backup the current database name before switch.
1156
520.1.22 by Brian Aker
Second pass of thd cleanup
1157
  @param[in]      session             thread handle
1 by brian
clean slate
1158
  @param[in, out] saved_db_name   IN: "str" points to a buffer where to store
1159
                                  the old database name, "length" contains the
1160
                                  buffer size
1161
                                  OUT: if the current (default) database is
1162
                                  not NULL, its name is copied to the
1163
                                  buffer pointed at by "str"
1164
                                  and "length" is updated accordingly.
1165
                                  Otherwise "str" is set to NULL and
1166
                                  "length" is set to 0.
1167
*/
1168
520.1.22 by Brian Aker
Second pass of thd cleanup
1169
static void backup_current_db_name(Session *session,
1 by brian
clean slate
1170
                                   LEX_STRING *saved_db_name)
1171
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1172
  if (!session->db)
1 by brian
clean slate
1173
  {
1174
    /* No current (default) database selected. */
1175
1176
    saved_db_name->str= NULL;
1177
    saved_db_name->length= 0;
1178
  }
1179
  else
1180
  {
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
1181
    strncpy(saved_db_name->str, session->db, saved_db_name->length - 1);
520.1.22 by Brian Aker
Second pass of thd cleanup
1182
    saved_db_name->length= session->db_length;
1 by brian
clean slate
1183
  }
1184
}
1185
1186
1187
/**
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1188
  Return true if db1_name is equal to db2_name, false otherwise.
1 by brian
clean slate
1189
1190
  The function allows to compare database names according to the MySQL
1191
  rules. The database names db1 and db2 are equal if:
1192
     - db1 is NULL and db2 is NULL;
1193
     or
1194
     - db1 is not-NULL, db2 is not-NULL, db1 is equal (ignoring case) to
1195
       db2 in system character set (UTF8).
1196
*/
1197
1198
static inline bool
1199
cmp_db_names(const char *db1_name,
1200
             const char *db2_name)
1201
{
1202
  return
1203
         /* db1 is NULL and db2 is NULL */
1204
         (!db1_name && !db2_name) ||
1205
1206
         /* db1 is not-NULL, db2 is not-NULL, db1 == db2. */
1207
         (db1_name && db2_name && my_strcasecmp(system_charset_info, db1_name, db2_name) == 0);
1208
}
1209
1210
1211
/**
1212
  @brief Change the current database and its attributes unconditionally.
1213
520.1.22 by Brian Aker
Second pass of thd cleanup
1214
  @param session          thread handle
1 by brian
clean slate
1215
  @param new_db_name  database name
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1216
  @param force_switch if force_switch is false, then the operation will fail if
1 by brian
clean slate
1217
1218
                        - new_db_name is NULL or empty;
1219
1220
                        - OR new database name is invalid
1221
                          (check_db_name() failed);
1222
1223
                        - OR user has no privilege on the new database;
1224
1225
                        - OR new database does not exist;
1226
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1227
                      if force_switch is true, then
1 by brian
clean slate
1228
1229
                        - if new_db_name is NULL or empty, the current
1230
                          database will be NULL, @@collation_database will
1231
                          be set to @@collation_server, the operation will
1232
                          succeed.
1233
1234
                        - if new database name is invalid
1235
                          (check_db_name() failed), the current database
1236
                          will be NULL, @@collation_database will be set to
1237
                          @@collation_server, but the operation will fail;
1238
1239
                        - user privileges will not be checked
520.1.21 by Brian Aker
THD -> Session rename
1240
                          (Session::db_access however is updated);
1 by brian
clean slate
1241
1242
                          TODO: is this really the intention?
1243
                                (see sp-security.test).
1244
1245
                        - if new database does not exist,the current database
1246
                          will be NULL, @@collation_database will be set to
1247
                          @@collation_server, a warning will be thrown, the
1248
                          operation will succeed.
1249
1250
  @details The function checks that the database name corresponds to a
1251
  valid and existent database, checks access rights and changes the current
1252
  database with database attributes (@@collation_database session variable,
520.1.21 by Brian Aker
THD -> Session rename
1253
  Session::db_access).
1 by brian
clean slate
1254
1255
  This function is not the only way to switch the database that is
1256
  currently employed. When the replication slave thread switches the
520.1.22 by Brian Aker
Second pass of thd cleanup
1257
  database before executing a query, it calls session->set_db directly.
1 by brian
clean slate
1258
  However, if the query, in turn, uses a stored routine, the stored routine
1259
  will use this function, even if it's run on the slave.
1260
1261
  This function allocates the name of the database on the system heap: this
1262
  is necessary to be able to uniformly change the database from any module
1263
  of the server. Up to 5.0 different modules were using different memory to
1264
  store the name of the database, and this led to memory corruption:
1265
  a stack pointer set by Stored Procedures was used by replication after
1266
  the stack address was long gone.
1267
1268
  @return Operation status
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1269
    @retval false Success
1270
    @retval true  Error
1 by brian
clean slate
1271
*/
1272
520.1.22 by Brian Aker
Second pass of thd cleanup
1273
bool mysql_change_db(Session *session, const LEX_STRING *new_db_name, bool force_switch)
1 by brian
clean slate
1274
{
1275
  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.
1276
  const CHARSET_INFO *db_default_cl;
1 by brian
clean slate
1277
1278
  if (new_db_name == NULL ||
1279
      new_db_name->length == 0)
1280
  {
1281
    if (force_switch)
1282
    {
1283
      /*
1284
        This can happen only if we're switching the current database back
1285
        after loading stored program. The thing is that loading of stored
1286
        program can happen when there is no current database.
1287
1288
        TODO: actually, new_db_name and new_db_name->str seem to be always
1289
        non-NULL. In case of stored program, new_db_name->str == "" and
1290
        new_db_name->length == 0.
1291
      */
1292
520.1.22 by Brian Aker
Second pass of thd cleanup
1293
      mysql_change_db_impl(session, NULL, session->variables.collation_server);
1 by brian
clean slate
1294
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1295
      return(false);
1 by brian
clean slate
1296
    }
1297
    else
1298
    {
1299
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
1300
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1301
      return(true);
1 by brian
clean slate
1302
    }
1303
  }
1304
1305
  if (my_strcasecmp(system_charset_info, new_db_name->str,
575.4.7 by Monty Taylor
More header cleanup.
1306
                    INFORMATION_SCHEMA_NAME.c_str()) == 0)
1 by brian
clean slate
1307
  {
1308
    /* Switch the current database to INFORMATION_SCHEMA. */
575.4.7 by Monty Taylor
More header cleanup.
1309
    /* const_cast<> is safe here: mysql_change_db_impl does a copy */
1310
    LEX_STRING is_name= { const_cast<char *>(INFORMATION_SCHEMA_NAME.c_str()),
1311
                          INFORMATION_SCHEMA_NAME.length() };
1312
    mysql_change_db_impl(session, &is_name, system_charset_info);
1 by brian
clean slate
1313
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1314
    return(false);
1 by brian
clean slate
1315
  }
1316
1317
  /*
1318
    Now we need to make a copy because check_db_name requires a
1319
    non-constant argument. Actually, it takes database file name.
1320
1321
    TODO: fix check_db_name().
1322
  */
1323
656.1.34 by Monty Taylor
Got closer...
1324
  new_db_file_name.length= new_db_name->length;
1325
  new_db_file_name.str= (char *)malloc(new_db_name->length + 1);
656.1.44 by Monty Taylor
Added some return checking.
1326
  if (new_db_file_name.str == NULL)
1327
    return(true);                             /* the error is set */
656.1.34 by Monty Taylor
Got closer...
1328
  memcpy(new_db_file_name.str, new_db_name->str, new_db_name->length);
1329
  new_db_file_name.str[new_db_name->length]= 0;
1330
1 by brian
clean slate
1331
1332
  /*
1333
    NOTE: if check_db_name() fails, we should throw an error in any case,
1334
    even if we are called from sp_head::execute().
1335
1336
    It's next to impossible however to get this error when we are called
1337
    from sp_head::execute(). But let's switch the current database to NULL
1338
    in this case to be sure.
1339
  */
1340
1341
  if (check_db_name(&new_db_file_name))
1342
  {
1343
    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.
1344
    free(new_db_file_name.str);
1 by brian
clean slate
1345
1346
    if (force_switch)
520.1.22 by Brian Aker
Second pass of thd cleanup
1347
      mysql_change_db_impl(session, NULL, session->variables.collation_server);
1 by brian
clean slate
1348
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1349
    return(true);
1 by brian
clean slate
1350
  }
1351
1352
  if (check_db_dir_existence(new_db_file_name.str))
1353
  {
1354
    if (force_switch)
1355
    {
1356
      /* Throw a warning and free new_db_file_name. */
1357
520.1.22 by Brian Aker
Second pass of thd cleanup
1358
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
1359
                          ER_BAD_DB_ERROR, ER(ER_BAD_DB_ERROR),
1360
                          new_db_file_name.str);
1361
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.
1362
      free(new_db_file_name.str);
1 by brian
clean slate
1363
1364
      /* Change db to NULL. */
1365
520.1.22 by Brian Aker
Second pass of thd cleanup
1366
      mysql_change_db_impl(session, NULL, session->variables.collation_server);
1 by brian
clean slate
1367
1368
      /* The operation succeed. */
1369
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1370
      return(false);
1 by brian
clean slate
1371
    }
1372
    else
1373
    {
1374
      /* Report an error and free new_db_file_name. */
1375
1376
      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.
1377
      free(new_db_file_name.str);
1 by brian
clean slate
1378
1379
      /* The operation failed. */
1380
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1381
      return(true);
1 by brian
clean slate
1382
    }
1383
  }
1384
1385
  /*
520.1.21 by Brian Aker
THD -> Session rename
1386
    NOTE: in mysql_change_db_impl() new_db_file_name is assigned to Session
1387
    attributes and will be freed in Session::~Session().
1 by brian
clean slate
1388
  */
1389
520.1.22 by Brian Aker
Second pass of thd cleanup
1390
  db_default_cl= get_default_db_collation(session, new_db_file_name.str);
1 by brian
clean slate
1391
520.1.22 by Brian Aker
Second pass of thd cleanup
1392
  mysql_change_db_impl(session, &new_db_file_name, db_default_cl);
1 by brian
clean slate
1393
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1394
  return(false);
1 by brian
clean slate
1395
}
1396
1397
1398
/**
1399
  Change the current database and its attributes if needed.
1400
520.1.22 by Brian Aker
Second pass of thd cleanup
1401
  @param          session             thread handle
1 by brian
clean slate
1402
  @param          new_db_name     database name
1403
  @param[in, out] saved_db_name   IN: "str" points to a buffer where to store
1404
                                  the old database name, "length" contains the
1405
                                  buffer size
1406
                                  OUT: if the current (default) database is
1407
                                  not NULL, its name is copied to the
1408
                                  buffer pointed at by "str"
1409
                                  and "length" is updated accordingly.
1410
                                  Otherwise "str" is set to NULL and
1411
                                  "length" is set to 0.
1412
  @param          force_switch    @see mysql_change_db()
1413
  @param[out]     cur_db_changed  out-flag to indicate whether the current
1414
                                  database has been changed (valid only if
1415
                                  the function suceeded)
1416
*/
1417
520.1.22 by Brian Aker
Second pass of thd cleanup
1418
bool mysql_opt_change_db(Session *session,
1 by brian
clean slate
1419
                         const LEX_STRING *new_db_name,
1420
                         LEX_STRING *saved_db_name,
1421
                         bool force_switch,
1422
                         bool *cur_db_changed)
1423
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1424
  *cur_db_changed= !cmp_db_names(session->db, new_db_name->str);
1 by brian
clean slate
1425
1426
  if (!*cur_db_changed)
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1427
    return false;
1 by brian
clean slate
1428
520.1.22 by Brian Aker
Second pass of thd cleanup
1429
  backup_current_db_name(session, saved_db_name);
1 by brian
clean slate
1430
520.1.22 by Brian Aker
Second pass of thd cleanup
1431
  return mysql_change_db(session, new_db_name, force_switch);
1 by brian
clean slate
1432
}
1433
1434
1435
/*
1436
  Check if there is directory for the database name.
1437
1438
  SYNOPSIS
1439
    check_db_dir_existence()
1440
    db_name   database name
1441
1442
  RETURN VALUES
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1443
    false   There is directory for the specified database name.
1444
    true    The directory does not exist.
1 by brian
clean slate
1445
*/
1446
1447
bool check_db_dir_existence(const char *db_name)
1448
{
1449
  char db_dir_path[FN_REFLEN];
482 by Brian Aker
Remove uint.
1450
  uint32_t db_dir_path_len;
1 by brian
clean slate
1451
1452
  db_dir_path_len= build_table_filename(db_dir_path, sizeof(db_dir_path),
1453
                                        db_name, "", "", 0);
1454
1455
  if (db_dir_path_len && db_dir_path[db_dir_path_len - 1] == FN_LIBCHAR)
1456
    db_dir_path[db_dir_path_len - 1]= 0;
1457
1458
  /* Check access. */
1459
1460
  return my_access(db_dir_path, F_OK);
1461
}