db/example/dbexample.c
7cbedd3b
 /*
  * $Id$
7dd0b342
  *
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
  * This file is part of ser, a free SIP server.
  *
  * ser is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
7cbedd3b
  */
 
7dd0b342
 
29cbdf77
 #include "../../sr_module.h"
 #include <stdio.h>
 #include "../../db/db.h"
 
 
3ee34899
 #define DB_URL   "mysql://root@localhost/ser"
29cbdf77
 #define DB_TABLE "location"
 
 #define TRUE 1
 #define FALSE 0
 
 
 /*
53c7e0f1
  * Database module client example
29cbdf77
  */
 
 
 static struct module_exports dbex_exports= {	
 	"DBExample", 
 	(char*[]) {
 	},
 	(cmd_function[]) {
 	},
 	(int[]) {
 	},
 	(fixup_function[]) {
 	},
 	0,       /* number of functions*/
 	NULL,    /* Module parameter names */
 	NULL,    /* Module parameter types */
 	NULL,    /* Module parameter variable pointers */
 	0,       /* Number of module parameters */
 	0,       /* response function*/
 	0        /* destroy function */
 };
 
 
 static int print_res(db_res_t* _r)
 {
 	int i, j;
 
 	for(i = 0; i < RES_COL_N(_r); i++) {
 		printf("%s ", RES_NAMES(_r)[i]);
 	}
 	printf("\n");
 
 	for(i = 0; i < RES_ROW_N(_r); i++) {
 		for(j = 0; j < RES_COL_N(_r); j++) {
 			if (RES_ROWS(_r)[i].values[j].nul == TRUE) {
 				printf("NULL ");
 				continue;
 			}
 			switch(RES_ROWS(_r)[i].values[j].type) {
 			case DB_INT:
 				printf("%d ", RES_ROWS(_r)[i].values[j].val.int_val);
 				break;
 			case DB_DOUBLE:
 				printf("%f ", RES_ROWS(_r)[i].values[j].val.double_val);
 				break;
 			case DB_DATETIME:
 				printf("%s ", ctime(&(RES_ROWS(_r)[i].values[j].val.time_val)));
 				break;
 			case DB_STRING:
 				printf("%s ", RES_ROWS(_r)[i].values[j].val.string_val);
225486fc
 				break;
 			case DB_STR:
 				printf("%.*s ", 
 				       RES_ROWS(_r)[i].values[j].val.str_val.len,
 				       RES_ROWS(_r)[i].values[j].val.str_val.s);
 				break;
18029bcb
 
 			case DB_BLOB:
 				printf("%.*s ",
 				       RES_ROWS(_r)[i].values[j].val.blob_val.len,
 				       RES_ROWS(_r)[i].values[j].val.blob_val.s);
 				break;
8d63acb7
 
 			case DB_BITMAP:
 				printf("%d ", RES_ROWS(_r)[i].values[j].val.bitmap_val);
 				break;
29cbdf77
 			}
 			
 		}
 		printf("\n");
 	}
 			
 	return TRUE;
 }
 
 
 
 
 
 struct module_exports* mod_register()
 {
 	     /*
 	      * Column names of table location
 	      */
8d63acb7
 	db_key_t keys1[] = {"username", "contact", "q", "expire", "opaque", "flags" };
41b502ec
 	db_key_t keys2[] = {"username", "q"};
 	db_key_t keys3[] = {"username", "contact"};
29cbdf77
 	db_key_t keys4[] = {"contact", "q"};
 
 	db_val_t vals1[] = { 
225486fc
 		{ DB_STRING  , 0, { .string_val = "foo@bar.com" }              },
 		{ DB_STR     , 0, { .str_val    = { "real@foo.bar.com", 18 } } },
 		{ DB_DOUBLE  , 0, { .double_val = 1.2 }                        },
2526b368
 		{ DB_DATETIME, 0, { .time_val   = 439826493 }                  },
8d63acb7
 		{ DB_BLOB    , 0, { .blob_val   = { "hdslgkhas\0glksf", 17 } } },
 		{ DB_BITMAP  , 0, { .bitmap_val = FLAG_NAT | FLAG_INVITE }     }
29cbdf77
 	};
 
 	db_val_t vals2[] = { 
225486fc
 		{ DB_STRING  , 0, { .string_val = "foo2@bar2.com" }              },
 		{ DB_STR     , 0, { .str_val    = { "real2@foo.bar2.com", 18 } } },
 		{ DB_DOUBLE  , 0, { .double_val = 1.3 }                          },
2526b368
 		{ DB_DATETIME, 0, { .time_val   = 12345 }                        },
8d63acb7
 		{ DB_BLOB    , 0, { .blob_val   = { "\0a\0balkdfj", 10 }       } },
                 { DB_BITMAP  , 0, { .bitmap_val = FLAG_NAT, FLAG_NOT_INVITE }    }
29cbdf77
 	};
 
 	db_val_t vals3[] = { 
225486fc
 		{ DB_STRING  , 0, { .string_val = "foo3@bar3.com" }              },
 		{ DB_STR     , 0, { .str_val    = { "real3@foo.bar3.com", 18 } } },
 		{ DB_DOUBLE  , 0, { .double_val = 1.5 }                          },
2526b368
 		{ DB_DATETIME, 0, { .time_val   = 123456 }                       },
8d63acb7
 		{ DB_BLOB    , 0, { .blob_val   = { "halgkasdg\'", 10 }        } },
                 { DB_BITMAP  , 0, { .blob_val   = FLAG_NAT }                     }
29cbdf77
 	};
 
 	db_val_t vals4[] = {
 		{ DB_STRING, 0, { .string_val = "foo2@bar2.com" } },
 		{ DB_DOUBLE, 0, { .double_val = 1.30 }            }
 	};
 		  
 	db_val_t vals5[] = {
 		{ DB_STRING, 0, { .string_val = "foo3@bar3.com" }      },
 		{ DB_STRING, 0, { .string_val = "real3@foo.bar3.com" } }
 	};
 
 	db_val_t vals6[] = {
 		{ DB_STRING, 0, { .string_val = "different@address.com" } },
 		{ DB_DOUBLE, 0, { .double_val = 2.5 }                     }
 	};
 
 	db_con_t* h;
 	db_res_t* res;
 
 	fprintf(stderr, "DBExample - registering...\n");
 
 	     /* The first call must be bind_dbmod
 	      * This call will search for functions
 	      * exported by a database module
 	      */
 	if (bind_dbmod()) {
 		fprintf(stderr, "Error while binding database module, did you forget to load a database module ?\n");
 		return &dbex_exports;
 	}
 
 	     /*
 	      * Create a database connection
 	      * DB_URL is database URL of form
3ee34899
 	      * mysql://user:password@host:port/database
29cbdf77
 	      * The function returns handle, that
 	      * represents a database connection
 	      */
 	h = db_init(DB_URL);
 	if (!h) {
 		fprintf(stderr, "Error while initializing database connection\n");
 		return &dbex_exports;
 	}
 
 	     /* 
 	      * Specify a table name, that will
 	      * be used for manipulations
 	      */
18029bcb
 	if (db_use_table(h, DB_TABLE) < 0) {
29cbdf77
 		fprintf(stderr, "Error while calling db_use_table\n");
 		return &dbex_exports;
 	}
 
 	     /* If you do not specify any keys and values to be
 	      * matched, all rows will be deleted
 	      */
829a01a4
 	if (db_delete(h, NULL, NULL, NULL, 0) < 0) {
29cbdf77
 		fprintf(stderr, "Error while flushing table\n");
 		return &dbex_exports;
 	}
 
8d63acb7
 	if (db_insert(h, keys1, vals1, 6) < 0) {
29cbdf77
 		fprintf(stderr, "Error while inserting line 1\n");
 		return &dbex_exports;
 	}
 
8d63acb7
 	if (db_insert(h, keys1, vals2, 6) < 0) {
29cbdf77
 		fprintf(stderr, "Error while inserting line 2\n");
 		return &dbex_exports;
 	}
 
8d63acb7
 	if (db_insert(h, keys1, vals3, 6) < 0) {
29cbdf77
 		fprintf(stderr, "Error while inserting line 3\n");
 		return &dbex_exports;
 	}
 
 	     /*
 	      * Let's delete middle line with
 	      * user = foo2@bar2.com and q = 1.3
 	      */
829a01a4
 	if (db_delete(h, keys2, NULL, vals4, 2) < 0) {
29cbdf77
 		fprintf(stderr, "Error while deleting line\n");
 		return &dbex_exports;
 	}
 
 	     /*
 	      * Modify last line
 	      */
829a01a4
 	if (db_update(h, keys3, NULL, vals5, keys4, vals6, 2, 2) < 0) {
29cbdf77
 		fprintf(stderr, "Error while modifying table\n");
 		return &dbex_exports;
 	}
 
 	     /*
 	      * Last but not least, dump the result of db_query
 	      */
 
829a01a4
 	if (db_query(h, NULL, NULL, NULL, NULL, 0, 0, NULL, &res) < 0) {
29cbdf77
 		fprintf(stderr, "Error while querying table\n");
 		return &dbex_exports;
 	}
 
 
 	print_res(res);
 
 	     /*
 	      * Free the result because we don't need it
 	      * anymore
 	      */
da8d3331
 	if (db_free_result(h, res) < 0) {
29cbdf77
 		fprintf(stderr, "Error while freeing result of query\n");
 		return &dbex_exports;
 	}
 
 	     /*
 	      * Close existing database connection
 	      * and free previously allocated 
 	      * memory
 	      */
 	db_close(h);
 	return &dbex_exports;
 }