#include "db_key.h"#include "db_op.h"#include "db_val.h"#include "db_con.h"#include "db_res.h"#include "db_cap.h"#include "db_row.h"#include "db_res.h"

Go to the source code of this file.
Generic Database Interface This is a generic database interface for modules that need to utilize a database. The interface should be used by all modules that access database. The interface will be independent of the underlying database server. Notes: If possible, use the predefined macros if you need to access any structure attributes. For additional description, see the comments in the sources of mysql module.
If you want to see more complicated examples of how the API could be used, take a look at the sources of the usrloc or auth modules.
Definition in file lib/srdb1/db.h.
| typedef int(* db_abort_transaction_f)(db1_con_t *_h) |
Use this function if you have an error after having started a transaction and you want to rollback any uncommitted changes before continuing.
| _h | structure representing database connection |
Definition at line 368 of file lib/srdb1/db.h.
| typedef int(* db_affected_rows_f)(const db1_con_t *_h) |
The function returns the rows affected by the last query. If any other type of query was the last, it returns null.
| _h | structure representing database connection |
Definition at line 342 of file lib/srdb1/db.h.
| typedef int(* db_bind_api_f)(db_func_t *dbb) |
The function links the functions implemented by the module to the members of db_func_t structure
| dbb | db_func_t structure representing the variable where to bind |
Definition at line 505 of file lib/srdb1/db.h.
| typedef void(* db_close_f)(db1_con_t *_h) |
The function closes previously open connection and frees all previously allocated memory. The function db_close must be the very last function called.
| _h | db1_con_t structure representing the database connection |
Definition at line 128 of file lib/srdb1/db.h.
| typedef int(* db_delete_f)(const db1_con_t *_h, const db_key_t *_k, const db_op_t *_o, const db_val_t *_v, const int _n) |
This function implements DELETE SQL directive, it is possible to delete one or more rows from a table. If _k is NULL and _v is NULL and _n is zero, all rows are deleted, the resulting table will be empty. If _o is NULL, the equal operator "=" will be used for the comparison.
| _h | database connection handle | |
| _k | array of keys (column names) that will be matched | |
| _o | array of operators to be used with key-value pairs | |
| _v | array of values that the row must match to be deleted | |
| _n | number of keys-value parameters in _k and _v parameters |
Definition at line 244 of file lib/srdb1/db.h.
| typedef int(* db_end_transaction_f)(db1_con_t *_h) |
| _h | structure representing database connection |
Definition at line 358 of file lib/srdb1/db.h.
| typedef int(* db_fetch_result_f)(const db1_con_t *_h, db1_res_t **_r, const int _n) |
Gets a partial result set, fetch a number of rows from a database result. This function initialize the given result structure on the first run, and fetches the nrows number of rows. On subsequenting runs, it uses the existing result and fetches more rows, until it reaches the end of the result set. Because of this the result needs to be null in the first invocation of the function. If the number of wanted rows is zero, the function returns anything with a result of zero.
| _h | structure representing database connection | |
| _r | structure for the result | |
| _n | the number of rows that should be fetched |
Definition at line 179 of file lib/srdb1/db.h.
| typedef int(* db_free_result_f)(db1_con_t *_h, db1_res_t *_r) |
This function frees all memory allocated previously in db_query. Its neccessary to call this function on a db1_res_t structure if you don't need the structure anymore. You must call this function before you call db_query again!
| _h | database connection handle | |
| _r | pointer to db1_res_t structure to destroy |
Definition at line 210 of file lib/srdb1/db.h.
This structure holds function pointer to all database functions. Before this structure can be used it must be initialized with bind_dbmod.
| typedef db1_con_t*(* db_init2_f)(const str *_sqlurl, db_pooling_t _pooling) |
This function initialize the database API and open a new database connection. This function must be called after bind_dbmod but before any other database API function is called.
The function takes one parameter, the parameter must contain the database connection URL. The URL is of the form mysql://username:password@host:port/database where:
username: Username to use when logging into database (optional). password: password if it was set (optional) host: Hosname or IP address of the host where database server lives (mandatory) port: Port number of the server if the port differs from default value (optional) database: If the database server supports multiple databases, you must specify the name of the database (optional).
| _sqlurl | database connection URL | |
| _pooling | whether or not to use a pooled connection |
Definition at line 119 of file lib/srdb1/db.h.
This function initialize the database API and open a new database connection. This function must be called after bind_dbmod but before any other database API function is called.
The function takes one parameter, the parameter must contain the database connection URL. The URL is of the form mysql://username:password@host:port/database where:
username: Username to use when logging into database (optional). password: password if it was set (optional) host: Hosname or IP address of the host where database server lives (mandatory) port: Port number of the server if the port differs from default value (optional) database: If the database server supports multiple databases, you must specify the name of the database (optional).
| _sqlurl | database connection URL |
Definition at line 94 of file lib/srdb1/db.h.
| typedef int(* db_insert_delayed_f)(const db1_con_t *_h, const db_key_t *_k, const db_val_t *_v, const int _n) |
This function implements INSERT DELAYED SQL directive. It is possible to insert one or more rows in a table with delay using this function.
| _h | database connection handle | |
| _k | array of keys (column names) | |
| _v | array of values for keys specified in _k parameter | |
| _n | number of keys-value pairs int _k and _v parameters |
Definition at line 330 of file lib/srdb1/db.h.
| typedef int(* db_insert_f)(const db1_con_t *_h, const db_key_t *_k, const db_val_t *_v, const int _n) |
This function implements INSERT SQL directive, you can insert one or more rows in a table using this function.
| _h | database connection handle | |
| _k | array of keys (column names) | |
| _v | array of values for keys specified in _k parameter | |
| _n | number of keys-value pairs int _k and _v parameters |
Definition at line 224 of file lib/srdb1/db.h.
| typedef int(* db_insert_update_f)(const db1_con_t *_h, const db_key_t *_k, const db_val_t *_v, const int _n) |
The function implements the INSERT ON DUPLICATE KEY UPDATE SQL directive. It is possible to insert a row and update if one already exists. The old row will not deleted before the insertion of the new data.
| _h | structure representing database connection | |
| _k | key names | |
| _v | values of the keys | |
| _n | number of key=value pairs |
Definition at line 315 of file lib/srdb1/db.h.
| typedef int(* db_last_inserted_id_f)(const db1_con_t *_h) |
The function returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement. Use this function after you have performed an INSERT statement into a table that contains an AUTO_INCREMENT field.
| _h | structure representing database connection |
Definition at line 300 of file lib/srdb1/db.h.
| typedef int(* db_query_f)(const db1_con_t *_h, const db_key_t *_k, const db_op_t *_op, const db_val_t *_v, const db_key_t *_c, const int _n, const int _nc, const db_key_t _o, db1_res_t **_r) |
This function implements the SELECT SQL directive. If _k and _v parameters are NULL and _n is zero, you will get the whole table.
if _c is NULL and _nc is zero, you will get all table columns in the result. _r will point to a dynamically allocated structure, it is neccessary to call db_free_result function once you are finished with the result.
If _op is 0, equal (=) will be used for all key-value pairs comparisons.
Strings in the result are not duplicated, they will be discarded if you call db_free_result, make a copy yourself if you need to keep it after db_free_result.
You must call db_free_result before you can call db_query again!
| _h | database connection handle | |
| _k | array of column names that will be compared and their values must match | |
| _op | array of operators to be used with key-value pairs | |
| _v | array of values, columns specified in _k parameter must match these values | |
| _c | array of column names that you are interested in | |
| _n | number of key-value pairs to match in _k and _v parameters | |
| _nc | number of columns in _c parameter | |
| _o | order by statement for query | |
| _r | address of variable where pointer to the result will be stored |
Definition at line 160 of file lib/srdb1/db.h.
| typedef int(* db_raw_query_f)(const db1_con_t *_h, const str *_s, db1_res_t **_r) |
This function can be used to do database specific queries. Please use this function only if needed, as this creates portability issues for the different databases. Also keep in mind that you need to escape all external data sources that you use. You could use the escape_common and unescape_common functions in the core for this task.
| _h | structure representing database connection | |
| _s | the SQL query | |
| _r | structure for the result |
Definition at line 197 of file lib/srdb1/db.h.
| typedef int(* db_replace_f)(const db1_con_t *handle, const db_key_t *keys, const db_val_t *vals, const int n, const int _un, const int _m) |
The function implements the REPLACE SQL directive. It is possible to insert a row and replace if one already exists. The old row will be deleted before the insertion of the new data.
| _h | structure representing database connection | |
| _k | key names | |
| _v | values of the keys | |
| _n | number of key=value pairs | |
| _un | number of keys to build the unique key, starting from first _k | |
| _m | mode - first update, then insert, or first insert, then update |
Definition at line 285 of file lib/srdb1/db.h.
| typedef int(* db_start_transaction_f)(db1_con_t *_h) |
| _h | structure representing database connection |
Definition at line 350 of file lib/srdb1/db.h.
| typedef int(* db_update_f)(const db1_con_t *_h, const db_key_t *_k, const db_op_t *_o, const db_val_t *_v, const db_key_t *_uk, const db_val_t *_uv, const int _n, const int _un) |
The function implements UPDATE SQL directive. It is possible to modify one or more rows in a table using this function.
| _h | database connection handle | |
| _k | array of keys (column names) that will be matched | |
| _o | array of operators to be used with key-value pairs | |
| _v | array of values that the row must match to be modified | |
| _uk | array of keys (column names) that will be modified | |
| _uv | new values for keys specified in _k parameter | |
| _n | number of key-value pairs in _k and _v parameters | |
| _un | number of key-value pairs in _uk and _uv parameters |
Definition at line 263 of file lib/srdb1/db.h.
| typedef int(* db_use_table_f)(db1_con_t *_h, const str *_t) |
The function db_use_table takes a table name and stores it db1_con_t structure. All subsequent operations (insert, delete, update, query) are performed on that table.
| _h | database connection handle | |
| _t | table name |
Definition at line 70 of file lib/srdb1/db.h.
| int db_api_init | ( | void | ) |
This function must be executed by DB connector modules at load time to initialize the internals of DB API library.
Definition at line 519 of file lib/srdb1/db.c.
References db_query_init().
Referenced by db_mysql_alloc_buffer().


