sql-api.h revision d5cebe7f98e63d4e2822863ef2faa4971e8b3a5d
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* This SQL API is designed to work asynchronously. The underlying drivers
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes however may not. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* Set if queries are not executed asynchronously */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholestypedef void sql_query_callback_t(struct sql_result *result, void *context);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholestypedef void sql_commit_callback_t(const char *error, void *context);
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes/* Initialize database connections. db_driver is the database driver name,
16b55a35cff91315d261d1baa776138af465c4e4fuankg eg. "mysql" or "pgsql". connect_string is driver-specific. */
16b55a35cff91315d261d1baa776138af465c4e4fuankgstruct sql_db *sql_init(const char *db_driver, const char *connect_string);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Returns SQL database state flags. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesenum sql_db_flags sql_get_flags(struct sql_db *db);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Explicitly connect to the database. It's not required to call this function
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes though. Returns -1 if we're not connected, 0 if we started connecting or
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes 1 if we are fully connected now. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Execute SQL query without waiting for results. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesvoid sql_exec(struct sql_db *db, const char *query);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Execute SQL query and return result in callback. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesvoid sql_query(struct sql_db *db, const char *query,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Execute blocking SQL query and return result. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstruct sql_result *sql_query_s(struct sql_db *db, const char *query);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Go to next row, returns 1 if ok, 0 if this was the last row or -1 if error
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes occurred. This needs to be the first call for result. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesint sql_result_next_row(struct sql_result *result);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Needs to be called only with sql_query_s(). */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Return number of fields in result. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesunsigned int sql_result_get_fields_count(struct sql_result *result);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Return name of the given field index. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesconst char *sql_result_get_field_name(struct sql_result *result,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes unsigned int idx);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Return field index for given name, or -1 if not found. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesint sql_result_find_field(struct sql_result *result, const char *field_name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Returns value of given field as string. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesconst char *sql_result_get_field_value(struct sql_result *result,
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg unsigned int idx);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesconst char *sql_result_find_field_value(struct sql_result *result,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes const char *field_name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Return all values of current row. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesconst char *const *sql_result_get_values(struct sql_result *result);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Return last error message in result. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesconst char *sql_result_get_error(struct sql_result *result);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Begin a new transaction. Currently you're limited to only one open
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes transaction at a time. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstruct sql_transaction_context *sql_transaction_begin(struct sql_db *db);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Commit transaction. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesvoid sql_transaction_commit(struct sql_transaction_context **ctx,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Synchronous commit. Returns 0 if ok, -1 if error. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesint sql_transaction_commit_s(struct sql_transaction_context **ctx,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes const char **error_r);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesvoid sql_transaction_rollback(struct sql_transaction_context **ctx);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* Execute query in given transaction. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesvoid sql_update(struct sql_transaction_context *ctx, const char *query);