Mstdlib-1.24.0
|
Typedefs | |
typedef struct M_sql_table | M_sql_table_t |
typedef struct M_sql_index | M_sql_index_t |
Enumerations | |
enum | M_sql_table_col_flags_t { M_SQL_TABLE_COL_FLAG_NONE = 0 , M_SQL_TABLE_COL_FLAG_NOTNULL = 1 << 0 } |
enum | M_sql_table_index_flags_t { M_SQL_INDEX_FLAG_NONE = 0 , M_SQL_INDEX_FLAG_UNIQUE = 1 << 0 } |
Functions | |
M_bool | M_sql_table_exists (M_sql_connpool_t *pool, const char *name) |
M_sql_table_t * | M_sql_table_create (const char *name) |
void | M_sql_table_destroy (M_sql_table_t *table) |
M_bool | M_sql_table_add_col (M_sql_table_t *table, M_uint32 flags, const char *col_name, M_sql_data_type_t datatype, size_t max_len, const char *default_value) |
M_bool | M_sql_table_add_pk_col (M_sql_table_t *table, const char *col_name) |
M_sql_index_t * | M_sql_table_add_index (M_sql_table_t *table, M_uint32 flags, const char *idx_name) |
M_bool | M_sql_index_add_col (M_sql_index_t *idx, const char *col_name) |
M_bool | M_sql_table_add_index_str (M_sql_table_t *table, M_uint32 flags, const char *idx_name, const char *idx_cols_csv) |
M_sql_error_t | M_sql_table_execute (M_sql_connpool_t *pool, M_sql_table_t *table, char *error, size_t error_size) |
SQL Table/Schema Management
typedef struct M_sql_table M_sql_table_t |
Type holding table definition
typedef struct M_sql_index M_sql_index_t |
Type holding index definition
Flags passed to M_sql_table_add_col() for a column
Enumerator | |
---|---|
M_SQL_TABLE_COL_FLAG_NONE | Default, no special flags |
M_SQL_TABLE_COL_FLAG_NOTNULL | Column is not allowed to be NULL |
Index creation flags used by M_sql_table_add_index()
Enumerator | |
---|---|
M_SQL_INDEX_FLAG_NONE | Default, no special flags |
M_SQL_INDEX_FLAG_UNIQUE | Index enforces a unique constraint |
M_bool M_sql_table_exists | ( | M_sql_connpool_t * | pool, |
const char * | name | ||
) |
Check to see if a table exists by name.
[in] | pool | Initialized M_sql_connpool_t object |
[in] | name | Table name to check for |
M_sql_table_t * M_sql_table_create | ( | const char * | name | ) |
Create a table object which aids in creating a table definition, including indexes to be added to a database.
Table names must start with an alpha character or underscore, and can only contain alpha-numerics and underscores.
The table will not be created until M_sql_table_execute() is called.
[in] | name | Table name to create |
void M_sql_table_destroy | ( | M_sql_table_t * | table | ) |
Destroy a table object created with M_sql_table_create().
[in] | table | Table object initialized by M_sql_table_create() |
M_bool M_sql_table_add_col | ( | M_sql_table_t * | table, |
M_uint32 | flags, | ||
const char * | col_name, | ||
M_sql_data_type_t | datatype, | ||
size_t | max_len, | ||
const char * | default_value | ||
) |
Add a column to a table.
Column names have a maximum length of 63 characters and must start with an alpha character or underscore, and can only contain alpha-numerics and underscores. However, some older databases might have shorter limits, such as versions of Oracle prior to 12c R2 (March 2017), were limited to 30 characters.
[in] | table | Table object initialized by M_sql_table_create() |
[in] | flags | Bitmap of M_sql_table_col_flags_t flags |
[in] | col_name | Column name to create. |
[in] | datatype | Datatype of column |
[in] | max_len | Maximum length of column (meant for text or binary columns). Use 0 for the maximum size supported by the database for the data type. It is strongly recommended to specify a reasonable maximum size as it may have a significant impact on performance of some databases. Typically databases have maximum row sizes, and data over these limits will be stored separately (meaning the sum of all columns also matters). |
[in] | default_value | Default value to assign to column. There is little to no validation performed on this value, use caution as it is inserted directly into the create statement. Strings must be quoted with single quotes. |
M_bool M_sql_table_add_pk_col | ( | M_sql_table_t * | table, |
const char * | col_name | ||
) |
Add a column in the table to the primary key.
The order in which the columns are added to the primary key is how the primary key will be indexed/created.
The column name specified must exist in the table object.
[in] | table | Table object initialized by M_sql_table_create() |
[in] | col_name | Column name to add to the primary key |
M_sql_index_t * M_sql_table_add_index | ( | M_sql_table_t * | table, |
M_uint32 | flags, | ||
const char * | idx_name | ||
) |
Add an index to the table
[in] | table | Table object initialized by M_sql_table_create() |
[in] | flags | Bitmap of M_sql_table_index_flags_t flags |
[in] | idx_name | User-chosen index name. This should be as short as reasonably possible. |
M_bool M_sql_index_add_col | ( | M_sql_index_t * | idx, |
const char * | col_name | ||
) |
Add a column to an index
The order in which the columns are added to the index is how the it will be indexed/created.
The referenced column name must exist in the table definition.
[in] | idx | Index object initialized by M_sql_table_add_index() |
[in] | col_name | Column name to add to index |
M_bool M_sql_table_add_index_str | ( | M_sql_table_t * | table, |
M_uint32 | flags, | ||
const char * | idx_name, | ||
const char * | idx_cols_csv | ||
) |
Simplified method to add an index to a table using a comma-delimited string of column names.
Identical to M_sql_table_add_index() followed by M_sql_index_add_col() for each column in the comma-separated string.
[in] | table | Table object initialized by M_sql_table_create() |
[in] | flags | Bitmap of M_sql_table_index_flags_t flags |
[in] | idx_name | User-chosen index name. This should be as short as reasonably possible. |
[in] | idx_cols_csv | Comma separated list of column names to add to the index. The columns must already exist in the table object. |
M_sql_error_t M_sql_table_execute | ( | M_sql_connpool_t * | pool, |
M_sql_table_t * | table, | ||
char * | error, | ||
size_t | error_size | ||
) |
Apply the table object definition to the database.
[in] | pool | Initialized M_sql_connpool_t object |
[in] | table | Table object initialized by M_sql_table_create() and populated with columns/indexes and primary keys. |
[out] | error | User-supplied error buffer to output error message. |
[in] | error_size | Size of user-supplied error buffer |