Browse Source

- safer public function names
- short test code added
- fixed module name in comments
- support for NULL values in where clause
- LOG(L_ERR, -> ERR
- PQserverVersion compiled conditionaly

Jan Janak 19 years ago
parent
commit
96d2a408ac

+ 2 - 2
modules/db_postgres/db_con.c

@@ -40,10 +40,10 @@
  * Store name of table that will be used by
  * Store name of table that will be used by
  * subsequent database functions
  * subsequent database functions
  */
  */
-int use_table(db_con_t* _h, const char* _t)
+int pg_use_table(db_con_t* _h, const char* _t)
 {
 {
 	if ((!_h) || (!_t)) {
 	if ((!_h) || (!_t)) {
-		LOG(L_ERR, "use_table: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return -1;
 		return -1;
 	}
 	}
 
 

+ 108 - 10
modules/db_postgres/db_mod.c

@@ -43,15 +43,15 @@ int reconnect_attempts = 2; /* How many times should the module try to reconnect
  * Postgres database module interface
  * Postgres database module interface
  */
  */
 static cmd_export_t cmds[]={
 static cmd_export_t cmds[]={
-	{"db_use_table",   (cmd_function)use_table,      2, 0, 0},
-	{"db_init",        (cmd_function)db_init,        1, 0, 0},
-	{"db_close",       (cmd_function)db_close,       2, 0, 0},
-	{"db_query",       (cmd_function)db_query,       2, 0, 0},
-	{"db_raw_query",   (cmd_function)db_raw_query,   2, 0, 0},
-	{"db_free_result", (cmd_function)db_free_result, 2, 0, 0},
-	{"db_insert",      (cmd_function)db_insert,      2, 0, 0},
-	{"db_delete",      (cmd_function)db_delete,      2, 0, 0},
-	{"db_update",      (cmd_function)db_update,      2, 0, 0},
+	{"db_use_table",   (cmd_function)pg_use_table,      2, 0, 0},
+	{"db_init",        (cmd_function)pg_init,           1, 0, 0},
+	{"db_close",       (cmd_function)pg_close,          2, 0, 0},
+	{"db_query",       (cmd_function)pg_query,          2, 0, 0},
+	{"db_raw_query",   (cmd_function)pg_raw_query,      2, 0, 0},
+	{"db_free_result", (cmd_function)pg_db_free_result, 2, 0, 0},
+	{"db_insert",      (cmd_function)pg_insert,         2, 0, 0},
+	{"db_delete",      (cmd_function)pg_delete,         2, 0, 0},
+	{"db_update",      (cmd_function)pg_update,         2, 0, 0},
 	{0, 0, 0, 0, 0}
 	{0, 0, 0, 0, 0}
 };
 };
 
 
@@ -66,6 +66,105 @@ static param_export_t params[] = {
 };
 };
 
 
 
 
+/*
+ * create table test (
+ *	bool_col BOOL NULL,
+ *	int2_col INT2 NULL,
+ *	int4_col INT4 NULL,
+ *	int8_col INT8 NULL,
+ *	float4_col FLOAT4 NULL,
+ *	float8_col FLOAT8 NULL,
+ *	timestamp_col TIMESTAMP NULL,
+ *	char_col CHAR NULL,
+ *	text_col TEXT NULL,
+ *	bpchar_col BPCHAR NULL,
+ *	varchar_col VARCHAR(255) NULL,
+ *	bytea_col BYTEA NULL,
+ *	bit_col BIT(32) NULL,
+ *	varbit_col VARBIT NULL
+ * );
+ *
+ * insert into test (bool_col, int2_col, int4_col, int8_col, float4_col, float8_col, 
+ *                   timestamp_col, char_col, text_col, bpchar_col, varchar_col, 
+ *                   bytea_col, bit_col, varbit_col) 
+ *                   values 
+ *                   (true, 22, 23, 24, 25.21, 25.22, '1999-10-18 21:35:00', 'a', 
+ *                   'ab', 'a', 'abcde', 'abcdddd', B'00110011001100110011001100110011', 
+ *                    b'10101010101010101010101010101010');
+ */
+static int pg_test(void)
+{
+	int row, col;
+	db_res_t* res;
+	db_con_t* con;
+	struct tm* tt;
+
+	con = pg_init("postgres://ser:heslo@localhost/ser");
+	if (!con) {
+		ERR("Unable to connect database\n");
+		return -1;
+	}
+	INFO("Successfuly connected\n");
+	pg_use_table(con, "test");
+
+	pg_query(con, 0, 0, 0, 0, 0, 0, 0, &res);
+	if (!res) {
+		ERR("No result received\n");
+		return -1;
+	}
+	if (!res->n) {
+		ERR("Result contains no rows\n");
+		return -1;
+	} else {
+		INFO("Result contains %d rows\n", res->n);
+	}
+
+	INFO("Result contains %d columns\n", res->col.n);
+	
+	for(row = 0; row < res->n; row++) {
+		for(col = 0; col < res->col.n; col++) {
+			switch(res->col.types[col]) {
+			case DB_INT:
+				INFO("INT(%d)", res->rows[row].values[col].val.int_val);
+				break;
+
+			case DB_DOUBLE:
+				INFO("DOUBLE(%f)", res->rows[row].values[col].val.double_val);
+				break;
+
+			case DB_STRING:
+				INFO("STRING(%s)", res->rows[row].values[col].val.string_val);
+				break;
+
+			case DB_STR:
+				INFO("STR(%.*s)", res->rows[row].values[col].val.str_val.len, res->rows[row].values[col].val.str_val.s);
+				break;
+
+			case DB_DATETIME:
+				tt = gmtime(&res->rows[row].values[col].val.time_val);
+				INFO("DATETIME(%s)", asctime(tt));
+				break;
+
+			case DB_BLOB:
+				INFO("BLOB(%.*s)", res->rows[row].values[col].val.str_val.len, res->rows[row].values[col].val.str_val.s);
+				break;
+
+			case DB_BITMAP:
+				INFO("INT(%x)", res->rows[row].values[col].val.bitmap_val);
+				break;
+
+			default:
+				ERR("Unsupported column type\n");
+				return -1;
+			}
+		}
+	}
+
+	pg_close(con);
+	return -1;
+}
+
+
 struct module_exports exports = {	
 struct module_exports exports = {	
 	"postgres",
 	"postgres",
 	cmds,
 	cmds,
@@ -77,4 +176,3 @@ struct module_exports exports = {
 	0,         /* oncancel function */
 	0,         /* oncancel function */
 	0          /* per-child init function */
 	0          /* per-child init function */
 };
 };
-

+ 153 - 139
modules/db_postgres/dbase.c

@@ -1,7 +1,7 @@
 /* 
 /* 
  * $Id$ 
  * $Id$ 
  *
  *
- * MySQL module core functions
+ * Postgres module core functions
  *
  *
  * Portions Copyright (C) 2001-2003 FhG FOKUS
  * Portions Copyright (C) 2001-2003 FhG FOKUS
  * Copyright (C) 2003 August.Net Services, LLC
  * Copyright (C) 2003 August.Net Services, LLC
@@ -58,6 +58,7 @@
 #define UPDATE    "update "
 #define UPDATE    "update "
 #define SET       "set "
 #define SET       "set "
 
 
+#define MAX_OPERATOR_LEN (sizeof(" is NULL") - 1)
 
 
 struct pg_params {
 struct pg_params {
 	int n;
 	int n;
@@ -101,7 +102,7 @@ static struct pg_params* new_pg_params(int n)
 	return ptr;
 	return ptr;
 
 
  error:
  error:
-	LOG(L_ERR, "postgres:new_pg_params: No memory left\n");
+	ERR("No memory left\n");
 	free_pg_params(ptr);
 	free_pg_params(ptr);
 	return 0;
 	return 0;
 }
 }
@@ -113,86 +114,73 @@ static inline int params_add(struct pg_params* p, db_con_t* con, db_val_t* vals,
 	db_val_t* val;
 	db_val_t* val;
 
 
 	if (!p) {
 	if (!p) {
-		LOG(L_ERR, "postgres:params_add: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
 	if (p->cur + n > p->n) {
 	if (p->cur + n > p->n) {
-		LOG(L_ERR, "postgres:params_add: Arrays too short (bug in postgres module)\n");
+		ERR("Arrays too short (bug in postgres module)\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
 	for(i = 0; i < n; i++) {
 	for(i = 0; i < n; i++) {
 		val = &vals[i];
 		val = &vals[i];
 		p->formats[p->cur] = 1;
 		p->formats[p->cur] = 1;
+		if (val->nul) continue;
 		switch(val->type) {
 		switch(val->type) {
 		case DB_INT:      
 		case DB_INT:      
-			if (!val->nul) {
-				val->val.int_val = ntohl(val->val.int_val);
-				p->data[p->cur] = (const char*)&val->val.int_val;
-				p->len[p->cur] = 4;
-			}
+			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;
 			break;
 			
 			
 		case DB_DOUBLE:
 		case DB_DOUBLE:
-			if (!val->nul) {
-				     /* Change the byte order of 8-byte value to network
-				      * byte order if necessary
-				      */
-				i1 = htonl(val->val.int8_val >> 32);
-				i2 = htonl(val->val.int8_val & 0xffffffff);
-				val->val.int_val = i1;
-				(&val->val.int_val)[1] = i2;
-				p->data[p->cur] = (const char*)&val->val.int_val;
-				p->len[p->cur] = 8;
-			}
+			     /* Change the byte order of 8-byte value to network
+			      * byte order if necessary
+			      */
+			i1 = htonl(val->val.int8_val >> 32);
+			i2 = htonl(val->val.int8_val & 0xffffffff);
+			val->val.int_val = i1;
+			(&val->val.int_val)[1] = i2;
+			p->data[p->cur] = (const char*)&val->val.int_val;
+			p->len[p->cur] = 8;
 			break;
 			break;
 			
 			
 		case DB_STRING:
 		case DB_STRING:
 			p->formats[p->cur] = 0;
 			p->formats[p->cur] = 0;
-			if (!val->nul) {
-				p->data[p->cur] = val->val.string_val;
-			}
+			p->data[p->cur] = val->val.string_val;
 			break;
 			break;
 			
 			
 		case DB_STR:
 		case DB_STR:
-			if (!val->nul) {
-				p->data[p->cur] = val->val.str_val.s;
-				p->len[p->cur] = val->val.str_val.len;
-			}
+			p->data[p->cur] = val->val.str_val.s;
+			p->len[p->cur] = val->val.str_val.len;
 			break;
 			break;
 			
 			
 		case DB_DATETIME:
 		case DB_DATETIME:
-			if (!val->nul) {
-				if (CON_FLAGS(con) & PG_INT8_TIMESTAMP) {
-					val->val.int8_val = ((long long)val->val.time_val - PG_EPOCH_TIME) * 1000000;
-				} else {
-					val->val.double_val = (double)val->val.time_val - (double)PG_EPOCH_TIME;
-					
-				}
-				i1 = htonl(val->val.int8_val >> 32);
-				i2 = htonl(val->val.int8_val & 0xffffffff);
-				val->val.int_val = i1;
-				(&val->val.int_val)[1] = i2;
-				p->data[p->cur] = (const char*)&val->val.int_val;
-				p->len[p->cur] = 8;
+			if (CON_FLAGS(con) & PG_INT8_TIMESTAMP) {
+				val->val.int8_val = ((long long)val->val.time_val - PG_EPOCH_TIME) * 1000000;
+			} else {
+				val->val.double_val = (double)val->val.time_val - (double)PG_EPOCH_TIME;
+				
 			}
 			}
+			i1 = htonl(val->val.int8_val >> 32);
+			i2 = htonl(val->val.int8_val & 0xffffffff);
+			val->val.int_val = i1;
+			(&val->val.int_val)[1] = i2;
+			p->data[p->cur] = (const char*)&val->val.int_val;
+			p->len[p->cur] = 8;
 			break;
 			break;
 			
 			
 		case DB_BLOB:
 		case DB_BLOB:
-			if (!val->nul) {
-				p->data[p->cur] = val->val.blob_val.s;
-				p->len[p->cur] = val->val.blob_val.len;
-			}
+			p->data[p->cur] = val->val.blob_val.s;
+			p->len[p->cur] = val->val.blob_val.len;
 			break;
 			break;
 			
 			
 		case DB_BITMAP: 
 		case DB_BITMAP: 
-			if (!val->nul) {
-				(&val->val.int_val)[1] = htonl(val->val.int_val);
-				val->val.int_val = htonl(32);
-				p->data[p->cur] = (const char*)&val->val.int_val;
-				p->len[p->cur] = 8;
-			}
+			(&val->val.int_val)[1] = htonl(val->val.int_val);
+			val->val.int_val = htonl(32);
+			p->data[p->cur] = (const char*)&val->val.int_val;
+			p->len[p->cur] = 8;
 			break;
 			break;
 		}
 		}
 		
 		
@@ -214,7 +202,7 @@ static inline void free_params(struct pg_params* p)
  * Initialize database module
  * Initialize database module
  * No function should be called before this
  * No function should be called before this
  */
  */
-db_con_t* db_init(const char* url)
+db_con_t* pg_init(const char* url)
 {
 {
 	struct db_id* id;
 	struct db_id* id;
 	struct pg_con* con;
 	struct pg_con* con;
@@ -224,35 +212,35 @@ db_con_t* db_init(const char* url)
 	res = 0;
 	res = 0;
 
 
 	if (!url) {
 	if (!url) {
-		LOG(L_ERR, "postgres:db_init: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return 0;
 		return 0;
 	}
 	}
 
 
 	res = pkg_malloc(sizeof(db_con_t) + sizeof(struct pg_con*));
 	res = pkg_malloc(sizeof(db_con_t) + sizeof(struct pg_con*));
 	if (!res) {
 	if (!res) {
-		LOG(L_ERR, "postgres:db_init: No memory left\n");
+		ERR("No memory left\n");
 		return 0;
 		return 0;
 	}
 	}
 	memset(res, 0, sizeof(db_con_t) + sizeof(struct pg_con*));
 	memset(res, 0, sizeof(db_con_t) + sizeof(struct pg_con*));
 
 
 	id = new_db_id(url);
 	id = new_db_id(url);
 	if (!id) {
 	if (!id) {
-		LOG(L_ERR, "postgres:db_init: Cannot parse URL '%s'\n", url);
+		ERR("Cannot parse URL '%s'\n", url);
 		goto err;
 		goto err;
 	}
 	}
 
 
 	     /* Find the connection in the pool */
 	     /* Find the connection in the pool */
 	con = (struct pg_con*)pool_get(id);
 	con = (struct pg_con*)pool_get(id);
 	if (!con) {
 	if (!con) {
-		DBG("postgres:db_init: Connection '%s' not found in pool\n", url);
+		DBG("Connection '%s' not found in pool\n", url);
 		     /* Not in the pool yet */
 		     /* Not in the pool yet */
-		con = new_connection(id);
+		con = pg_new_connection(id);
 		if (!con) {
 		if (!con) {
 			goto err;
 			goto err;
 		}
 		}
 		pool_insert((struct pool_con*)con);
 		pool_insert((struct pool_con*)con);
 	} else {
 	} else {
-		DBG("postgres:db_init: Connection '%s' found in pool\n", url);
+		DBG("Connection '%s' found in pool\n", url);
 	}
 	}
 
 
 	res->tail = (unsigned long)con;
 	res->tail = (unsigned long)con;
@@ -269,18 +257,18 @@ db_con_t* db_init(const char* url)
  * Shut down database module
  * Shut down database module
  * No function should be called after this
  * No function should be called after this
  */
  */
-void db_close(db_con_t* handle)
+void pg_close(db_con_t* handle)
 {
 {
 	struct pool_con* con;
 	struct pool_con* con;
 
 
 	if (!handle) {
 	if (!handle) {
-		LOG(L_ERR, "postgres:db_close: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return;
 		return;
 	}
 	}
 
 
 	con = (struct pool_con*)handle->tail;
 	con = (struct pool_con*)handle->tail;
 	if (pool_remove(con) != 0) {
 	if (pool_remove(con) != 0) {
-		free_connection((struct pg_con*)con);
+		pg_free_connection((struct pg_con*)con);
 	}
 	}
 
 
 	pkg_free(handle);
 	pkg_free(handle);
@@ -387,7 +375,7 @@ static unsigned int calc_delete_len(db_con_t* con, db_key_t* keys, int n)
 	if (n) {
 	if (n) {
 		len += 1; /* _ */
 		len += 1; /* _ */
 		len += sizeof(WHERE) - 1;
 		len += sizeof(WHERE) - 1;
-		len += n * 2; /* <= */
+		len += n * MAX_OPERATOR_LEN;
 		len += (sizeof(AND) - 1) * (n - 1);
 		len += (sizeof(AND) - 1) * (n - 1);
 		for(i = 0; i < n; i++) {
 		for(i = 0; i < n; i++) {
 			len += strlen(keys[i]);
 			len += strlen(keys[i]);
@@ -417,7 +405,7 @@ static unsigned int calc_select_len(db_con_t* con, db_key_t* cols, db_key_t* key
 	len += 1; /* _ */
 	len += 1; /* _ */
 	if (n) {
 	if (n) {
 		len += sizeof(WHERE) - 1;
 		len += sizeof(WHERE) - 1;
-		len += n * 2; /* <= */
+		len += n * MAX_OPERATOR_LEN;
 		len += (sizeof(AND) - 1) * (n - 1);
 		len += (sizeof(AND) - 1) * (n - 1);
 		for(i = 0; i < n; i++) {
 		for(i = 0; i < n; i++) {
 			len += strlen(keys[i]);
 			len += strlen(keys[i]);
@@ -452,7 +440,7 @@ static unsigned int calc_update_len(db_con_t* con, db_key_t* ukeys, db_key_t* ke
 	
 	
 	if (n) {
 	if (n) {
 		len += sizeof(WHERE) - 1;
 		len += sizeof(WHERE) - 1;
-		len += n * 2; /* <= */
+		len += n * MAX_OPERATOR_LEN;
 		len += (sizeof(AND) - 1) * (n - 1);
 		len += (sizeof(AND) - 1) * (n - 1);
 		for(i = 0; i < n; i++) {
 		for(i = 0; i < n; i++) {
 			len += strlen(keys[i]);
 			len += strlen(keys[i]);
@@ -463,7 +451,7 @@ static unsigned int calc_update_len(db_con_t* con, db_key_t* ukeys, db_key_t* ke
 }
 }
 
 
 
 
-char* print_insert(db_con_t* con, db_key_t* keys, int n)
+static char* print_insert(db_con_t* con, db_key_t* keys, int n)
 {
 {
 	unsigned int len;
 	unsigned int len;
 	int i;
 	int i;
@@ -471,7 +459,7 @@ char* print_insert(db_con_t* con, db_key_t* keys, int n)
 	str p;
 	str p;
 
 
 	if (!n || !keys) {
 	if (!n || !keys) {
-		LOG(L_ERR, "postgres:print_insert: Nothing to insert\n");
+		ERR("Nothing to insert\n");
 		return 0;
 		return 0;
 	}
 	}
 
 
@@ -479,7 +467,7 @@ char* print_insert(db_con_t* con, db_key_t* keys, int n)
 	
 	
 	s = (char*)pkg_malloc(len + 1);
 	s = (char*)pkg_malloc(len + 1);
 	if (!s) {
 	if (!s) {
-		LOG(L_ERR, "postgres:print_insert: Unable to allocate %d of memory\n", len);
+		ERR("Unable to allocate %d of memory\n", len);
 		return 0;
 		return 0;
 	}
 	}
 	p.s = s;
 	p.s = s;
@@ -506,14 +494,15 @@ char* print_insert(db_con_t* con, db_key_t* keys, int n)
 	return s;
 	return s;
 
 
  shortbuf:
  shortbuf:
-	LOG(L_ERR, "postgres:print_insert: Buffer too short (bug in postgres module)\n");
+	ERR("Buffer too short (bug in postgres module)\n");
 	pkg_free(s);
 	pkg_free(s);
 	return 0;
 	return 0;
 }
 }
 
 
 
 
 
 
-char* print_select(db_con_t* con, db_key_t* cols, db_key_t* keys, int n, int ncol, db_op_t* ops, db_key_t order)
+static char* print_select(db_con_t* con, db_key_t* cols, db_key_t* keys, db_val_t* vals, 
+			  int n, int ncol, db_op_t* ops, db_key_t order)
 {
 {
 	unsigned int len;
 	unsigned int len;
 	int i;
 	int i;
@@ -524,7 +513,7 @@ char* print_select(db_con_t* con, db_key_t* cols, db_key_t* keys, int n, int nco
 
 
 	s = (char*)pkg_malloc(len + 1);
 	s = (char*)pkg_malloc(len + 1);
 	if (!s) {
 	if (!s) {
-		LOG(L_ERR, "postrgres:print_select: Unable to allocate %d of memory\n", len);
+		ERR("Unable to allocate %d of memory\n", len);
 		return 0;
 		return 0;
 	}
 	}
 	p.s = s;
 	p.s = s;
@@ -547,23 +536,31 @@ char* print_select(db_con_t* con, db_key_t* cols, db_key_t* keys, int n, int nco
 	if (n) {
 	if (n) {
 		append(p, WHERE);
 		append(p, WHERE);
 		append_str(p, keys[0]);
 		append_str(p, keys[0]);
-		if (ops) {
-			append_str(p, *ops);
-			ops++;
+		if (vals[0].nul) {
+			append(p, " is NULL");
 		} else {
 		} else {
-			append(p, "=");
-		}
-		append_param(p, 1);
-		for(i = 1; i < n; i++) {
-			append(p, AND);
-			append_str(p, keys[i]);
 			if (ops) {
 			if (ops) {
 				append_str(p, *ops);
 				append_str(p, *ops);
 				ops++;
 				ops++;
 			} else {
 			} else {
 				append(p, "=");
 				append(p, "=");
 			}
 			}
-			append_param(p, i + 1);
+			append_param(p, 1);
+		}
+		for(i = 1; i < n; i++) {
+			append(p, AND);
+			append_str(p, keys[i]);
+			if (vals[i].nul) {
+				append(p, " is NULL");
+			} else {
+				if (ops) {
+					append_str(p, *ops);
+					ops++;
+				} else {
+					append(p, "=");
+				}
+				append_param(p, i + 1);
+			}
 		}
 		}
 		append(p, " ");
 		append(p, " ");
 	}
 	}
@@ -576,13 +573,13 @@ char* print_select(db_con_t* con, db_key_t* cols, db_key_t* keys, int n, int nco
 	return s;
 	return s;
 
 
  shortbuf:
  shortbuf:
-	LOG(L_ERR, "postgres:print_select: Buffer too short (bug in postgres module)\n");
+	ERR("Buffer too short (bug in postgres module)\n");
 	pkg_free(s);
 	pkg_free(s);
 	return 0;
 	return 0;
 }
 }
 
 
 
 
-char* print_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, int n)
+static char* print_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, int n)
 {
 {
 	unsigned int len;
 	unsigned int len;
 	int i;
 	int i;
@@ -593,7 +590,7 @@ char* print_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, int n)
 
 
 	s = (char*)pkg_malloc(len + 1);
 	s = (char*)pkg_malloc(len + 1);
 	if (!s) {
 	if (!s) {
-		LOG(L_ERR, "postrgres:print_delete: Unable to allocate %d of memory\n", len);
+		ERR("Unable to allocate %d of memory\n", len);
 		return 0;
 		return 0;
 	}
 	}
 	p.s = s;
 	p.s = s;
@@ -605,23 +602,31 @@ char* print_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, int n)
 	if (n) {
 	if (n) {
 		append(p, WHERE);
 		append(p, WHERE);
 		append_str(p, keys[0]);
 		append_str(p, keys[0]);
-		if (ops) {
-			append_str(p, *ops);
-			ops++;
+		if (vals[0].nul) {
+			append(p, " is NULL");
 		} else {
 		} else {
-			append(p, "=");
-		}
-		append_param(p, 1);
-		for(i = 1; i < n; i++) {
-			append(p, AND);
-			append_str(p, keys[i]);
 			if (ops) {
 			if (ops) {
 				append_str(p, *ops);
 				append_str(p, *ops);
 				ops++;
 				ops++;
 			} else {
 			} else {
 				append(p, "=");
 				append(p, "=");
 			}
 			}
-			append_param(p, i + 1);
+			append_param(p, 1);
+		}
+		for(i = 1; i < n; i++) {
+			append(p, AND);
+			append_str(p, keys[i]);
+			if (vals[i].nul) {
+				append(p, " is NULL");
+			} else {
+				if (ops) {
+					append_str(p, *ops);
+					ops++;
+				} else {
+					append(p, "=");
+				}
+				append_param(p, i + 1);
+			}
 		}
 		}
 	}
 	}
 
 
@@ -629,13 +634,14 @@ char* print_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, int n)
 	return s;
 	return s;
 
 
  shortbuf:
  shortbuf:
-	LOG(L_ERR, "postgres:print_delete: Buffer too short (bug in postgres module)\n");
+	ERR("Buffer too short (bug in postgres module)\n");
 	pkg_free(s);
 	pkg_free(s);
 	return 0;
 	return 0;
 }
 }
 
 
 
 
-char* print_update(db_con_t* con, db_key_t* ukeys, db_key_t* keys, db_op_t* ops, int un, int n)
+static char* print_update(db_con_t* con, db_key_t* ukeys, db_key_t* keys, db_op_t* ops, 
+			  db_val_t* vals, int un, int n)
 {
 {
 	unsigned int len, param_no;
 	unsigned int len, param_no;
 	char* s;
 	char* s;
@@ -643,7 +649,7 @@ char* print_update(db_con_t* con, db_key_t* ukeys, db_key_t* keys, db_op_t* ops,
 	str p;
 	str p;
 
 
 	if (!un) {
 	if (!un) {
-		LOG(L_ERR, "postgres:print_update: Nothing to update\n");
+		ERR("Nothing to update\n");
 		return 0;
 		return 0;
 	}
 	}
 
 
@@ -652,7 +658,7 @@ char* print_update(db_con_t* con, db_key_t* ukeys, db_key_t* keys, db_op_t* ops,
 
 
 	s = (char*)pkg_malloc(len + 1);
 	s = (char*)pkg_malloc(len + 1);
 	if (!s) {
 	if (!s) {
-		LOG(L_ERR, "postrgres:print_update: Unable to allocate %d of memory\n", len);
+		ERR("Unable to allocate %d of memory\n", len);
 		return 0;
 		return 0;
 	}
 	}
 	p.s = s;
 	p.s = s;
@@ -677,17 +683,9 @@ char* print_update(db_con_t* con, db_key_t* ukeys, db_key_t* keys, db_op_t* ops,
 	if (n) {
 	if (n) {
 		append(p, WHERE);
 		append(p, WHERE);
 		append_str(p, keys[0]);
 		append_str(p, keys[0]);
-		if (ops) {
-			append_str(p, *ops);
-			ops++;
+		if (vals[0].nul) {
+			append(p, " is NULL");
 		} else {
 		} else {
-			append(p, "=");
-		}
-		append_param(p, param_no++);
-		
-		for(i = 1; i < n; i++) {
-			append(p, AND);
-			append_str(p, keys[i]);
 			if (ops) {
 			if (ops) {
 				append_str(p, *ops);
 				append_str(p, *ops);
 				ops++;
 				ops++;
@@ -696,13 +694,29 @@ char* print_update(db_con_t* con, db_key_t* ukeys, db_key_t* keys, db_op_t* ops,
 			}
 			}
 			append_param(p, param_no++);
 			append_param(p, param_no++);
 		}
 		}
+		
+		for(i = 1; i < n; i++) {
+			append(p, AND);
+			append_str(p, keys[i]);
+			if (vals[i].nul) {
+				append(p, " is NULL");
+			} else {
+				if (ops) {
+					append_str(p, *ops);
+					ops++;
+				} else {
+					append(p, "=");
+				}
+				append_param(p, param_no++);
+			}
+		}
 	}	
 	}	
 
 
 	*p.s = '\0';
 	*p.s = '\0';
 	return s;
 	return s;
 
 
  shortbuf:
  shortbuf:
-	LOG(L_ERR, "postgres:print_update: Buffer too short (bug in postgres module)\n");
+	ERR("Buffer too short (bug in postgres module)\n");
 	pkg_free(s);
 	pkg_free(s);
 	return 0; 
 	return 0; 
 }
 }
@@ -716,10 +730,10 @@ static int submit_query(db_res_t** res, db_con_t* con, const char* query, struct
 {
 {
 	PGresult* pgres;
 	PGresult* pgres;
 
 
-	DBG("postgres: Executing '%s'\n", query);
-	if (params) {
+	DBG("Executing '%s'\n", query);
+	if (params && params->cur) {
 	        pgres = PQexecParams(CON_CONNECTION(con), query,
 	        pgres = PQexecParams(CON_CONNECTION(con), query,
-				     params->n, 0,
+				     params->cur, 0,
 				     params->data, params->len,
 				     params->data, params->len,
 				     params->formats, 1);
 				     params->formats, 1);
 	} else {
 	} else {
@@ -727,7 +741,7 @@ static int submit_query(db_res_t** res, db_con_t* con, const char* query, struct
 	}
 	}
 	switch(PQresultStatus(pgres)) {
 	switch(PQresultStatus(pgres)) {
 	case PGRES_EMPTY_QUERY:
 	case PGRES_EMPTY_QUERY:
-		LOG(L_ERR, "postgres:submit_query:BUG: db_raw_query received an empty query\n");
+		ERR("BUG: db_raw_query received an empty query\n");
 		goto error;
 		goto error;
 		
 		
 	case PGRES_COMMAND_OK:
 	case PGRES_COMMAND_OK:
@@ -738,23 +752,22 @@ static int submit_query(db_res_t** res, db_con_t* con, const char* query, struct
 		
 		
 	case PGRES_COPY_OUT:
 	case PGRES_COPY_OUT:
 	case PGRES_COPY_IN:
 	case PGRES_COPY_IN:
-		LOG(L_ERR, "postgres:submit_query: Unsupported transfer mode\n");
+		ERR("Unsupported transfer mode\n");
 		goto error;
 		goto error;
 
 
 	case PGRES_BAD_RESPONSE:
 	case PGRES_BAD_RESPONSE:
 	case PGRES_FATAL_ERROR:
 	case PGRES_FATAL_ERROR:
-		LOG(L_ERR, "postgres: Error: %s", PQresultErrorMessage(pgres));
+		ERR("Error: %s", PQresultErrorMessage(pgres));
 		if (PQstatus(CON_CONNECTION(con)) != CONNECTION_BAD) {
 		if (PQstatus(CON_CONNECTION(con)) != CONNECTION_BAD) {
-				LOG(L_ERR, "postgres: Unknown error occurred, giving up\n");
 				goto error;
 				goto error;
 		}
 		}
-		LOG(L_ERR, "postgres:submit_query: Bad connection\n");
+		ERR("Bad connection\n");
 		PQclear(pgres);
 		PQclear(pgres);
 		return 1;
 		return 1;
 	}
 	}
 
 
 	if (res) {
 	if (res) {
-		*res = new_result(pgres);
+		*res = pg_new_result(pgres);
 		if (!(*res)) goto error;
 		if (!(*res)) goto error;
 	} else {
 	} else {
 		PQclear(pgres);
 		PQclear(pgres);
@@ -771,16 +784,16 @@ static int reconnect(db_con_t* con)
 {
 {
 	int attempts_left = reconnect_attempts;
 	int attempts_left = reconnect_attempts;
 	while(attempts_left) {
 	while(attempts_left) {
-		LOG(L_ERR, "postgres: Trying to recover the connection\n");
+		ERR("Trying to recover the connection\n");
 		PQreset(CON_CONNECTION(con));
 		PQreset(CON_CONNECTION(con));
 		if (PQstatus(CON_CONNECTION(con)) == CONNECTION_OK) {
 		if (PQstatus(CON_CONNECTION(con)) == CONNECTION_OK) {
-			LOG(L_ERR, "postgres: Successfuly reconnected\n");
+			ERR("Successfuly reconnected\n");
 			return 0;
 			return 0;
 		}
 		}
-		LOG(L_ERR, "postgres: Reconnect attempt failed\n");
+		ERR("Reconnect attempt failed\n");
 		attempts_left--;
 		attempts_left--;
 	}
 	}
-	LOG(L_ERR, "postgres: No more reconnect attempts left, giving up\n");
+        ERR("No more reconnect attempts left, giving up\n");
 	return -1;
 	return -1;
 }
 }
 
 
@@ -797,7 +810,7 @@ static int reconnect(db_con_t* con)
  * order: order by the specified column
  * order: order by the specified column
  * res:   query result
  * res:   query result
  */
  */
-int db_query(db_con_t* con, db_key_t* keys, db_op_t* ops,
+int pg_query(db_con_t* con, db_key_t* keys, db_op_t* ops,
 	     db_val_t* vals, db_key_t* cols, int n, int ncols,
 	     db_val_t* vals, db_key_t* cols, int n, int ncols,
 	     db_key_t order, db_res_t** res)
 	     db_key_t order, db_res_t** res)
 {
 {
@@ -807,13 +820,14 @@ int db_query(db_con_t* con, db_key_t* keys, db_op_t* ops,
 	
 	
 	params = 0;
 	params = 0;
 	select = 0;
 	select = 0;
+	if (res) *res = 0;
 
 
 	if (!con) {
 	if (!con) {
-		LOG(L_ERR, "postgres:db_query: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
-	select = print_select(con, cols, keys, n, ncols, ops, order);
+	select = print_select(con, cols, keys, vals, n, ncols, ops, order);
 	if (!select) goto err;
 	if (!select) goto err;
 
 
 	params = new_pg_params(n);
 	params = new_pg_params(n);
@@ -829,8 +843,8 @@ int db_query(db_con_t* con, db_key_t* keys, db_op_t* ops,
 		}
 		}
 	} while(ret != 0);
 	} while(ret != 0);
 	
 	
-	if (res && convert_result(*res, con) < 0) {
-		free_result(*res);
+	if (res && pg_convert_result(*res, con) < 0) {
+		pg_free_result(*res);
 		goto err;
 		goto err;
 	}
 	}
 
 
@@ -848,12 +862,12 @@ int db_query(db_con_t* con, db_key_t* keys, db_op_t* ops,
 /*
 /*
  * Execute a raw SQL query
  * Execute a raw SQL query
  */
  */
-int db_raw_query(db_con_t* con, char* query, db_res_t** res)
+int pg_raw_query(db_con_t* con, char* query, db_res_t** res)
 {
 {
 	int ret;
 	int ret;
 
 
 	if (!con || !query) {
 	if (!con || !query) {
-		LOG(L_ERR, "postgres:db_raw_query: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
@@ -866,8 +880,8 @@ int db_raw_query(db_con_t* con, char* query, db_res_t** res)
 		}
 		}
 	} while(ret != 0);
 	} while(ret != 0);
 	
 	
-	if (res && (convert_result(*res, con) < 0)) {
-		free_result(*res);
+	if (res && (pg_convert_result(*res, con) < 0)) {
+		pg_free_result(*res);
 		return -1;
 		return -1;
 	}
 	}
 	return 0;
 	return 0;
@@ -881,14 +895,14 @@ int db_raw_query(db_con_t* con, char* query, db_res_t** res)
  * vals: values of the keys
  * vals: values of the keys
  * n:    number of key=value pairs
  * n:    number of key=value pairs
  */
  */
-int db_insert(db_con_t* con, db_key_t* keys, db_val_t* vals, int n)
+int pg_insert(db_con_t* con, db_key_t* keys, db_val_t* vals, int n)
 {
 {
 	int ret;
 	int ret;
 	char* insert;
 	char* insert;
 	struct pg_params* params;
 	struct pg_params* params;
 
 
 	if (!con || !keys || !vals || !n) {
 	if (!con || !keys || !vals || !n) {
-		LOG(L_ERR, "postgres:db_insert: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
@@ -930,21 +944,21 @@ int db_insert(db_con_t* con, db_key_t* keys, db_val_t* vals, int n)
  * vals: values of the keys that must match
  * vals: values of the keys that must match
  * n   : number of key=value pairs
  * n   : number of key=value pairs
  */
  */
-int db_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, int n)
+int pg_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, int n)
 {
 {
 	int ret;
 	int ret;
 	char* delete;
 	char* delete;
 	struct pg_params* params;
 	struct pg_params* params;
 
 
 	if (!con) {
 	if (!con) {
-		LOG(L_ERR, "postgres:db_insert: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
 	params = 0;
 	params = 0;
 	delete = 0;
 	delete = 0;
 
 
-        delete = print_delete(con, keys, ops, n);
+        delete = print_delete(con, keys, ops, vals, n);
 	if (!delete) goto err;
 	if (!delete) goto err;
 
 
 	params = new_pg_params(n);
 	params = new_pg_params(n);
@@ -982,7 +996,7 @@ int db_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, int n
  * n    : number of key=value pairs
  * n    : number of key=value pairs
  * un   : number of columns to update
  * un   : number of columns to update
  */
  */
-int db_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals,
+int pg_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals,
 	      db_key_t* ucols, db_val_t* uvals, int n, int un)
 	      db_key_t* ucols, db_val_t* uvals, int n, int un)
 {
 {
 	int ret;
 	int ret;
@@ -990,14 +1004,14 @@ int db_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals,
 	struct pg_params* params;
 	struct pg_params* params;
 
 
 	if (!con || !ucols || !uvals || !un) {
 	if (!con || !ucols || !uvals || !un) {
-		LOG(L_ERR, "db_update: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
 	params = 0;
 	params = 0;
 	update = 0;
 	update = 0;
 
 
-	update = print_update(con, ucols, keys, ops, un, n);
+	update = print_update(con, ucols, keys, ops, vals, un, n);
 	if (!update) goto err;
 	if (!update) goto err;
 
 
 	params = new_pg_params(n + un);
 	params = new_pg_params(n + un);
@@ -1028,8 +1042,8 @@ int db_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals,
 /*
 /*
  * Release a result set from memory
  * Release a result set from memory
  */
  */
-int db_free_result(db_con_t* con, db_res_t* res)
+int pg_db_free_result(db_con_t* con, db_res_t* res)
 {
 {
-	free_result(res);
+	pg_free_result(res);
 	return 0;
 	return 0;
 }
 }

+ 9 - 9
modules/db_postgres/dbase.h

@@ -44,50 +44,50 @@
 /*
 /*
  * Initialize database connection
  * Initialize database connection
  */
  */
-db_con_t* db_init(const char* uri);
+db_con_t* pg_init(const char* uri);
 
 
 
 
 /*
 /*
  * Close a database connection
  * Close a database connection
  */
  */
-void db_close(db_con_t* con);
+void pg_close(db_con_t* con);
 
 
 
 
 /*
 /*
  * Free all memory allocated by get_result
  * Free all memory allocated by get_result
  */
  */
-int db_free_result(db_con_t* con, db_res_t* res);
+int pg_db_free_result(db_con_t* con, db_res_t* res);
 
 
 
 
 /*
 /*
  * Do a query
  * Do a query
  */
  */
-int db_query(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, db_key_t* cols, int n, int nc,
+int pg_query(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, db_key_t* cols, int n, int nc,
 	     db_key_t order, db_res_t** res);
 	     db_key_t order, db_res_t** res);
 
 
 
 
 /*
 /*
  * Raw SQL query
  * Raw SQL query
  */
  */
-int db_raw_query(db_con_t* con, char* query, db_res_t** res);
+int pg_raw_query(db_con_t* con, char* query, db_res_t** res);
 
 
 
 
 /*
 /*
  * Insert a row into table
  * Insert a row into table
  */
  */
-int db_insert(db_con_t* con, db_key_t* keys, db_val_t* vals, int n);
+int pg_insert(db_con_t* con, db_key_t* keys, db_val_t* vals, int n);
 
 
 
 
 /*
 /*
  * Delete a row from table
  * Delete a row from table
  */
  */
-int db_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, int n);
+int pg_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, int n);
 
 
 
 
 /*
 /*
  * Update a row in table
  * Update a row in table
  */
  */
-int db_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals,
+int pg_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals,
 	      db_key_t* ucols, db_val_t* uvals, int n, int un);
 	      db_key_t* ucols, db_val_t* uvals, int n, int un);
 
 
 
 
@@ -96,7 +96,7 @@ int db_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals,
  * Store name of table that will be used by
  * Store name of table that will be used by
  * subsequent database functions
  * subsequent database functions
  */
  */
-int use_table(db_con_t* con, const char* table);
+int pg_use_table(db_con_t* con, const char* table);
 
 
 
 
 #endif /* _DBASE_H */
 #endif /* _DBASE_H */

+ 21 - 16
modules/db_postgres/pg_con.c

@@ -52,7 +52,7 @@ static void notice_processor(void* arg, const char* message)
  * The function returns 1 if the server stores timestamps as int8 and 0
  * The function returns 1 if the server stores timestamps as int8 and 0
  * if it is stored as double
  * if it is stored as double
  */
  */
-int timestamp_format(PGconn* con)
+static int timestamp_format(PGconn* con)
 {
 {
 	unsigned long long offset;
 	unsigned long long offset;
 	PGresult* res = 0;
 	PGresult* res = 0;
@@ -61,17 +61,17 @@ int timestamp_format(PGconn* con)
 	res = PQexecParams(con, "select timestamp '2000-01-01 00:00:00' + time '00:00:01'", 0, 0, 0, 0, 0, 1);	
 	res = PQexecParams(con, "select timestamp '2000-01-01 00:00:00' + time '00:00:01'", 0, 0, 0, 0, 0, 1);	
 
 
 	if (PQfformat(res, 0) != 1) {
 	if (PQfformat(res, 0) != 1) {
-		LOG(L_ERR, "postgres:timestamp_format: Binary format expected but server sent text\n");
+		ERR("Binary format expected but server sent text\n");
 		goto err;
 		goto err;
 	}
 	}
 
 
 	if (PQntuples(res) != 1) {
 	if (PQntuples(res) != 1) {
-		LOG(L_ERR, "postgres:timestamp_format: 1 column expected, %d received\n", PQntuples(res));
+		ERR("1 column expected, %d received\n", PQntuples(res));
 		goto err;
 		goto err;
 	}
 	}
 
 
 	if (PQnfields(res) != 1) {
 	if (PQnfields(res) != 1) {
-		LOG(L_ERR, "postgres:timestamp_format: 1 Row expected, %d received\n", PQnfields(res));
+		ERR("1 Row expected, %d received\n", PQnfields(res));
 		goto err;
 		goto err;
 	}
 	}
 
 
@@ -89,10 +89,10 @@ int timestamp_format(PGconn* con)
 	      * occupied by the variable is read as unsigned long long.
 	      * occupied by the variable is read as unsigned long long.
 	      */
 	      */
 	if (offset == 1000000) {
 	if (offset == 1000000) {
-	        DBG("postgres:int_timestamp_format: Server uses int8 format for timestamps.\n");
+	        DBG("Server uses int8 format for timestamps.\n");
 		return 1;
 		return 1;
 	} else {
 	} else {
-		DBG("postgres:int_timestamp_format: Server uses double format for timestamps.\n");
+		DBG("Server uses double format for timestamps.\n");
 		return 0;
 		return 0;
 	}
 	}
 	
 	
@@ -106,20 +106,20 @@ int timestamp_format(PGconn* con)
  * Create a new connection structure,
  * Create a new connection structure,
  * open the Postgres connection and set reference count to 1
  * open the Postgres connection and set reference count to 1
  */
  */
-struct pg_con* new_connection(struct db_id* id)
+struct pg_con* pg_new_connection(struct db_id* id)
 {
 {
 	struct pg_con* ptr;
 	struct pg_con* ptr;
 	char* port_str;
 	char* port_str;
 	int ret;
 	int ret;
 	
 	
 	if (!id) {
 	if (!id) {
-		LOG(L_ERR, "postgres:new_connection: Invalid parameter value\n");
+		ERR("Invalid parameter value\n");
 		return 0;
 		return 0;
 	}
 	}
 	
 	
 	ptr = (struct pg_con*)pkg_malloc(sizeof(struct pg_con));
 	ptr = (struct pg_con*)pkg_malloc(sizeof(struct pg_con));
 	if (!ptr) {
 	if (!ptr) {
-		LOG(L_ERR, "postgres:new_connection: No memory left\n");
+		ERR("No memory left\n");
 		return 0;
 		return 0;
 	}
 	}
 	memset(ptr, 0, sizeof(struct pg_con));
 	memset(ptr, 0, sizeof(struct pg_con));
@@ -132,7 +132,7 @@ struct pg_con* new_connection(struct db_id* id)
 	}
 	}
 
 
 	if (id->port) {
 	if (id->port) {
-		DBG("postgres: Opening connection to: %s://%s:%s@%s:%d/%s\n",
+		DBG("Opening connection to: %s://%s:%s@%s:%d/%s\n",
 		    ZSW(id->scheme),
 		    ZSW(id->scheme),
 		    ZSW(id->username),
 		    ZSW(id->username),
 		    ZSW(id->password),
 		    ZSW(id->password),
@@ -141,7 +141,7 @@ struct pg_con* new_connection(struct db_id* id)
 		    ZSW(id->database)
 		    ZSW(id->database)
 		    );
 		    );
 	} else {
 	} else {
-		DBG("postgres: Opening connection to: %s://%s:%s@%s/%s\n",
+		DBG("Opening connection to: %s://%s:%s@%s/%s\n",
 		    ZSW(id->scheme),
 		    ZSW(id->scheme),
 		    ZSW(id->username),
 		    ZSW(id->username),
 		    ZSW(id->password),
 		    ZSW(id->password),
@@ -155,12 +155,12 @@ struct pg_con* new_connection(struct db_id* id)
 				id->username, id->password);
 				id->username, id->password);
 	
 	
 	if (ptr->con == 0) {
 	if (ptr->con == 0) {
-		LOG(L_ERR, "postgres:new_connection: PQsetdbLogin ran out of memory\n");
+		ERR("PQsetdbLogin ran out of memory\n");
 		goto err;
 		goto err;
 	}
 	}
 	
 	
 	if (PQstatus(ptr->con) != CONNECTION_OK) {
 	if (PQstatus(ptr->con) != CONNECTION_OK) {
-		LOG(L_ERR, "postgres:new_connection: %s\n",
+		ERR("postgres:new_connection: %s\n",
 		    PQerrorMessage(ptr->con));
 		    PQerrorMessage(ptr->con));
 		goto err;
 		goto err;
 	}
 	}
@@ -168,9 +168,14 @@ struct pg_con* new_connection(struct db_id* id)
 	     /* Override default notice processor */
 	     /* Override default notice processor */
 	PQsetNoticeProcessor(ptr->con, notice_processor, 0);
 	PQsetNoticeProcessor(ptr->con, notice_processor, 0);
 
 
-	DBG("postgres:new_connection: Connected. Protocol version=%d, Server version=%d\n", 
+	DBG("Connected. Protocol version=%d, Server version=%d\n", 
 	    PQprotocolVersion(ptr->con),
 	    PQprotocolVersion(ptr->con),
-	    PQserverVersion(ptr->con));
+#ifdef HAVE_PGSERVERVERSION
+	    PQserverVersion(ptr->con)
+#else
+	    0
+#endif
+	    );
 
 
 	ptr->timestamp = time(0);
 	ptr->timestamp = time(0);
 	ptr->id = id;
 	ptr->id = id;
@@ -193,7 +198,7 @@ struct pg_con* new_connection(struct db_id* id)
 /*
 /*
  * Close the connection and release memory
  * Close the connection and release memory
  */
  */
-void free_connection(struct pg_con* con)
+void pg_free_connection(struct pg_con* con)
 {
 {
 	if (!con) return;
 	if (!con) return;
 	if (con->id) free_db_id(con->id);
 	if (con->id) free_db_id(con->id);

+ 2 - 2
modules/db_postgres/pg_con.h

@@ -63,12 +63,12 @@ struct pg_con {
  * Create a new connection structure,
  * Create a new connection structure,
  * open the MySQL connection and set reference count to 1
  * open the MySQL connection and set reference count to 1
  */
  */
-struct pg_con* new_connection(struct db_id* id);
+struct pg_con* pg_new_connection(struct db_id* id);
 
 
 
 
 /*
 /*
  * Close the connection and release memory
  * Close the connection and release memory
  */
  */
-void free_connection(struct pg_con* con);
+void pg_free_connection(struct pg_con* con);
 
 
 #endif /* _PG_CON_H */
 #endif /* _PG_CON_H */

+ 23 - 23
modules/db_postgres/res.c

@@ -48,19 +48,19 @@ static inline int get_columns(db_res_t* res)
 	int n, i, type;
 	int n, i, type;
 
 
 	if (!res) {
 	if (!res) {
-		LOG(L_ERR, "postgres:get_columns: Invalid parameter\n");
+	        ERR("Invalid parameter\n");
 		goto err;
 		goto err;
 	}
 	}
 
 
 	pgres = (PGresult*)res->data;
 	pgres = (PGresult*)res->data;
 	if (!pgres) {
 	if (!pgres) {
-		LOG(L_ERR, "postgres:get_columns: No postgres result found\n");
+		ERR("No postgres result found\n");
 		goto err;
 		goto err;
 	}
 	}
 
 
 	n = PQnfields(pgres);
 	n = PQnfields(pgres);
 	if (!n) {
 	if (!n) {
-		LOG(L_ERR, "postgres:get_columns: No columns\n");
+		ERR("No columns\n");
 		goto err;
 		goto err;
 	}
 	}
 
 
@@ -68,13 +68,13 @@ static inline int get_columns(db_res_t* res)
 
 
         res->col.names = (db_key_t*)pkg_malloc(sizeof(db_key_t) * n);
         res->col.names = (db_key_t*)pkg_malloc(sizeof(db_key_t) * n);
 	if (!res->col.names) {
 	if (!res->col.names) {
-		LOG(L_ERR, "postgres:get_columns: No memory left\n");
+		ERR("No memory left\n");
 		goto err;
 		goto err;
 	}
 	}
 
 
 	res->col.types = (db_type_t*)pkg_malloc(sizeof(db_type_t) * n);
 	res->col.types = (db_type_t*)pkg_malloc(sizeof(db_type_t) * n);
 	if (!res->col.types) {
 	if (!res->col.types) {
-		LOG(L_ERR, "postgres:get_columns: No memory left\n");
+		ERR("No memory left\n");
 		goto err;
 		goto err;
 	}
 	}
 
 
@@ -82,7 +82,7 @@ static inline int get_columns(db_res_t* res)
 
 
 	for(i = 0; i < n; i++) {
 	for(i = 0; i < n; i++) {
 		if (PQfformat(pgres, i) == 0) {
 		if (PQfformat(pgres, i) == 0) {
-			LOG(L_ERR, "postgres:get_column: Text format of columns not supported\n");
+			ERR("Text format of columns not supported\n");
 			goto err;
 			goto err;
 		}
 		}
 
 
@@ -123,7 +123,7 @@ static inline int get_columns(db_res_t* res)
 			break;
 			break;
 
 
 		default:
 		default:
-			LOG(L_ERR, "postgres:get_columns: Unsupported column type with oid %d\n", type);
+			ERR("Unsupported column type with oid %d\n", type);
 			goto err;
 			goto err;
 		}
 		}
 	}
 	}
@@ -144,7 +144,7 @@ static inline int get_columns(db_res_t* res)
 static inline int free_columns(db_res_t* res)
 static inline int free_columns(db_res_t* res)
 {
 {
 	if (!res) {
 	if (!res) {
-		LOG(L_ERR, "postgres:free_columns: Invalid parameter\n");
+		ERR("Invalid parameter\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
@@ -277,14 +277,14 @@ static inline int convert_cell(db_con_t* con, db_res_t* res, int row, int col)
 	case BITOID:    /* fixed-length bit string */
 	case BITOID:    /* fixed-length bit string */
 	case VARBITOID: /* variable-length bit string */
 	case VARBITOID: /* variable-length bit string */
 		if (ntohl(*(unsigned int*)pgval) != 32) {
 		if (ntohl(*(unsigned int*)pgval) != 32) {
-			LOG(L_ERR, "postgres:convert_cell: Only 32-bit long bitfieds supported\n");
+			ERR("Only 32-bit long bitfieds supported\n");
 			return -1;
 			return -1;
 		}
 		}
 		val->val.bitmap_val = ntohl(*(unsigned int*)(pgval + 4));
 		val->val.bitmap_val = ntohl(*(unsigned int*)(pgval + 4));
 		break;
 		break;
 		
 		
 	default:
 	default:
-		LOG(L_ERR, "postgres:convert_cell: Unsupported column type with oid %d\n", type);
+		ERR("Unsupported column type with oid %d\n", type);
 		return -1;
 		return -1;
 		
 		
 	}
 	}
@@ -302,7 +302,7 @@ static inline int convert_rows(db_con_t* con, db_res_t* res)
 	int r, c;
 	int r, c;
 
 
 	if (!res) {
 	if (!res) {
-		LOG(L_ERR, "postgres:convert_rows: Invalid parameter\n");
+		ERR("Invalid parameter\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
@@ -316,7 +316,7 @@ static inline int convert_rows(db_con_t* con, db_res_t* res)
 
 
 	res->rows = (struct db_row*)pkg_malloc(sizeof(db_row_t) * res->n);
 	res->rows = (struct db_row*)pkg_malloc(sizeof(db_row_t) * res->n);
 	if (!res->rows) {
 	if (!res->rows) {
-		LOG(L_ERR, "postgres:convert_rows: No memory left\n");
+		ERR("No memory left\n");
 		goto err;
 		goto err;
 	}
 	}
 
 
@@ -324,7 +324,7 @@ static inline int convert_rows(db_con_t* con, db_res_t* res)
 		row = &res->rows[r];
 		row = &res->rows[r];
 		row->values = (db_val_t*)pkg_malloc(sizeof(db_val_t) * res->col.n);
 		row->values = (db_val_t*)pkg_malloc(sizeof(db_val_t) * res->col.n);
 		if (!row->values) {
 		if (!row->values) {
-			LOG(L_ERR, "postgres:convert_rows: No memory left to allocate row\n");
+			ERR("No memory left to allocate row\n");
 			res->n = r; /* This is to make sure that the cleanup function release only rows
 			res->n = r; /* This is to make sure that the cleanup function release only rows
 				     * that has been really allocated
 				     * that has been really allocated
 				     */
 				     */
@@ -344,7 +344,7 @@ static inline int convert_rows(db_con_t* con, db_res_t* res)
 	return 0;
 	return 0;
 
 
  err:
  err:
-	free_rows(res);
+	     /*	free_rows(res); Do not free here, pg_free_result will take care of it */
 	return -1;
 	return -1;
 }
 }
 
 
@@ -352,12 +352,12 @@ static inline int convert_rows(db_con_t* con, db_res_t* res)
 /*
 /*
  * Create a new result structure and initialize it
  * Create a new result structure and initialize it
  */
  */
-db_res_t* new_result(PGresult* pgres)
+db_res_t* pg_new_result(PGresult* pgres)
 {
 {
 	db_res_t* r;
 	db_res_t* r;
 	r = (db_res_t*)pkg_malloc(sizeof(db_res_t));
 	r = (db_res_t*)pkg_malloc(sizeof(db_res_t));
 	if (!r) {
 	if (!r) {
-		LOG(L_ERR, "postgres:new_result: No memory left\n");
+		ERR("No memory left\n");
 		return 0;
 		return 0;
 	}
 	}
 
 
@@ -370,21 +370,21 @@ db_res_t* new_result(PGresult* pgres)
 /*
 /*
  * Fill the structure with data from database
  * Fill the structure with data from database
  */
  */
-int convert_result(db_res_t* res, db_con_t* con)
+int pg_convert_result(db_res_t* res, db_con_t* con)
 {
 {
 	if (!res) {
 	if (!res) {
-		LOG(L_ERR, "postgres:convert_result: Invalid parameter\n");
+		ERR("Invalid parameter\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
 	if (get_columns(res) < 0) {
 	if (get_columns(res) < 0) {
-		LOG(L_ERR, "postgres:convert_result: Error while getting column names\n");
+		ERR("Error while getting column names\n");
 		return -2;
 		return -2;
 	}
 	}
 
 
 	if (convert_rows(con, res) < 0) {
 	if (convert_rows(con, res) < 0) {
-		LOG(L_ERR, "postgres:convert_result: Error while converting rows\n");
-		free_columns(res);
+		ERR("Error while converting rows\n");
+		     /* Do not free columns here, pg_free_result will do it */
 		return -3;
 		return -3;
 	}
 	}
 	return 0;
 	return 0;
@@ -394,10 +394,10 @@ int convert_result(db_res_t* res, db_con_t* con)
 /*
 /*
  * Release memory used by a result structure
  * Release memory used by a result structure
  */
  */
-int free_result(db_res_t* res)
+int pg_free_result(db_res_t* res)
 {
 {
 	if (!res) {
 	if (!res) {
-		LOG(L_ERR, "postgres:free_result: Invalid parameter\n");
+		ERR("Invalid parameter\n");
 		return -1;
 		return -1;
 	}
 	}
 
 

+ 3 - 3
modules/db_postgres/res.h

@@ -39,19 +39,19 @@
 /*
 /*
  * Create a new result structure and initialize it
  * Create a new result structure and initialize it
  */
  */
-db_res_t* new_result(PGresult* pgres);
+db_res_t* pg_new_result(PGresult* pgres);
 
 
 
 
 /*
 /*
  * Fill the structure with data from database
  * Fill the structure with data from database
  */
  */
-int convert_result(db_res_t* res, db_con_t* con);
+int pg_convert_result(db_res_t* res, db_con_t* con);
 
 
 
 
 /*
 /*
  * Release memory used by a result structure
  * Release memory used by a result structure
  */
  */
-int free_result(db_res_t* res);
+int pg_free_result(db_res_t* res);
 
 
 
 
 #endif /* _RES_H */
 #endif /* _RES_H */