Mstdlib-1.24.0
|
Typedefs | |
typedef struct M_sql_trans | M_sql_trans_t |
typedef M_sql_error_t(* | M_sql_trans_commands_t) (M_sql_trans_t *trans, void *arg, char *error, size_t error_size) |
Enumerations | |
enum | M_sql_isolation_t { M_SQL_ISOLATION_UNKNOWN = 0 , M_SQL_ISOLATION_READUNCOMMITTED = 1 , M_SQL_ISOLATION_READCOMMITTED = 2 , M_SQL_ISOLATION_REPEATABLEREAD = 3 , M_SQL_ISOLATION_SNAPSHOT = 4 , M_SQL_ISOLATION_SERIALIZABLE = 5 } |
Functions | |
M_sql_error_t | M_sql_trans_begin (M_sql_trans_t **trans, M_sql_connpool_t *pool, M_sql_isolation_t isolation, char *error, size_t error_size) |
M_sql_error_t | M_sql_trans_rollback (M_sql_trans_t *trans) |
M_sql_error_t | M_sql_trans_commit (M_sql_trans_t *trans, char *error, size_t error_size) |
M_sql_error_t | M_sql_trans_execute (M_sql_trans_t *trans, M_sql_stmt_t *stmt) |
M_sql_error_t | M_sql_trans_process (M_sql_connpool_t *pool, M_sql_isolation_t isolation, M_sql_trans_commands_t cmd, void *cmd_arg, char *error, size_t error_size) |
M_sql_connpool_t * | M_sql_trans_get_pool (M_sql_trans_t *trans) |
SQL Transaction Handling
typedef struct M_sql_trans M_sql_trans_t |
Object holding the state for an active transaction
typedef M_sql_error_t(* M_sql_trans_commands_t) (M_sql_trans_t *trans, void *arg, char *error, size_t error_size) |
Function prototype called by M_sql_trans_process().
Inside the function created, the integrator should perform each step of the SQL transaction, and if an error occurs, return the appropriate error condition, whether it is an error condition as returned by M_sql_trans_execute(), which should be passed through unmodified, or an internally generated error condition if internal logic fails. For user-logic generated errors, special error conditions of M_SQL_ERROR_USER_SUCCESS, M_SQL_ERROR_USER_RETRY and M_SQL_ERROR_USER_FAILURE exist to more accurately identify the condition rather than attempting to map to the generic SQL subsystem condtions.
[in] | trans | Pointer to initialized transaction object to use to execute the transaction. |
[in] | arg | User-specified argument used for storing metadata about the flow/process. |
[out] | error | User-supplied error buffer to output error message. |
[in] | error_size | Size of user-supplied error buffer. |
enum M_sql_isolation_t |
Transaction isolation levels
M_sql_error_t M_sql_trans_begin | ( | M_sql_trans_t ** | trans, |
M_sql_connpool_t * | pool, | ||
M_sql_isolation_t | isolation, | ||
char * | error, | ||
size_t | error_size | ||
) |
Begin a new SQL transaction at the requested isolation level.
Beginning a new transaction will reserve an SQL connection from the pool until either a rollback or commit is performed. Callers in most cases should not start more than one SQL transaction per thread as it could lead to deadlocks waiting on a connection to become available if insufficient connections are available in the pool.
In order to clean up the returned transaction handle, a caller must call either M_sql_trans_commit() or M_sql_trans_rollback() as appropriate.
[out] | trans | Returns initialized transaction handle to be used for queries. |
[in] | pool | Initialized M_sql_connpool_t object |
[in] | isolation | Requested isolation level. The database may choose the closest match if the isolation level requested is not supported. |
[out] | error | User-supplied buffer to hold error message. |
[in] | error_size | Size of User-supplied buffer. |
M_sql_error_t M_sql_trans_rollback | ( | M_sql_trans_t * | trans | ) |
Rollback an SQL transaction.
This function should be called if the caller needs to cancel the transaction, or must be called to clean up the M_sql_trans_t handle when an unrecoverable error has occurred such as a server disconnect or deadlock.
The passed in trans handle will be destroyed regardless if this function returns success or fail.
[in] | trans | Initialized transaction handle that will be used to rollback the pending transaction, and will be will be destroyed automatically upon return of this function. |
M_sql_error_t M_sql_trans_commit | ( | M_sql_trans_t * | trans, |
char * | error, | ||
size_t | error_size | ||
) |
Commit a pending SQL transaction.
Any statements executed against the transaction handle will not be applied to the database until this command is called.
The associated transaction handle will be automatically destroyed regardless if this function returns success or fail. If a failure occurs, the caller must assume the transaction was NOT applied (e.g. rolled back).
[in] | trans | Initialized transaction handle that will be used to commit the pending transaction, and will be will be destroyed automatically upon return of this function. |
[out] | error | User-supplied buffer to hold error message. |
[in] | error_size | Size of User-supplied buffer. |
M_sql_error_t M_sql_trans_execute | ( | M_sql_trans_t * | trans, |
M_sql_stmt_t * | stmt | ||
) |
Execute a query against the database that is part of an open transaction. This request will not automatically commit and must be manually committed via M_sql_trans_commit().
Must call M_sql_stmt_prepare() or M_sql_stmt_prepare_buf() prior to execution. Must also bind any parameters using M_sql_stmt_bind_*() series of functions.
This function will NOT destroy the passed in M_sql_trans_t object, it is kept open so additional statements can be executed within the same transaction. If NOT using the M_sql_trans_process() helper, it is the caller's responsibility to call M_sql_trans_commit() or M_sql_trans_rollback() as appropriate.
[in] | trans | Initialized M_sql_trans_t object. |
[in] | stmt | Initialized and prepared M_sql_stmt_t object |
M_sql_error_t M_sql_trans_process | ( | M_sql_connpool_t * | pool, |
M_sql_isolation_t | isolation, | ||
M_sql_trans_commands_t | cmd, | ||
void * | cmd_arg, | ||
char * | error, | ||
size_t | error_size | ||
) |
Helper function for processing a sequence of SQL commands as a single atomic operation, while automatically handling things like rollback and connectivity failure situations.
Usage Example:
[in] | pool | Initialized and started pool object. |
[in] | isolation | Requested isolation level. The database may choose the closest match if the isolation level requested is not supported. |
[in] | cmd | User-specified function to call to step through the sequence of SQL commands to run as part of the transaction. |
[in] | cmd_arg | Argument to pass to User-specified function for metadata about the command(s) being executed. |
[out] | error | User-supplied error buffer to output error message. |
[in] | error_size | Size of user-supplied error buffer. |
M_sql_connpool_t * M_sql_trans_get_pool | ( | M_sql_trans_t * | trans | ) |
Retrieve the M_sql_connpool_t object from a transaction handle typically used within M_sql_trans_process() for using the SQL helpers like M_sql_query_append_updlock() and M_sql_query_append_bitop().
[in] | trans | Transaction object |