db_cmd.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef _DB_CMD_H
  29. #define _DB_CMD_H 1
  30. /** \ingroup DB_API
  31. * @{
  32. */
  33. /** \file
  34. * Representation of database commands
  35. */
  36. #include "db_fld.h"
  37. #include "db_drv.h"
  38. #include "db_gen.h"
  39. #include "db_res.h"
  40. #include "db_rec.h"
  41. #include "db_ctx.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif /* __cplusplus */
  45. struct db_cmd;
  46. struct db_res;
  47. struct db_rec;
  48. typedef int (*db_exec_func_t)(struct db_res* res, struct db_cmd* cmd);
  49. typedef int (*db_first_func_t)(struct db_res* res);
  50. typedef int (*db_next_func_t)(struct db_res* res);
  51. enum db_cmd_type {
  52. DB_PUT, /* Insert or update new record in database */
  53. DB_DEL, /* Delete all matching records from database */
  54. DB_GET, /* Get matching records from database */
  55. DB_UPD, /* Update matching records in database */
  56. DB_SQL, /* Raw SQL query */
  57. };
  58. /**
  59. * Structure db_cmd describes command in DB-API
  60. */
  61. typedef struct db_cmd {
  62. db_gen_t gen; /**< Generic part of the structure, must be the 1st attribute */
  63. enum db_cmd_type type; /**< Type of the command to be executed */
  64. struct db_ctx* ctx; /**< Context containing database connections to be used */
  65. str table; /**< Name of the table to perform the command on */
  66. db_exec_func_t exec[DB_PAYLOAD_MAX]; /**< Array of exec functions provided by modules */
  67. db_first_func_t first[DB_PAYLOAD_MAX];
  68. db_next_func_t next[DB_PAYLOAD_MAX];
  69. db_fld_t* result; /**< Fields to to be returned in result */
  70. db_fld_t* match; /**< Fields describing WHERE clause */
  71. db_fld_t* vals; /**< Fields with values for UPDATE and INSERT statements */
  72. unsigned int result_count; /* Number of fields in the result set */
  73. unsigned int match_count; /* Number of fields in the result set */
  74. unsigned int vals_count; /* Number of fields in the result set */
  75. } db_cmd_t;
  76. #define DB_SET_EXEC(db_cmd, func) do { \
  77. (db_cmd)->exec[db_payload_idx] = (func); \
  78. } while(0)
  79. struct db_cmd* db_cmd(enum db_cmd_type type, struct db_ctx* ctx, char* table,
  80. db_fld_t* result, db_fld_t* match, db_fld_t* value);
  81. void db_cmd_free(struct db_cmd* cmd);
  82. int db_exec(struct db_res** res, struct db_cmd* cmd);
  83. int db_getopt(db_cmd_t* cmd, char* optname, ...);
  84. int db_setopt(db_cmd_t* cmd, char* optname, ...);
  85. #ifdef __cplusplus
  86. }
  87. #endif /* __cplusplus */
  88. /** @} */
  89. #endif /* _DB_CMD_H */