pg_cmd.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * $Id$
  3. *
  4. * PostgreSQL Database Driver for SER
  5. *
  6. * Portions Copyright (C) 2001-2003 FhG FOKUS
  7. * Copyright (C) 2003 August.Net Services, LLC
  8. * Portions Copyright (C) 2005-2008 iptelorg GmbH
  9. *
  10. * This file is part of SER, a free SIP server.
  11. *
  12. * SER is free software; you can redistribute it and/or modify it under the
  13. * terms of the GNU General Public License as published by the Free Software
  14. * Foundation; either version 2 of the License, or (at your option) any later
  15. * version
  16. *
  17. * For a license to use the ser software under conditions other than those
  18. * described here, or to purchase support for this software, please contact
  19. * iptel.org by e-mail at the following addresses: [email protected]
  20. *
  21. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  22. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  23. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  24. * details.
  25. *
  26. * You should have received a copy of the GNU General Public License along
  27. * with this program; if not, write to the Free Software Foundation, Inc., 59
  28. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #ifndef _PG_CMD_H
  31. #define _PG_CMD_H
  32. /** \addtogroup postgres
  33. * @{
  34. */
  35. /** \file
  36. * Declaration of pg_cmd data structure that contains PostgreSQL specific data
  37. * stored in db_cmd structures and related functions.
  38. */
  39. #include "pg_oid.h"
  40. #include "../../db/db_drv.h"
  41. #include "../../db/db_cmd.h"
  42. #include "../../db/db_res.h"
  43. #include "../../str.h"
  44. #include <stdarg.h>
  45. #include <libpq-fe.h>
  46. struct pg_params {
  47. int n;
  48. const char** val;
  49. int* len;
  50. int* fmt;
  51. };
  52. /** Extension structure of db_cmd adding PostgreSQL specific data.
  53. * This data structure extends the generic data structure db_cmd in the
  54. * database API with data specific to the postgresql driver.
  55. */
  56. struct pg_cmd {
  57. db_drv_t gen; /**< Generic part of the data structure (must be first */
  58. char* name; /**< Name of the prepared query on the server */
  59. str sql_cmd; /**< Database command represented in SQL language */
  60. struct pg_params params;
  61. PGresult* types;
  62. };
  63. /** Creates a new pg_cmd data structure.
  64. * This function allocates and initializes memory for a new pg_cmd data
  65. * structure. The data structure is then attached to the generic db_cmd
  66. * structure in cmd parameter.
  67. * @param cmd A generic db_cmd structure to which the newly created pg_cmd
  68. * structure will be attached.
  69. */
  70. int pg_cmd(db_cmd_t* cmd);
  71. /** The main execution function in postgres SER driver.
  72. * This is the main execution function in this driver. It is executed whenever
  73. * a SER module calls db_exec and the target database of the commands is
  74. * PostgreSQL. The function contains all the necessary logic to detect reset
  75. * or disconnected database connections and uploads commands to the server if
  76. * necessary.
  77. * @param res A pointer to (optional) result structure if the command returns
  78. * a result.
  79. * @retval 0 if executed successfully
  80. * @retval A negative number if the database server failed to execute command
  81. * @retval A positive number if there was an error on client side (SER)
  82. */
  83. int pg_cmd_exec(db_res_t* res, db_cmd_t* cmd);
  84. /** Retrieves the first record from a result set received from PostgreSQL server.
  85. * This function is executed whenever a SER module calls db_first to retrieve
  86. * the first record from a result. The function retrieves the first record
  87. * from a PGresult structure and converts the fields from PostgreSQL to
  88. * internal SER representation.
  89. *
  90. * @param res A result set retrieved from PostgreSQL server.
  91. * @retval 0 If executed successfully.
  92. * @retval 1 If the result is empty.
  93. * @retival A negative number on error.
  94. */
  95. int pg_cmd_first(db_res_t* res);
  96. /** Retrieves the next record from a result set received from PostgreSQL server.
  97. * This function is executed whenever a SER module calls db_next to retrieve
  98. * the first record from a result. The function advances current cursor
  99. * position in the result, retrieves the next record from a PGresult structure
  100. * and converts the fields from PostgreSQL to internal SER representation.
  101. *
  102. * @param res A result set retrieved from PostgreSQL server.
  103. * @retval 0 If executed successfully.
  104. * @retval 1 If there are no more records in the result.
  105. * @retival A negative number on error.
  106. */
  107. int pg_cmd_next(db_res_t* res);
  108. /** Retrieves the value of an db_cmd option.
  109. * This function is called when a SER module uses db_getopt to retrieve the
  110. * value of db_cmd parameter.
  111. * @param cmd A db_cmd structure representing the command.
  112. * @param optname Name of the option.
  113. * @param ap A pointer the result variable.
  114. * @retval 0 on success.
  115. * @retval A positive number of the option is not supported/implemented.
  116. * @retval A negative number on error.
  117. */
  118. int pg_getopt(db_cmd_t* cmd, char* optname, va_list ap);
  119. /** Sets the value of an db_cmd option.
  120. * This function is called when a SER module uses db_setopt to set the
  121. * value of db_cmd parameter.
  122. * @param cmd A db_cmd structure representing the command.
  123. * @param optname Name of the option.
  124. * @param ap A variable with the value to be set.
  125. * @retval 0 on success.
  126. * @retval A positive number of the option is not supported/implemented.
  127. * @retval A negative number on error.
  128. */
  129. int pg_setopt(db_cmd_t* cmd, char* optname, va_list ap);
  130. /** @} */
  131. #endif /* _PG_CMD_H */