123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918 |
- /*
- * Portions Copyright (C) 2001-2003 FhG FOKUS
- * Copyright (C) 2003 August.Net Services, LLC
- * Portions Copyright (C) 2005-2008 iptelorg GmbH
- *
- * This file is part of SER, a free SIP server.
- *
- * SER is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option) any later
- * version
- *
- * For a license to use the ser software under conditions other than those
- * described here, or to purchase support for this software, please contact
- * iptel.org by e-mail at the following addresses: [email protected]
- *
- * SER is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc., 59
- * Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- /*!
- * \file
- * \brief DB_POSTGRES :: Data field conversion and type checking functions.
- * \ingroup db_postgres
- * Module: \ref db_postgres
- */
- #include "pg_fld.h"
- #include "pg_con.h" /* flags */
- #include "pg_mod.h"
- #include "../../lib/srdb2/db_drv.h"
- #include "../../mem/mem.h"
- #include "../../dprint.h"
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdint.h>
- #include <string.h>
- /**
- * This is the epoch time in time_t format, this value is used to convert
- * timestamp values to/from PostgreSQL format.
- * 2000-01-01 00:00:00 +0000 as the value of time_t in UTC
- */
- #define PG_EPOCH_TIME ((int64_t)946684800)
- /** Frees memory used by a pg_fld structure.
- * This function frees all memory used by a pg_fld structure
- * @param fld Generic db_fld_t* structure being freed.
- * @param payload The postgresql extension structure to be freed
- */
- static void pg_fld_free(db_fld_t* fld, struct pg_fld* payload)
- {
- db_drv_free(&payload->gen);
- if (payload->name) pkg_free(payload->name);
- pkg_free(payload);
- }
- int pg_fld(db_fld_t* fld, char* table)
- {
- struct pg_fld* res;
- res = (struct pg_fld*)pkg_malloc(sizeof(struct pg_fld));
- if (res == NULL) {
- ERR("postgres: No memory left\n");
- return -1;
- }
- memset(res, '\0', sizeof(struct pg_fld));
- if (db_drv_init(&res->gen, pg_fld_free) < 0) goto error;
- DB_SET_PAYLOAD(fld, res);
- return 0;
- error:
- if (res) pkg_free(res);
- return -1;
- }
- union ull {
- uint64_t ui64;
- uint32_t ui32[2];
- };
- static inline uint64_t htonll(uint64_t in)
- {
- union ull* p = (union ull*)∈
-
- return ((uint64_t)htonl(p->ui32[0]) << 32) + (uint64_t)htonl(p->ui32[1]);
- }
- static inline uint64_t ntohll(uint64_t in)
- {
- union ull* p = (union ull*)∈
- return ((uint64_t)ntohl(p->ui32[0]) << 32) + (uint64_t)ntohl(p->ui32[1]);
- }
- static inline void db_int2pg_int4(struct pg_params* dst, int i,
- db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- pfld->v.int4[0] = htonl(src->v.int4);
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 4;
- }
- static inline void db_int2pg_int2(struct pg_params* dst, int i,
- db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- pfld->v.int2[0] = htons(src->v.int4);
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 2;
- }
- static inline void db_int2pg_timestamp(struct pg_params* dst, int i,
- db_fld_t* src, unsigned int flags)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- if (flags & PG_INT8_TIMESTAMP) {
- pfld->v.int8 = ((int64_t)src->v.int4 - PG_EPOCH_TIME) * 1000000;
- } else {
- pfld->v.dbl = (double)src->v.int4 - (double)PG_EPOCH_TIME;
- }
- pfld->v.int8 = htonll(pfld->v.int8);
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 8;
- }
- static inline void db_int2pg_int8(struct pg_params* dst, int i,
- db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- pfld->v.int4[0] = 0;
- pfld->v.int4[1] = htonl(src->v.int4);
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 8;
- }
- static inline void db_int2pg_bool(struct pg_params* dst, int i, db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- if (src->v.int4) pfld->v.byte[0] = 1;
- else pfld->v.byte[0] = 0;
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 1;
- }
- static inline void db_int2pg_inet(struct pg_params* dst, int i, db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- pfld->v.byte[0] = AF_INET; /* Address family */
- pfld->v.byte[1] = 32; /* Netmask */
- pfld->v.byte[2] = 0; /* is CIDR */
- pfld->v.byte[3] = 4; /* Number of bytes */
- pfld->v.int4[1] = htonl(src->v.int4); /* Actuall IP address */
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 8;
- }
- static inline void db_float2pg_float4(struct pg_params* dst, int i, db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- pfld->v.int4[0] = htonl(src->v.int4);
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 4;
- }
- static inline void db_float2pg_float8(struct pg_params* dst, int i, db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- pfld->v.dbl = src->v.flt;
- pfld->v.int8 = htonll(pfld->v.int8);
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 8;
- }
- static inline void db_double2pg_float8(struct pg_params* dst, int i, db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- pfld->v.int8 = htonll(src->v.int8);
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 8;
- }
- static inline void db_double2pg_float4(struct pg_params* dst, int i, db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- pfld->v.flt = src->v.dbl;
- pfld->v.int4[0] = htonl(pfld->v.int4[0]);
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 4;
- }
- static inline void db_int2pg_bit(struct pg_params* dst, int i, db_fld_t* src)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(src);
- pfld->v.int4[0] = htonl(32);
- pfld->v.int4[1] = htonl(src->v.int4);
- dst->fmt[i] = 1;
- dst->val[i] = pfld->v.byte;
- dst->len[i] = 8;
- }
- static inline void db_str2pg_string(struct pg_params* dst, int i,
- db_fld_t* src)
- {
- dst->fmt[i] = 1;
- dst->val[i] = src->v.lstr.s;
- dst->len[i] = src->v.lstr.len;
- }
- static inline void db_cstr2pg_string(struct pg_params* dst, int i,
- db_fld_t* src)
- {
- dst->fmt[i] = 0;
- dst->val[i] = src->v.cstr;
- }
- int pg_fld2pg(struct pg_params* dst, int off, pg_type_t* types,
- db_fld_t* src, unsigned int flags)
- {
- int i;
- struct pg_fld* pfld;
- if (src == NULL) return 0;
- for(i = 0; !DB_FLD_EMPTY(src) && !DB_FLD_LAST(src[i]); i++) {
- pfld = DB_GET_PAYLOAD(src + i);
- /* NULL value */
- if (src[i].flags & DB_NULL) {
- dst->val[off + i] = NULL;
- dst->len[off + i] = 0;
- continue;
- }
- switch(src[i].type) {
- case DB_INT:
- if (pfld->oid == types[PG_INT2].oid)
- db_int2pg_int2(dst, off + i, src + i);
- else if (pfld->oid == types[PG_INT4].oid)
- db_int2pg_int4(dst, off + i, src + i);
- else if ((pfld->oid == types[PG_TIMESTAMP].oid) ||
- (pfld->oid == types[PG_TIMESTAMPTZ].oid))
- db_int2pg_timestamp(dst, off + i, src + i, flags);
- else if (pfld->oid == types[PG_INT8].oid)
- db_int2pg_int8(dst, off + i, src + i);
- else if (pfld->oid == types[PG_INET].oid)
- db_int2pg_inet(dst, off + i, src + i);
- else if (pfld->oid == types[PG_BOOL].oid)
- db_int2pg_bool(dst, off + i, src + i);
- else if (pfld->oid == types[PG_BIT].oid)
- db_int2pg_bit(dst, off + i, src + i);
- else if (pfld->oid == types[PG_VARBIT].oid)
- db_int2pg_bit(dst, off + i, src + i);
- else goto bug;
- break;
- case DB_BITMAP:
- if (pfld->oid == types[PG_INT4].oid)
- db_int2pg_int4(dst, off + i, src + i);
- else if (pfld->oid == types[PG_INT8].oid)
- db_int2pg_int8(dst, off + i, src + i);
- else if (pfld->oid == types[PG_BIT].oid)
- db_int2pg_bit(dst, off + i, src + i);
- else if (pfld->oid == types[PG_VARBIT].oid)
- db_int2pg_bit(dst, off + i, src + i);
- else goto bug;
- break;
- case DB_DATETIME:
- if (pfld->oid == types[PG_INT4].oid)
- db_int2pg_int4(dst, off + i, src + i);
- else if ((pfld->oid == types[PG_TIMESTAMP].oid) ||
- (pfld->oid == types[PG_TIMESTAMPTZ].oid))
- db_int2pg_timestamp(dst, off + i, src + i, flags);
- else if (pfld->oid == types[PG_INT8].oid)
- db_int2pg_int8(dst, off + i, src + i);
- else goto bug;
- break;
-
- case DB_FLOAT:
- if (pfld->oid == types[PG_FLOAT4].oid)
- db_float2pg_float4(dst, off + i, src + i);
- else if (pfld->oid == types[PG_FLOAT8].oid)
- db_float2pg_float8(dst, off + i, src + i);
- else goto bug;
- break;
- case DB_DOUBLE:
- if (pfld->oid == types[PG_FLOAT4].oid)
- db_double2pg_float4(dst, off + i, src + i);
- else if (pfld->oid == types[PG_FLOAT8].oid)
- db_double2pg_float8(dst, off + i, src + i);
- else goto bug;
- break;
- case DB_STR:
- if (pfld->oid == types[PG_VARCHAR].oid ||
- pfld->oid == types[PG_BYTE].oid ||
- pfld->oid == types[PG_CHAR].oid ||
- pfld->oid == types[PG_TEXT].oid ||
- pfld->oid == types[PG_BPCHAR].oid)
- db_str2pg_string(dst, off + i, src + i);
- else goto bug;
- break;
- case DB_CSTR:
- if (pfld->oid == types[PG_VARCHAR].oid ||
- pfld->oid == types[PG_BYTE].oid ||
- pfld->oid == types[PG_CHAR].oid ||
- pfld->oid == types[PG_TEXT].oid ||
- pfld->oid == types[PG_BPCHAR].oid)
- db_cstr2pg_string(dst, off + i, src + i);
- else goto bug;
- break;
- case DB_BLOB:
- if (pfld->oid == types[PG_BYTE].oid)
- db_str2pg_string(dst, off + i, src + i);
- else goto bug;
- break;
- default:
- BUG("postgres: Unsupported field type %d in field %s\n",
- src[i].type, src[i].name);
- return -1;
- }
- }
- return 0;
- bug:
- BUG("postgres: Error while converting DB API type %d to Postgres Oid %d\n",
- src[i].type, pfld->oid);
- return -1;
- }
- int pg_check_fld2pg(db_fld_t* fld, pg_type_t* types)
- {
- int i;
- const char* name = "UNKNOWN";
- struct pg_fld* pfld;
- if (fld == NULL) return 0;
- for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
- pfld = DB_GET_PAYLOAD(fld + i);
- switch(fld[i].type) {
- case DB_INT:
- if (pfld->oid == types[PG_INT2].oid) continue;
- if (pfld->oid == types[PG_INT4].oid) continue;
- if (pfld->oid == types[PG_INT8].oid) continue;
- if (pfld->oid == types[PG_BOOL].oid) continue;
- if (pfld->oid == types[PG_INET].oid) continue;
- if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
- if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
- if (pfld->oid == types[PG_BIT].oid) continue;
- if (pfld->oid == types[PG_VARBIT].oid) continue;
- break;
- case DB_BITMAP:
- if (pfld->oid == types[PG_INT4].oid) continue;
- if (pfld->oid == types[PG_INT8].oid) continue;
- if (pfld->oid == types[PG_BIT].oid) continue;
- if (pfld->oid == types[PG_VARBIT].oid) continue;
- break;
- case DB_FLOAT:
- case DB_DOUBLE:
- if (pfld->oid == types[PG_FLOAT4].oid) continue;
- if (pfld->oid == types[PG_FLOAT8].oid) continue;
- break;
- case DB_CSTR:
- case DB_STR:
- if (pfld->oid == types[PG_BYTE].oid) continue;
- if (pfld->oid == types[PG_CHAR].oid) continue;
- if (pfld->oid == types[PG_TEXT].oid) continue;
- if (pfld->oid == types[PG_BPCHAR].oid) continue;
- if (pfld->oid == types[PG_VARCHAR].oid) continue;
- break;
- case DB_BLOB:
- if (pfld->oid == types[PG_BYTE].oid) continue;
- break;
- case DB_DATETIME:
- if (pfld->oid == types[PG_INT4].oid) continue;
- if (pfld->oid == types[PG_INT8].oid) continue;
- if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
- if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
- break;
- default:
- BUG("postgres: Unsupported field type %d, bug in postgres module\n",
- fld[i].type);
- return -1;
- }
- pg_oid2name(&name, types, pfld->oid);
- ERR("postgres: Cannot convert column '%s' of type %s "
- "to PostgreSQL column type '%s'\n",
- fld[i].name, db_fld_str[fld[i].type], name);
- return -1;
- }
- return 0;
- }
- int pg_resolve_param_oids(db_fld_t* vals, db_fld_t* match, int n1, int n2, PGresult* types)
- {
- struct pg_fld* pfld;
- int i;
- if (n1 + n2 != PQnparams(types)) {
- ERR("postgres: Number of command parameters do not match\n");
- return -1;
- }
- for(i = 0; i < n1; i++) {
- pfld = DB_GET_PAYLOAD(vals + i);
- pfld->oid = PQparamtype(types, i);
- }
- for(i = 0; i < n2; i++) {
- pfld = DB_GET_PAYLOAD(match + i);
- pfld->oid = PQparamtype(types, n1 + i);
- }
- return 0;
- }
- int pg_resolve_result_oids(db_fld_t* fld, int n, PGresult* types)
- {
- struct pg_fld* pfld;
- int i;
- if (fld == NULL) return 0;
- if (n != PQnfields(types)) {
- ERR("postgres: Result field numbers do not match\n");
- return -1;
- }
- for(i = 0; i < n; i++) {
- pfld = DB_GET_PAYLOAD(fld + i);
- pfld->oid = PQftype(types, i);
- }
- return 0;
- }
- int pg_check_pg2fld(db_fld_t* fld, pg_type_t* types)
- {
- int i;
- const char* name = "UNKNOWN";
- struct pg_fld* pfld;
- if (fld == NULL) return 0;
- for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
- pfld = DB_GET_PAYLOAD(fld + i);
- if (pfld->oid == 0) {
- ERR("postgres: Unknown type fields not supported\n");
- return -1;
- }
- switch(fld[i].type) {
- case DB_INT:
- if (pfld->oid == types[PG_INT2].oid) continue;
- if (pfld->oid == types[PG_INT4].oid) continue;
- if (pfld->oid == types[PG_INT8].oid) continue;
- if (pfld->oid == types[PG_BOOL].oid) continue;
- if (pfld->oid == types[PG_INET].oid) continue;
- if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
- if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
- if (pfld->oid == types[PG_BIT].oid) continue;
- if (pfld->oid == types[PG_VARBIT].oid) continue;
- break;
- case DB_BITMAP:
- if (pfld->oid == types[PG_INT2].oid) continue;
- if (pfld->oid == types[PG_INT4].oid) continue;
- if (pfld->oid == types[PG_INT8].oid) continue;
- if (pfld->oid == types[PG_BIT].oid) continue;
- if (pfld->oid == types[PG_VARBIT].oid) continue;
- break;
- case DB_FLOAT:
- if (pfld->oid == types[PG_FLOAT4].oid) continue;
- break;
- case DB_DOUBLE:
- if (pfld->oid == types[PG_FLOAT4].oid) continue;
- if (pfld->oid == types[PG_FLOAT8].oid) continue;
- break;
- case DB_CSTR:
- if (pfld->oid == types[PG_CHAR].oid) continue;
- if (pfld->oid == types[PG_TEXT].oid) continue;
- if (pfld->oid == types[PG_BPCHAR].oid) continue;
- if (pfld->oid == types[PG_VARCHAR].oid) continue;
- if (pfld->oid == types[PG_INT2].oid) continue;
- if (pfld->oid == types[PG_INT4].oid) continue;
- break;
- case DB_STR:
- case DB_BLOB:
- if (pfld->oid == types[PG_BYTE].oid) continue;
- if (pfld->oid == types[PG_CHAR].oid) continue;
- if (pfld->oid == types[PG_TEXT].oid) continue;
- if (pfld->oid == types[PG_BPCHAR].oid) continue;
- if (pfld->oid == types[PG_VARCHAR].oid) continue;
- if (pfld->oid == types[PG_INT2].oid) continue;
- if (pfld->oid == types[PG_INT4].oid) continue;
- break;
- case DB_DATETIME:
- if (pfld->oid == types[PG_INT2].oid) continue;
- if (pfld->oid == types[PG_INT4].oid) continue;
- if (pfld->oid == types[PG_TIMESTAMP].oid) continue;
- if (pfld->oid == types[PG_TIMESTAMPTZ].oid) continue;
- break;
- default:
- BUG("postgres: Unsupported field type %d, bug in postgres module\n",
- fld[i].type);
- return -1;
- }
- pg_oid2name(&name, types, pfld->oid);
- ERR("postgres: Cannot convert column '%s' of type %s "
- "to DB API field of type %s\n",
- fld[i].name, name, db_fld_str[fld[i].type]);
- return -1;
- }
- return 0;
- }
- static inline int pg_int2_2_db_cstr(db_fld_t* fld, char* val, int len)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
- int size, v;
- v = (int16_t)ntohs(*((int16_t*)val));
- size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
- if (size < 0 || size >= INT2STR_MAX_LEN) {
- BUG("postgres: Error while converting integer to string\n");
- return -1;
- }
- fld->v.cstr = pfld->buf;
- return 0;
- }
- static inline int pg_int4_2_db_cstr(db_fld_t* fld, char* val, int len)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
- int size, v;
- v = (int32_t)ntohl(*((int32_t*)val));
- size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
- if (len < 0 || size >= INT2STR_MAX_LEN) {
- BUG("postgres: Error while converting integer to string\n");
- return -1;
- }
- fld->v.cstr = pfld->buf;
- return 0;
- }
- static inline int pg_int2_2_db_str(db_fld_t* fld, char* val, int len)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
- int size, v;
- v = (int16_t)ntohs(*((int16_t*)val));
- size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
- if (size < 0 || size >= INT2STR_MAX_LEN) {
- BUG("postgres: Error while converting integer to string\n");
- return -1;
- }
- fld->v.lstr.s = pfld->buf;
- fld->v.lstr.len = size;
- return 0;
- }
- static inline int pg_int4_2_db_str(db_fld_t* fld, char* val, int len)
- {
- struct pg_fld* pfld = DB_GET_PAYLOAD(fld);
- int size, v;
- v = (int32_t)ntohl(*((int32_t*)val));
- size = snprintf(pfld->buf, INT2STR_MAX_LEN, "%-d", v);
- if (size < 0 || size >= INT2STR_MAX_LEN) {
- BUG("postgres: Error while converting integer to string\n");
- return -1;
- }
- fld->v.lstr.s = pfld->buf;
- fld->v.lstr.len = size;
- return 0;
- }
- static inline int pg_int2_2_db_int(db_fld_t* fld, char* val, int len)
- {
- fld->v.int4 = (int16_t)ntohs(*((int16_t*)val));
- return 0;
- }
- static inline int pg_int4_2_db_int(db_fld_t* fld, char* val, int len)
- {
- fld->v.int4 = (int32_t)ntohl(*((int32_t*)val));
- return 0;
- }
- static inline int pg_int8_2_db_int(db_fld_t* fld, char* val, int len)
- {
- fld->v.int8 = (int64_t)ntohll(*((int64_t*)val));
- return 0;
- }
- static inline int pg_bool2db_int(db_fld_t* fld, char* val, int len)
- {
- fld->v.int4 = val[0];
- return 0;
- }
- static inline int pg_inet2db_int(db_fld_t* fld, char* val, int len)
- {
- if (len != 8 || val[2] != 0) {
- ERR("postgres: Unsupported 'inet' format, column %s\n", fld->name);
- return -1;
- }
- if (val[0] != AF_INET) {
- ERR("postgres: Unsupported address family %d in field %s\n",
- val[0], fld->name);
- return -1;
- }
- if (val[1] != 32) {
- WARN("postgres: Netmasks shorter than 32-bits not supported, "
- "column %s\n", fld->name);
- }
- if (val[3] != 4) {
- ERR("postgres: Unsupported IP address size %d in column %s\n",
- val[3], fld->name);
- return -1;
- }
- fld->v.int4 = (int32_t)ntohl(((int32_t*)val)[1]);
- return 0;
- }
- static inline int pg_timestamp2db_int(db_fld_t* fld, char* val, int len,
- unsigned int flags)
- {
- if (flags & PG_INT8_TIMESTAMP) {
- /* int8 format */
- fld->v.int4 = (int64_t)ntohll(((int64_t*)val)[0]) / (int64_t)1000000 + PG_EPOCH_TIME;
- } else {
- /* double format */
- fld->v.int4 = PG_EPOCH_TIME + ntohll(((int64_t*)val)[0]);
- }
- return 0;
- }
- static inline int pg_bit2db_int(db_fld_t* fld, char* val, int len)
- {
- int size;
- size = ntohl(*(uint32_t*)val);
- if (size != 32) {
- ERR("postgres: Unsupported bit field size (%d), column %s\n",
- size, fld->name);
- return -1;
- }
- fld->v.int4 = ntohl(((uint32_t*)val)[1]);
- return 0;
- }
- static inline int pg_float42db_float(db_fld_t* fld, char* val, int len)
- {
- fld->v.int4 = (uint32_t)ntohl(*(uint32_t*)val);
- return 0;
- }
- static inline int pg_float42db_double(db_fld_t* fld, char* val, int len)
- {
- float tmp;
- tmp = ntohl(*(uint32_t*)val);
- fld->v.dbl = tmp;
- return 0;
- }
- static inline int pg_float82db_double(db_fld_t* fld, char* val, int len)
- {
- fld->v.int8 = ntohll(*(uint64_t*)val);
- return 0;
- }
- static inline int pg_string2db_cstr(db_fld_t* fld, char* val, int len)
- {
- fld->v.cstr = val;
- return 0;
- }
- static inline int pg_string2db_str(db_fld_t* fld, char* val, int len)
- {
- fld->v.lstr.s = val;
- fld->v.lstr.len = len;
- return 0;
- }
- int pg_pg2fld(db_fld_t* dst, PGresult* src, int row,
- pg_type_t* types, unsigned int flags)
- {
- char* val;
- int i, len, ret;
- Oid type;
-
- if (dst == NULL || src == NULL) return 0;
- ret = 0;
- for(i = 0; !DB_FLD_EMPTY(dst) && !DB_FLD_LAST(dst[i]); i++) {
- if (PQgetisnull(src, row, i)) {
- dst[i].flags |= DB_NULL;
- continue;
- } else {
- dst[i].flags &= ~DB_NULL;
- }
- type = PQftype(src, i);
- val = PQgetvalue(src, row, i);
- len = PQgetlength(src, row, i);
- switch(dst[i].type) {
- case DB_INT:
- if (type == types[PG_INT2].oid)
- ret |= pg_int2_2_db_int(dst + i, val, len);
- else if (type == types[PG_INT4].oid)
- ret |= pg_int4_2_db_int(dst + i, val, len);
- else if (type == types[PG_INT8].oid)
- ret |= pg_int8_2_db_int(dst + i, val, len);
- else if (type == types[PG_BOOL].oid)
- ret |= pg_bool2db_int(dst + i, val, len);
- else if (type == types[PG_INET].oid)
- ret |= pg_inet2db_int(dst + i, val, len);
- else if ((type == types[PG_TIMESTAMP].oid) ||
- (type == types[PG_TIMESTAMPTZ].oid))
- ret |= pg_timestamp2db_int(dst + i, val, len, flags);
- else if (type == types[PG_BIT].oid)
- ret |= pg_bit2db_int(dst + i, val, len);
- else if (type == types[PG_VARBIT].oid)
- ret |= pg_bit2db_int(dst + i, val, len);
- else goto bug;
- break;
- case DB_FLOAT:
- if (type == types[PG_FLOAT4].oid)
- ret |= pg_float42db_float(dst + i, val, len);
- else goto bug;
- break;
- case DB_DOUBLE:
- if (type == types[PG_FLOAT4].oid)
- ret |= pg_float42db_double(dst + i, val, len);
- else if (type == types[PG_FLOAT8].oid)
- ret |= pg_float82db_double(dst + i, val, len);
- else goto bug;
- break;
- case DB_DATETIME:
- if (type == types[PG_INT2].oid)
- ret |= pg_int2_2_db_int(dst + i, val, len);
- else if (type == types[PG_INT4].oid)
- ret |= pg_int4_2_db_int(dst + i, val, len);
- else if ((type == types[PG_TIMESTAMP].oid) ||
- (type == types[PG_TIMESTAMPTZ].oid))
- ret |= pg_timestamp2db_int(dst + i, val, len, flags);
- else goto bug;
- break;
- case DB_CSTR:
- if ((type == types[PG_CHAR].oid) ||
- (type == types[PG_TEXT].oid) ||
- (type == types[PG_BPCHAR].oid) ||
- (type == types[PG_VARCHAR].oid))
- ret |= pg_string2db_cstr(dst + i, val, len);
- else if (type == types[PG_INT2].oid)
- ret |= pg_int2_2_db_cstr(dst + i, val, len);
- else if (type == types[PG_INT4].oid)
- ret |= pg_int4_2_db_cstr(dst + i, val, len);
- else goto bug;
- break;
- case DB_STR:
- case DB_BLOB:
- if ((type == types[PG_BYTE].oid) ||
- (type == types[PG_CHAR].oid) ||
- (type == types[PG_TEXT].oid) ||
- (type == types[PG_BPCHAR].oid) ||
- (type == types[PG_VARCHAR].oid))
- ret |= pg_string2db_str(dst + i, val, len);
- else if (type == types[PG_INT2].oid)
- ret |= pg_int2_2_db_str(dst + i, val, len);
- else if (type == types[PG_INT4].oid)
- ret |= pg_int4_2_db_str(dst + i, val, len);
- else goto bug;
- break;
- case DB_BITMAP:
- if (type == types[PG_INT2].oid)
- ret |= pg_int2_2_db_int(dst + i, val, len);
- else if (type == types[PG_INT4].oid)
- ret |= pg_int4_2_db_int(dst + i, val, len);
- else if (type == types[PG_INT8].oid)
- ret |= pg_int8_2_db_int(dst + i, val, len);
- else if (type == types[PG_BIT].oid)
- ret |= pg_bit2db_int(dst + i, val, len);
- else if (type == types[PG_VARBIT].oid)
- ret |= pg_bit2db_int(dst + i, val, len);
- else goto bug;
- break;
- default:
- BUG("postgres: Unsupported field type %d in field %s\n",
- dst[i].type, dst[i].name);
- return -1;
- }
- }
- return ret;
- bug:
- BUG("postgres: Error while converting Postgres Oid %d to DB API type %d\n",
- type, dst[i].type);
- return -1;
- }
|