Module: sip-router Branch: janakj/postgres Commit: e28d38dc23a279ca3d2cba6f921dc9bd6b27d5ee URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=e28d38dc...
Author: Jan Janak jan@iptel.org Committer: Jan Janak jan@iptel.org Date: Wed May 21 12:24:59 2008 +0000
- support for conversions from int to str
---
modules/db_postgres/pg_fld.c | 86 ++++++++++++++++++++++++++++++++++++++++++ modules/db_postgres/pg_fld.h | 4 +- modules/db_postgres/pg_mod.c | 2 +- 3 files changed, 90 insertions(+), 2 deletions(-)
diff --git a/modules/db_postgres/pg_fld.c b/modules/db_postgres/pg_fld.c index fb1d6fb..8d80f56 100644 --- a/modules/db_postgres/pg_fld.c +++ b/modules/db_postgres/pg_fld.c @@ -561,6 +561,8 @@ int pg_check_pg2fld(db_fld_t* fld, pg_type_t* types) 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: @@ -570,6 +572,8 @@ int pg_check_pg2fld(db_fld_t* fld, pg_type_t* types) 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: @@ -594,6 +598,80 @@ int pg_check_pg2fld(db_fld_t* fld, pg_type_t* types) }
+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 = (int16_t)ntohs(*((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 = (int16_t)ntohs(*((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)); @@ -793,6 +871,10 @@ int pg_pg2fld(db_fld_t* dst, PGresult* src, int row, (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;
@@ -804,6 +886,10 @@ int pg_pg2fld(db_fld_t* dst, PGresult* src, int row, (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;
diff --git a/modules/db_postgres/pg_fld.h b/modules/db_postgres/pg_fld.h index a42ee4d..a228cf8 100644 --- a/modules/db_postgres/pg_fld.h +++ b/modules/db_postgres/pg_fld.h @@ -42,6 +42,7 @@
#include "pg_oid.h" #include "pg_cmd.h" +#include "../../ut.h" #include "../../db/db_gen.h" #include "../../db/db_fld.h" #include <libpq-fe.h> @@ -64,7 +65,8 @@ struct pg_fld { long long int8; /**< 8-byte integer value in network byte order */ char byte[8]; } v; - Oid oid; /**< Type of the field on the server */ + char buf[INT2STR_MAX_LEN]; /**< Buffer for int2str conversions */ + Oid oid; /**< Type of the field on the server */ };
diff --git a/modules/db_postgres/pg_mod.c b/modules/db_postgres/pg_mod.c index 027e4af..2316bb3 100644 --- a/modules/db_postgres/pg_mod.c +++ b/modules/db_postgres/pg_mod.c @@ -221,7 +221,7 @@ int pg_test(void) ERR("Error while initializing database layer\n"); goto error; } - if (db_add_db(db, "postgres://janakj:honzacvut@localhost/ser") < 0) goto error; + if (db_add_db(db, "postgres://janakj:heslo@localhost/ser") < 0) goto error; if (db_connect(db) < 0) goto error; del = db_cmd(DB_DEL, db, "test", NULL, NULL, NULL);