00001 #ifndef MYDB_H
00002 #define MYDB_H
00003
00004 #include <unistd.h>
00005 #include <pthread.h>
00006 #include <sys/uio.h>
00007 #include <netinet/in.h>
00008 #include <string.h>
00009 #include <stdio.h>
00010
00011
00012
00013 #ifdef ORACLE
00014 typedef char db_char_t;
00015 typedef int db_int1_t;
00016 typedef int db_int2_t;
00017 typedef int db_int4_t;
00018 typedef int db_int8_t;
00019 typedef float db_float_t;
00020 typedef double db_double_t;
00021 typedef char * db_string_t;
00022 typedef char * db_varchar_t;
00023 #endif
00024 #ifdef MYSQL
00025 typedef char db_char_t;
00026 typedef char db_int1_t;
00027 typedef short db_int2_t;
00028 typedef int db_int4_t;
00029 typedef long long db_int8_t;
00030 typedef float db_float_t;
00031 typedef double db_double_t;
00032 typedef char * db_string_t;
00033 typedef char * db_varchar_t;
00034 #endif
00035 #ifdef POSTGRESQL
00036 typedef char db_char_t;
00037 typedef short db_int1_t;
00038 typedef short db_int2_t;
00039 typedef int db_int4_t;
00040 typedef long long db_int8_t;
00041 typedef float db_float_t;
00042 typedef double db_double_t;
00043 typedef char * db_string_t;
00044 typedef char * db_varchar_t;
00045 #endif
00046
00047
00048 #ifdef DB_COMMON_C
00049 char __db_error_message[4096]={0};
00050 #else
00051 extern char __db_error_message[4096];
00052 #endif
00053
00054 #define MAXARG 1024
00055
00056 typedef void(*db_sigblock_fn)(sigset_t *set, void *data);
00057
00058
00059 typedef enum DB_Type_enum {
00060 DB_CHAR, DB_INT1, DB_INT2, DB_INT4, DB_INT8,
00061 DB_FLOAT, DB_DOUBLE, DB_STRING, DB_VARCHAR
00062 } DB_Type_t;
00063
00064
00065 typedef struct DB_Handle_struct
00066 {
00067 void *db_connection;
00068 char dbhost[1024], dbname[1024], dbuser[1024];
00069 pthread_mutex_t *db_lock;
00070 int abort_now;
00071 unsigned int stmt_num;
00072 int isolation_level;
00073 char dbport[1024];
00074 char errmsg[4096];
00075 } DB_Handle_t;
00076
00077 static inline void DB_ResetErrmsg(DB_Handle_t *dbh)
00078 {
00079 if (dbh && *dbh->errmsg != 0)
00080 {
00081 *dbh->errmsg = '\0';
00082 }
00083 }
00084
00085 static inline void DB_SetErrmsg(DB_Handle_t *dbh, const char *msg)
00086 {
00087 if (dbh)
00088 {
00089 snprintf(dbh->errmsg, sizeof(dbh->errmsg), "%s", msg);
00090 }
00091 }
00092
00093 static inline const char *DB_GetErrmsg(DB_Handle_t *dbh)
00094 {
00095 if (dbh && *dbh->errmsg != 0)
00096 {
00097 return dbh->errmsg;
00098 }
00099
00100 return NULL;
00101 }
00102
00103
00104
00105 #define DB_TRANS_READCOMMIT 0
00106 #define DB_TRANS_SERIALIZABLE 1
00107 #define DB_TRANS_READONLY 2
00108
00109
00110
00111 #define DB_PRIV_SELECT 1
00112 #define DB_PRIV_INSERT 2
00113 #define DB_PRIV_UPDATE 4
00114
00115
00116
00117 typedef struct DB_Column_struct
00118 {
00119 char *column_name;
00120 DB_Type_t type;
00121 unsigned int num_rows;
00122 unsigned int size;
00123 char *data;
00124
00125 short *is_null;
00126
00127 } DB_Column_t;
00128
00129
00130 typedef struct DB_Binary_Result_struct
00131 {
00132 unsigned int num_rows;
00133 unsigned int num_cols;
00134 DB_Column_t *column;
00135 } DB_Binary_Result_t;
00136
00137
00138
00139 typedef struct DB_Text_Result_struct
00140 {
00141 unsigned int num_rows;
00142 unsigned int num_cols;
00143 char **column_name;
00144 int *column_width;
00145 char *buffer;
00146 char ***field;
00147
00148 } DB_Text_Result_t;
00149
00150
00151
00152
00153
00154
00155
00156 void db_set_error_message(char *err);
00157 void db_get_error_message(int maxlen, char *err);
00158
00159
00160
00161 int db_sizeof(DB_Type_t type);
00162
00163
00164 const char *db_type_string(DB_Type_t dbtype);
00165 char *db_stringtype_maxlen(int maxlen);
00166
00167
00168 DB_Handle_t *db_connect(const char *host, const char *user,
00169 const char *passwd, const char *db_name,
00170 const int lock);
00171
00172 void db_disconnect(DB_Handle_t **db);
00173
00174
00175
00176 int db_dms(DB_Handle_t *db, int *row_count, char *query_string);
00177
00178
00179 int db_dmsv(DB_Handle_t *dbin, int *row_count, char *query_string,
00180 int n_rows, ...);
00181 int db_dms_array(DB_Handle_t *dbin, int *row_count,
00182 char *oquery, int n_rows,
00183 int n, DB_Type_t *intype, void **argin );
00184
00185
00186 int db_bulk_insertv(DB_Handle_t *dbin, char *table,
00187 int n_rows, int n_cols, ...);
00188
00189 int db_bulk_insert_array(DB_Handle_t *dbin, char *table, int n_rows,
00190 int n_args, DB_Type_t *intype, void **argin );
00191
00192
00193 DB_Text_Result_t *db_query_txt(DB_Handle_t *db, char *query_string);
00194
00195
00196 DB_Text_Result_t *db_unpack_text(char *buf);
00197
00198
00199 DB_Binary_Result_t *db_query_bin(DB_Handle_t *db, char *query_string);
00200 DB_Binary_Result_t *db_query_binv(DB_Handle_t *dbin, char *query, ...);
00201 DB_Binary_Result_t *db_query_bin_array(DB_Handle_t *dbin,
00202 char *query, int n_args,
00203 DB_Type_t *intype, void **argin );
00204 DB_Binary_Result_t **db_query_bin_ntuple(DB_Handle_t *dbin, const char *stmnt, unsigned int nelems, unsigned int nargs, DB_Type_t *dbtypes, void **values);
00205
00206
00207
00208
00209
00210 int db_binary_field_is_null(DB_Binary_Result_t *res, unsigned int row,
00211 unsigned int col);
00212
00213 void db_print_binary_field_type(DB_Type_t dbtype);
00214 DB_Text_Result_t *db_binary_to_text(DB_Binary_Result_t *binres);
00215
00216
00217 char db_binary_field_getchar(DB_Binary_Result_t *res, unsigned int row,
00218 unsigned int col);
00219 int db_binary_field_getint(DB_Binary_Result_t *res, unsigned int row,
00220 unsigned int col);
00221 long long db_binary_field_getlonglong(DB_Binary_Result_t *res,
00222 unsigned int row,
00223 unsigned int col);
00224 float db_binary_field_getfloat(DB_Binary_Result_t *res, unsigned int row,
00225 unsigned int col);
00226 double db_binary_field_getdouble(DB_Binary_Result_t *res, unsigned int row,
00227 unsigned int col);
00228 void db_binary_field_getstr(DB_Binary_Result_t *res, unsigned int row,
00229 unsigned int col, int len, char *str);
00230
00231
00232 void db_print_binary_result(DB_Binary_Result_t *res);
00233 void db_print_binary_field(DB_Type_t dbtype, int width, char *data);
00234 int db_sprint_binary_field(DB_Type_t dbtype, int width, char *data, char *dst);
00235 int db_binary_default_width(DB_Type_t dbtype);
00236 void db_print_text_result(DB_Text_Result_t *res);
00237
00238
00239
00240 int db_sort_binary_result(DB_Binary_Result_t *res, int num_cols, int *cols);
00241 int db_maxbygroup(DB_Binary_Result_t *res, int maxcol, int num_cols, int *cols);
00242
00243
00244
00245 void db_free_binary_result(DB_Binary_Result_t *db_result);
00246 void db_free_text_result(DB_Text_Result_t *db_result);
00247 void db_free_binary_result_tuple(DB_Binary_Result_t ***tuple, unsigned int nelems);
00248
00249
00250
00251 long long db_sequence_getnext(DB_Handle_t *db, char *tablename);
00252 long long *db_sequence_getnext_n(DB_Handle_t *db, char *tablename, int n);
00253 long long db_sequence_getcurrent(DB_Handle_t *db, char *tablename);
00254 long long db_sequence_getlast(DB_Handle_t *db, char *tablename);
00255 int db_sequence_create(DB_Handle_t *db, char *tablename);
00256 int db_sequence_drop(DB_Handle_t *db, char *tablename);
00257
00258
00259
00260 int db_commit(DB_Handle_t *db);
00261 int db_start_transaction(DB_Handle_t *db);
00262 int db_rollback(DB_Handle_t *db);
00263 int db_cancel(DB_Handle_t *db, char *effbuf, int size);
00264 int db_settimeout(DB_Handle_t *db, unsigned int timeoutval);
00265 int db_setutf8clientencoding(DB_Handle_t *db);
00266
00267
00268
00269
00270
00271 int db_isolation_level(DB_Handle_t *dbin, int level);
00272
00273
00274
00275 void *safemalloc(size_t size);
00276 char *search_replace(const char *string, const char *search,
00277 const char *replace);
00278
00279
00280 char dbtype2char(DB_Type_t dbtype, char *data);
00281 short dbtype2short(DB_Type_t dbtype, char *data);
00282 int dbtype2int(DB_Type_t dbtype, char *data);
00283 long long dbtype2longlong(DB_Type_t dbtype, char *data);
00284 float dbtype2float(DB_Type_t dbtype, char *data);
00285 double dbtype2double(DB_Type_t dbtype, char *data);
00286 void dbtype2str(DB_Type_t dbtype, char *data, int len, char *str);
00287
00288
00289
00290 int db_getsocketbufsize(int sockfd, int *sndbuf, int *rcvbuf);
00291 int db_setsocketbufsize(int sockfd, int sndbuf, int rcvbuf);
00292 long long htonll(long long val);
00293 long long ntohll(long long val);
00294 int db_tcp_listen(char *host, int len, short *port);
00295 void send_string(int fd, const char *str);
00296 char *receive_string(int fd);
00297 void Writen(int fd, const void *ptr, size_t nbytes);
00298 void Writen_ntoh(int fd, const void *ptr, size_t size);
00299 ssize_t Readn(int fd, void *ptr, size_t nbytes);
00300 ssize_t Readn_ntoh(int fd, void *ptr, size_t size);
00301 void Write_dbtype(DB_Type_t type, char *val, int fd);
00302 void Writevn(int fd, struct iovec *vector, int count );
00303
00304 long long Readlonglong(int fd);
00305 int Readint(int fd);
00306 short Readshort(int fd);
00307 void *Read_dbtype(DB_Type_t *type, int fd);
00308 int readlonglong(int fd, long long *val);
00309 int readint(int fd, int *val);
00310 int readshort(int fd, int *val);
00311
00312 int db_send_text_result(int sockfd, DB_Text_Result_t *result, int compress);
00313 DB_Text_Result_t *db_recv_text_query(int sockfd, int compress, char **errmsg);
00314 int db_send_binary_result(int sockfd, DB_Binary_Result_t *result, int comp);
00315 DB_Binary_Result_t *db_recv_binary_query(int sockfd, int comp, char **errmsg);
00316 void db_hton(DB_Type_t dbtype, int n, void *data);
00317 #define db_ntoh(type,n,data) db_hton((type),(n),(data))
00318 void db_byteswap(DB_Type_t dbtype, int n, char *val);
00319
00320
00321 int db_server_query_txt(int sockfd, DB_Handle_t *db_handle);
00322 int db_server_query_bin(int sockfd, DB_Handle_t *db_handle);
00323 int db_server_query_bin_array(int sockfd, DB_Handle_t *db_handle);
00324 int db_server_query_bin_ntuple(int sockfd, DB_Handle_t *db_handle);
00325 int db_server_dms(int sockfd, DB_Handle_t *db_handle);
00326 int db_server_dms_array(int sockfd, DB_Handle_t *db_handle);
00327 int db_server_bulk_insert_array(int sockfd, DB_Handle_t *db_handle);
00328 int db_server_sequence_create(int sockfd, DB_Handle_t *db_handle);
00329 int db_server_sequence_drop(int sockfd, DB_Handle_t *db_handle);
00330 int db_server_sequence_getnext(int sockfd, DB_Handle_t *db_handle);
00331 int db_server_sequence_getnext_n(int sockfd, DB_Handle_t *db_handle);
00332 int db_server_sequence_getcurrent(int sockfd, DB_Handle_t *db_handle);
00333 int db_server_sequence_getlast(int sockfd, DB_Handle_t *db_handle);
00334
00335
00336 DB_Text_Result_t *db_client_query_txt(int sockfd, char *query,
00337 int compress,
00338 char **errmsg);
00339 DB_Binary_Result_t *db_client_query_bin(int sockfd, char *query,
00340 int compress,
00341 char **errmsg);
00342 DB_Binary_Result_t *db_client_query_bin_array(int sockfd, char *query,
00343 int compress, int n_args,
00344 DB_Type_t *intype, void **argin);
00345 DB_Binary_Result_t **db_client_query_bin_ntuple(int sockfd, const char *stmnt, unsigned int nexes, unsigned int nargs, DB_Type_t *dbtypes, void **values);
00346 int db_client_dmsv(int sockfd, int *row_count, char *query,
00347 int n_rows, ...);
00348 int db_client_dms_array(int sockfd, int *row_count, char *query,
00349 int n_rows, int n_args, DB_Type_t *intype,
00350 void **argin );
00351 int db_client_bulk_insert_array(int sockfd, char *table,
00352 int n_rows, int n_args, DB_Type_t *intype,
00353 void **argin );
00354 int db_client_dms(int sockfd, int *row_count, char *query);
00355 int db_client_sequence_create(int sockfd, char *table);
00356 int db_client_sequence_drop(int sockfd, char *table);
00357 long long db_client_sequence_getnext(int sockfd, char *table);
00358 long long *db_client_sequence_getnext_n(int sockfd, char *table, int n);
00359 long long db_client_sequence_getcurrent(int sockfd, char *table);
00360 long long db_client_sequence_getlast(int sockfd, char *table);
00361
00362
00363
00364 void db_lock(DB_Handle_t *h);
00365 void db_unlock(DB_Handle_t *h);
00366
00367 void db_register_sigblock(db_sigblock_fn fn, void *data);
00368
00369
00370
00371 static inline void net_packint(int val, int *buf, struct iovec *vec)
00372 {
00373 *buf=htonl((val));
00374 vec->iov_len=sizeof(int);
00375 vec->iov_base = buf;
00376 }
00377
00378 static inline void net_packlonglong(long long val, long long *buf, struct iovec *vec)
00379 {
00380 *buf=htonll((val));
00381 vec->iov_len=sizeof(long long);
00382 vec->iov_base = buf;
00383 }
00384
00385 static inline void net_packstring(char *str, int *buf, struct iovec *vec)
00386 {
00387 (vec+1)->iov_len=strlen(str);
00388 (vec+1)->iov_base = str;
00389 *buf=htonl((vec+1)->iov_len);
00390 vec->iov_len=sizeof(int);
00391 vec->iov_base = buf;
00392 }
00393
00394 static inline DB_Type_t db_binary_column_type(DB_Binary_Result_t *res,
00395 unsigned int col)
00396 {
00397 return res->column[col].type;
00398 }
00399
00400
00401 static inline char *db_binary_field_get(DB_Binary_Result_t *res,
00402 unsigned int row,
00403 unsigned int col)
00404 {
00405 if ( row<res->num_rows && col<res->num_cols )
00406 return (res->column[col].data+row*res->column[col].size);
00407 else
00408 return NULL;
00409 }
00410
00411 #ifdef ICCCOMP
00412 #pragma warning (disable : 810 1469)
00413 #endif
00414
00415 static inline void Writeshort(int fd, short val)
00416 { short tmp; tmp = htons((val)); Writen((fd), &tmp, sizeof(short)); }
00417 #ifdef ICCCOMP
00418 #pragma warning (default : 810 1469)
00419 #endif
00420
00421 static inline void Writeint(int fd, int val)
00422 { int tmp; tmp = htonl((val)); Writen((fd), &tmp, sizeof(int)); }
00423 static inline void Writelonglong(int fd, long long val)
00424 { long long tmp; tmp = htonll((val)); Writen((fd), &tmp, sizeof(long long)); }
00425
00426
00427 #define QUERY_ERROR(__query__) fprintf(stderr,"Error at %s, line %d: Query '" \
00428 "%s' failed.\n", __FILE__, __LINE__,__query__);
00429
00430 #endif
00431