pg_fld.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. db_int2pg_timestamp(dst, off + i, src + i, flags);
  243. else if (pfld->oid == types[PG_INT8].oid)
  244. db_int2pg_int8(dst, off + i, src + i);
  245. else if (pfld->oid == types[PG_INET].oid)
  246. db_int2pg_inet(dst, off + i, src + i);
  247. else if (pfld->oid == types[PG_BOOL].oid)
  248. db_int2pg_bool(dst, off + i, src + i);
  249. else if (pfld->oid == types[PG_BIT].oid)
  250. db_int2pg_bit(dst, off + i, src + i);
  251. else if (pfld->oid == types[PG_VARBIT].oid)
  252. db_int2pg_bit(dst, off + i, src + i);
  253. else goto bug;
  254. break;
  255. case DB_BITMAP:
  256. if (pfld->oid == types[PG_INT4].oid)
  257. db_int2pg_int4(dst, off + i, src + i);
  258. else if (pfld->oid == types[PG_INT8].oid)
  259. db_int2pg_int8(dst, off + i, src + i);
  260. else if (pfld->oid == types[PG_BIT].oid)
  261. db_int2pg_bit(dst, off + i, src + i);
  262. else if (pfld->oid == types[PG_VARBIT].oid)
  263. db_int2pg_bit(dst, off + i, src + i);
  264. else goto bug;
  265. break;
  266. case DB_DATETIME:
  267. if (pfld->oid == types[PG_INT4].oid)
  268. db_int2pg_int4(dst, off + i, src + i);
  269. else if (pfld->oid == types[PG_TIMESTAMP].oid)
  270. db_int2pg_timestamp(dst, off + i, src + i, flags);
  271. else if (pfld->oid == types[PG_INT8].oid)
  272. db_int2pg_int8(dst, off + i, src + i);
  273. else goto bug;
  274. break;
  275. case DB_FLOAT:
  276. if (pfld->oid == types[PG_FLOAT4].oid)
  277. db_float2pg_float4(dst, off + i, src + i);
  278. else if (pfld->oid == types[PG_FLOAT8].oid)
  279. db_float2pg_float8(dst, off + i, src + i);
  280. else goto bug;
  281. break;
  282. case DB_DOUBLE:
  283. if (pfld->oid == types[PG_FLOAT4].oid)
  284. db_double2pg_float4(dst, off + i, src + i);
  285. else if (pfld->oid == types[PG_FLOAT8].oid)
  286. db_double2pg_float8(dst, off + i, src + i);
  287. else goto bug;
  288. break;
  289. case DB_STR:
  290. if (pfld->oid == types[PG_VARCHAR].oid ||
  291. pfld->oid == types[PG_BYTE].oid ||
  292. pfld->oid == types[PG_CHAR].oid ||
  293. pfld->oid == types[PG_TEXT].oid ||
  294. pfld->oid == types[PG_BPCHAR].oid)
  295. db_str2pg_string(dst, off + i, src + i);
  296. else goto bug;
  297. break;
  298. case DB_CSTR:
  299. if (pfld->oid == types[PG_VARCHAR].oid ||
  300. pfld->oid == types[PG_BYTE].oid ||
  301. pfld->oid == types[PG_CHAR].oid ||
  302. pfld->oid == types[PG_TEXT].oid ||
  303. pfld->oid == types[PG_BPCHAR].oid)
  304. db_cstr2pg_string(dst, off + i, src + i);
  305. else goto bug;
  306. break;
  307. case DB_BLOB:
  308. if (pfld->oid == types[PG_BYTE].oid)
  309. db_str2pg_string(dst, off + i, src + i);
  310. else goto bug;
  311. break;
  312. default:
  313. BUG("postgres: Unsupported field type %d in field %s\n",
  314. src[i].type, src[i].name);
  315. return -1;
  316. }
  317. }
  318. return 0;
  319. bug:
  320. BUG("postgres: Error while converting DB API type %d to Postgres Oid %d\n",
  321. src[i].type, pfld->oid);
  322. return -1;
  323. }
  324. int pg_check_fld2pg(db_fld_t* fld, pg_type_t* types)
  325. {
  326. int i;
  327. const char* name = "UNKNOWN";
  328. struct pg_fld* pfld;
  329. if (fld == NULL) return 0;
  330. for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
  331. pfld = DB_GET_PAYLOAD(fld + i);
  332. switch(fld[i].type) {
  333. case DB_INT:
  334. if (pfld->oid == types[PG_INT2].oid) continue;
  335. if (pfld->oid == types[PG_INT4].oid) continue;
  336. if (pfld->oid == types[PG_INT8].oid) continue;
  337. if (pfld->oid == types[PG_BOOL].oid) continue;
  338. if (pfld->oid == types[PG_INET].oid) continue;
  339. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  340. if (pfld->oid == types[PG_BIT].oid) continue;
  341. if (pfld->oid == types[PG_VARBIT].oid) continue;
  342. break;
  343. case DB_BITMAP:
  344. if (pfld->oid == types[PG_INT4].oid) continue;
  345. if (pfld->oid == types[PG_INT8].oid) continue;
  346. if (pfld->oid == types[PG_BIT].oid) continue;
  347. if (pfld->oid == types[PG_VARBIT].oid) continue;
  348. break;
  349. case DB_FLOAT:
  350. case DB_DOUBLE:
  351. if (pfld->oid == types[PG_FLOAT4].oid) continue;
  352. if (pfld->oid == types[PG_FLOAT8].oid) continue;
  353. break;
  354. case DB_CSTR:
  355. case DB_STR:
  356. if (pfld->oid == types[PG_BYTE].oid) continue;
  357. if (pfld->oid == types[PG_CHAR].oid) continue;
  358. if (pfld->oid == types[PG_TEXT].oid) continue;
  359. if (pfld->oid == types[PG_BPCHAR].oid) continue;
  360. if (pfld->oid == types[PG_VARCHAR].oid) continue;
  361. break;
  362. case DB_BLOB:
  363. if (pfld->oid == types[PG_BYTE].oid) continue;
  364. break;
  365. case DB_DATETIME:
  366. if (pfld->oid == types[PG_INT4].oid) continue;
  367. if (pfld->oid == types[PG_INT8].oid) continue;
  368. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  369. break;
  370. default:
  371. BUG("postgres: Unsupported field type %d, bug in postgres module\n",
  372. fld[i].type);
  373. return -1;
  374. }
  375. pg_oid2name(&name, types, pfld->oid);
  376. ERR("postgres: Cannot convert column '%s' of type %s "
  377. "to PostgreSQL column type '%s'\n",
  378. fld[i].name, db_fld_str[fld[i].type], name);
  379. return -1;
  380. }
  381. return 0;
  382. }
  383. int pg_resolve_param_oids(db_fld_t* vals, db_fld_t* match, int n1, int n2, PGresult* types)
  384. {
  385. struct pg_fld* pfld;
  386. int i;
  387. if (n1 + n2 != PQnparams(types)) {
  388. ERR("postgres: Number of command parameters do not match\n");
  389. return -1;
  390. }
  391. for(i = 0; i < n1; i++) {
  392. pfld = DB_GET_PAYLOAD(vals + i);
  393. pfld->oid = PQparamtype(types, i);
  394. }
  395. for(i = 0; i < n2; i++) {
  396. pfld = DB_GET_PAYLOAD(match + i);
  397. pfld->oid = PQparamtype(types, n1 + i);
  398. }
  399. return 0;
  400. }
  401. int pg_resolve_result_oids(db_fld_t* fld, int n, PGresult* types)
  402. {
  403. struct pg_fld* pfld;
  404. int i;
  405. if (fld == NULL) return 0;
  406. if (n != PQnfields(types)) {
  407. ERR("postgres: Result field numbers do not match\n");
  408. return -1;
  409. }
  410. for(i = 0; i < n; i++) {
  411. pfld = DB_GET_PAYLOAD(fld + i);
  412. pfld->oid = PQftype(types, i);
  413. }
  414. return 0;
  415. }
  416. int pg_check_pg2fld(db_fld_t* fld, pg_type_t* types)
  417. {
  418. int i;
  419. const char* name = "UNKNOWN";
  420. struct pg_fld* pfld;
  421. if (fld == NULL) return 0;
  422. for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
  423. pfld = DB_GET_PAYLOAD(fld + i);
  424. if (pfld->oid == 0) {
  425. ERR("postgres: Unknown type fields not supported\n");
  426. return -1;
  427. }
  428. switch(fld[i].type) {
  429. case DB_INT:
  430. if (pfld->oid == types[PG_INT2].oid) continue;
  431. if (pfld->oid == types[PG_INT4].oid) continue;
  432. if (pfld->oid == types[PG_INT8].oid) continue;
  433. if (pfld->oid == types[PG_BOOL].oid) continue;
  434. if (pfld->oid == types[PG_INET].oid) continue;
  435. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  436. if (pfld->oid == types[PG_BIT].oid) continue;
  437. if (pfld->oid == types[PG_VARBIT].oid) continue;
  438. break;
  439. case DB_BITMAP:
  440. if (pfld->oid == types[PG_INT2].oid) continue;
  441. if (pfld->oid == types[PG_INT4].oid) continue;
  442. if (pfld->oid == types[PG_INT8].oid) continue;
  443. if (pfld->oid == types[PG_BIT].oid) continue;
  444. if (pfld->oid == types[PG_VARBIT].oid) continue;
  445. break;
  446. case DB_FLOAT:
  447. if (pfld->oid == types[PG_FLOAT4].oid) continue;
  448. break;
  449. case DB_DOUBLE:
  450. if (pfld->oid == types[PG_FLOAT4].oid) continue;
  451. if (pfld->oid == types[PG_FLOAT8].oid) continue;
  452. break;
  453. case DB_CSTR:
  454. if (pfld->oid == types[PG_CHAR].oid) continue;
  455. if (pfld->oid == types[PG_TEXT].oid) continue;
  456. if (pfld->oid == types[PG_BPCHAR].oid) continue;
  457. if (pfld->oid == types[PG_VARCHAR].oid) continue;
  458. if (pfld->oid == types[PG_INT2].oid) continue;
  459. if (pfld->oid == types[PG_INT4].oid) continue;
  460. break;
  461. case DB_STR:
  462. case DB_BLOB:
  463. if (pfld->oid == types[PG_BYTE].oid) continue;
  464. if (pfld->oid == types[PG_CHAR].oid) continue;
  465. if (pfld->oid == types[PG_TEXT].oid) continue;
  466. if (pfld->oid == types[PG_BPCHAR].oid) continue;
  467. if (pfld->oid == types[PG_VARCHAR].oid) continue;
  468. if (pfld->oid == types[PG_INT2].oid) continue;
  469. if (pfld->oid == types[PG_INT4].oid) continue;
  470. break;
  471. case DB_DATETIME:
  472. if (pfld->oid == types[PG_INT2].oid) continue;
  473. if (pfld->oid == types[PG_INT4].oid) continue;
  474. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  475. break;
  476. default:
  477. BUG("postgres: Unsupported field type %d, bug in postgres module\n",
  478. fld[i].type);
  479. return -1;
  480. }
  481. pg_oid2name(&name, types, pfld->oid);
  482. ERR("postgres: Cannot convert column '%s' of type %s "
  483. "to DB API field of type %s\n",
  484. fld[i].name, name, db_fld_str[fld[i].type]);
  485. return -1;
  486. }
  487. return 0;
  488. }
  489. static inline int pg_int2_2_db_cstr(db_fld_t* fld, char* val, int len)
  490. {
  491. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  492. int size, v;
  493. v = (int16_t)ntohs(*((int16_t*)val));
  494. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  495. if (size < 0 || size >= INT2STR_MAX_LEN) {
  496. BUG("postgres: Error while converting integer to string\n");
  497. return -1;
  498. }
  499. fld->v.cstr = pfld->buf;
  500. return 0;
  501. }
  502. static inline int pg_int4_2_db_cstr(db_fld_t* fld, char* val, int len)
  503. {
  504. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  505. int size, v;
  506. v = (int16_t)ntohs(*((int32_t*)val));
  507. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  508. if (len < 0 || size >= INT2STR_MAX_LEN) {
  509. BUG("postgres: Error while converting integer to string\n");
  510. return -1;
  511. }
  512. fld->v.cstr = pfld->buf;
  513. return 0;
  514. }
  515. static inline int pg_int2_2_db_str(db_fld_t* fld, char* val, int len)
  516. {
  517. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  518. int size, v;
  519. v = (int16_t)ntohs(*((int16_t*)val));
  520. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  521. if (size < 0 || size >= INT2STR_MAX_LEN) {
  522. BUG("postgres: Error while converting integer to string\n");
  523. return -1;
  524. }
  525. fld->v.lstr.s = pfld->buf;
  526. fld->v.lstr.len = size;
  527. return 0;
  528. }
  529. static inline int pg_int4_2_db_str(db_fld_t* fld, char* val, int len)
  530. {
  531. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  532. int size, v;
  533. v = (int16_t)ntohs(*((int32_t*)val));
  534. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  535. if (size < 0 || size >= INT2STR_MAX_LEN) {
  536. BUG("postgres: Error while converting integer to string\n");
  537. return -1;
  538. }
  539. fld->v.lstr.s = pfld->buf;
  540. fld->v.lstr.len = size;
  541. return 0;
  542. }
  543. static inline int pg_int2_2_db_int(db_fld_t* fld, char* val, int len)
  544. {
  545. fld->v.int4 = (int16_t)ntohs(*((int16_t*)val));
  546. return 0;
  547. }
  548. static inline int pg_int4_2_db_int(db_fld_t* fld, char* val, int len)
  549. {
  550. fld->v.int4 = (int32_t)ntohl(*((int32_t*)val));
  551. return 0;
  552. }
  553. static inline int pg_int8_2_db_int(db_fld_t* fld, char* val, int len)
  554. {
  555. fld->v.int8 = (int64_t)ntohll(*((int64_t*)val));
  556. return 0;
  557. }
  558. static inline int pg_bool2db_int(db_fld_t* fld, char* val, int len)
  559. {
  560. fld->v.int4 = val[0];
  561. return 0;
  562. }
  563. static inline int pg_inet2db_int(db_fld_t* fld, char* val, int len)
  564. {
  565. if (len != 8 || val[2] != 0) {
  566. ERR("postgres: Unsupported 'inet' format, column %s\n", fld->name);
  567. return -1;
  568. }
  569. if (val[0] != AF_INET) {
  570. ERR("postgres: Unsupported address family %d in field %s\n",
  571. val[0], fld->name);
  572. return -1;
  573. }
  574. if (val[1] != 32) {
  575. WARN("postgres: Netmasks shorter than 32-bits not supported, "
  576. "column %s\n", fld->name);
  577. }
  578. if (val[3] != 4) {
  579. ERR("postgres: Unsupported IP address size %d in column %s\n",
  580. val[3], fld->name);
  581. return -1;
  582. }
  583. fld->v.int4 = (int32_t)ntohl(((int32_t*)val)[1]);
  584. return 0;
  585. }
  586. static inline int pg_timestamp2db_int(db_fld_t* fld, char* val, int len,
  587. unsigned int flags)
  588. {
  589. if (flags & PG_INT8_TIMESTAMP) {
  590. /* int8 format */
  591. fld->v.int4 = (int64_t)ntohll(((int64_t*)val)[0]) / (int64_t)1000000 + PG_EPOCH_TIME;
  592. } else {
  593. /* double format */
  594. fld->v.int4 = PG_EPOCH_TIME + ntohll(((int64_t*)val)[0]);
  595. }
  596. return 0;
  597. }
  598. static inline int pg_bit2db_int(db_fld_t* fld, char* val, int len)
  599. {
  600. int size;
  601. size = ntohl(*(uint32_t*)val);
  602. if (size != 32) {
  603. ERR("postgres: Unsupported bit field size (%d), column %s\n",
  604. size, fld->name);
  605. return -1;
  606. }
  607. fld->v.int4 = ntohl(((uint32_t*)val)[1]);
  608. return 0;
  609. }
  610. static inline int pg_float42db_float(db_fld_t* fld, char* val, int len)
  611. {
  612. fld->v.int4 = (uint32_t)ntohl(*(uint32_t*)val);
  613. return 0;
  614. }
  615. static inline int pg_float42db_double(db_fld_t* fld, char* val, int len)
  616. {
  617. float tmp;
  618. tmp = ntohl(*(uint32_t*)val);
  619. fld->v.dbl = tmp;
  620. return 0;
  621. }
  622. static inline int pg_float82db_double(db_fld_t* fld, char* val, int len)
  623. {
  624. fld->v.int8 = ntohll(*(uint64_t*)val);
  625. return 0;
  626. }
  627. static inline int pg_string2db_cstr(db_fld_t* fld, char* val, int len)
  628. {
  629. fld->v.cstr = val;
  630. return 0;
  631. }
  632. static inline int pg_string2db_str(db_fld_t* fld, char* val, int len)
  633. {
  634. fld->v.lstr.s = val;
  635. fld->v.lstr.len = len;
  636. return 0;
  637. }
  638. int pg_pg2fld(db_fld_t* dst, PGresult* src, int row,
  639. pg_type_t* types, unsigned int flags)
  640. {
  641. char* val;
  642. int i, len, ret;
  643. Oid type;
  644. if (dst == NULL || src == NULL) return 0;
  645. ret = 0;
  646. for(i = 0; !DB_FLD_EMPTY(dst) && !DB_FLD_LAST(dst[i]); i++) {
  647. if (PQgetisnull(src, row, i)) {
  648. dst[i].flags |= DB_NULL;
  649. continue;
  650. } else {
  651. dst[i].flags &= ~DB_NULL;
  652. }
  653. type = PQftype(src, i);
  654. val = PQgetvalue(src, row, i);
  655. len = PQgetlength(src, row, i);
  656. switch(dst[i].type) {
  657. case DB_INT:
  658. if (type == types[PG_INT2].oid)
  659. ret |= pg_int2_2_db_int(dst + i, val, len);
  660. else if (type == types[PG_INT4].oid)
  661. ret |= pg_int4_2_db_int(dst + i, val, len);
  662. else if (type == types[PG_INT8].oid)
  663. ret |= pg_int8_2_db_int(dst + i, val, len);
  664. else if (type == types[PG_BOOL].oid)
  665. ret |= pg_bool2db_int(dst + i, val, len);
  666. else if (type == types[PG_INET].oid)
  667. ret |= pg_inet2db_int(dst + i, val, len);
  668. else if (type == types[PG_TIMESTAMP].oid)
  669. ret |= pg_timestamp2db_int(dst + i, val, len, flags);
  670. else if (type == types[PG_BIT].oid)
  671. ret |= pg_bit2db_int(dst + i, val, len);
  672. else if (type == types[PG_VARBIT].oid)
  673. ret |= pg_bit2db_int(dst + i, val, len);
  674. else goto bug;
  675. break;
  676. case DB_FLOAT:
  677. if (type == types[PG_FLOAT4].oid)
  678. ret |= pg_float42db_float(dst + i, val, len);
  679. else goto bug;
  680. break;
  681. case DB_DOUBLE:
  682. if (type == types[PG_FLOAT4].oid)
  683. ret |= pg_float42db_double(dst + i, val, len);
  684. else if (type == types[PG_FLOAT8].oid)
  685. ret |= pg_float82db_double(dst + i, val, len);
  686. else goto bug;
  687. break;
  688. case DB_DATETIME:
  689. if (type == types[PG_INT2].oid)
  690. ret |= pg_int2_2_db_int(dst + i, val, len);
  691. else if (type == types[PG_INT4].oid)
  692. ret |= pg_int4_2_db_int(dst + i, val, len);
  693. else if (type == types[PG_TIMESTAMP].oid)
  694. ret |= pg_timestamp2db_int(dst + i, val, len, flags);
  695. else goto bug;
  696. break;
  697. case DB_CSTR:
  698. if ((type == types[PG_CHAR].oid) ||
  699. (type == types[PG_TEXT].oid) ||
  700. (type == types[PG_BPCHAR].oid) ||
  701. (type == types[PG_VARCHAR].oid))
  702. ret |= pg_string2db_cstr(dst + i, val, len);
  703. else if (type == types[PG_INT2].oid)
  704. ret |= pg_int2_2_db_cstr(dst + i, val, len);
  705. else if (type == types[PG_INT4].oid)
  706. ret |= pg_int4_2_db_cstr(dst + i, val, len);
  707. else goto bug;
  708. break;
  709. case DB_STR:
  710. case DB_BLOB:
  711. if ((type == types[PG_BYTE].oid) ||
  712. (type == types[PG_CHAR].oid) ||
  713. (type == types[PG_TEXT].oid) ||
  714. (type == types[PG_BPCHAR].oid) ||
  715. (type == types[PG_VARCHAR].oid))
  716. ret |= pg_string2db_str(dst + i, val, len);
  717. else if (type == types[PG_INT2].oid)
  718. ret |= pg_int2_2_db_str(dst + i, val, len);
  719. else if (type == types[PG_INT4].oid)
  720. ret |= pg_int4_2_db_str(dst + i, val, len);
  721. else goto bug;
  722. break;
  723. case DB_BITMAP:
  724. if (type == types[PG_INT2].oid)
  725. ret |= pg_int2_2_db_int(dst + i, val, len);
  726. else if (type == types[PG_INT4].oid)
  727. ret |= pg_int4_2_db_int(dst + i, val, len);
  728. else if (type == types[PG_INT8].oid)
  729. ret |= pg_int8_2_db_int(dst + i, val, len);
  730. else if (type == types[PG_BIT].oid)
  731. ret |= pg_bit2db_int(dst + i, val, len);
  732. else if (type == types[PG_VARBIT].oid)
  733. ret |= pg_bit2db_int(dst + i, val, len);
  734. else goto bug;
  735. break;
  736. default:
  737. BUG("postgres: Unsupported field type %d in field %s\n",
  738. dst[i].type, dst[i].name);
  739. return -1;
  740. }
  741. }
  742. return ret;
  743. bug:
  744. BUG("postgres: Error while converting Postgres Oid %d to DB API type %d\n",
  745. type, dst[i].type);
  746. return -1;
  747. }
  748. /** @} */