db/db.h

Go to the documentation of this file.
00001 /*
00002  * $Id: db.h 5088 2008-10-17 08:01:23Z henningw $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  * Copyright (C) 2007-2008 1&1 Internet AG
00006  *
00007  * This file is part of Kamailio, a free SIP server.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License 
00020  * along with this program; if not, write to the Free Software 
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 /**
00025  * \file db/db.h
00026  * \ingroup db
00027  * \ref db.c
00028  * \brief Generic Database Interface
00029  *
00030  * This is a generic database interface for modules that need to utilize a
00031  * database. The interface should be used by all modules that access database.
00032  * The interface will be independent of the underlying database server.
00033  * Notes:
00034  * If possible, use the predefined macros if you need to access any structure
00035  * attributes.
00036  * For additional description, see the comments in the sources of mysql module.
00037  *
00038  * If you want to see more complicated examples of how the API could be used,
00039  * take a look at the sources of the usrloc or auth modules.
00040  */
00041 
00042 #ifndef DB_H
00043 #define DB_H
00044 
00045 #include "db_key.h"
00046 #include "db_op.h"
00047 #include "db_val.h"
00048 #include "db_con.h"
00049 #include "db_res.h"
00050 #include "db_cap.h"
00051 #include "db_con.h"
00052 #include "db_row.h"
00053 
00054 
00055 /**
00056  * \brief Specify table name that will be used for subsequent operations.
00057  * 
00058  * The function db_use_table takes a table name and stores it db_con_t structure.
00059  * All subsequent operations (insert, delete, update, query) are performed on
00060  * that table.
00061  * \param _h database connection handle
00062  * \param _t table name
00063  * \return returns 0 if everything is OK, otherwise returns value < 0
00064  */
00065 typedef int (*db_use_table_f)(db_con_t* _h, const str * _t);
00066 
00067 /**
00068  * \brief Initialize database connection and obtain the connection handle.
00069  *
00070  * This function initialize the database API and open a new database
00071  * connection. This function must be called after bind_dbmod but before any
00072  * other database API function is called.
00073  * 
00074  * The function takes one parameter, the parameter must contain the database
00075  * connection URL. The URL is of the form 
00076  * mysql://username:password\@host:port/database where:
00077  * 
00078  * username: Username to use when logging into database (optional).
00079  * password: password if it was set (optional)
00080  * host:     Hosname or IP address of the host where database server lives (mandatory)
00081  * port:     Port number of the server if the port differs from default value (optional)
00082  * database: If the database server supports multiple databases, you must specify the
00083  * name of the database (optional).
00084  * \see bind_dbmod
00085  * \param _sqlurl database connection URL
00086  * \return returns a pointer to the db_con_t representing the connection if it was
00087  * successful, otherwise 0 is returned
00088  */
00089 typedef db_con_t* (*db_init_f) (const str* _sqlurl);
00090 
00091 /**
00092  * \brief Close a database connection and free all memory used.
00093  *
00094  * The function closes previously open connection and frees all previously 
00095  * allocated memory. The function db_close must be the very last function called.
00096  * \param _h db_con_t structure representing the database connection
00097  */
00098 typedef void (*db_close_f) (db_con_t* _h); 
00099 
00100 
00101 /**
00102  * \brief Query table for specified rows.
00103  *
00104  * This function implements the SELECT SQL directive.
00105  * If _k and _v parameters are NULL and _n is zero, you will get the whole table.
00106  *
00107  * if _c is NULL and _nc is zero, you will get all table columns in the result.
00108  * _r will point to a dynamically allocated structure, it is neccessary to call
00109  * db_free_result function once you are finished with the result.
00110  *
00111  * If _op is 0, equal (=) will be used for all key-value pairs comparisons.
00112  *
00113  * Strings in the result are not duplicated, they will be discarded if you call
00114  * db_free_result, make a copy yourself if you need to keep it after db_free_result.
00115  *
00116  * You must call db_free_result before you can call db_query again!
00117  * \see db_free_result
00118  *
00119  * \param _h database connection handle
00120  * \param _k array of column names that will be compared and their values must match
00121  * \param _op array of operators to be used with key-value pairs
00122  * \param _v array of values, columns specified in _k parameter must match these values
00123  * \param _c array of column names that you are interested in
00124  * \param _n number of key-value pairs to match in _k and _v parameters
00125  * \param _nc number of columns in _c parameter
00126  * \param _o order by statement for query
00127  * \param _r address of variable where pointer to the result will be stored
00128  * \return returns 0 if everything is OK, otherwise returns value < 0
00129  */
00130 typedef int (*db_query_f) (const db_con_t* _h, const db_key_t* _k, const db_op_t* _op,
00131             const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
00132             const db_key_t _o, db_res_t** _r);
00133 
00134 /**
00135  * \brief Gets a partial result set, fetch rows from a result
00136  *
00137  * Gets a partial result set, fetch a number of rows from a database result.
00138  * This function initialize the given result structure on the first run, and
00139  * fetches the nrows number of rows. On subsequenting runs, it uses the
00140  * existing result and fetches more rows, until it reaches the end of the
00141  * result set. Because of this the result needs to be null in the first
00142  * invocation of the function. If the number of wanted rows is zero, the
00143  * function returns anything with a result of zero.
00144  * \param _h structure representing database connection
00145  * \param _r structure for the result
00146  * \param _n the number of rows that should be fetched
00147  * \return returns 0 if everything is OK, otherwise returns value < 0
00148  */
00149 typedef int (*db_fetch_result_f) (const db_con_t* _h, db_res_t** _r, const int _n);
00150 
00151 
00152 /**
00153  * \brief Raw SQL query.
00154  *
00155  * This function can be used to do database specific queries. Please
00156  * use this function only if needed, as this creates portability issues
00157  * for the different databases. Also keep in mind that you need to
00158  * escape all external data sources that you use. You could use the
00159  * escape_common and unescape_common functions in the core for this task.
00160  * \see escape_common
00161  * \see unescape_common
00162  * \param _h structure representing database connection
00163  * \param _s the SQL query
00164  * \param _r structure for the result
00165  * \return returns 0 if everything is OK, otherwise returns value < 0
00166  */
00167 typedef int (*db_raw_query_f) (const db_con_t* _h, const str* _s, db_res_t** _r);
00168 
00169 
00170 /**
00171  * \brief Free a result allocated by db_query.
00172  *
00173  * This function frees all memory allocated previously in db_query. Its
00174  * neccessary to call this function on a db_res_t structure if you don't need the
00175  * structure anymore. You must call this function before you call db_query again!
00176  * \param _h database connection handle
00177  * \param _r pointer to db_res_t structure to destroy
00178  * \return returns 0 if everything is OK, otherwise returns value < 0
00179  */
00180 typedef int (*db_free_result_f) (db_con_t* _h, db_res_t* _r);
00181 
00182 
00183 /**
00184  * \brief Insert a row into the specified table.
00185  * 
00186  * This function implements INSERT SQL directive, you can insert one or more
00187  * rows in a table using this function.
00188  * \param _h database connection handle
00189  * \param _k array of keys (column names) 
00190  * \param _v array of values for keys specified in _k parameter
00191  * \param _n number of keys-value pairs int _k and _v parameters
00192  * \return returns 0 if everything is OK, otherwise returns value < 0
00193  */
00194 typedef int (*db_insert_f) (const db_con_t* _h, const db_key_t* _k,
00195             const db_val_t* _v, const int _n);
00196 
00197 
00198 /**
00199  * \brief Delete a row from the specified table.
00200  *
00201  * This function implements DELETE SQL directive, it is possible to delete one or
00202  * more rows from a table.
00203  * If _k is NULL and _v is NULL and _n is zero, all rows are deleted, the
00204  * resulting table will be empty.
00205  * If _o is NULL, the equal operator "=" will be used for the comparison.
00206  * 
00207  * \param _h database connection handle
00208  * \param _k array of keys (column names) that will be matched
00209  * \param _o array of operators to be used with key-value pairs
00210  * \param _v array of values that the row must match to be deleted
00211  * \param _n number of keys-value parameters in _k and _v parameters
00212  * \return returns 0 if everything is OK, otherwise returns value < 0
00213  */
00214 typedef int (*db_delete_f) (const db_con_t* _h, const db_key_t* _k, const db_op_t* _o,
00215             const db_val_t* _v, const int _n);
00216 
00217 
00218 /**
00219  * \brief Update some rows in the specified table.
00220  *
00221  * The function implements UPDATE SQL directive. It is possible to modify one
00222  * or more rows in a table using this function.
00223  * \param _h database connection handle
00224  * \param _k array of keys (column names) that will be matched
00225  * \param _o array of operators to be used with key-value pairs
00226  * \param _v array of values that the row must match to be modified
00227  * \param _uk array of keys (column names) that will be modified
00228  * \param _uv new values for keys specified in _k parameter
00229  * \param _n number of key-value pairs in _k and _v parameters
00230  * \param _un number of key-value pairs in _uk and _uv parameters
00231  * \return returns 0 if everything is OK, otherwise returns value < 0
00232  */
00233 typedef int (*db_update_f) (const db_con_t* _h, const db_key_t* _k, const db_op_t* _o,
00234             const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv,
00235             const int _n, const int _un);
00236 
00237 
00238 /**
00239  * \brief Insert a row and replace if one already exists.
00240  *
00241  * The function implements the REPLACE SQL directive. It is possible to insert
00242  * a row and replace if one already exists. The old row will be deleted before
00243  * the insertion of the new data.
00244  * \param _h structure representing database connection
00245  * \param _k key names
00246  * \param _v values of the keys
00247  * \param _n number of key=value pairs
00248  * \return returns 0 if everything is OK, otherwise returns value < 0
00249 */
00250 typedef int (*db_replace_f) (const db_con_t* handle, const db_key_t* keys,
00251             const db_val_t* vals, const int n);
00252 
00253 
00254 /**
00255  * \brief Retrieve the last inserted ID in a table.
00256  *
00257  * The function returns the value generated for an AUTO_INCREMENT column by the
00258  * previous INSERT or UPDATE  statement. Use this function after you have 
00259  * performed an INSERT statement into a table that contains an AUTO_INCREMENT
00260  * field.
00261  * \param _h structure representing database connection
00262  * \return returns the ID as integer or returns 0 if the previous statement
00263  * does not use an AUTO_INCREMENT value.
00264  */
00265 typedef int (*db_last_inserted_id_f) (const db_con_t* _h);
00266 
00267 
00268 /**
00269  * \brief Insert a row into specified table, update on duplicate key.
00270  * 
00271  * The function implements the INSERT ON DUPLICATE KEY UPDATE SQL directive.
00272  * It is possible to insert a row and update if one already exists.
00273  * The old row will not deleted before the insertion of the new data.
00274  * \param _h structure representing database connection
00275  * \param _k key names
00276  * \param _v values of the keys
00277  * \param _n number of key=value pairs
00278  * \return returns 0 if everything is OK, otherwise returns value < 0
00279  */
00280 typedef int (*db_insert_update_f) (const db_con_t* _h, const db_key_t* _k,
00281             const db_val_t* _v, const int _n);
00282 
00283 
00284 /**
00285  * \brief Database module callbacks
00286  * 
00287  * This structure holds function pointer to all database functions. Before this
00288  * structure can be used it must be initialized with bind_dbmod.
00289  * \see bind_dbmod
00290  */
00291 typedef struct db_func {
00292    unsigned int      cap;           /* Capability vector of the database transport */
00293    db_use_table_f    use_table;     /* Specify table name */
00294    db_init_f         init;          /* Initialize database connection */
00295    db_close_f        close;         /* Close database connection */
00296    db_query_f        query;         /* query a table */
00297    db_fetch_result_f fetch_result;  /* fetch result */
00298    db_raw_query_f    raw_query;     /* Raw query - SQL */
00299    db_free_result_f  free_result;   /* Free a query result */
00300    db_insert_f       insert;        /* Insert into table */
00301    db_delete_f       delete;        /* Delete from table */ 
00302    db_update_f       update;        /* Update table */
00303    db_replace_f      replace;       /* Replace row in a table */
00304    db_last_inserted_id_f  last_inserted_id;  /* Retrieve the last inserted ID
00305                                                in a table */
00306    db_insert_update_f insert_update; /* Insert into table, update on duplicate key */ 
00307 } db_func_t;
00308 
00309 
00310 /**
00311  * \brief Bind database module functions
00312  *
00313  * This function is special, it's only purpose is to call find_export function in
00314  * the core and find the addresses of all other database related functions. The 
00315  * db_func_t callback given as parameter is updated with the found addresses.
00316  *
00317  * This function must be called before any other database API call!
00318  *
00319  * The database URL is of the form "mysql://username:password@host:port/database" or
00320  * "mysql" (database module name).
00321  * In the case of a database connection URL, this function looks only at the first
00322  * token (the database protocol). In the example above that would be "mysql":
00323  * \see db_func_t
00324  * \param mod database connection URL or a database module name
00325  * \param dbf database module callbacks
00326  * \return returns 0 if everything is OK, otherwise returns value < 0
00327  */
00328 int db_bind_mod(const str* mod, db_func_t* dbf);
00329 
00330 
00331 /**
00332  * \brief Helper for db_init function.
00333  *
00334  * This helper method do the actual work for the database specific db_init
00335  * functions.
00336  * \param url database connection URL
00337  * \param (*new_connection)() Pointer to the db specific connection creation method
00338  * \return returns a pointer to the db_con_t representing the connection if it was
00339    successful, otherwise 0 is returned.
00340  */
00341 db_con_t* db_do_init(const str* url, void* (*new_connection)());
00342 
00343 
00344 /**
00345  * \brief Helper for db_close function.
00346  *
00347  * This helper method does some work for the closing of a database 
00348  * connection. No function should be called after this
00349  * \param _h database connection handle
00350  * \param (*free_connection) Pointer to the db specifc free_connection method
00351  */
00352 void db_do_close(db_con_t* _h, void (*free_connection)());
00353 
00354 
00355 /**
00356  * \brief Get the version of a table.
00357  *
00358  * Returns the version number of a given table from the version table.
00359  * Instead of this function you could also use db_check_table_version
00360  * \param dbf database module callbacks
00361  * \param con database connection handle
00362  * \param table checked table
00363  * \return the version number if present, 0 if no version data available, < 0 on error
00364  */
00365 int db_table_version(const db_func_t* dbf, db_con_t* con, const str* table);
00366 
00367 /**
00368  * \brief Check the table version
00369  *
00370  * Small helper function to check the table version.
00371  * \param dbf database module callbacks
00372  * \param dbh database connection handle
00373  * \param table checked table
00374  * \param version checked version
00375  * \return 0 means ok, -1 means an error occured
00376  */
00377 int db_check_table_version(db_func_t* dbf, db_con_t* dbh, const str* table, const unsigned int version);
00378 
00379 /**
00380  * \brief Stores the name of a table.
00381  *
00382  * Stores the name of the table that will be used by subsequent database
00383  * functions calls in a db_con_t structure.
00384  * \param _h database connection handle
00385  * \param _t stored name
00386  * \return 0 if everything is ok, otherwise returns value < 0
00387  */
00388 int db_use_table(db_con_t* _h, const str* _t);
00389 
00390 /**
00391  * \brief Bind the DB API exported by a module.
00392  *
00393  * The function links the functions implemented by the module to the members
00394  * of db_func_t structure
00395  * \param dbb db_func_t structure representing the variable where to bind
00396  * \return 0 if everything is ok, otherwise returns -1
00397  */
00398 
00399 typedef int (*db_bind_api_f)(db_func_t *dbb);
00400 
00401 
00402 #endif /* DB_H */

Generated on Mon May 21 18:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6