Module: sip-router
Branch: janakj/postgres
Commit: bf846b8d43e0f4d30bcc9b3074f605690175bfe3
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=bf846b8…
Author: Jan Janak <jan(a)iptel.org>
Committer: Jan Janak <jan(a)iptel.org>
Date: Tue Jan 10 22:16:07 2006 +0000
- DB_FLOAT support
- fixed error in params_add in postgres module
---
modules/db_postgres/db_mod.c | 15 +++++++++++++--
modules/db_postgres/dbase.c | 30 +++++++++++++++++++++++-------
modules/db_postgres/res.c | 4 ++++
3 files changed, 40 insertions(+), 9 deletions(-)
diff --git a/modules/db_postgres/db_mod.c b/modules/db_postgres/db_mod.c
index abed575..f6cf5b3 100644
--- a/modules/db_postgres/db_mod.c
+++ b/modules/db_postgres/db_mod.c
@@ -98,6 +98,8 @@ static int pg_test(void)
db_res_t* res;
db_con_t* con;
struct tm* tt;
+ db_key_t keys[1];
+ db_val_t vals[1];
con = pg_init("postgres://ser:heslo@localhost/ser");
if (!con) {
@@ -107,7 +109,12 @@ static int pg_test(void)
INFO("Successfuly connected\n");
pg_use_table(con, "test");
- pg_query(con, 0, 0, 0, 0, 0, 0, 0, &res);
+ keys[0] = "int4_col";
+ vals[0].type = DB_INT;
+ vals[0].nul = 1;
+ vals[0].val.int_val = 1;
+
+ pg_query(con, keys, 0, vals, 0, 1, 0, 0, &res);
if (!res) {
ERR("No result received\n");
return -1;
@@ -128,6 +135,10 @@ static int pg_test(void)
INFO("INT(%d)", res->rows[row].values[col].val.int_val);
break;
+ case DB_FLOAT:
+ INFO("FLOAT(%f)", res->rows[row].values[col].val.float_val);
+ break;
+
case DB_DOUBLE:
INFO("DOUBLE(%f)", res->rows[row].values[col].val.double_val);
break;
@@ -170,7 +181,7 @@ struct module_exports exports = {
cmds,
0, /* RPC methods */
params, /* module parameters */
- 0, /* module initialization function */
+ pg_test, /* module initialization function */
0, /* response function*/
0, /* destroy function */
0, /* oncancel function */
diff --git a/modules/db_postgres/dbase.c b/modules/db_postgres/dbase.c
index b0f121e..60ccc95 100644
--- a/modules/db_postgres/dbase.c
+++ b/modules/db_postgres/dbase.c
@@ -108,7 +108,7 @@ static struct pg_params* new_pg_params(int n)
}
-static inline int params_add(struct pg_params* p, db_con_t* con, db_val_t* vals, int n)
+static inline int params_add(struct pg_params* p, db_con_t* con, db_val_t* vals, int n,
int skip_null)
{
int i, i1, i2;
db_val_t* val;
@@ -126,13 +126,29 @@ static inline int params_add(struct pg_params* p, db_con_t* con,
db_val_t* vals,
for(i = 0; i < n; i++) {
val = &vals[i];
p->formats[p->cur] = 1;
- if (val->nul) continue;
+ if (val->nul) {
+ /* When assembling parameters for where clause we skip parameters
+ * that have null values because they are expressed as is null.
+ * At other places we include them.
+ */
+ if (!skip_null) p->cur++;
+ continue;
+ }
switch(val->type) {
case DB_INT:
val->val.int_val = ntohl(val->val.int_val);
p->data[p->cur] = (const char*)&val->val.int_val;
p->len[p->cur] = 4;
break;
+
+ case DB_FLOAT:
+ /* Change the byte order of 4-byte value to network
+ * byte order if necessary
+ */
+ val->val.int_val = htonl(val->val.int_val);
+ p->data[p->cur] = (const char*)&val->val.int_val;
+ p->len[p->cur] = 4;
+ break;
case DB_DOUBLE:
/* Change the byte order of 8-byte value to network
@@ -832,7 +848,7 @@ int pg_query(db_con_t* con, db_key_t* keys, db_op_t* ops,
params = new_pg_params(n);
if (!params) goto err;
- if (params_add(params, con, vals, n) < 0) goto err;
+ if (params_add(params, con, vals, n, 1) < 0) goto err;
do {
ret = submit_query(res, con, select, params);
@@ -914,7 +930,7 @@ int pg_insert(db_con_t* con, db_key_t* keys, db_val_t* vals, int n)
params = new_pg_params(n);
if (!params) goto err;
- if (params_add(params, con, vals, n) < 0) goto err;
+ if (params_add(params, con, vals, n, 0) < 0) goto err;
do {
ret = submit_query(0, con, insert, params);
@@ -963,7 +979,7 @@ int pg_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t*
vals, int n
params = new_pg_params(n);
if (!params) goto err;
- if (params_add(params, con, vals, n) < 0) goto err;
+ if (params_add(params, con, vals, n, 1) < 0) goto err;
do {
ret = submit_query(0, con, delete, params);
@@ -1016,8 +1032,8 @@ int pg_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t*
vals,
params = new_pg_params(n + un);
if (!params) goto err;
- if (params_add(params, con, uvals, un) < 0) goto err;
- if (params_add(params, con, vals, n) < 0) goto err;
+ if (params_add(params, con, uvals, un, 0) < 0) goto err;
+ if (params_add(params, con, vals, n, 1) < 0) goto err;
do {
ret = submit_query(0, con, update, params);
diff --git a/modules/db_postgres/res.c b/modules/db_postgres/res.c
index 3722ce0..b94e51c 100644
--- a/modules/db_postgres/res.c
+++ b/modules/db_postgres/res.c
@@ -97,6 +97,9 @@ static inline int get_columns(db_res_t* res)
break;
case FLOAT4OID: /* single-precision floating point number, 4-byte storage */
+ res->col.types[i] = DB_FLOAT;
+ break;
+
case FLOAT8OID: /* double-precision floating point number, 8-byte storage */
res->col.types[i] = DB_DOUBLE;
break;
@@ -195,6 +198,7 @@ static inline int convert_cell(db_con_t* con, db_res_t* res, int row,
int col)
val->nul = 1;
switch(res->col.types[col]) {
case DB_INT: val->val.int_val = 0; break;
+ case DB_FLOAT: val->val.float_val = 0; break;
case DB_DOUBLE: val->val.double_val = 0; break;
case DB_STRING: val->val.string_val = dummy_str.s; break;
case DB_STR: val->val.str_val = dummy_str; break;