This function is special, it's only purpose is to call find_export function in the core and find the addresses of all other database related functions. The db_func_t callback given as parameter is updated with the found addresses.
This function must be called before any other database API call!
The database URL is of the form "mysql://username:password@host:port/database" or "mysql" (database module name). In the case of a database connection URL, this function looks only at the first token (the database protocol). In the example above that would be "mysql":
| mod | database connection URL or a database module name | |
| dbf | database module callbacks |
Bind database module functions.
| mod | ||
| mydbf |
Definition at line 158 of file lib/srdb1/db.c.
References _str::len, MAX_URL_LENGTH, and _str::s.
Referenced by carrierroute_db_init(), domainpolicy_db_bind(), group_db_bind(), init_ds_db(), matrix_db_init(), mod_init(), pl_init_db(), and userblacklist_db_init().

| int db_check_table_version | ( | db_func_t * | dbf, | |
| db1_con_t * | dbh, | |||
| const str * | table, | |||
| const unsigned int | version | |||
| ) |
Small helper function to check the table version.
| dbf | database module callbacks | |
| dbh | database connection handle | |
| table | checked table | |
| version | checked version |
Definition at line 432 of file lib/srdb1/db.c.
References db_table_version(), _str::len, and _str::s.
Referenced by carrierroute_db_init(), matrix_db_init(), mod_init(), register_udomain(), and userblacklist_db_init().


