sql-api.h revision 137ea7ca34005345aa2304a940149b7f3774d727
ccffb125d94adff0ad776de5a96e22f864d6fb0aTimo Sirainen /* Set if queries are not executed asynchronously */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* This SQL API is designed to work asynchronously. The underlying drivers
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen however may not. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainentypedef void sql_query_callback_t(struct sql_result *result, void *context);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Initialize database connections. db_driver is the database driver name,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen eg. "mysql" or "pgsql". connect_string is driver-specific. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstruct sql_db *sql_init(const char *db_driver, const char *connect_string);
ccffb125d94adff0ad776de5a96e22f864d6fb0aTimo Sirainen/* Returns SQL database state flags. */
ccffb125d94adff0ad776de5a96e22f864d6fb0aTimo Sirainenenum sql_db_flags sql_get_flags(struct sql_db *db);
a0b89f3b1df99b3a32f44623f13ad1893118825bTimo Sirainen/* Explicitly connect to the database. It's not required to call this function
a0b89f3b1df99b3a32f44623f13ad1893118825bTimo Sirainen though. Returns -1 if we're not connected, 0 if we started connecting or
a0b89f3b1df99b3a32f44623f13ad1893118825bTimo Sirainen 1 if we are fully connected now. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Execute SQL query without waiting for results. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenvoid sql_exec(struct sql_db *db, const char *query);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Execute SQL query and return result in callback. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenvoid sql_query(struct sql_db *db, const char *query,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen sql_query_callback_t *callback, void *context);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Go to next row, returns 1 if ok, 0 if this was the last row or -1 if error
137ea7ca34005345aa2304a940149b7f3774d727Timo Sirainen occurred. This needs to be the first call for result. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenint sql_result_next_row(struct sql_result *result);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Return number of fields in result. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenunsigned int sql_result_get_fields_count(struct sql_result *result);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Return name of the given field index. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenconst char *sql_result_get_field_name(struct sql_result *result,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unsigned int idx);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Return field index for given name, or -1 if not found. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenint sql_result_find_field(struct sql_result *result, const char *field_name);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Returns value of given field as string. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenconst char *sql_result_get_field_value(struct sql_result *result,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unsigned int idx);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenconst char *sql_result_find_field_value(struct sql_result *result,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Return all values of current row. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenconst char *const *sql_result_get_values(struct sql_result *result);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Return last error message in result. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenconst char *sql_result_get_error(struct sql_result *result);