pg_fld.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_FLD_H
  31. #define _PG_FLD_H
  32. /** \addtogroup postgres
  33. * @{
  34. */
  35. /** \file
  36. * Implementation of pg_fld data structure representing PostgreSQL fields and
  37. * related functions.
  38. */
  39. #include "pg_oid.h"
  40. #include "pg_cmd.h"
  41. #include "../../db/db_gen.h"
  42. #include "../../db/db_fld.h"
  43. #include <libpq-fe.h>
  44. struct pg_fld {
  45. db_drv_t gen;
  46. /**
  47. * A union of varius data types from db_fld, postgres expects binary
  48. * data in network byte order so we use these variables as temporary
  49. * buffer to store values after the conversion.
  50. */
  51. union {
  52. int int4[2]; /**< Integer value in network byte order */
  53. short int2[4];
  54. float flt; /**< Float value in network byte order */
  55. double dbl; /**< Double value in network byte order */
  56. time_t time; /**< Unix timestamp in network byte order */
  57. unsigned int bitmap; /**< Bitmap value in network byte order */
  58. long long int8; /**< 8-byte integer value in network byte order */
  59. char byte[8];
  60. } v;
  61. Oid oid; /**< Type of the field on the server */
  62. };
  63. /** Creates a new PostgreSQL specific payload.
  64. * This function creates a new PostgreSQL specific payload structure and
  65. * attaches the structure to the generic db_fld structure.
  66. * @param fld A generic db_fld structure to be exended.
  67. * @param table Name of the table on the server.
  68. * @retval 0 on success.
  69. * @retval A negative number on error.
  70. */
  71. int pg_fld(db_fld_t* fld, char* table);
  72. int pg_resolve_param_oids(db_fld_t* vals, db_fld_t* match,
  73. int n1, int n2, PGresult* res);
  74. int pg_resolve_result_oids(db_fld_t* fld, int n, PGresult* res);
  75. /** Converts arrays of db_fld fields to PostgreSQL parameters.
  76. * The function converts fields in SER db_fld format to parameters suitable
  77. * for PostgreSQL API functions.
  78. * @param values An array of pointers to values in PostgreSQL format. The
  79. * function will store pointers to converted values there.
  80. * @param lenghts An array of integers that will be filled with lenghts
  81. * of values.
  82. * @param formats An array of value formats, see PostgreSQL API client
  83. * library documentation for more detail.
  84. * @param oids Types of corresponding columns on the server.
  85. * @param types A type conversion table.
  86. * @param fld An array of db_fld fields to be converted.
  87. * @param flags Connection flags controlling how values are converted.
  88. * @todo Implement support for bit fields with size bigger than 32
  89. * @todo Implement support for varbit properly to remove leading zeroes
  90. * @todo Check if timezones are handled properly
  91. * @todo Support for DB_NONE in pg_pg2fld and pg_check_pg2fld
  92. * @todo local->UTC conversion (also check the SQL command in ser-oob)
  93. */
  94. int pg_fld2pg(struct pg_params* dst, int off, pg_type_t* types,
  95. db_fld_t* src, unsigned int flags);
  96. /** Converts fields from result in PGresult format into SER format.
  97. * The function converts fields from PostgreSQL result (PGresult structure)
  98. * into the internal format used in SER. The function converts one row at a
  99. * time.
  100. * @param fld The destination array of db_fld fields to be filled with converted
  101. * values.
  102. * @param pres A PostgreSQL result structure to be converted into SER format.
  103. * @param row Number of the row to be converted.
  104. * @param flags Connection flags controlling how values are converted.
  105. * @retval 0 on success
  106. * @retval A negative number on error.
  107. * @todo UTC->local conversion
  108. */
  109. int pg_pg2fld(db_fld_t* dst, PGresult* src, int row, pg_type_t* types,
  110. unsigned int flags);
  111. /** Checks if all db_fld fields have types compatible with corresponding field
  112. * types on the server.
  113. * The functions checks whether all db_fld fields in the last parameter are
  114. * compatible with column types on the server.
  115. * @param oids An array of Oids of corresponding columns on the server.
  116. * @param lenghts An array of sizes of corresponding columns on the server.
  117. * @param types An array used to map internal field types to Oids.
  118. * @param fld An array of db_fld fields to be checked.
  119. * @retval 0 on success
  120. * @retval A negative number on error.
  121. */
  122. int pg_check_fld2pg(db_fld_t* fld, pg_type_t* types);
  123. int pg_check_pg2fld(db_fld_t* fld, pg_type_t* types);
  124. /** @} */
  125. #endif /* _PG_FLD_H */