db_cmd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2005 FhG FOKUS
  5. * Copyright (C) 2006-2007 iptelorg GmbH
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. /** \ingroup DB_API
  29. * @{
  30. */
  31. #include "db_cmd.h"
  32. #include "../../dprint.h"
  33. #include "../../mem/mem.h"
  34. #include "../../ut.h"
  35. #include <string.h>
  36. #include <stdarg.h>
  37. db_cmd_t* db_cmd(enum db_cmd_type type, db_ctx_t* ctx, char* table,
  38. db_fld_t* result, db_fld_t* match, db_fld_t* values)
  39. {
  40. char* fname;
  41. db_cmd_t* newp;
  42. db_con_t* con;
  43. int i, r, j;
  44. db_drv_func_t func;
  45. newp = (db_cmd_t*)pkg_malloc(sizeof(db_cmd_t));
  46. if (newp == NULL) goto err;
  47. memset(newp, '\0', sizeof(db_cmd_t));
  48. if (db_gen_init(&newp->gen) < 0) goto err;
  49. newp->ctx = ctx;
  50. newp->table.len = strlen(table);
  51. newp->table.s = (char*)pkg_malloc(newp->table.len + 1);
  52. if (newp->table.s == NULL) goto err;
  53. memcpy(newp->table.s, table, newp->table.len + 1);
  54. newp->type = type;
  55. /** FIXME: it is not clear now that this is necessary
  56. * when we have match and value separate arrays */
  57. if (result) {
  58. newp->result = db_fld_copy(result);
  59. if (newp->result == NULL) goto err;
  60. }
  61. if (match) {
  62. newp->match = db_fld_copy(match);
  63. if (newp->match == NULL) goto err;
  64. }
  65. if (values) {
  66. newp->vals = db_fld_copy(values);
  67. if (newp->vals == NULL) goto err;
  68. }
  69. /* FIXME: This should be redesigned so that we do not need to connect
  70. * connections in context before comands are created, this takes splitting
  71. * the command initializatio sequence in two steps, one would be creating
  72. * all the data structures and the second would be checking corresponding
  73. * fields and tables on the server.
  74. */
  75. if (ctx->con_n == 0) {
  76. ERR("No connections found in context %.*s\n", STR_FMT(&ctx->id));
  77. goto err;
  78. }
  79. for(i = 0; i < ctx->con_n; i++) {
  80. con = ctx->con[i];
  81. r = db_drv_func(&func, &con->uri->scheme, "db_fld");
  82. if (r < 0) goto err;
  83. if (r > 0) func = NULL;
  84. db_payload_idx = i;
  85. if (!DB_FLD_EMPTY(newp->result)) {
  86. for(j = 0; !DB_FLD_LAST(newp->result[j]); j++) {
  87. if (func && func(newp->result + j, table) < 0) goto err;
  88. }
  89. newp->result_count = j;
  90. }
  91. if (!DB_FLD_EMPTY(newp->match)) {
  92. for(j = 0; !DB_FLD_LAST(newp->match[j]); j++) {
  93. if (func && func(newp->match + j, table) < 0) goto err;
  94. }
  95. newp->match_count = j;
  96. }
  97. if (!DB_FLD_EMPTY(newp->vals)) {
  98. for(j = 0; !DB_FLD_LAST(newp->vals[j]); j++) {
  99. if (func && func(newp->vals + j, table) < 0) goto err;
  100. }
  101. newp->vals_count = j;
  102. }
  103. r = db_drv_call(&con->uri->scheme, "db_cmd", newp, i);
  104. if (r < 0) {
  105. ERR("db_drv_call(\"db_cmd\") failed\n");
  106. goto err;
  107. }
  108. if (r > 0) {
  109. ERR("DB driver %.*s does not implement mandatory db_cmd function\n",
  110. con->uri->scheme.len, ZSW(con->uri->scheme.s));
  111. goto err;
  112. }
  113. if (newp->exec[i] == NULL) {
  114. /* db_cmd in the db driver did not provide any runtime function, so try to lookup the
  115. * default one through the module API
  116. */
  117. switch(type) {
  118. case DB_PUT: fname = "db_put"; break;
  119. case DB_DEL: fname = "db_del"; break;
  120. case DB_GET: fname = "db_get"; break;
  121. case DB_UPD: fname = "db_upd"; break;
  122. case DB_SQL: fname = "db_sql"; break;
  123. default: ERR("db_cmd: Unsupported command type\n"); goto err;
  124. }
  125. r = db_drv_func((void*)&(newp->exec[i]), &con->uri->scheme, fname);
  126. if (r < 0) goto err;
  127. if (r > 0) {
  128. ERR("DB driver %.*s does not provide runtime execution function %s\n",
  129. con->uri->scheme.len, ZSW(con->uri->scheme.s), fname);
  130. goto err;
  131. }
  132. }
  133. if (type == DB_GET || type == DB_SQL) {
  134. r = db_drv_func((void*)(&newp->first[i]), &con->uri->scheme, "db_first");
  135. if (r < 0) goto err;
  136. if (r > 0) {
  137. ERR("DB driver %.*s does not implement mandatory db_first function\n",
  138. con->uri->scheme.len, ZSW(con->uri->scheme.s));
  139. goto err;
  140. }
  141. r = db_drv_func((void*)(&newp->next[i]), &con->uri->scheme, "db_next");
  142. if (r < 0) goto err;
  143. if (r > 0) {
  144. ERR("DB driver %.*s does not implement mandatory db_next function\n",
  145. con->uri->scheme.len, ZSW(con->uri->scheme.s));
  146. goto err;
  147. }
  148. }
  149. }
  150. return newp;
  151. err:
  152. ERR("db_cmd: Cannot create db_cmd structure\n");
  153. if (newp) {
  154. db_gen_free(&newp->gen);
  155. if (newp->result) db_fld_free(newp->result);
  156. if (newp->match) db_fld_free(newp->match);
  157. if (newp->vals) db_fld_free(newp->vals);
  158. if (newp->table.s) pkg_free(newp->table.s);
  159. pkg_free(newp);
  160. }
  161. return NULL;
  162. }
  163. void db_cmd_free(db_cmd_t* cmd)
  164. {
  165. if (cmd == NULL) return;
  166. db_gen_free(&cmd->gen);
  167. if (cmd->result) db_fld_free(cmd->result);
  168. if (cmd->match) db_fld_free(cmd->match);
  169. if (cmd->vals) db_fld_free(cmd->vals);
  170. if (cmd->table.s) pkg_free(cmd->table.s);
  171. pkg_free(cmd);
  172. }
  173. int db_exec(db_res_t** res, db_cmd_t* cmd)
  174. {
  175. db_res_t* r = NULL;
  176. int ret;
  177. if (res) {
  178. r = db_res(cmd);
  179. if (r == NULL) return -1;
  180. }
  181. /* FIXME */
  182. db_payload_idx = 0;
  183. ret = cmd->exec[0](r, cmd);
  184. if (ret < 0) {
  185. if (r) db_res_free(r);
  186. return ret;
  187. }
  188. if (res) *res = r;
  189. return ret;
  190. }
  191. int db_getopt(db_cmd_t* cmd, char* optname, ...)
  192. {
  193. int i, r;
  194. db_drv_func_t func;
  195. db_con_t* con;
  196. va_list ap;
  197. for(i = 0; i < cmd->ctx->con_n; i++) {
  198. con = cmd->ctx->con[i];
  199. r = db_drv_func(&func, &con->uri->scheme, "db_getopt");
  200. if (r < 0) return -1;
  201. if (r > 0) func = NULL;
  202. db_payload_idx = i;
  203. va_start(ap, optname);
  204. if (func && func(cmd, optname, ap) < 0) {
  205. va_end(ap);
  206. return -1;
  207. }
  208. va_end(ap);
  209. }
  210. return 0;
  211. }
  212. int db_setopt(db_cmd_t* cmd, char* optname, ...)
  213. {
  214. int i, r;
  215. db_drv_func_t func;
  216. db_con_t* con;
  217. va_list ap;
  218. for(i = 0; i < cmd->ctx->con_n; i++) {
  219. con = cmd->ctx->con[i];
  220. r = db_drv_func(&func, &con->uri->scheme, "db_setopt");
  221. if (r < 0) return -1;
  222. if (r > 0) func = NULL;
  223. db_payload_idx = i;
  224. va_start(ap, optname);
  225. if (func && func(cmd, optname, ap) < 0) {
  226. va_end(ap);
  227. return -1;
  228. }
  229. va_end(ap);
  230. }
  231. return 0;
  232. }
  233. /** @} */