pg_cmd.h 5.3 KB

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