pg_fld.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * Portions Copyright (C) 2001-2003 FhG FOKUS
  3. * Copyright (C) 2003 August.Net Services, LLC
  4. * Portions Copyright (C) 2005-2008 iptelorg GmbH
  5. *
  6. * This file is part of SER, a free SIP server.
  7. *
  8. * SER is free software; you can redistribute it and/or modify it under the
  9. * terms of the GNU General Public License as published by the Free Software
  10. * Foundation; either version 2 of the License, or (at your option) any later
  11. * version
  12. *
  13. * For a license to use the ser software under conditions other than those
  14. * described here, or to purchase support for this software, please contact
  15. * iptel.org by e-mail at the following addresses: [email protected]
  16. *
  17. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  18. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc., 59
  24. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. /*!
  27. * \file
  28. * \brief DB_POSTGRES :: Data field conversion and type checking functions.
  29. * \ingroup db_postgres
  30. * Module: \ref db_postgres
  31. */
  32. #include "pg_fld.h"
  33. #include "pg_con.h" /* flags */
  34. #include "pg_mod.h"
  35. #include "../../lib/srdb2/db_drv.h"
  36. #include "../../mem/mem.h"
  37. #include "../../dprint.h"
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <stdint.h>
  42. #include <string.h>
  43. /**
  44. * This is the epoch time in time_t format, this value is used to convert
  45. * timestamp values to/from PostgreSQL format.
  46. * 2000-01-01 00:00:00 +0000 as the value of time_t in UTC
  47. */
  48. #define PG_EPOCH_TIME ((int64_t)946684800)
  49. /** Frees memory used by a pg_fld structure.
  50. * This function frees all memory used by a pg_fld structure
  51. * @param fld Generic db_fld_t* structure being freed.
  52. * @param payload The postgresql extension structure to be freed
  53. */
  54. static void pg_fld_free(db_fld_t* fld, struct pg_fld* payload)
  55. {
  56. db_drv_free(&payload->gen);
  57. pkg_free(payload);
  58. }
  59. int pg_fld(db_fld_t* fld, char* table)
  60. {
  61. struct pg_fld* res;
  62. res = (struct pg_fld*)pkg_malloc(sizeof(struct pg_fld));
  63. if (res == NULL) {
  64. ERR("postgres: No memory left\n");
  65. return -1;
  66. }
  67. memset(res, '\0', sizeof(struct pg_fld));
  68. if (db_drv_init(&res->gen, pg_fld_free) < 0) goto error;
  69. DB_SET_PAYLOAD(fld, res);
  70. return 0;
  71. error:
  72. if (res) pkg_free(res);
  73. return -1;
  74. }
  75. union ull {
  76. uint64_t ui64;
  77. uint32_t ui32[2];
  78. };
  79. static inline uint64_t htonll(uint64_t in)
  80. {
  81. union ull* p = (union ull*)&in;
  82. return ((uint64_t)htonl(p->ui32[0]) << 32) + (uint64_t)htonl(p->ui32[1]);
  83. }
  84. static inline uint64_t ntohll(uint64_t in)
  85. {
  86. union ull* p = (union ull*)&in;
  87. return ((uint64_t)ntohl(p->ui32[0]) << 32) + (uint64_t)ntohl(p->ui32[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. static inline void db_str2pg_string(struct pg_params* dst, int i,
  196. db_fld_t* src)
  197. {
  198. dst->fmt[i] = 1;
  199. dst->val[i] = src->v.lstr.s;
  200. dst->len[i] = src->v.lstr.len;
  201. }
  202. static inline void db_cstr2pg_string(struct pg_params* dst, int i,
  203. db_fld_t* src)
  204. {
  205. dst->fmt[i] = 0;
  206. dst->val[i] = src->v.cstr;
  207. }
  208. int pg_fld2pg(struct pg_params* dst, int off, pg_type_t* types,
  209. db_fld_t* src, unsigned int flags)
  210. {
  211. int i;
  212. struct pg_fld* pfld;
  213. if (src == NULL) return 0;
  214. for(i = 0; !DB_FLD_EMPTY(src) && !DB_FLD_LAST(src[i]); i++) {
  215. pfld = DB_GET_PAYLOAD(src + i);
  216. /* NULL value */
  217. if (src[i].flags & DB_NULL) {
  218. dst->val[off + i] = NULL;
  219. dst->len[off + i] = 0;
  220. continue;
  221. }
  222. switch(src[i].type) {
  223. case DB_INT:
  224. if (pfld->oid == types[PG_INT2].oid)
  225. db_int2pg_int2(dst, off + i, src + i);
  226. else if (pfld->oid == types[PG_INT4].oid)
  227. db_int2pg_int4(dst, off + i, src + i);
  228. else if ((pfld->oid == types[PG_TIMESTAMP].oid) ||
  229. (pfld->oid == types[PG_TIMESTAMPTZ].oid))
  230. db_int2pg_timestamp(dst, off + i, src + i, flags);
  231. else if (pfld->oid == types[PG_INT8].oid)
  232. db_int2pg_int8(dst, off + i, src + i);
  233. else if (pfld->oid == types[PG_INET].oid)
  234. db_int2pg_inet(dst, off + i, src + i);
  235. else if (pfld->oid == types[PG_BOOL].oid)
  236. db_int2pg_bool(dst, off + i, src + i);
  237. else if (pfld->oid == types[PG_BIT].oid)
  238. db_int2pg_bit(dst, off + i, src + i);
  239. else if (pfld->oid == types[PG_VARBIT].oid)
  240. db_int2pg_bit(dst, off + i, src + i);
  241. else goto bug;
  242. break;
  243. case DB_BITMAP:
  244. if (pfld->oid == types[PG_INT4].oid)
  245. db_int2pg_int4(dst, off + i, src + i);
  246. else if (pfld->oid == types[PG_INT8].oid)
  247. db_int2pg_int8(dst, off + i, src + i);
  248. else if (pfld->oid == types[PG_BIT].oid)
  249. db_int2pg_bit(dst, off + i, src + i);
  250. else if (pfld->oid == types[PG_VARBIT].oid)
  251. db_int2pg_bit(dst, off + i, src + i);
  252. else goto bug;
  253. break;
  254. case DB_DATETIME:
  255. if (pfld->oid == types[PG_INT4].oid)
  256. db_int2pg_int4(dst, off + i, src + i);
  257. else if ((pfld->oid == types[PG_TIMESTAMP].oid) ||
  258. (pfld->oid == types[PG_TIMESTAMPTZ].oid))
  259. db_int2pg_timestamp(dst, off + i, src + i, flags);
  260. else if (pfld->oid == types[PG_INT8].oid)
  261. db_int2pg_int8(dst, off + i, src + i);
  262. else goto bug;
  263. break;
  264. case DB_FLOAT:
  265. if (pfld->oid == types[PG_FLOAT4].oid)
  266. db_float2pg_float4(dst, off + i, src + i);
  267. else if (pfld->oid == types[PG_FLOAT8].oid)
  268. db_float2pg_float8(dst, off + i, src + i);
  269. else goto bug;
  270. break;
  271. case DB_DOUBLE:
  272. if (pfld->oid == types[PG_FLOAT4].oid)
  273. db_double2pg_float4(dst, off + i, src + i);
  274. else if (pfld->oid == types[PG_FLOAT8].oid)
  275. db_double2pg_float8(dst, off + i, src + i);
  276. else goto bug;
  277. break;
  278. case DB_STR:
  279. if (pfld->oid == types[PG_VARCHAR].oid ||
  280. pfld->oid == types[PG_BYTE].oid ||
  281. pfld->oid == types[PG_CHAR].oid ||
  282. pfld->oid == types[PG_TEXT].oid ||
  283. pfld->oid == types[PG_BPCHAR].oid)
  284. db_str2pg_string(dst, off + i, src + i);
  285. else goto bug;
  286. break;
  287. case DB_CSTR:
  288. if (pfld->oid == types[PG_VARCHAR].oid ||
  289. pfld->oid == types[PG_BYTE].oid ||
  290. pfld->oid == types[PG_CHAR].oid ||
  291. pfld->oid == types[PG_TEXT].oid ||
  292. pfld->oid == types[PG_BPCHAR].oid)
  293. db_cstr2pg_string(dst, off + i, src + i);
  294. else goto bug;
  295. break;
  296. case DB_BLOB:
  297. if (pfld->oid == types[PG_BYTE].oid)
  298. db_str2pg_string(dst, off + i, src + i);
  299. else goto bug;
  300. break;
  301. default:
  302. BUG("postgres: Unsupported field type %d in field %s\n",
  303. src[i].type, src[i].name);
  304. return -1;
  305. }
  306. }
  307. return 0;
  308. bug:
  309. BUG("postgres: Error while converting DB API type %d to Postgres Oid %d\n",
  310. src[i].type, pfld->oid);
  311. return -1;
  312. }
  313. int pg_check_fld2pg(db_fld_t* fld, pg_type_t* types)
  314. {
  315. int i;
  316. const char* name = "UNKNOWN";
  317. struct pg_fld* pfld;
  318. if (fld == NULL) return 0;
  319. for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
  320. pfld = DB_GET_PAYLOAD(fld + i);
  321. switch(fld[i].type) {
  322. case DB_INT:
  323. if (pfld->oid == types[PG_INT2].oid) continue;
  324. if (pfld->oid == types[PG_INT4].oid) continue;
  325. if (pfld->oid == types[PG_INT8].oid) continue;
  326. if (pfld->oid == types[PG_BOOL].oid) continue;
  327. if (pfld->oid == types[PG_INET].oid) continue;
  328. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  329. if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
  330. if (pfld->oid == types[PG_BIT].oid) continue;
  331. if (pfld->oid == types[PG_VARBIT].oid) continue;
  332. break;
  333. case DB_BITMAP:
  334. if (pfld->oid == types[PG_INT4].oid) continue;
  335. if (pfld->oid == types[PG_INT8].oid) continue;
  336. if (pfld->oid == types[PG_BIT].oid) continue;
  337. if (pfld->oid == types[PG_VARBIT].oid) continue;
  338. break;
  339. case DB_FLOAT:
  340. case DB_DOUBLE:
  341. if (pfld->oid == types[PG_FLOAT4].oid) continue;
  342. if (pfld->oid == types[PG_FLOAT8].oid) continue;
  343. break;
  344. case DB_CSTR:
  345. case DB_STR:
  346. if (pfld->oid == types[PG_BYTE].oid) continue;
  347. if (pfld->oid == types[PG_CHAR].oid) continue;
  348. if (pfld->oid == types[PG_TEXT].oid) continue;
  349. if (pfld->oid == types[PG_BPCHAR].oid) continue;
  350. if (pfld->oid == types[PG_VARCHAR].oid) continue;
  351. break;
  352. case DB_BLOB:
  353. if (pfld->oid == types[PG_BYTE].oid) continue;
  354. break;
  355. case DB_DATETIME:
  356. if (pfld->oid == types[PG_INT4].oid) continue;
  357. if (pfld->oid == types[PG_INT8].oid) continue;
  358. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  359. if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
  360. break;
  361. default:
  362. BUG("postgres: Unsupported field type %d, bug in postgres module\n",
  363. fld[i].type);
  364. return -1;
  365. }
  366. pg_oid2name(&name, types, pfld->oid);
  367. ERR("postgres: Cannot convert column '%s' of type %s "
  368. "to PostgreSQL column type '%s'\n",
  369. fld[i].name, db_fld_str[fld[i].type], name);
  370. return -1;
  371. }
  372. return 0;
  373. }
  374. int pg_resolve_param_oids(db_fld_t* vals, db_fld_t* match, int n1, int n2, PGresult* types)
  375. {
  376. struct pg_fld* pfld;
  377. int i;
  378. if (n1 + n2 != PQnparams(types)) {
  379. ERR("postgres: Number of command parameters do not match\n");
  380. return -1;
  381. }
  382. for(i = 0; i < n1; i++) {
  383. pfld = DB_GET_PAYLOAD(vals + i);
  384. pfld->oid = PQparamtype(types, i);
  385. }
  386. for(i = 0; i < n2; i++) {
  387. pfld = DB_GET_PAYLOAD(match + i);
  388. pfld->oid = PQparamtype(types, n1 + i);
  389. }
  390. return 0;
  391. }
  392. int pg_resolve_result_oids(db_fld_t* fld, int n, PGresult* types)
  393. {
  394. struct pg_fld* pfld;
  395. int i;
  396. if (fld == NULL) return 0;
  397. if (n != PQnfields(types)) {
  398. ERR("postgres: Result field numbers do not match\n");
  399. return -1;
  400. }
  401. for(i = 0; i < n; i++) {
  402. pfld = DB_GET_PAYLOAD(fld + i);
  403. pfld->oid = PQftype(types, i);
  404. }
  405. return 0;
  406. }
  407. int pg_check_pg2fld(db_fld_t* fld, pg_type_t* types)
  408. {
  409. int i;
  410. const char* name = "UNKNOWN";
  411. struct pg_fld* pfld;
  412. if (fld == NULL) return 0;
  413. for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
  414. pfld = DB_GET_PAYLOAD(fld + i);
  415. if (pfld->oid == 0) {
  416. ERR("postgres: Unknown type fields not supported\n");
  417. return -1;
  418. }
  419. switch(fld[i].type) {
  420. case DB_INT:
  421. if (pfld->oid == types[PG_INT2].oid) continue;
  422. if (pfld->oid == types[PG_INT4].oid) continue;
  423. if (pfld->oid == types[PG_INT8].oid) continue;
  424. if (pfld->oid == types[PG_BOOL].oid) continue;
  425. if (pfld->oid == types[PG_INET].oid) continue;
  426. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  427. if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
  428. if (pfld->oid == types[PG_BIT].oid) continue;
  429. if (pfld->oid == types[PG_VARBIT].oid) continue;
  430. break;
  431. case DB_BITMAP:
  432. if (pfld->oid == types[PG_INT2].oid) continue;
  433. if (pfld->oid == types[PG_INT4].oid) continue;
  434. if (pfld->oid == types[PG_INT8].oid) continue;
  435. if (pfld->oid == types[PG_BIT].oid) continue;
  436. if (pfld->oid == types[PG_VARBIT].oid) continue;
  437. break;
  438. case DB_FLOAT:
  439. if (pfld->oid == types[PG_FLOAT4].oid) continue;
  440. break;
  441. case DB_DOUBLE:
  442. if (pfld->oid == types[PG_FLOAT4].oid) continue;
  443. if (pfld->oid == types[PG_FLOAT8].oid) continue;
  444. break;
  445. case DB_CSTR:
  446. if (pfld->oid == types[PG_CHAR].oid) continue;
  447. if (pfld->oid == types[PG_TEXT].oid) continue;
  448. if (pfld->oid == types[PG_BPCHAR].oid) continue;
  449. if (pfld->oid == types[PG_VARCHAR].oid) continue;
  450. if (pfld->oid == types[PG_INT2].oid) continue;
  451. if (pfld->oid == types[PG_INT4].oid) continue;
  452. break;
  453. case DB_STR:
  454. case DB_BLOB:
  455. if (pfld->oid == types[PG_BYTE].oid) continue;
  456. if (pfld->oid == types[PG_CHAR].oid) continue;
  457. if (pfld->oid == types[PG_TEXT].oid) continue;
  458. if (pfld->oid == types[PG_BPCHAR].oid) continue;
  459. if (pfld->oid == types[PG_VARCHAR].oid) continue;
  460. if (pfld->oid == types[PG_INT2].oid) continue;
  461. if (pfld->oid == types[PG_INT4].oid) continue;
  462. break;
  463. case DB_DATETIME:
  464. if (pfld->oid == types[PG_INT2].oid) continue;
  465. if (pfld->oid == types[PG_INT4].oid) continue;
  466. if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
  467. if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
  468. break;
  469. default:
  470. BUG("postgres: Unsupported field type %d, bug in postgres module\n",
  471. fld[i].type);
  472. return -1;
  473. }
  474. pg_oid2name(&name, types, pfld->oid);
  475. ERR("postgres: Cannot convert column '%s' of type %s "
  476. "to DB API field of type %s\n",
  477. fld[i].name, name, db_fld_str[fld[i].type]);
  478. return -1;
  479. }
  480. return 0;
  481. }
  482. static inline int pg_int2_2_db_cstr(db_fld_t* fld, char* val, int len)
  483. {
  484. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  485. int size, v;
  486. v = (int16_t)ntohs(*((int16_t*)val));
  487. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  488. if (size < 0 || size >= INT2STR_MAX_LEN) {
  489. BUG("postgres: Error while converting integer to string\n");
  490. return -1;
  491. }
  492. fld->v.cstr = pfld->buf;
  493. return 0;
  494. }
  495. static inline int pg_int4_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 = (int32_t)ntohl(*((int32_t*)val));
  500. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  501. if (len < 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_int2_2_db_str(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(*((int16_t*)val));
  513. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  514. if (size < 0 || size >= INT2STR_MAX_LEN) {
  515. BUG("postgres: Error while converting integer to string\n");
  516. return -1;
  517. }
  518. fld->v.lstr.s = pfld->buf;
  519. fld->v.lstr.len = size;
  520. return 0;
  521. }
  522. static inline int pg_int4_2_db_str(db_fld_t* fld, char* val, int len)
  523. {
  524. struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
  525. int size, v;
  526. v = (int32_t)ntohl(*((int32_t*)val));
  527. size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
  528. if (size < 0 || size >= INT2STR_MAX_LEN) {
  529. BUG("postgres: Error while converting integer to string\n");
  530. return -1;
  531. }
  532. fld->v.lstr.s = pfld->buf;
  533. fld->v.lstr.len = size;
  534. return 0;
  535. }
  536. static inline int pg_int2_2_db_int(db_fld_t* fld, char* val, int len)
  537. {
  538. fld->v.int4 = (int16_t)ntohs(*((int16_t*)val));
  539. return 0;
  540. }
  541. static inline int pg_int4_2_db_int(db_fld_t* fld, char* val, int len)
  542. {
  543. fld->v.int4 = (int32_t)ntohl(*((int32_t*)val));
  544. return 0;
  545. }
  546. static inline int pg_int8_2_db_int(db_fld_t* fld, char* val, int len)
  547. {
  548. fld->v.int8 = (int64_t)ntohll(*((int64_t*)val));
  549. return 0;
  550. }
  551. static inline int pg_bool2db_int(db_fld_t* fld, char* val, int len)
  552. {
  553. fld->v.int4 = val[0];
  554. return 0;
  555. }
  556. static inline int pg_inet2db_int(db_fld_t* fld, char* val, int len)
  557. {
  558. if (len != 8 || val[2] != 0) {
  559. ERR("postgres: Unsupported 'inet' format, column %s\n", fld->name);
  560. return -1;
  561. }
  562. if (val[0] != AF_INET) {
  563. ERR("postgres: Unsupported address family %d in field %s\n",
  564. val[0], fld->name);
  565. return -1;
  566. }
  567. if (val[1] != 32) {
  568. WARN("postgres: Netmasks shorter than 32-bits not supported, "
  569. "column %s\n", fld->name);
  570. }
  571. if (val[3] != 4) {
  572. ERR("postgres: Unsupported IP address size %d in column %s\n",
  573. val[3], fld->name);
  574. return -1;
  575. }
  576. fld->v.int4 = (int32_t)ntohl(((int32_t*)val)[1]);
  577. return 0;
  578. }
  579. static inline int pg_timestamp2db_int(db_fld_t* fld, char* val, int len,
  580. unsigned int flags)
  581. {
  582. if (flags & PG_INT8_TIMESTAMP) {
  583. /* int8 format */
  584. fld->v.int4 = (int64_t)ntohll(((int64_t*)val)[0]) / (int64_t)1000000 + PG_EPOCH_TIME;
  585. } else {
  586. /* double format */
  587. fld->v.int4 = PG_EPOCH_TIME + ntohll(((int64_t*)val)[0]);
  588. }
  589. return 0;
  590. }
  591. static inline int pg_bit2db_int(db_fld_t* fld, char* val, int len)
  592. {
  593. int size;
  594. size = ntohl(*(uint32_t*)val);
  595. if (size != 32) {
  596. ERR("postgres: Unsupported bit field size (%d), column %s\n",
  597. size, fld->name);
  598. return -1;
  599. }
  600. fld->v.int4 = ntohl(((uint32_t*)val)[1]);
  601. return 0;
  602. }
  603. static inline int pg_float42db_float(db_fld_t* fld, char* val, int len)
  604. {
  605. fld->v.int4 = (uint32_t)ntohl(*(uint32_t*)val);
  606. return 0;
  607. }
  608. static inline int pg_float42db_double(db_fld_t* fld, char* val, int len)
  609. {
  610. float tmp;
  611. tmp = ntohl(*(uint32_t*)val);
  612. fld->v.dbl = tmp;
  613. return 0;
  614. }
  615. static inline int pg_float82db_double(db_fld_t* fld, char* val, int len)
  616. {
  617. fld->v.int8 = ntohll(*(uint64_t*)val);
  618. return 0;
  619. }
  620. static inline int pg_string2db_cstr(db_fld_t* fld, char* val, int len)
  621. {
  622. fld->v.cstr = val;
  623. return 0;
  624. }
  625. static inline int pg_string2db_str(db_fld_t* fld, char* val, int len)
  626. {
  627. fld->v.lstr.s = val;
  628. fld->v.lstr.len = len;
  629. return 0;
  630. }
  631. int pg_pg2fld(db_fld_t* dst, PGresult* src, int row,
  632. pg_type_t* types, unsigned int flags)
  633. {
  634. char* val;
  635. int i, len, ret;
  636. Oid type;
  637. if (dst == NULL || src == NULL) return 0;
  638. ret = 0;
  639. for(i = 0; !DB_FLD_EMPTY(dst) && !DB_FLD_LAST(dst[i]); i++) {
  640. if (PQgetisnull(src, row, i)) {
  641. dst[i].flags |= DB_NULL;
  642. continue;
  643. } else {
  644. dst[i].flags &= ~DB_NULL;
  645. }
  646. type = PQftype(src, i);
  647. val = PQgetvalue(src, row, i);
  648. len = PQgetlength(src, row, i);
  649. switch(dst[i].type) {
  650. case DB_INT:
  651. if (type == types[PG_INT2].oid)
  652. ret |= pg_int2_2_db_int(dst + i, val, len);
  653. else if (type == types[PG_INT4].oid)
  654. ret |= pg_int4_2_db_int(dst + i, val, len);
  655. else if (type == types[PG_INT8].oid)
  656. ret |= pg_int8_2_db_int(dst + i, val, len);
  657. else if (type == types[PG_BOOL].oid)
  658. ret |= pg_bool2db_int(dst + i, val, len);
  659. else if (type == types[PG_INET].oid)
  660. ret |= pg_inet2db_int(dst + i, val, len);
  661. else if ((type == types[PG_TIMESTAMP].oid) ||
  662. (type == types[PG_TIMESTAMPTZ].oid))
  663. ret |= pg_timestamp2db_int(dst + i, val, len, flags);
  664. else if (type == types[PG_BIT].oid)
  665. ret |= pg_bit2db_int(dst + i, val, len);
  666. else if (type == types[PG_VARBIT].oid)
  667. ret |= pg_bit2db_int(dst + i, val, len);
  668. else goto bug;
  669. break;
  670. case DB_FLOAT:
  671. if (type == types[PG_FLOAT4].oid)
  672. ret |= pg_float42db_float(dst + i, val, len);
  673. else goto bug;
  674. break;
  675. case DB_DOUBLE:
  676. if (type == types[PG_FLOAT4].oid)
  677. ret |= pg_float42db_double(dst + i, val, len);
  678. else if (type == types[PG_FLOAT8].oid)
  679. ret |= pg_float82db_double(dst + i, val, len);
  680. else goto bug;
  681. break;
  682. case DB_DATETIME:
  683. if (type == types[PG_INT2].oid)
  684. ret |= pg_int2_2_db_int(dst + i, val, len);
  685. else if (type == types[PG_INT4].oid)
  686. ret |= pg_int4_2_db_int(dst + i, val, len);
  687. else if ((type == types[PG_TIMESTAMP].oid) ||
  688. (type == types[PG_TIMESTAMPTZ].oid))
  689. ret |= pg_timestamp2db_int(dst + i, val, len, flags);
  690. else goto bug;
  691. break;
  692. case DB_CSTR:
  693. if ((type == types[PG_CHAR].oid) ||
  694. (type == types[PG_TEXT].oid) ||
  695. (type == types[PG_BPCHAR].oid) ||
  696. (type == types[PG_VARCHAR].oid))
  697. ret |= pg_string2db_cstr(dst + i, val, len);
  698. else if (type == types[PG_INT2].oid)
  699. ret |= pg_int2_2_db_cstr(dst + i, val, len);
  700. else if (type == types[PG_INT4].oid)
  701. ret |= pg_int4_2_db_cstr(dst + i, val, len);
  702. else goto bug;
  703. break;
  704. case DB_STR:
  705. case DB_BLOB:
  706. if ((type == types[PG_BYTE].oid) ||
  707. (type == types[PG_CHAR].oid) ||
  708. (type == types[PG_TEXT].oid) ||
  709. (type == types[PG_BPCHAR].oid) ||
  710. (type == types[PG_VARCHAR].oid))
  711. ret |= pg_string2db_str(dst + i, val, len);
  712. else if (type == types[PG_INT2].oid)
  713. ret |= pg_int2_2_db_str(dst + i, val, len);
  714. else if (type == types[PG_INT4].oid)
  715. ret |= pg_int4_2_db_str(dst + i, val, len);
  716. else goto bug;
  717. break;
  718. case DB_BITMAP:
  719. if (type == types[PG_INT2].oid)
  720. ret |= pg_int2_2_db_int(dst + i, val, len);
  721. else if (type == types[PG_INT4].oid)
  722. ret |= pg_int4_2_db_int(dst + i, val, len);
  723. else if (type == types[PG_INT8].oid)
  724. ret |= pg_int8_2_db_int(dst + i, val, len);
  725. else if (type == types[PG_BIT].oid)
  726. ret |= pg_bit2db_int(dst + i, val, len);
  727. else if (type == types[PG_VARBIT].oid)
  728. ret |= pg_bit2db_int(dst + i, val, len);
  729. else goto bug;
  730. break;
  731. default:
  732. BUG("postgres: Unsupported field type %d in field %s\n",
  733. dst[i].type, dst[i].name);
  734. return -1;
  735. }
  736. }
  737. return ret;
  738. bug:
  739. BUG("postgres: Error while converting Postgres Oid %d to DB API type %d\n",
  740. type, dst[i].type);
  741. return -1;
  742. }