pg_fld.c 23 KB

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