| void db_do_close | ( | db1_con_t * | _h, | |
| void(*)() | free_connection | |||
| ) |
This helper method does some work for the closing of a database connection. No function should be called after this
| _h | database connection handle | |
| (*free_connection) | Pointer to the db specifc free_connection method |
Helper for db_close function.
Definition at line 342 of file lib/srdb1/db.c.
References pool_remove(), and db1_con_t::tail.
Referenced by db_mysql_close(), and db_postgres_close().


This helper method do the actual work for the database specific db_init functions.
| url | database connection URL | |
| (*new_connection)() | Pointer to the db specific connection creation method |
Helper for db_init function.
Definition at line 269 of file lib/srdb1/db.c.
References db_do_init2().
Referenced by db_mysql_init(), and db_postgres_init().


This helper method do the actual work for the database specific db_init functions.
| url | database connection URL | |
| (*new_connection)() | Pointer to the db specific connection creation method | |
| pooling | whether or not to use a pooled connection |
Helper for db_init2 function.
Definition at line 279 of file lib/srdb1/db.c.
References free_db_id(), _str::len, MAX_URL_LENGTH, new_db_id(), pool_get(), pool_insert(), _str::s, and db1_con_t::tail.
Referenced by db_do_init(), and db_postgres_init2().


Definition at line 435 of file db_query.c.
References DB_CAP_FETCH, and DB_CAPABILITY.
| int db_fetch_query | ( | db_func_t * | dbf, | |
| int | frows, | |||
| db1_con_t * | _h, | |||
| const db_key_t * | _k, | |||
| const db_op_t * | _op, | |||
| const db_val_t * | _v, | |||
| const db_key_t * | _c, | |||
| const int | _n, | |||
| const int | _nc, | |||
| const db_key_t | _o, | |||
| db1_res_t ** | _r | |||
| ) |
Definition at line 389 of file db_query.c.
References DB_CAP_FETCH, and DB_CAPABILITY.
| int db_load_bulk_data | ( | db_func_t * | binding, | |
| db1_con_t * | handle, | |||
| str * | name, | |||
| db_key_t * | cols, | |||
| unsigned int | count, | |||
| unsigned int | strict, | |||
| db1_res_t * | res | |||
| ) |
Generic query helper method for load bulk data, e.g. lcr tables
| binding | database module binding | |
| handle | database connection | |
| name | database table name | |
| cols | queried columns | |
| count | number of queried columns | |
| strict | if set to 1 an error is returned when no data could be loaded, otherwise just a warning is logged | |
| res | database result, unchanged on failure and if no data could be found |
Definition at line 474 of file lib/srdb1/db.c.
Returns the version number of a given table from the version table. Instead of this function you could also use db_check_table_version
| dbf | database module callbacks | |
| con | database connection handle | |
| table | checked table |
Get the version of a table.
| dbf | ||
| connection | ||
| table |
Definition at line 368 of file lib/srdb1/db.c.
References DB1_INT, _str::len, RES_ROW_N, RES_ROWS, ROW_VALUES, _str::s, TABLENAME_COLUMN, VAL_INT, VAL_NULL, VAL_STR, VAL_TYPE, VERSION_COLUMN, and VERSION_TABLE.
Referenced by db_check_table_version(), domainpolicy_db_ver(), init_ds_db(), and pl_init_db().

Stores the name of the table that will be used by subsequent database functions calls in a db1_con_t structure.
| _h | database connection handle | |
| _t | stored name |
Definition at line 449 of file lib/srdb1/db.c.
References CON_TABLE, and _str::s.
Referenced by db_mysql_use_table(), and db_postgres_use_table().

1.7.1