pg_fld.h 5.6 KB

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