pg_fld.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. * Data field conversion and type checking functions.
  35. */
  36. #include "pg_fld.h"
  37. #include "pg_con.h" /* flags */
  38. #include "pg_mod.h"
  39. #include "../../db/db_drv.h"
  40. #include "../../mem/mem.h"
  41. #include "../../dprint.h"
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <netinet/in.h>
  45. #include <stdint.h>
  46. #include <string.h>
  47. /**
  48. * This is the epoch time in time_t format, this value is used to convert
  49. * timestamp values to/from PostgreSQL format.
  50. * 2000-01-01 00:00:00 +0000 as the value of time_t in UTC
  51. */
  52. #define PG_EPOCH_TIME ((int64_t)946684800)
  53. /** Frees memory used by a pg_fld structure.
  54. * This function frees all memory used by a pg_fld structure
  55. * @param fld Generic db_fld_t* structure being freed.
  56. * @param payload The postgresql extension structure to be freed
  57. */
  58. static void pg_fld_free(db_fld_t* fld, struct pg_fld* payload)
  59. {
  60. db_drv_free(&payload->gen);
  61. pkg_free(payload);
  62. }
  63. int pg_fld(db_fld_t* fld, char* table)
  64. {
  65. struct pg_fld* res;
  66. res = (struct pg_fld*)pkg_malloc(sizeof(struct pg_fld));
  67. if (res == NULL) {
  68. ERR("postgres: No memory left\n");
  69. return -1;
  70. }
  71. memset(res, '\0', sizeof(struct pg_fld));
  72. if (db_drv_init(&res->gen, pg_fld_free) < 0) goto error;
  73. DB_SET_PAYLOAD(fld, res);
  74. return 0;
  75. error:
  76. if (res) pkg_free(res);
  77. return -1;
  78. }
  79. static inline uint64_t htonll(uint64_t in)
  80. {
  81. uint32_t* p = (uint32_t*)&in;
  82. return ((uint64_t)htonl(p[0]) << 32) + (uint64_t)htonl(p[1]);
  83. }
  84. static inline uint64_t ntohll(uint64_t in)
  85. {
  86. uint32_t* p = (uint32_t*)&in;
  87. return ((uint64_t)ntohl(p[0]) << 32) + (uint64_t)ntohl(p[1]);
  88. }
  89. static inline void db_int2pg_int4(struct pg_params* dst, int i,
  90. db_fld_t* src)
  91. {
  92. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  93. pfld->v.int4[0] = htonl(src->v.int4);
  94. dst->fmt[i] = 1;
  95. dst->val[i] = pfld->v.byte;
  96. dst->len[i] = 4;
  97. }
  98. static inline void db_int2pg_int2(struct pg_params* dst, int i,
  99. db_fld_t* src)
  100. {
  101. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  102. pfld->v.int2[0] = htons(src->v.int4);
  103. dst->fmt[i] = 1;
  104. dst->val[i] = pfld->v.byte;
  105. dst->len[i] = 2;
  106. }
  107. static inline void db_int2pg_timestamp(struct pg_params* dst, int i,
  108. db_fld_t* src, unsigned int flags)
  109. {
  110. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  111. if (flags & PG_INT8_TIMESTAMP) {
  112. pfld->v.int8 = ((int64_t)src->v.int4 - PG_EPOCH_TIME) * 1000000;
  113. } else {
  114. pfld->v.dbl = (double)src->v.int4 - (double)PG_EPOCH_TIME;
  115. }
  116. pfld->v.int8 = htonll(pfld->v.int8);
  117. dst->fmt[i] = 1;
  118. dst->val[i] = pfld->v.byte;
  119. dst->len[i] = 8;
  120. }
  121. static inline void db_int2pg_int8(struct pg_params* dst, int i,
  122. db_fld_t* src)
  123. {
  124. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  125. pfld->v.int4[0] = 0;
  126. pfld->v.int4[1] = htonl(src->v.int4);
  127. dst->fmt[i] = 1;
  128. dst->val[i] = pfld->v.byte;
  129. dst->len[i] = 8;
  130. }
  131. static inline void db_int2pg_bool(struct pg_params* dst, int i, db_fld_t* src)
  132. {
  133. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  134. if (src->v.int4) pfld->v.byte[0] = 1;
  135. else pfld->v.byte[0] = 0;
  136. dst->fmt[i] = 1;
  137. dst->val[i] = pfld->v.byte;
  138. dst->len[i] = 1;
  139. }
  140. static inline void db_int2pg_inet(struct pg_params* dst, int i, db_fld_t* src)
  141. {
  142. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  143. pfld->v.byte[0] = AF_INET; /* Address family */
  144. pfld->v.byte[1] = 32; /* Netmask */
  145. pfld->v.byte[2] = 0; /* is CIDR */
  146. pfld->v.byte[3] = 4; /* Number of bytes */
  147. pfld->v.int4[1] = htonl(src->v.int4); /* Actuall IP address */
  148. dst->fmt[i] = 1;
  149. dst->val[i] = pfld->v.byte;
  150. dst->len[i] = 8;
  151. }
  152. static inline void db_float2pg_float4(struct pg_params* dst, int i, db_fld_t* src)
  153. {
  154. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  155. pfld->v.int4[0] = htonl(src->v.int4);
  156. dst->fmt[i] = 1;
  157. dst->val[i] = pfld->v.byte;
  158. dst->len[i] = 4;
  159. }
  160. static inline void db_float2pg_float8(struct pg_params* dst, int i, db_fld_t* src)
  161. {
  162. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  163. pfld->v.dbl = src->v.flt;
  164. pfld->v.int8 = htonll(pfld->v.int8);
  165. dst->fmt[i] = 1;
  166. dst->val[i] = pfld->v.byte;
  167. dst->len[i] = 8;
  168. }
  169. static inline void db_double2pg_float8(struct pg_params* dst, int i, db_fld_t* src)
  170. {
  171. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  172. pfld->v.int8 = htonll(src->v.int8);
  173. dst->fmt[i] = 1;
  174. dst->val[i] = pfld->v.byte;
  175. dst->len[i] = 8;
  176. }
  177. static inline void db_double2pg_float4(struct pg_params* dst, int i, db_fld_t* src)
  178. {
  179. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  180. pfld->v.flt = src->v.dbl;
  181. pfld->v.int4[0] = htonl(pfld->v.int4[0]);
  182. dst->fmt[i] = 1;
  183. dst->val[i] = pfld->v.byte;
  184. dst->len[i] = 4;
  185. }
  186. static inline void db_int2pg_bit(struct pg_params* dst, int i, db_fld_t* src)
  187. {
  188. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  189. pfld->v.int4[0] = htonl(32);
  190. pfld->v.int4[1] = htonl(src->v.int4);
  191. dst->fmt[i] = 1;
  192. dst->val[i] = pfld->v.byte;
  193. dst->len[i] = 8;
  194. }
  195. /*
  196. static inline void db_int2pg_varbit(struct pg_params* dst, int i,
  197. db_fld_t* src)
  198. {
  199. unsigned int len = 32;
  200. struct pg_fld* pfld = DB_GET_PAYLOAD(src);
  201. pfld->v.int4[0] = htonl(len);
  202. pfld->v.int4[1] = htonl(src->v.int4);
  203. dst->fmt[i] = 1;
  204. dst->val[i] = pfld->v.byte;
  205. dst->len[i] = 4 + len / 8 + (len % 8 ? 1 : 0);
  206. }
  207. */
  208. static inline void db_str2pg_string(struct pg_params* dst, int i,
  209. db_fld_t* src)
  210. {
  211. dst->fmt[i] = 1;
  212. dst->val[i] = src->v.lstr.s;
  213. dst->len[i] = src->v.lstr.len;
  214. }
  215. static inline void db_cstr2pg_string(struct pg_params* dst, int i,
  216. db_fld_t* src)
  217. {
  218. dst->fmt[i] = 0;
  219. dst->val[i] = src->v.cstr;
  220. }
  221. int pg_fld2pg(struct pg_params* dst, int off, pg_type_t* types,
  222. db_fld_t* src, unsigned int flags)
  223. {
  224. int i;
  225. struct pg_fld* pfld;
  226. if (src == NULL) return 0;
  227. for(i = 0; !DB_FLD_EMPTY(src) && !DB_FLD_LAST(src[i]); i++) {
  228. pfld = DB_GET_PAYLOAD(src + i);
  229. /* NULL value */
  230. if (src[i].flags & DB_NULL) {
  231. dst->val[off + i] = NULL;
  232. dst->len[off + i] = 0;
  233. continue;
  234. }
  235. switch(src[i].type) {
  236. case DB_INT:
  237. if (pfld->oid == types[PG_INT2].oid)
  238. db_int2pg_int2(dst, off + i, src + i);
  239. else if (pfld->oid == types[PG_INT4].oid)
  240. db_int2pg_int4(dst, off + i, src + i);
  241. else if ((pfld->oid == types[PG_TIMESTAMP].oid) ||
  242. (pfld->oid == types[PG_TIMESTAMPTZ].oid))
  243. db_int2pg_timestamp(dst, off + i, src + i, flags);
  244. else if (pfld->oid == types[PG_INT8].oid)
  245. db_int2pg_int8(dst, off + i, src + i);
  246. else if (pfld->oid == types[PG_INET].oid)
  247. db_int2pg_inet(dst, off + i, src + i);
  248. else if (pfld->oid == types[PG_BOOL].oid)
  249. db_int2pg_bool(dst, off + i, src + i);
  250. else if (pfld->oid == types[PG_BIT].oid)
  251. db_int2pg_bit(dst, off + i, src + i);
  252. else if (pfld->oid == types[PG_VARBIT].oid)
  253. db_int2pg_bit(dst, off + i, src + i);
  254. else goto bug;
  255. break;
  256. case DB_BITMAP:
  257. if (pfld->oid == types[PG_INT4].oid)
  258. db_int2pg_int4(dst, off + i, src + i);
  259. else if (pfld->oid == types[PG_INT8].oid)
  260. db_int2pg_int8(dst, off + i, src + i);
  261. else if (pfld->oid == types[PG_BIT].oid)
  262. db_int2pg_bit(dst, off + i, src + i);
  263. else if (pfld->oid == types[PG_VARBIT].oid)
  264. db_int2pg_bit(dst, off + i, src + i);
  265. else goto bug;
  266. break;
  267. case DB_DATETIME:
  268. if (pfld->oid == types[PG_INT4].oid)
  269. db_int2pg_int4(dst, off + i, src + i);
  270. else if ((pfld->oid == types[PG_TIMESTAMP].oid) ||
  271. (pfld->oid == types[PG_TIMESTAMPTZ].oid))
  272. db_int2pg_timestamp(dst, off + i, src + i, flags);
  273. else if (pfld->oid == types[PG_INT8].oid)
  274. db_int2pg_int8(dst, off + i, src + i);
  275. else goto bug;
  276. break;
  277. case DB_FLOAT:
  278. if (pfld->oid == types[PG_FLOAT4].oid)
  279. db_float2pg_float4(dst, off + i, src + i);
  280. else if (pfld->oid == types[PG_FLOAT8].oid)
  281. db_float2pg_float8(dst, off + i, src + i);
  282. else goto bug;
  283. break;
  284. case DB_DOUBLE:
  285. if (pfld->oid == types[PG_FLOAT4].oid)
  286. db_double2pg_float4(dst, off + i, src + i);
  287. else if (pfld->oid == types[PG_FLOAT8].oid)
  288. db_double2pg_float8(dst, off + i, src + i);
  289. else goto bug;
  290. break;
  291. case DB_STR:
  292. if (pfld->oid == types[PG_VARCHAR].oid ||
  293. pfld->oid == types[PG_BYTE].oid ||
  294. pfld->oid == types[PG_CHAR].oid ||
  295. pfld->oid == types[PG_TEXT].oid ||
  296. pfld->oid == types[PG_BPCHAR].oid)
  297. db_str2pg_string(dst, off + i, src + i);
  298. else goto bug;
  299. break;
  300. case DB_CSTR:
  301. if (pfld->oid == types[PG_VARCHAR].oid ||
  302. pfld->oid == types[PG_BYTE].oid ||
  303. pfld->oid == types[PG_CHAR].oid ||
  304. pfld->oid == types[PG_TEXT].oid ||
  305. pfld->oid == types[PG_BPCHAR].oid)
  306. db_cstr2pg_string(dst, off + i, src + i);
  307. else goto bug;
  308. break;
  309. case DB_BLOB:
  310. if (pfld->oid == types[PG_BYTE].oid)
  311. db_str2pg_string(dst, off + i, src + i);
  312. else goto bug;
  313. break;
  314. default:
  315. BUG("postgres: Unsupported field type %d in field %s\n",
  316. src[i].type, src[i].name);
  317. return -1;
  318. }
  319. }
  320. return 0;
  321. bug:
  322. BUG("postgres: Error while converting DB API type %d to Postgres Oid %d\n",
  323. src[i].type, pfld->oid);
  324. return -1;
  325. }
  326. int pg_check_fld2pg(db_fld_t* fld, pg_type_t* types)
  327. {
  328. int i;
  329. const char* name = "UNKNOWN";
  330. struct pg_fld* pfld;
  331. if (fld == NULL) return 0;
  332. for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
  333. pfld = DB_GET_PAYLOAD(fld + i);
  334. switch(fld[i].type) {
  335. case DB_INT:
  336. if (pfld->oid == types[PG_INT2].oid) continue;
  337. if (pfld->oid == types[PG_INT4].oid) continue;
  338. if (pfld->oid == types[PG_INT8].oid) continue;
  339. if (pfld->oid == types[PG_BOOL].oid) continue;
  340. if (pfld->oid == types[PG_INET].oid) continue;
  341. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  342. if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
  343. if (pfld->oid == types[PG_BIT].oid) continue;
  344. if (pfld->oid == types[PG_VARBIT].oid) continue;
  345. break;
  346. case DB_BITMAP:
  347. if (pfld->oid == types[PG_INT4].oid) continue;
  348. if (pfld->oid == types[PG_INT8].oid) continue;
  349. if (pfld->oid == types[PG_BIT].oid) continue;
  350. if (pfld->oid == types[PG_VARBIT].oid) continue;
  351. break;
  352. case DB_FLOAT:
  353. case DB_DOUBLE:
  354. if (pfld->oid == types[PG_FLOAT4].oid) continue;
  355. if (pfld->oid == types[PG_FLOAT8].oid) continue;
  356. break;
  357. case DB_CSTR:
  358. case DB_STR:
  359. if (pfld->oid == types[PG_BYTE].oid) continue;
  360. if (pfld->oid == types[PG_CHAR].oid) continue;
  361. if (pfld->oid == types[PG_TEXT].oid) continue;
  362. if (pfld->oid == types[PG_BPCHAR].oid) continue;
  363. if (pfld->oid == types[PG_VARCHAR].oid) continue;
  364. break;
  365. case DB_BLOB:
  366. if (pfld->oid == types[PG_BYTE].oid) continue;
  367. break;
  368. case DB_DATETIME:
  369. if (pfld->oid == types[PG_INT4].oid) continue;
  370. if (pfld->oid == types[PG_INT8].oid) continue;
  371. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  372. if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
  373. break;
  374. default:
  375. BUG("postgres: Unsupported field type %d, bug in postgres module\n",
  376. fld[i].type);
  377. return -1;
  378. }
  379. pg_oid2name(&name, types, pfld->oid);
  380. ERR("postgres: Cannot convert column '%s' of type %s "
  381. "to PostgreSQL column type '%s'\n",
  382. fld[i].name, db_fld_str[fld[i].type], name);
  383. return -1;
  384. }
  385. return 0;
  386. }
  387. int pg_resolve_param_oids(db_fld_t* vals, db_fld_t* match, int n1, int n2, PGresult* types)
  388. {
  389. struct pg_fld* pfld;
  390. int i;
  391. if (n1 + n2 != PQnparams(types)) {
  392. ERR("postgres: Number of command parameters do not match\n");
  393. return -1;
  394. }
  395. for(i = 0; i < n1; i++) {
  396. pfld = DB_GET_PAYLOAD(vals + i);
  397. pfld->oid = PQparamtype(types, i);
  398. }
  399. for(i = 0; i < n2; i++) {
  400. pfld = DB_GET_PAYLOAD(match + i);
  401. pfld->oid = PQparamtype(types, n1 + i);
  402. }
  403. return 0;
  404. }
  405. int pg_resolve_result_oids(db_fld_t* fld, int n, PGresult* types)
  406. {
  407. struct pg_fld* pfld;
  408. int i;
  409. if (fld == NULL) return 0;
  410. if (n != PQnfields(types)) {
  411. ERR("postgres: Result field numbers do not match\n");
  412. return -1;
  413. }
  414. for(i = 0; i < n; i++) {
  415. pfld = DB_GET_PAYLOAD(fld + i);
  416. pfld->oid = PQftype(types, i);
  417. }
  418. return 0;
  419. }
  420. int pg_check_pg2fld(db_fld_t* fld, pg_type_t* types)
  421. {
  422. int i;
  423. const char* name = "UNKNOWN";
  424. struct pg_fld* pfld;
  425. if (fld == NULL) return 0;
  426. for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
  427. pfld = DB_GET_PAYLOAD(fld + i);
  428. if (pfld->oid == 0) {
  429. ERR("postgres: Unknown type fields not supported\n");
  430. return -1;
  431. }
  432. switch(fld[i].type) {
  433. case DB_INT:
  434. if (pfld->oid == types[PG_INT2].oid) continue;
  435. if (pfld->oid == types[PG_INT4].oid) continue;
  436. if (pfld->oid == types[PG_INT8].oid) continue;
  437. if (pfld->oid == types[PG_BOOL].oid) continue;
  438. if (pfld->oid == types[PG_INET].oid) continue;
  439. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  440. if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
  441. if (pfld->oid == types[PG_BIT].oid) continue;
  442. if (pfld->oid == types[PG_VARBIT].oid) continue;
  443. break;
  444. case DB_BITMAP:
  445. if (pfld->oid == types[PG_INT2].oid) continue;
  446. if (pfld->oid == types[PG_INT4].oid) continue;
  447. if (pfld->oid == types[PG_INT8].oid) continue;
  448. if (pfld->oid == types[PG_BIT].oid) continue;
  449. if (pfld->oid == types[PG_VARBIT].oid) continue;
  450. break;
  451. case DB_FLOAT:
  452. if (pfld->oid == types[PG_FLOAT4].oid) continue;
  453. break;
  454. case DB_DOUBLE:
  455. if (pfld->oid == types[PG_FLOAT4].oid) continue;
  456. if (pfld->oid == types[PG_FLOAT8].oid) continue;
  457. break;
  458. case DB_CSTR:
  459. if (pfld->oid == types[PG_CHAR].oid) continue;
  460. if (pfld->oid == types[PG_TEXT].oid) continue;
  461. if (pfld->oid == types[PG_BPCHAR].oid) continue;
  462. if (pfld->oid == types[PG_VARCHAR].oid) continue;
  463. if (pfld->oid == types[PG_INT2].oid) continue;
  464. if (pfld->oid == types[PG_INT4].oid) continue;
  465. break;
  466. case DB_STR:
  467. case DB_BLOB:
  468. if (pfld->oid == types[PG_BYTE].oid) continue;
  469. if (pfld->oid == types[PG_CHAR].oid) continue;
  470. if (pfld->oid == types[PG_TEXT].oid) continue;
  471. if (pfld->oid == types[PG_BPCHAR].oid) continue;
  472. if (pfld->oid == types[PG_VARCHAR].oid) continue;
  473. if (pfld->oid == types[PG_INT2].oid) continue;
  474. if (pfld->oid == types[PG_INT4].oid) continue;
  475. break;
  476. case DB_DATETIME:
  477. if (pfld->oid == types[PG_INT2].oid) continue;
  478. if (pfld->oid == types[PG_INT4].oid) continue;
  479. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  480. if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
  481. break;
  482. default:
  483. BUG("postgres: Unsupported field type %d, bug in postgres module\n",
  484. fld[i].type);
  485. return -1;
  486. }
  487. pg_oid2name(&name, types, pfld->oid);
  488. ERR("postgres: Cannot convert column '%s' of type %s "
  489. "to DB API field of type %s\n",
  490. fld[i].name, name, db_fld_str[fld[i].type]);
  491. return -1;
  492. }
  493. return 0;
  494. }
  495. static inline int pg_int2_2_db_cstr(db_fld_t* fld, char* val, int len)
  496. {
  497. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  498. int size, v;
  499. v = (int16_t)ntohs(*((int16_t*)val));
  500. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  501. if (size < 0 || size >= INT2STR_MAX_LEN) {
  502. BUG("postgres: Error while converting integer to string\n");
  503. return -1;
  504. }
  505. fld->v.cstr = pfld->buf;
  506. return 0;
  507. }
  508. static inline int pg_int4_2_db_cstr(db_fld_t* fld, char* val, int len)
  509. {
  510. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  511. int size, v;
  512. v = (int16_t)ntohs(*((int32_t*)val));
  513. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  514. if (len < 0 || size >= INT2STR_MAX_LEN) {
  515. BUG("postgres: Error while converting integer to string\n");
  516. return -1;
  517. }
  518. fld->v.cstr = pfld->buf;
  519. return 0;
  520. }
  521. static inline int pg_int2_2_db_str(db_fld_t* fld, char* val, int len)
  522. {
  523. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  524. int size, v;
  525. v = (int16_t)ntohs(*((int16_t*)val));
  526. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  527. if (size < 0 || size >= INT2STR_MAX_LEN) {
  528. BUG("postgres: Error while converting integer to string\n");
  529. return -1;
  530. }
  531. fld->v.lstr.s = pfld->buf;
  532. fld->v.lstr.len = size;
  533. return 0;
  534. }
  535. static inline int pg_int4_2_db_str(db_fld_t* fld, char* val, int len)
  536. {
  537. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  538. int size, v;
  539. v = (int16_t)ntohs(*((int32_t*)val));
  540. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  541. if (size < 0 || size >= INT2STR_MAX_LEN) {
  542. BUG("postgres: Error while converting integer to string\n");
  543. return -1;
  544. }
  545. fld->v.lstr.s = pfld->buf;
  546. fld->v.lstr.len = size;
  547. return 0;
  548. }
  549. static inline int pg_int2_2_db_int(db_fld_t* fld, char* val, int len)
  550. {
  551. fld->v.int4 = (int16_t)ntohs(*((int16_t*)val));
  552. return 0;
  553. }
  554. static inline int pg_int4_2_db_int(db_fld_t* fld, char* val, int len)
  555. {
  556. fld->v.int4 = (int32_t)ntohl(*((int32_t*)val));
  557. return 0;
  558. }
  559. static inline int pg_int8_2_db_int(db_fld_t* fld, char* val, int len)
  560. {
  561. fld->v.int8 = (int64_t)ntohll(*((int64_t*)val));
  562. return 0;
  563. }
  564. static inline int pg_bool2db_int(db_fld_t* fld, char* val, int len)
  565. {
  566. fld->v.int4 = val[0];
  567. return 0;
  568. }
  569. static inline int pg_inet2db_int(db_fld_t* fld, char* val, int len)
  570. {
  571. if (len != 8 || val[2] != 0) {
  572. ERR("postgres: Unsupported 'inet' format, column %s\n", fld->name);
  573. return -1;
  574. }
  575. if (val[0] != AF_INET) {
  576. ERR("postgres: Unsupported address family %d in field %s\n",
  577. val[0], fld->name);
  578. return -1;
  579. }
  580. if (val[1] != 32) {
  581. WARN("postgres: Netmasks shorter than 32-bits not supported, "
  582. "column %s\n", fld->name);
  583. }
  584. if (val[3] != 4) {
  585. ERR("postgres: Unsupported IP address size %d in column %s\n",
  586. val[3], fld->name);
  587. return -1;
  588. }
  589. fld->v.int4 = (int32_t)ntohl(((int32_t*)val)[1]);
  590. return 0;
  591. }
  592. static inline int pg_timestamp2db_int(db_fld_t* fld, char* val, int len,
  593. unsigned int flags)
  594. {
  595. if (flags & PG_INT8_TIMESTAMP) {
  596. /* int8 format */
  597. fld->v.int4 = (int64_t)ntohll(((int64_t*)val)[0]) / (int64_t)1000000 + PG_EPOCH_TIME;
  598. } else {
  599. /* double format */
  600. fld->v.int4 = PG_EPOCH_TIME + ntohll(((int64_t*)val)[0]);
  601. }
  602. return 0;
  603. }
  604. static inline int pg_bit2db_int(db_fld_t* fld, char* val, int len)
  605. {
  606. int size;
  607. size = ntohl(*(uint32_t*)val);
  608. if (size != 32) {
  609. ERR("postgres: Unsupported bit field size (%d), column %s\n",
  610. size, fld->name);
  611. return -1;
  612. }
  613. fld->v.int4 = ntohl(((uint32_t*)val)[1]);
  614. return 0;
  615. }
  616. static inline int pg_float42db_float(db_fld_t* fld, char* val, int len)
  617. {
  618. fld->v.int4 = (uint32_t)ntohl(*(uint32_t*)val);
  619. return 0;
  620. }
  621. static inline int pg_float42db_double(db_fld_t* fld, char* val, int len)
  622. {
  623. float tmp;
  624. tmp = ntohl(*(uint32_t*)val);
  625. fld->v.dbl = tmp;
  626. return 0;
  627. }
  628. static inline int pg_float82db_double(db_fld_t* fld, char* val, int len)
  629. {
  630. fld->v.int8 = ntohll(*(uint64_t*)val);
  631. return 0;
  632. }
  633. static inline int pg_string2db_cstr(db_fld_t* fld, char* val, int len)
  634. {
  635. fld->v.cstr = val;
  636. return 0;
  637. }
  638. static inline int pg_string2db_str(db_fld_t* fld, char* val, int len)
  639. {
  640. fld->v.lstr.s = val;
  641. fld->v.lstr.len = len;
  642. return 0;
  643. }
  644. int pg_pg2fld(db_fld_t* dst, PGresult* src, int row,
  645. pg_type_t* types, unsigned int flags)
  646. {
  647. char* val;
  648. int i, len, ret;
  649. Oid type;
  650. if (dst == NULL || src == NULL) return 0;
  651. ret = 0;
  652. for(i = 0; !DB_FLD_EMPTY(dst) && !DB_FLD_LAST(dst[i]); i++) {
  653. if (PQgetisnull(src, row, i)) {
  654. dst[i].flags |= DB_NULL;
  655. continue;
  656. } else {
  657. dst[i].flags &= ~DB_NULL;
  658. }
  659. type = PQftype(src, i);
  660. val = PQgetvalue(src, row, i);
  661. len = PQgetlength(src, row, i);
  662. switch(dst[i].type) {
  663. case DB_INT:
  664. if (type == types[PG_INT2].oid)
  665. ret |= pg_int2_2_db_int(dst + i, val, len);
  666. else if (type == types[PG_INT4].oid)
  667. ret |= pg_int4_2_db_int(dst + i, val, len);
  668. else if (type == types[PG_INT8].oid)
  669. ret |= pg_int8_2_db_int(dst + i, val, len);
  670. else if (type == types[PG_BOOL].oid)
  671. ret |= pg_bool2db_int(dst + i, val, len);
  672. else if (type == types[PG_INET].oid)
  673. ret |= pg_inet2db_int(dst + i, val, len);
  674. else if ((type == types[PG_TIMESTAMP].oid) ||
  675. (type == types[PG_TIMESTAMPTZ].oid))
  676. ret |= pg_timestamp2db_int(dst + i, val, len, flags);
  677. else if (type == types[PG_BIT].oid)
  678. ret |= pg_bit2db_int(dst + i, val, len);
  679. else if (type == types[PG_VARBIT].oid)
  680. ret |= pg_bit2db_int(dst + i, val, len);
  681. else goto bug;
  682. break;
  683. case DB_FLOAT:
  684. if (type == types[PG_FLOAT4].oid)
  685. ret |= pg_float42db_float(dst + i, val, len);
  686. else goto bug;
  687. break;
  688. case DB_DOUBLE:
  689. if (type == types[PG_FLOAT4].oid)
  690. ret |= pg_float42db_double(dst + i, val, len);
  691. else if (type == types[PG_FLOAT8].oid)
  692. ret |= pg_float82db_double(dst + i, val, len);
  693. else goto bug;
  694. break;
  695. case DB_DATETIME:
  696. if (type == types[PG_INT2].oid)
  697. ret |= pg_int2_2_db_int(dst + i, val, len);
  698. else if (type == types[PG_INT4].oid)
  699. ret |= pg_int4_2_db_int(dst + i, val, len);
  700. else if ((type == types[PG_TIMESTAMP].oid) ||
  701. (type == types[PG_TIMESTAMPTZ].oid))
  702. ret |= pg_timestamp2db_int(dst + i, val, len, flags);
  703. else goto bug;
  704. break;
  705. case DB_CSTR:
  706. if ((type == types[PG_CHAR].oid) ||
  707. (type == types[PG_TEXT].oid) ||
  708. (type == types[PG_BPCHAR].oid) ||
  709. (type == types[PG_VARCHAR].oid))
  710. ret |= pg_string2db_cstr(dst + i, val, len);
  711. else if (type == types[PG_INT2].oid)
  712. ret |= pg_int2_2_db_cstr(dst + i, val, len);
  713. else if (type == types[PG_INT4].oid)
  714. ret |= pg_int4_2_db_cstr(dst + i, val, len);
  715. else goto bug;
  716. break;
  717. case DB_STR:
  718. case DB_BLOB:
  719. if ((type == types[PG_BYTE].oid) ||
  720. (type == types[PG_CHAR].oid) ||
  721. (type == types[PG_TEXT].oid) ||
  722. (type == types[PG_BPCHAR].oid) ||
  723. (type == types[PG_VARCHAR].oid))
  724. ret |= pg_string2db_str(dst + i, val, len);
  725. else if (type == types[PG_INT2].oid)
  726. ret |= pg_int2_2_db_str(dst + i, val, len);
  727. else if (type == types[PG_INT4].oid)
  728. ret |= pg_int4_2_db_str(dst + i, val, len);
  729. else goto bug;
  730. break;
  731. case DB_BITMAP:
  732. if (type == types[PG_INT2].oid)
  733. ret |= pg_int2_2_db_int(dst + i, val, len);
  734. else if (type == types[PG_INT4].oid)
  735. ret |= pg_int4_2_db_int(dst + i, val, len);
  736. else if (type == types[PG_INT8].oid)
  737. ret |= pg_int8_2_db_int(dst + i, val, len);
  738. else if (type == types[PG_BIT].oid)
  739. ret |= pg_bit2db_int(dst + i, val, len);
  740. else if (type == types[PG_VARBIT].oid)
  741. ret |= pg_bit2db_int(dst + i, val, len);
  742. else goto bug;
  743. break;
  744. default:
  745. BUG("postgres: Unsupported field type %d in field %s\n",
  746. dst[i].type, dst[i].name);
  747. return -1;
  748. }
  749. }
  750. return ret;
  751. bug:
  752. BUG("postgres: Error while converting Postgres Oid %d to DB API type %d\n",
  753. type, dst[i].type);
  754. return -1;
  755. }
  756. /** @} */