dbexample.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 Fhg Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include "../../sr_module.h"
  28. #include <stdio.h>
  29. #include "../../db/db.h"
  30. #define DB_URL "sql://root@localhost/ser"
  31. #define DB_TABLE "location"
  32. #define TRUE 1
  33. #define FALSE 0
  34. /*
  35. * Dabase module client example
  36. */
  37. static struct module_exports dbex_exports= {
  38. "DBExample",
  39. (char*[]) {
  40. },
  41. (cmd_function[]) {
  42. },
  43. (int[]) {
  44. },
  45. (fixup_function[]) {
  46. },
  47. 0, /* number of functions*/
  48. NULL, /* Module parameter names */
  49. NULL, /* Module parameter types */
  50. NULL, /* Module parameter variable pointers */
  51. 0, /* Number of module parameters */
  52. 0, /* response function*/
  53. 0 /* destroy function */
  54. };
  55. static int print_res(db_res_t* _r)
  56. {
  57. int i, j;
  58. for(i = 0; i < RES_COL_N(_r); i++) {
  59. printf("%s ", RES_NAMES(_r)[i]);
  60. }
  61. printf("\n");
  62. for(i = 0; i < RES_ROW_N(_r); i++) {
  63. for(j = 0; j < RES_COL_N(_r); j++) {
  64. if (RES_ROWS(_r)[i].values[j].nul == TRUE) {
  65. printf("NULL ");
  66. continue;
  67. }
  68. switch(RES_ROWS(_r)[i].values[j].type) {
  69. case DB_INT:
  70. printf("%d ", RES_ROWS(_r)[i].values[j].val.int_val);
  71. break;
  72. case DB_DOUBLE:
  73. printf("%f ", RES_ROWS(_r)[i].values[j].val.double_val);
  74. break;
  75. case DB_DATETIME:
  76. printf("%s ", ctime(&(RES_ROWS(_r)[i].values[j].val.time_val)));
  77. break;
  78. case DB_STRING:
  79. printf("%s ", RES_ROWS(_r)[i].values[j].val.string_val);
  80. break;
  81. case DB_STR:
  82. printf("%.*s ",
  83. RES_ROWS(_r)[i].values[j].val.str_val.len,
  84. RES_ROWS(_r)[i].values[j].val.str_val.s);
  85. break;
  86. case DB_BLOB:
  87. printf("%.*s ",
  88. RES_ROWS(_r)[i].values[j].val.blob_val.len,
  89. RES_ROWS(_r)[i].values[j].val.blob_val.s);
  90. break;
  91. }
  92. }
  93. printf("\n");
  94. }
  95. return TRUE;
  96. }
  97. struct module_exports* mod_register()
  98. {
  99. /*
  100. * Column names of table location
  101. */
  102. db_key_t keys1[] = {"user", "contact", "q", "expire", "opaque" };
  103. db_key_t keys2[] = {"user", "q"};
  104. db_key_t keys3[] = {"user", "contact"};
  105. db_key_t keys4[] = {"contact", "q"};
  106. db_val_t vals1[] = {
  107. { DB_STRING , 0, { .string_val = "[email protected]" } },
  108. { DB_STR , 0, { .str_val = { "[email protected]", 18 } } },
  109. { DB_DOUBLE , 0, { .double_val = 1.2 } },
  110. { DB_DATETIME, 0, { .time_val = 439826493 } },
  111. { DB_BLOB , 0, { .blob_val = { "hdslgkhas\0glksf", 17 } } }
  112. };
  113. db_val_t vals2[] = {
  114. { DB_STRING , 0, { .string_val = "[email protected]" } },
  115. { DB_STR , 0, { .str_val = { "[email protected]", 18 } } },
  116. { DB_DOUBLE , 0, { .double_val = 1.3 } },
  117. { DB_DATETIME, 0, { .time_val = 12345 } },
  118. { DB_BLOB , 0, { .blob_val = { "\0a\0balkdfj", 10 } } }
  119. };
  120. db_val_t vals3[] = {
  121. { DB_STRING , 0, { .string_val = "[email protected]" } },
  122. { DB_STR , 0, { .str_val = { "[email protected]", 18 } } },
  123. { DB_DOUBLE , 0, { .double_val = 1.5 } },
  124. { DB_DATETIME, 0, { .time_val = 123456 } },
  125. { DB_BLOB , 0, { .blob_val = { "halgkasdg\'", 10 } } }
  126. };
  127. db_val_t vals4[] = {
  128. { DB_STRING, 0, { .string_val = "[email protected]" } },
  129. { DB_DOUBLE, 0, { .double_val = 1.30 } }
  130. };
  131. db_val_t vals5[] = {
  132. { DB_STRING, 0, { .string_val = "[email protected]" } },
  133. { DB_STRING, 0, { .string_val = "[email protected]" } }
  134. };
  135. db_val_t vals6[] = {
  136. { DB_STRING, 0, { .string_val = "[email protected]" } },
  137. { DB_DOUBLE, 0, { .double_val = 2.5 } }
  138. };
  139. db_con_t* h;
  140. db_res_t* res;
  141. fprintf(stderr, "DBExample - registering...\n");
  142. /* The first call must be bind_dbmod
  143. * This call will search for functions
  144. * exported by a database module
  145. */
  146. if (bind_dbmod()) {
  147. fprintf(stderr, "Error while binding database module, did you forget to load a database module ?\n");
  148. return &dbex_exports;
  149. }
  150. /*
  151. * Create a database connection
  152. * DB_URL is database URL of form
  153. * sql://user:password@host:port/database
  154. * The function returns handle, that
  155. * represents a database connection
  156. */
  157. h = db_init(DB_URL);
  158. if (!h) {
  159. fprintf(stderr, "Error while initializing database connection\n");
  160. return &dbex_exports;
  161. }
  162. /*
  163. * Specify a table name, that will
  164. * be used for manipulations
  165. */
  166. if (db_use_table(h, DB_TABLE) < 0) {
  167. fprintf(stderr, "Error while calling db_use_table\n");
  168. return &dbex_exports;
  169. }
  170. /* If you do not specify any keys and values to be
  171. * matched, all rows will be deleted
  172. */
  173. if (db_delete(h, NULL, NULL, NULL, 0) < 0) {
  174. fprintf(stderr, "Error while flushing table\n");
  175. return &dbex_exports;
  176. }
  177. if (db_insert(h, keys1, vals1, 5) < 0) {
  178. fprintf(stderr, "Error while inserting line 1\n");
  179. return &dbex_exports;
  180. }
  181. if (db_insert(h, keys1, vals2, 5) < 0) {
  182. fprintf(stderr, "Error while inserting line 2\n");
  183. return &dbex_exports;
  184. }
  185. if (db_insert(h, keys1, vals3, 5) < 0) {
  186. fprintf(stderr, "Error while inserting line 3\n");
  187. return &dbex_exports;
  188. }
  189. /*
  190. * Let's delete middle line with
  191. * user = [email protected] and q = 1.3
  192. */
  193. if (db_delete(h, keys2, NULL, vals4, 2) < 0) {
  194. fprintf(stderr, "Error while deleting line\n");
  195. return &dbex_exports;
  196. }
  197. /*
  198. * Modify last line
  199. */
  200. if (db_update(h, keys3, NULL, vals5, keys4, vals6, 2, 2) < 0) {
  201. fprintf(stderr, "Error while modifying table\n");
  202. return &dbex_exports;
  203. }
  204. /*
  205. * Last but not least, dump the result of db_query
  206. */
  207. if (db_query(h, NULL, NULL, NULL, NULL, 0, 0, NULL, &res) < 0) {
  208. fprintf(stderr, "Error while querying table\n");
  209. return &dbex_exports;
  210. }
  211. print_res(res);
  212. /*
  213. * Free the result because we don't need it
  214. * anymore
  215. */
  216. if (db_free_query(h, res) < 0) {
  217. fprintf(stderr, "Error while freeing result of query\n");
  218. return &dbex_exports;
  219. }
  220. /*
  221. * Close existing database connection
  222. * and free previously allocated
  223. * memory
  224. */
  225. db_close(h);
  226. return &dbex_exports;
  227. }