dbtex.c

00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of ser, a free SIP server.
00007  *
00008  * ser is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * ser is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License 
00024  * along with this program; if not, write to the Free Software 
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00027 
00028 
00029 #include "../../sr_module.h"
00030 #include <stdio.h>
00031 #include "../../db/db.h"
00032 
00033 /*
00034  * database URL - the path to the directory where the tables are located
00035  */
00036 #define DB_URL   "/tmp/dbtext"
00037 
00041 #define DB_TABLE "location"
00042 
00043 #define TRUE 1
00044 #define FALSE 0
00045 
00046 
00047 /*
00048  * Database module client example
00049  */
00050 
00051 static int mod_init();
00052 
00053 struct module_exports exports= {        
00054         "DBTExample", 
00055         (char*[]) {
00056         },
00057         (cmd_function[]) {
00058         },
00059         (int[]) {
00060         },
00061         (fixup_function[]) {
00062         },
00063         0,       /* number of functions*/
00064         0,   /* Module parameter names */
00065         0,   /* Module parameter types */
00066         0,   /* Module parameter variable pointers */
00067         0,   /* Number of module parameters */
00068 
00069         mod_init, /* module initialization function */
00070         0,        /* response function*/
00071         0,        /* destroy function */
00072         0,        /* oncancel function */
00073         0         /* per-child init function */
00074 };
00075 
00076 
00077 static int print_res(db_res_t* _r)
00078 {
00079         int i, j;
00080 
00081         for(i = 0; i < RES_COL_N(_r); i++) {
00082                 printf("%s ", RES_NAMES(_r)[i]);
00083         }
00084         printf("\n");
00085 
00086         for(i = 0; i < RES_ROW_N(_r); i++) {
00087                 for(j = 0; j < RES_COL_N(_r); j++) {
00088                         if (RES_ROWS(_r)[i].values[j].nul == TRUE) {
00089                                 printf("NULL ");
00090                                 continue;
00091                         }
00092                         switch(RES_ROWS(_r)[i].values[j].type) {
00093                         case DB_INT:
00094                                 printf("%d ", RES_ROWS(_r)[i].values[j].val.int_val);
00095                                 break;
00096                         case DB_FLOAT:
00097                                 printf("%f ", RES_ROWS(_r)[i].values[j].val.float_val);
00098                                 break;
00099                         case DB_DOUBLE:
00100                                 printf("%f ", RES_ROWS(_r)[i].values[j].val.double_val);
00101                                 break;
00102                         case DB_DATETIME:
00103                                 printf("%s ", ctime(&(RES_ROWS(_r)[i].values[j].val.time_val)));
00104                                 break;
00105                         case DB_STRING:
00106                                 printf("%s ", RES_ROWS(_r)[i].values[j].val.string_val);
00107                                 break;
00108                         case DB_STR:
00109                                 printf("%.*s ", 
00110                                        RES_ROWS(_r)[i].values[j].val.str_val.len,
00111                                        RES_ROWS(_r)[i].values[j].val.str_val.s);
00112                                 break;
00113 
00114                         case DB_BLOB:
00115                                 printf("%.*s ",
00116                                        RES_ROWS(_r)[i].values[j].val.blob_val.len,
00117                                        RES_ROWS(_r)[i].values[j].val.blob_val.s);
00118                                 break;
00119                         }
00120                         
00121                 }
00122                 printf("\n");
00123         }
00124                         
00125         return TRUE;
00126 }
00127 
00128 
00129 
00130 
00131 
00132 int mod_init()
00133 {
00134              /*
00135               * Column names of table location
00136               */
00137         db_key_t keys1[] = {"user", "contact", "q", "expire", "opaque" };
00138         db_key_t keys2[] = {"user", "q"};
00139         db_key_t keys3[] = {"user", "contact"};
00140         db_key_t keys4[] = {"contact", "q"};
00141 
00142         db_val_t vals1[] = { 
00143                 { DB_STRING  , 0, { .string_val = "foo@bar.com" }              },
00144                 { DB_STR     , 0, { .str_val    = { "real@foo.bar.com", 18 } } },
00145                 { DB_DOUBLE  , 0, { .double_val = 1.2 }                        },
00146                 { DB_DATETIME, 0, { .time_val   = 439826493 }                  },
00147                 { DB_BLOB    , 0, { .blob_val   = { "hdslgkhas\0glksf", 17 } } }
00148         };
00149 
00150         db_val_t vals2[] = { 
00151                 { DB_STRING  , 0, { .string_val = "foo2@bar2.com" }              },
00152                 { DB_STR     , 0, { .str_val    = { "real2@foo.bar2.com", 18 } } },
00153                 { DB_DOUBLE  , 0, { .double_val = 1.3 }                          },
00154                 { DB_DATETIME, 0, { .time_val   = 12345 }                        },
00155                 { DB_BLOB    , 0, { .blob_val   = { "\0a\0balkdfj", 10 }       } }
00156         };
00157 
00158         db_val_t vals3[] = { 
00159                 { DB_STRING  , 0, { .string_val = "foo3@bar3.com" }              },
00160                 { DB_STR     , 0, { .str_val    = { "real3@foo.bar3.com", 18 } } },
00161                 { DB_DOUBLE  , 0, { .double_val = 1.5 }                          },
00162                 { DB_DATETIME, 0, { .time_val   = 123456 }                       },
00163                 { DB_BLOB    , 0, { .blob_val   = { "halgkasdg\'", 10 }        } }
00164         };
00165 
00166         db_val_t vals4[] = {
00167                 { DB_STRING, 0, { .string_val = "foo2@bar2.com" } },
00168                 { DB_DOUBLE, 0, { .double_val = 1.30 }            }
00169         };
00170                   
00171         db_val_t vals5[] = {
00172                 { DB_STRING, 0, { .string_val = "foo3@bar3.com" }      },
00173                 { DB_STRING, 0, { .string_val = "real3@foo.bar3.com" } }
00174         };
00175 
00176         db_val_t vals6[] = {
00177                 { DB_STRING, 0, { .string_val = "different@address.com" } },
00178                 { DB_DOUBLE, 0, { .double_val = 2.5 }                     }
00179         };
00180 
00181         db_con_t* h;
00182         db_res_t* res;
00183 
00184         fprintf(stderr, "DBExample - registering...\n");
00185 
00186              /* The first call must be bind_dbmod
00187               * This call will search for functions
00188               * exported by a database module
00189               */
00190         if (bind_dbmod()) {
00191                 fprintf(stderr, "Error while binding database module, did you forget to load a database module ?\n");
00192                 return -1;
00193         }
00194 
00195              /*
00196               * Create a database connection
00197               * DB_URL is database URL of form
00198               * /path/to/dbtext/database
00199               * The function returns handle, that
00200               * represents a database connection
00201               */
00202         h = db_init(DB_URL);
00203         if (!h) {
00204                 fprintf(stderr, "Error while initializing database connection\n");
00205                 return -1;
00206         }
00207 
00208              /* 
00209               * Specify a table name, that will
00210               * be used for manipulations
00211               */
00212         if (db_use_table(h, DB_TABLE) < 0) {
00213                 fprintf(stderr, "Error while calling db_use_table\n");
00214                 return -1;
00215         }
00216 
00217              /* If you do not specify any keys and values to be
00218               * matched, all rows will be deleted
00219               */
00220         if (db_delete(h, NULL, NULL, NULL, 0) < 0) {
00221                 fprintf(stderr, "Error while flushing table\n");
00222                 return -1;
00223         }
00224 
00225         if (db_insert(h, keys1, vals1, 5) < 0) {
00226                 fprintf(stderr, "Error while inserting line 1\n");
00227                 return -1;
00228         }
00229 
00230         if (db_insert(h, keys1, vals2, 5) < 0) {
00231                 fprintf(stderr, "Error while inserting line 2\n");
00232                 return -1;
00233         }
00234 
00235         if (db_insert(h, keys1, vals3, 5) < 0) {
00236                 fprintf(stderr, "Error while inserting line 3\n");
00237                 return -1;
00238         }
00239 
00240              /*
00241               * Let's delete middle line with
00242               * user = foo2@bar2.com and q = 1.3
00243               */
00244         if (db_delete(h, keys2, NULL, vals4, 2) < 0) {
00245                 fprintf(stderr, "Error while deleting line\n");
00246                 return -1;
00247         }
00248 
00249              /*
00250               * Modify last line
00251               */
00252         if (db_update(h, keys3, NULL, vals5, keys4, vals6, 2, 2) < 0) {
00253                 fprintf(stderr, "Error while modifying table\n");
00254                 return -1;
00255         }
00256 
00257              /*
00258               * Last but not least, dump the result of db_query
00259               */
00260 
00261         if (db_query(h, NULL, NULL, NULL, NULL, 0, 0, NULL, &res) < 0) {
00262                 fprintf(stderr, "Error while querying table\n");
00263                 return -1;
00264         }
00265 
00266 
00267         print_res(res);
00268 
00269              /*
00270               * Free the result because we don't need it
00271               * anymore
00272               */
00273         if (db_free_query(h, res) < 0) {
00274                 fprintf(stderr, "Error while freeing result of query\n");
00275                 return -1;
00276         }
00277 
00278              /*
00279               * Close existing database connection
00280               * and free previously allocated 
00281               * memory
00282               */
00283         db_close(h);
00284         return 0;
00285 }