197
Free database option hash and locked databases hash.
200
void my_database_names_free(void)
205
hash_free(&dboptions);
206
(void) pthread_rwlock_destroy(&LOCK_dboptions);
207
hash_free(&lock_db_cache);
213
Cleanup cached options
216
void my_dbopt_cleanup(void)
218
pthread_rwlock_wrlock(&LOCK_dboptions);
219
hash_free(&dboptions);
220
hash_init(&dboptions, lower_case_table_names ?
221
&my_charset_bin : system_charset_info,
222
32, 0, 0, (hash_get_key) dboptions_get_key,
224
pthread_rwlock_unlock(&LOCK_dboptions);
229
Find database options in the hash.
232
Search a database options in the hash, usings its path.
233
Fills "create" on success.
240
static bool get_dbopt(const char *dbname, HA_CREATE_INFO *create)
246
length= (uint) strlen(dbname);
248
pthread_rwlock_rdlock(&LOCK_dboptions);
249
if ((opt= (my_dbopt_t*) hash_search(&dboptions, (unsigned char*) dbname, length)))
251
create->default_table_charset= opt->charset;
254
pthread_rwlock_unlock(&LOCK_dboptions);
260
Writes database options into the hash.
263
Inserts database options into the hash, or updates
264
options if they are already in the hash.
271
static bool put_dbopt(const char *dbname, HA_CREATE_INFO *create)
277
length= (uint) strlen(dbname);
279
pthread_rwlock_wrlock(&LOCK_dboptions);
280
if (!(opt= (my_dbopt_t*) hash_search(&dboptions, (unsigned char*) dbname, length)))
282
/* Options are not in the hash, insert them */
284
if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
285
&opt, (uint) sizeof(*opt), &tmp_name, (uint) length+1,
293
strcpy(opt->name, dbname);
294
opt->name_length= length;
296
if ((error= my_hash_insert(&dboptions, (unsigned char*) opt)))
303
/* Update / write options in hash */
304
opt->charset= create->default_table_charset;
307
pthread_rwlock_unlock(&LOCK_dboptions);
313
Deletes database options from the hash.
316
void del_dbopt(const char *path)
319
pthread_rwlock_wrlock(&LOCK_dboptions);
320
if ((opt= (my_dbopt_t *)hash_search(&dboptions, (const unsigned char*) path,
322
hash_delete(&dboptions, (unsigned char*) opt);
323
pthread_rwlock_unlock(&LOCK_dboptions);
328
Create database options file:
331
Currently database default charset is only stored there.
335
1 Could not create file or write to it. Error sent through my_error()
338
static bool write_db_opt(Session *session, const char *path, const char *name, HA_CREATE_INFO *create)
149
Return default database collation.
151
@param session Thread context.
152
@param db_name Database name.
154
@return CHARSET_INFO object. The operation always return valid character
155
set, even if the database does not exist.
158
const CHARSET_INFO *get_default_db_collation(Session *session, const char *db_name)
160
HA_CREATE_INFO db_info;
162
if (session->db != NULL && strcmp(db_name, session->db) == 0)
163
return session->db_charset;
166
db_info.default_table_charset contains valid character set
170
load_db_opt_by_name(session, db_name, &db_info);
172
return db_info.default_table_charset;
175
/* path is path to database, not schema file */
176
static int write_schema_file(Session *session,
177
const char *path, const char *name,
178
HA_CREATE_INFO *create)
341
180
drizzle::Schema db;
181
char schema_file_tmp[FN_REFLEN];
182
string schema_file(path);
188
snprintf(schema_file_tmp, FN_REFLEN, "%s%c%s.tmpXXXXXX", path, FN_LIBCHAR, MY_DB_OPT_FILE);
190
schema_file.append(1, FN_LIBCHAR);
191
schema_file.append(MY_DB_OPT_FILE);
193
int fd= mkstemp(schema_file_tmp);
345
198
if (!create->default_table_charset)
346
199
create->default_table_charset= session->variables.collation_server;
348
if (put_dbopt(path, create))
351
201
db.set_name(name);
352
db.set_characterset(create->default_table_charset->csname);
353
202
db.set_collation(create->default_table_charset->name);
355
fstream output(path, ios::out | ios::trunc | ios::binary);
356
if (!db.SerializeToOstream(&output))
204
if (!db.SerializeToFileDescriptor(fd))
207
unlink(schema_file_tmp);
211
if (rename(schema_file_tmp, schema_file.c_str()) == -1)
364
Load database options file
367
path Path for option file
368
create Where to store the read options
374
1 No database file or could not open it
378
bool load_db_opt(Session *session, const char *path, HA_CREATE_INFO *create)
221
int load_db_opt(Session *session, const char *path, HA_CREATE_INFO *create)
381
223
drizzle::Schema db;
384
226
memset(create, 0, sizeof(*create));
385
227
create->default_table_charset= session->variables.collation_server;
387
/* Check if options for this database are already in the hash */
388
if (!get_dbopt(path, create))
391
fstream input(path, ios::in | ios::binary);
394
else if (!db.ParseFromIstream(&input))
397
buffer= db.characterset();
398
if (!(create->default_table_charset= get_charset_by_csname(buffer.c_str(), MY_CS_PRIMARY, MYF(0))))
229
int fd= open(path, O_RDONLY);
234
if (!db.ParseFromFileDescriptor(fd))
400
errmsg_printf(ERRMSG_LVL_ERROR, _("Error while loading database options: '%s':"),path);
401
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
402
create->default_table_charset= default_charset_info;
405
241
buffer= db.collation();
406
if (!(create->default_table_charset= get_charset_by_name(buffer.c_str(), MYF(0))))
242
if (!(create->default_table_charset= get_charset_by_name(buffer.c_str(),
408
errmsg_printf(ERRMSG_LVL_ERROR, _("Error while loading database options: '%s':"),path);
245
errmsg_printf(ERRMSG_LVL_ERROR,
246
_("Error while loading database options: '%s':"),path);
409
247
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
410
248
create->default_table_charset= default_charset_info;
414
Put the loaded value into the hash.
415
Note that another thread could've added the same
416
entry to the hash after we called get_dbopt(),
417
but it's not an error, as put_dbopt() takes this
418
possibility into account.
420
error= put_dbopt(path, create);
428
Retrieve database options by name. Load database options file or fetch from
432
load_db_opt_by_name()
433
db_name Database name
434
db_create_info Where to store the database options
437
load_db_opt_by_name() is a shortcut for load_db_opt().
440
Although load_db_opt_by_name() (and load_db_opt()) returns status of
441
the operation, it is useless usually and should be ignored. The problem
442
is that there are 1) system databases ("mysql") and 2) virtual
443
databases ("information_schema"), which do not contain options file.
444
So, load_db_opt[_by_name]() returns false for these databases, but this
447
load_db_opt[_by_name]() clears db_create_info structure in any case, so
448
even on failure it contains valid data. So, common use case is just
449
call load_db_opt[_by_name]() without checking return value and use
450
db_create_info right after that.
452
RETURN VALUES (read NOTE!)
454
true Failed to retrieve options
457
bool load_db_opt_by_name(Session *session, const char *db_name,
458
HA_CREATE_INFO *db_create_info)
255
int load_db_opt_by_name(Session *session, const char *db_name,
256
HA_CREATE_INFO *db_create_info)
460
258
char db_opt_path[FN_REFLEN];
566
332
path_len= build_table_filename(path, sizeof(path), db, "", "", 0);
567
333
path[path_len-1]= 0; // Remove last '/' from path
569
if (!stat(path,&stat_info))
335
if (mkdir(path,0777) == -1)
571
if (!(create_options & HA_LEX_CREATE_IF_NOT_EXISTS))
573
my_error(ER_DB_CREATE_EXISTS, MYF(0), db);
339
if (!(create_options & HA_LEX_CREATE_IF_NOT_EXISTS))
341
my_error(ER_DB_CREATE_EXISTS, MYF(0), db);
345
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
346
ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS), db);
577
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
578
ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS), db);
353
my_error(ER_CANT_CREATE_DB, MYF(0), db, my_errno);
588
my_error(EE_STAT, MYF(0), path, errno);
591
if (mkdir(path,0777) < 0)
593
my_error(ER_CANT_CREATE_DB, MYF(0), db, my_errno);
599
path[path_len-1]= FN_LIBCHAR;
600
strncpy(path+path_len, MY_DB_OPT_FILE, sizeof(path)-path_len-1);
601
if (write_db_opt(session, path, db, create_info))
358
error= write_schema_file(session, path, db, create_info);
359
if (error && error != EEXIST)
604
Could not create options file.
605
Restore things to beginning.
608
361
if (rmdir(path) >= 0)
614
We come here when we managed to create the database, but not the option
615
file. In this case it's best to just continue as if nothing has
616
happened. (This is a very unlikely senario)