db.h

Go to the documentation of this file.
00001 #ifndef _CP_PERSISTENCE_H
00002 #define _CP_PERSISTENCE_H
00003 
00014 #include "common.h"
00015 #include "config.h"
00016 
00017 #include <stdarg.h>
00018 #ifdef CP_HAS_SYS_TIME_H
00019 #include <sys/time.h>
00020 #endif /* CP_HAS_SYS_TIME_H */
00021 
00022 #include "collection.h"
00023 #include "hashtable.h"
00024 #include "linked_list.h"
00025 #include "vector.h"
00026 #include "str.h"
00027 
00028 __BEGIN_DECLS
00029 
00030 /* ----------------------------------------------------------------------- */
00031 /*                                                                         */
00032 /*                       general framework declarations                    */
00033 /*                                                                         */
00034 /* ----------------------------------------------------------------------- */
00035 
00036 typedef CPROPS_DLL enum
00037 {
00038     CP_FIELD_TYPE_BOOLEAN,   //  0
00039     CP_FIELD_TYPE_CHAR,      //  1
00040     CP_FIELD_TYPE_SHORT,     //  2
00041     CP_FIELD_TYPE_INT,       //  3
00042     CP_FIELD_TYPE_LONG,      //  4
00043     CP_FIELD_TYPE_LONG_LONG, //  5
00044     CP_FIELD_TYPE_FLOAT,     //  6
00045     CP_FIELD_TYPE_DOUBLE,    //  7
00046     CP_FIELD_TYPE_VARCHAR,   //  8
00047     CP_FIELD_TYPE_BLOB,      //  9
00048     CP_FIELD_TYPE_DATE,      // 10
00049     CP_FIELD_TYPE_TIME,      // 11
00050     CP_FIELD_TYPE_TIMESTAMP  // 12
00051 } cp_field_type;
00052 
00054 CPROPS_DLL
00055 int cp_db_init();
00057 CPROPS_DLL
00058 int cp_db_register_dbms(char *dbms_name, void (*shutdown_call)());
00060 CPROPS_DLL
00061 int cp_db_shutdown();
00062 
00063 
00064 /* ----------------------------------------------------------------------- */
00065 /*                                                                         */
00066 /*                        cp_timestampz declarations                       */
00067 /*                                                                         */
00068 /* ----------------------------------------------------------------------- */
00069 
00070 typedef CPROPS_DLL struct _cp_timestampz
00071 {
00072     struct timeval tm;
00073     int tz_minuteswest;
00074 } cp_timestampz;
00075 
00076 CPROPS_DLL
00077 cp_timestampz *cp_timestampz_create(struct timeval *tm, int minuteswest);
00078 CPROPS_DLL
00079 cp_timestampz *
00080     cp_timestampz_create_prm(int sec_since_epoch, int microsec, int minwest);
00081 CPROPS_DLL
00082 void cp_timestampz_destroy(cp_timestampz *tm);
00083 CPROPS_DLL
00084 struct tm *cp_timestampz_localtime(cp_timestampz *tm, struct tm *res);
00085 
00086 
00087 /* ----------------------------------------------------------------------- */
00088 /*                                                                         */
00089 /*                    connection parameter declarations                    */
00090 /*                                                                         */
00091 /* ----------------------------------------------------------------------- */
00092 
00093 typedef CPROPS_DLL struct _cp_db_connection_parameters 
00094 {
00095     void *impl;
00096     cp_destructor_fn impl_dtr;
00097 } cp_db_connection_parameters; 
00101 CPROPS_DLL
00102 void cp_db_connection_parameters_destroy(cp_db_connection_parameters *prm);
00103 
00104 
00105 /* ----------------------------------------------------------------------- */
00106 /*                                                                         */
00107 /*                          connection declarations                        */
00108 /*                                                                         */
00109 /* ----------------------------------------------------------------------- */
00110 
00111 CPROPS_DLL struct _cp_data_source;
00112 
00113 typedef CPROPS_DLL struct _cp_db_connection
00114 {
00115     struct _cp_data_source *data_source;    
00116     void *connection;                       
00117     void *store;                            
00118     short autofetch_query_metadata;
00119     short read_result_set_at_once;
00120     int fetch_size;
00121     int autocommit;
00122 } cp_db_connection; 
00125 /* ----------------------------------------------------------------------- */
00126 /*                                                                         */
00127 /*                     prepared statement declarations                     */
00128 /*                                                                         */
00129 /* ----------------------------------------------------------------------- */
00130 
00131 typedef CPROPS_DLL struct _cp_db_statement
00132 {
00133     cp_db_connection *connection;
00134     int prm_count;
00135     cp_field_type *types;
00136     void *source;
00137     void *store;
00138     short binary; 
00139 } cp_db_statement;
00140 
00145 CPROPS_DLL
00146 cp_db_statement *cp_db_statement_instantiate(cp_db_connection *connection,
00147                                              int prm_count, 
00148                                              cp_field_type *types, 
00149                                              void *source);
00150 
00152 CPROPS_DLL
00153 void cp_db_statement_set_binary(cp_db_statement *stmt, int binary);
00154 
00159 CPROPS_DLL
00160 void cp_db_statement_destroy(cp_db_statement *statement);
00161 
00162 /* ----------------------------------------------------------------------- */
00163 /*                                                                         */
00164 /*                         result set declarations                         */
00165 /*                                                                         */
00166 /* ----------------------------------------------------------------------- */
00167 
00168 typedef CPROPS_DLL struct _cp_result_set
00169 {
00170     short fetch_complete;
00171     cp_db_connection *connection;
00172     void *source;                   
00173     void *store;                    
00174     cp_vector *field_headers;
00175     cp_vector *field_types;
00176     int position; 
00177     int row_count;
00178     int field_count;
00179     cp_list *results;
00180     short dispose;
00181     cp_list *dispose_list;
00182     short binary;
00183 } cp_result_set; 
00186 CPROPS_DLL
00187 cp_vector *cp_result_set_get_headers(cp_result_set *result_set);
00189 CPROPS_DLL
00190 cp_vector *cp_result_set_get_field_types(cp_result_set *result_set);
00192 CPROPS_DLL
00193 int cp_result_set_field_count(cp_result_set *result_set);
00195 CPROPS_DLL
00196 int cp_result_set_row_count(cp_result_set *result_set);
00198 CPROPS_DLL
00199 char *cp_result_set_get_header(cp_result_set *result_set, int column);
00201 CPROPS_DLL
00202 cp_field_type cp_result_set_get_field_type(cp_result_set *rs, int column);
00204 CPROPS_DLL
00205 void cp_result_set_autodispose(cp_result_set *rs, int mode);
00207 CPROPS_DLL
00208 cp_vector *cp_result_set_next(cp_result_set *result_set);
00210 CPROPS_DLL
00211 void cp_result_set_destroy(cp_result_set *result_set);
00213 CPROPS_DLL
00214 void cp_result_set_release_row(cp_result_set *result_set, cp_vector *row);
00216 CPROPS_DLL
00217 int cp_result_set_is_binary(cp_result_set *result_set);
00218 
00220 CPROPS_DLL
00221 cp_result_set *
00222     cp_db_connection_select(cp_db_connection *connection, char *query);
00224 CPROPS_DLL
00225 int cp_db_connection_update(cp_db_connection *connection, char *query);
00227 CPROPS_DLL
00228 int cp_db_connection_close(cp_db_connection *connection);
00230 CPROPS_DLL
00231 void cp_db_connection_destroy(cp_db_connection *connection);
00232 
00234 CPROPS_DLL
00235 void 
00236     cp_db_connection_set_fetch_metadata(cp_db_connection *connection, int mode);
00238 CPROPS_DLL
00239 void cp_db_connection_set_read_result_set_at_once(cp_db_connection *connection, 
00240                                                   int mode);
00242 CPROPS_DLL
00243 void cp_db_connection_set_fetch_size(cp_db_connection *connection, int fetch_size);
00245 CPROPS_DLL
00246 char *cp_db_connection_escape_string(cp_db_connection *connection, 
00247                                      char *src,
00248                                      size_t len);
00250 CPROPS_DLL
00251 char *cp_db_connection_escape_binary(cp_db_connection *connection, 
00252                                      char *src, 
00253                                      size_t src_len, 
00254                                      size_t *res_len);
00256 CPROPS_DLL
00257 cp_string *cp_db_connection_unescape_binary(cp_db_connection *connection,
00258                                             char *src);
00260 CPROPS_DLL
00261 cp_db_statement *
00262     cp_db_connection_prepare_statement(cp_db_connection *connection,
00263                                        int prm_count,
00264                                        cp_field_type *prm_types, 
00265                                        char *query);
00267 CPROPS_DLL
00268 int cp_db_connection_execute_statement(cp_db_connection *connection,
00269                                        cp_db_statement *statement, 
00270                                        cp_vector *prm,
00271                                        cp_result_set **results);
00273 CPROPS_DLL
00274 int cp_db_connection_execute_statement_args(cp_db_connection *connection,
00275                                             cp_db_statement *statement, 
00276                                             cp_result_set **results, ...);
00278 CPROPS_DLL
00279 void cp_db_connection_close_statement(cp_db_connection *connection, 
00280                                       cp_db_statement *statement);
00282 CPROPS_DLL
00283 void cp_db_connection_set_autocommit(cp_db_connection *conn, int state);
00285 CPROPS_DLL
00286 int cp_db_connection_commit(cp_db_connection *conn);
00288 CPROPS_DLL
00289 int cp_db_connection_rollback(cp_db_connection *conn);
00290 
00291 
00292 /* ----------------------------------------------------------------------- */
00293 /*                                                                         */
00294 /*                          db_actions declarations                        */
00295 /*                                                                         */
00296 /* ----------------------------------------------------------------------- */
00297 
00301 typedef cp_db_connection *(*cp_db_open_fn)(struct _cp_data_source *);
00302 
00303 
00307 typedef cp_result_set *(*cp_db_select_fn)(cp_db_connection *conn, char *query);
00308 
00313 typedef int (*cp_db_fetch_metadata_fn)(cp_result_set *rs);
00314 
00318 typedef int (*cp_db_fetch_next_fn)(cp_result_set *rs);
00319 
00323 typedef int (*cp_db_release_result_set_fn)(cp_result_set *rs);
00324 
00329 typedef int (*cp_db_update_fn)(cp_db_connection *conn, char *query);
00330 
00334 typedef int (*cp_db_close_fn)(cp_db_connection *conn);
00335 
00339 typedef char *(*cp_db_escape_string_fn)(cp_db_connection *conn, char *src, size_t len);
00340 
00345 typedef char *(*cp_db_escape_binary_fn)(cp_db_connection *conn, char *src, 
00346                                         size_t src_len, size_t *res_len);
00351 typedef cp_string *(*cp_db_unescape_binary_fn)(cp_db_connection *conn, 
00352                                                char *src);
00356 typedef cp_db_statement *
00357     (*cp_db_prepare_statement_fn)(cp_db_connection *conn, 
00358                                   int prm_count,
00359                                   cp_field_type *prm_types, 
00360                                   char *query);
00361 
00365 typedef int (*cp_db_execute_statement_fn)(cp_db_connection *conn, 
00366                                           cp_db_statement *stmt, 
00367                                           cp_result_set **results, 
00368                                           int *lengths,
00369                                           void **prm);
00374 typedef void (*cp_db_release_statement_fn)(cp_db_statement *stmt);
00375 
00379 typedef void (*cp_db_set_autocommit_fn)(cp_db_connection *conn, int state);
00380 
00384 typedef int (*cp_db_commit_fn)(cp_db_connection *conn);
00385 
00389 typedef int (*cp_db_rollback_fn)(cp_db_connection *conn);
00390 
00391 typedef CPROPS_DLL struct _cp_db_actions
00392 {
00393     int                           dbms;
00394     char                         *dbms_lit;
00395     cp_db_open_fn                 open;
00396     cp_db_select_fn               select;
00397     cp_db_fetch_next_fn           fetch_next;
00398     cp_db_fetch_metadata_fn       fetch_metadata;
00399     cp_db_release_result_set_fn   release_result_set;
00400     cp_db_update_fn               update;
00401     cp_db_close_fn                close;
00402     cp_db_escape_string_fn        escape_string;
00403     cp_db_escape_binary_fn        escape_binary;
00404     cp_db_unescape_binary_fn      unescape_binary;
00405     cp_db_prepare_statement_fn    prepare_statement;
00406     cp_db_execute_statement_fn    execute_statement;
00407     cp_db_release_statement_fn    release_statement;
00408     cp_db_set_autocommit_fn       set_autocommit;
00409     cp_db_commit_fn               commit;
00410     cp_db_rollback_fn             rollback;
00411 } cp_db_actions; 
00414 CPROPS_DLL
00415 cp_db_actions *
00416     cp_db_actions_create(int dbms,
00417                          char *dbms_lit,
00418                          cp_db_open_fn open,
00419                          cp_db_select_fn select,
00420                          cp_db_fetch_metadata_fn fetch_metadata,
00421                          cp_db_fetch_next_fn fetch_next,
00422                          cp_db_release_result_set_fn release_result_set,
00423                          cp_db_update_fn update,
00424                          cp_db_close_fn close,
00425                          cp_db_escape_string_fn escape_string,
00426                          cp_db_escape_binary_fn escape_binary,
00427                          cp_db_unescape_binary_fn unescape_binary,
00428                          cp_db_prepare_statement_fn prepare_statement,
00429                          cp_db_execute_statement_fn execute_statement,
00430                          cp_db_release_statement_fn release_statement,
00431                          cp_db_set_autocommit_fn set_autocommit,
00432                          cp_db_commit_fn commit,
00433                          cp_db_rollback_fn rollback);
00435 CPROPS_DLL
00436 void cp_db_actions_destroy(cp_db_actions *actions);
00437 
00438 
00439 /* ----------------------------------------------------------------------- */
00440 /*                                                                         */
00441 /*                      driver descriptor declarations                     */
00442 /*                                                                         */
00443 /* ----------------------------------------------------------------------- */
00444 
00445 typedef struct _cp_data_source *
00446     (*cp_db_get_data_source_fn)(char *host, int port, char *user, 
00447                                 char *passwd, char *dbname);
00448 
00449 typedef struct _cp_data_source *
00450     (*cp_db_get_data_source_prm_fn)(char *host, int port, char *user, 
00451                                     char *passwd, char *dbname, 
00452                                     cp_hashtable *prm);
00453 
00454 typedef CPROPS_DLL struct _cp_dbms_driver_descriptor
00455 {
00456     void *lib;
00457     cp_db_get_data_source_fn get_data_source;
00458     cp_db_get_data_source_prm_fn get_data_source_prm;
00459 } cp_dbms_driver_descriptor;
00460 
00461 
00462 /* ----------------------------------------------------------------------- */
00463 /*                                                                         */
00464 /*                         data source declarations                        */
00465 /*                                                                         */
00466 /* ----------------------------------------------------------------------- */
00467 
00468 typedef CPROPS_DLL struct _cp_data_source
00469 {
00470     cp_db_connection_parameters *prm;
00471     cp_db_actions               *act;
00472 } cp_data_source; 
00474 #if defined(CP_HAS_DLFCN_H) || defined(_WINDOWS)
00475 CPROPS_DLL
00476 int cp_dbms_load_driver(char *name);
00477 #endif /* CP_HAS_DLFCN_H */
00478 
00479 CPROPS_DLL
00480 cp_data_source *
00481     cp_dbms_get_data_source(char *driver, char *host, int port, char *user,
00482             char *passwd, char *dbname);
00483 
00484 CPROPS_DLL
00485 cp_data_source *
00486     cp_dbms_get_data_source_prm(char *driver, char *host, int port, 
00487             char *user, char *passwd, char *dbname, cp_hashtable *prm);
00488 
00490 CPROPS_DLL
00491 void cp_data_source_destroy(cp_data_source *data_source);
00493 CPROPS_DLL
00494 cp_db_connection *
00495     cp_data_source_get_connection(cp_data_source *data_source); 
00496 
00497 
00498 /* ----------------------------------------------------------------------- */
00499 /*                                                                         */
00500 /*                       connection pool declarations                      */
00501 /*                                                                         */
00502 /* ----------------------------------------------------------------------- */
00503 
00504 typedef CPROPS_DLL struct _cp_db_connection_pool
00505 {
00506     int running;
00507     int block;
00508 
00509     int min_size;
00510     int max_size;
00511     int size;
00512     cp_data_source *data_source;
00513     cp_list *free_list;
00514 
00515     cp_mutex *lock;
00516     cp_cond  *cond;
00517 } cp_db_connection_pool; 
00520 CPROPS_DLL
00521 cp_db_connection_pool *
00522     cp_db_connection_pool_create(cp_data_source *data_source, 
00523                                  int min_size, 
00524                                  int max_size, 
00525                                  int initial_size);
00526 
00528 CPROPS_DLL
00529 int cp_db_connection_pool_shutdown(cp_db_connection_pool *pool);
00531 CPROPS_DLL
00532 void cp_db_connection_pool_destroy(cp_db_connection_pool *pool);
00533 
00538 CPROPS_DLL
00539 void cp_db_connection_pool_set_blocking(cp_db_connection_pool *pool, int block);
00540 
00542 CPROPS_DLL
00543 cp_db_connection *
00544     cp_db_connection_pool_get_connection(cp_db_connection_pool *pool);
00545 
00547 CPROPS_DLL
00548 void cp_db_connection_pool_release_connection(cp_db_connection_pool *pool, 
00549                                               cp_db_connection *connection);
00550 
00551 __END_DECLS
00552 
00555 #endif

Generated on Mon Dec 5 23:00:21 2011 for cprops by  doxygen 1.4.7