MySQL 8.4.0
Source Code Documentation
sql_db.h File Reference
#include "lex_string.h"

Go to the source code of this file.

Namespaces

namespace  dd
 The version of the current data dictionary table definitions.
 

Functions

bool mysql_create_db (THD *thd, const char *db, HA_CREATE_INFO *create)
 Create a database. More...
 
bool mysql_alter_db (THD *thd, const char *db, HA_CREATE_INFO *create)
 
bool mysql_rm_db (THD *thd, const LEX_CSTRING &db, bool if_exists)
 Drop all tables, routines and events in a database and the database itself. More...
 
bool mysql_change_db (THD *thd, const LEX_CSTRING &new_db_name, bool force_switch)
 Change the current database and its attributes unconditionally. More...
 
bool mysql_opt_change_db (THD *thd, const LEX_CSTRING &new_db_name, LEX_STRING *saved_db_name, bool force_switch, bool *cur_db_changed)
 Change the current database and its attributes if needed. More...
 
bool get_default_db_collation (const dd::Schema &schema, const CHARSET_INFO **collation)
 
bool get_default_db_collation (THD *thd, const char *db_name, const CHARSET_INFO **collation)
 Return default database collation. More...
 
bool check_schema_readonly (THD *thd, const char *schema_name, TABLE_SHARE *share=nullptr)
 Check the read_only option for the given schema, and report error if the schema is not writable. More...
 

Function Documentation

◆ check_schema_readonly()

bool check_schema_readonly ( THD thd,
const char *  schema_name,
TABLE_SHARE share 
)

Check the read_only option for the given schema, and report error if the schema is not writable.

Parameters
thdThread context.
schema_nameName of schema to check.
shareFor tables, we cache the read only option in the table share, and can therefore get the read only option from the share.

Caching the read only state in the table share is done for performance reasons. If a share is submitted, we get the read only state from the share. Otherwise, we get the schema object from the DD cache in order to see the read only state.

Returns
false if the schema is writable, true if not. If returning true, then error is already reported.

◆ get_default_db_collation() [1/2]

bool get_default_db_collation ( const dd::Schema schema,
const CHARSET_INFO **  collation 
)

◆ get_default_db_collation() [2/2]

bool get_default_db_collation ( THD thd,
const char *  db_name,
const CHARSET_INFO **  collation 
)

Return default database collation.

Parameters
thdThread context.
db_nameDatabase name.
[out]collationCharset object pointer if object exists else NULL.
Returns
false No error. true Error (thd->is_error is assumed to be set.)

◆ mysql_alter_db()

bool mysql_alter_db ( THD thd,
const char *  db,
HA_CREATE_INFO create 
)

◆ mysql_change_db()

bool mysql_change_db ( THD thd,
const LEX_CSTRING new_db_name,
bool  force_switch 
)

Change the current database and its attributes unconditionally.

Parameters
thdthread handle
new_db_namedatabase name
force_switchif force_switch is false, then the operation will fail if
                  - new_db_name is NULL or empty;

                  - OR new database name is invalid
                    (check_db_name() failed);

                  - OR user has no privilege on the new database;

                  - OR new database does not exist;

                if force_switch is true, then

                  - if new_db_name is NULL or empty, the current
                    database will be NULL, @@collation_database will
                    be set to @@collation_server, the operation will
                    succeed.

                  - if new database name is invalid
                    (check_db_name() failed), the current database
                    will be NULL, @@collation_database will be set to
                    @@collation_server, but the operation will fail;

                  - user privileges will not be checked
                    (THD::db_access however is updated);

                    TODO: is this really the intention?
                          (see sp-security.test).

                  - if new database does not exist,the current database
                    will be NULL, @@collation_database will be set to
                    @@collation_server, a warning will be thrown, the
                    operation will succeed.

The function checks that the database name corresponds to a valid and existent database, checks access rights and changes the current database with database attributes (@collation_database session variable, THD::db_access).

This function is not the only way to switch the database that is currently employed. When the replication slave thread switches the database before executing a query, it calls thd->set_db directly. However, if the query, in turn, uses a stored routine, the stored routine will use this function, even if it's run on the slave.

This function allocates the name of the database on the system heap: this is necessary to be able to uniformly change the database from any module of the server. Up to 5.0 different modules were using different memory to store the name of the database, and this led to memory corruption: a stack pointer set by Stored Procedures was used by replication after the stack address was long gone.

Returns
Operation status
Return values
falseSuccess
trueError

◆ mysql_create_db()

bool mysql_create_db ( THD thd,
const char *  db,
HA_CREATE_INFO create_info 
)

Create a database.

Parameters
thdThread handler
dbName of database to create Function assumes that this is already validated.
create_infoDatabase create options (like character set)

SIDE-EFFECTS

  1. Report back to client that command succeeded (my_ok)
  2. Report errors to client
  3. Log event to binary log
Return values
falseok
trueError

◆ mysql_opt_change_db()

bool mysql_opt_change_db ( THD thd,
const LEX_CSTRING new_db_name,
LEX_STRING saved_db_name,
bool  force_switch,
bool *  cur_db_changed 
)

Change the current database and its attributes if needed.

Parameters
thdthread handle
new_db_namedatabase name
[in,out]saved_db_nameIN: "str" points to a buffer where to store the old database name, "length" contains the buffer size OUT: if the current (default) database is not NULL, its name is copied to the buffer pointed at by "str" and "length" is updated accordingly. Otherwise "str" is set to NULL and "length" is set to 0.
force_switchif the change of the current database shall be forced
See also
mysql_change_db()
Parameters
[out]cur_db_changedout-flag to indicate whether the current database has been changed (valid only if the function succeeded)

◆ mysql_rm_db()

bool mysql_rm_db ( THD thd,
const LEX_CSTRING db,
bool  if_exists 
)

Drop all tables, routines and events in a database and the database itself.

Parameters
thdThread handle
dbDatabase name in the case given by user It's already validated and set to lower case (if needed) when we come here
if_existsDon't give error if database doesn't exists
Note
We do a "best effort" - try to drop as much as possible. If dropping the database itself fails, we try to binlog the drop of the tables we managed to do.
Return values
falseOK (Database dropped)
trueError