km_flatstore.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * $Id$
  3. *
  4. * Flatstore module interface
  5. *
  6. * Copyright (C) 2004 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2003-03-11 updated to the new module exports interface (andrei)
  27. * 2003-03-16 flags export parameter added (janakj)
  28. */
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include "../../mem/mem.h"
  32. #include "../../dprint.h"
  33. #include "km_flat_pool.h"
  34. #include "km_flat_con.h"
  35. #include "km_flatstore_mod.h"
  36. #include "km_flatstore.h"
  37. #include "flatstore_mod.h"
  38. static int parse_flat_url(const str* url, str* path)
  39. {
  40. if (!url || !url->s || !path) {
  41. LM_ERR("invalid parameter value\n");
  42. return -1;
  43. }
  44. path->s = strchr(url->s, ':') + 1;
  45. path->len = strlen(path->s);
  46. return 0;
  47. }
  48. /*
  49. * Initialize database module
  50. * No function should be called before this
  51. */
  52. db1_con_t* flat_db_init(const str* url)
  53. {
  54. db1_con_t* res;
  55. str* path;
  56. if (!url || !url->s) {
  57. LM_ERR("invalid parameter value\n");
  58. return 0;
  59. }
  60. /* We do not know the name of the table (and the name of the corresponding
  61. * file) at this point, we will simply store the path taken from the url
  62. * parameter in the table variable, flat_use_table will then pick that
  63. * value and open the file
  64. */
  65. /* as the table (path) is a substring of the received str, we need to
  66. * allocate a separate str struct for it -bogdan
  67. */
  68. res = pkg_malloc(sizeof(db1_con_t)+sizeof(struct flat_con*)+sizeof(str));
  69. if (!res) {
  70. LM_ERR("no pkg memory left\n");
  71. return 0;
  72. }
  73. memset(res, 0, sizeof(db1_con_t) + sizeof(struct flat_con*) + sizeof(str));
  74. path = (str*)(((char*)res) + sizeof(db1_con_t) + sizeof(struct flat_con*));
  75. if (parse_flat_url(url, path) < 0) {
  76. pkg_free(res);
  77. return 0;
  78. }
  79. res->table = path;
  80. return res;
  81. }
  82. /*
  83. * Store name of table that will be used by
  84. * subsequent database functions
  85. */
  86. int flat_use_table(db1_con_t* h, const str* t)
  87. {
  88. struct flat_con* con;
  89. if (!h || !t || !t->s) {
  90. LM_ERR("invalid parameter value\n");
  91. return -1;
  92. }
  93. if (CON_TABLE(h)->s != t->s) {
  94. if (CON_TAIL(h)) {
  95. /* Decrement the reference count
  96. * of the connection but do not remove
  97. * it from the connection pool
  98. */
  99. con = (struct flat_con*)CON_TAIL(h);
  100. con->ref--;
  101. }
  102. CON_TAIL(h) = (unsigned long)
  103. flat_get_connection((char*)CON_TABLE(h)->s, (char*)t->s);
  104. if (!CON_TAIL(h)) {
  105. return -1;
  106. }
  107. }
  108. return 0;
  109. }
  110. void flat_db_close(db1_con_t* h)
  111. {
  112. struct flat_con* con;
  113. if (!h) {
  114. LM_ERR("invalid parameter value\n");
  115. return;
  116. }
  117. con = (struct flat_con*)CON_TAIL(h);
  118. if (con) {
  119. flat_release_connection(con);
  120. }
  121. pkg_free(h);
  122. }
  123. /*
  124. * Insert a row into specified table
  125. * h: structure representing database connection
  126. * k: key names
  127. * v: values of the keys
  128. * n: number of key=value pairs
  129. */
  130. int flat_db_insert(const db1_con_t* h, const db_key_t* k, const db_val_t* v,
  131. const int n)
  132. {
  133. FILE* f;
  134. int i;
  135. int l;
  136. char *s, *p;
  137. if (km_local_timestamp < *km_flat_rotate) {
  138. flat_rotate_logs();
  139. km_local_timestamp = *km_flat_rotate;
  140. }
  141. f = CON_FILE(h);
  142. if (!f) {
  143. LM_ERR("uninitialized connection\n");
  144. return -1;
  145. }
  146. for(i = 0; i < n; i++) {
  147. switch(VAL_TYPE(v + i)) {
  148. case DB1_INT:
  149. fprintf(f, "%d", VAL_INT(v + i));
  150. break;
  151. case DB1_BIGINT:
  152. LM_ERR("BIGINT not supported");
  153. return -1;
  154. case DB1_DOUBLE:
  155. fprintf(f, "%f", VAL_DOUBLE(v + i));
  156. break;
  157. case DB1_STRING:
  158. fprintf(f, "%s", VAL_STRING(v + i));
  159. break;
  160. case DB1_STR:
  161. fprintf(f, "%.*s", VAL_STR(v + i).len, VAL_STR(v + i).s);
  162. break;
  163. case DB1_DATETIME:
  164. fprintf(f, "%u", (unsigned int)VAL_TIME(v + i));
  165. break;
  166. case DB1_BLOB:
  167. l = VAL_BLOB(v+i).len;
  168. s = p = VAL_BLOB(v+i).s;
  169. while (l--) {
  170. if ( !(isprint((int)*s) && *s != '\\' && *s != '|')) {
  171. fprintf(f,"%.*s\\x%02X",(int)(s-p),p,(*s & 0xff));
  172. p = s+1;
  173. }
  174. ++s;
  175. }
  176. if (p!=s)
  177. fprintf(f,"%.*s",(int)(s-p),p);
  178. break;
  179. case DB1_BITMAP:
  180. fprintf(f, "%u", VAL_BITMAP(v + i));
  181. break;
  182. default:
  183. LM_ERR("val type [%d] not supported", VAL_TYPE(v + i));
  184. return -1;
  185. }
  186. if (i < (n - 1)) {
  187. fprintf(f, "%c", *km_flat_delimiter);
  188. }
  189. }
  190. fprintf(f, "\n");
  191. if (flat_flush) {
  192. fflush(f);
  193. }
  194. return 0;
  195. }