Переглянути джерело

- support for conversions from int to str

Jan Janak 17 роки тому
батько
коміт
e28d38dc23

+ 86 - 0
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;
 

+ 3 - 1
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 */
 };
 
 

+ 1 - 1
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);