pg_oid.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /** \addtogroup postgres
  31. * @{
  32. */
  33. /** \file
  34. * Implementation of functions related to PostgreSQL Oid identifiers.
  35. */
  36. #include "pg_oid.h"
  37. #include "../../dprint.h"
  38. #include "../../ut.h"
  39. #include <strings.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. /** An array of supported PostgreSQL field types. */
  43. static char* pg_type_id_name[] = {
  44. "bool",
  45. "bytea",
  46. "char",
  47. "int8",
  48. "int2",
  49. "int4",
  50. "text",
  51. "float4",
  52. "float8",
  53. "inet",
  54. "bpchar",
  55. "varchar",
  56. "timestamp",
  57. "timestamptz",
  58. "bit",
  59. "varbit",
  60. };
  61. static int get_index(char* name)
  62. {
  63. int i;
  64. for(i = 0; i < PG_ID_MAX; i++) {
  65. if (strcasecmp(name, pg_type_id_name[i]) == 0) return i;
  66. }
  67. return -1;
  68. }
  69. pg_type_t* pg_new_oid_table(PGresult* res)
  70. {
  71. pg_type_t* table = NULL;
  72. int row, n = 0, end, idx, fields;
  73. str s;
  74. if (res == NULL || PQresultStatus(res) != PGRES_TUPLES_OK) goto error;
  75. n = PQntuples(res);
  76. if (n <= 0) goto error;
  77. fields = PQnfields(res);
  78. if (fields != 2) goto error;
  79. table = (pg_type_t*)malloc(sizeof(pg_type_t) * (n + 1));
  80. if (table == NULL) goto error;
  81. memset(table, '\0', sizeof(pg_type_t) * (n + 1));
  82. end = n - 1;
  83. for(row = 0; row < n; row++) {
  84. /* Get name */
  85. s.s = PQgetvalue(res, row, 0);
  86. if (s.s == NULL) goto error;
  87. /* Find index where the record is to be stored */
  88. idx = get_index(s.s);
  89. if (idx == -1) idx = end--;
  90. /* Store the name */
  91. table[idx].name = strdup(s.s);
  92. if (table[idx].name == NULL) goto error;
  93. /* Oid */
  94. s.s = PQgetvalue(res, row, 1);
  95. if (s.s == NULL) goto error;
  96. s.len = strlen(s.s);
  97. if (str2int(&s, &table[idx].oid) < 0) goto error;
  98. DBG("postgres: Type %s maps to Oid %d\n", table[idx].name, table[idx].oid);
  99. }
  100. return table;
  101. error:
  102. ERR("postgres: Error while obtaining field/data type description from server\n");
  103. if (table) {
  104. for(idx = 0; idx < n; idx++) {
  105. if (table[idx].name) free(table[idx].name);
  106. }
  107. free(table);
  108. }
  109. return NULL;
  110. }
  111. void pg_destroy_oid_table(pg_type_t* table)
  112. {
  113. int i;
  114. if (table) {
  115. for(i = 0; table[i].name; i++) {
  116. free(table[i].name);
  117. }
  118. free(table);
  119. }
  120. }
  121. int pg_name2oid(Oid* oid, pg_type_t* table, const char* name)
  122. {
  123. int i;
  124. if (!oid || !table) {
  125. BUG("postgres: Invalid parameters to pg_name2oid\n");
  126. return -1;
  127. }
  128. if (name == NULL || name[0] == '\0') return 1;
  129. for(i = 0; table[i].name; i++) {
  130. if (strcasecmp(table[i].name, name) == 0) {
  131. *oid = table[i].oid;
  132. return 0;
  133. }
  134. }
  135. return 1;
  136. }
  137. int pg_oid2name(const char** name, pg_type_t* table, Oid oid)
  138. {
  139. int i;
  140. if (!table || !name) {
  141. BUG("postgres: Invalid parameters to pg_oid2name\n");
  142. return -1;
  143. }
  144. for(i = 0; table[i].name; i++) {
  145. if (oid == table[i].oid) {
  146. *name = table[i].name;
  147. return 0;
  148. }
  149. }
  150. return 1;
  151. }
  152. /** @} */