this time without colorized output
--
________________________________________________________________
Paul Stevens paul at nfg.nl
NET FACILITIES GROUP GPG/PGP: 1024D/11F8CD31
The Netherlands________________________________http://www.nfg.nl
diff --git a/db.c b/db.c
index fd6fc91..f94f50c 100644
--- a/db.c
+++ b/db.c
@@ -1119,7 +1119,7 @@ int db_insert_message_block_physmessage(const char *block,
blocktype_t is_header)
{
char *escaped_query = NULL;
- unsigned maxesclen = (READ_BLOCK_SIZE + 1) * 2 + DEF_QUERYSIZE;
+ unsigned maxesclen = (READ_BLOCK_SIZE + 1) * 5 + DEF_QUERYSIZE;
unsigned startlen = 0;
unsigned esclen = 0;
@@ -1155,8 +1155,7 @@ int db_insert_message_block_physmessage(const char *block,
" VALUES ('%u','", is_header);
/* escape & add data */
- esclen =
- db_escape_direct(&escaped_query[startlen], block, block_size);
+ esclen = db_escape_binary(&escaped_query[startlen], block, block_size);
snprintf(&escaped_query[esclen + startlen],
maxesclen - esclen - startlen, "', '%llu', '%llu')",
diff --git a/db.h b/db.h
index 1272a12..b956918 100644
--- a/db.h
+++ b/db.h
@@ -198,6 +198,17 @@ int db_escape_string(char **escaped, const char * const instr);
int db_escape_string_length(char **escaped, const char * const instr, unsigned long inlen);
/**
+ * \brief escape a binary data for use in query
+ * \param to string to copy escaped string to. Must be allocated by caller
+ * \param from original string
+ * \param length of orginal string
+ * \return length of escaped string
+ * \attention behaviour is undefined if to and from overlap
+ */
+unsigned long db_escape_binary(char *to,
+ const char *from, unsigned long length);
+
+/**
* \brief get length in bytes of a result field in a result set.
* \param row row of field
* \param field field number (0..nfields)
diff --git a/pgsql/dbpgsql.c b/pgsql/dbpgsql.c
index 8c951d4..ad391a4 100644
--- a/pgsql/dbpgsql.c
+++ b/pgsql/dbpgsql.c
@@ -36,6 +36,8 @@
#include <stdlib.h>
#include <string.h>
+#define BYTEAOID 17
+
const char *TO_CHAR = "TO_CHAR(%s, 'YYYY-MM-DD HH24:MI:SS' )";
const char *TO_DATE = "TO_TIMESTAMP('%s', 'YYYY-MM-DD HH:MI:SS')";
@@ -45,6 +47,10 @@ static PGresult *msgbuf_res;
static PGresult *stored_res;
static u64_t affected_rows; /**< stores the number of rows affected by the
* the last query */
+static void _create_binary_table(void);
+static void _free_binary_table(void);
+static void _set_binary_table(unsigned row, unsigned field);
+static char*** bintbl = NULL;
db_param_t _db_params;
int db_connect()
@@ -130,6 +136,49 @@ void db_free_result()
PQclear(res);
res = NULL;
+ _free_binary_table();
+}
+
+static void _create_binary_table(void){
+ unsigned rows, fields, i;
+ rows = db_num_rows(); fields = db_num_fields();
+
+ if(!bintbl){
+ bintbl = (char***)malloc(sizeof(char**) * rows);
+ memset(bintbl, 0, sizeof(char**) * rows);
+ for(i = 0; i < rows; i++){
+ *(bintbl + i) = (char**)malloc(sizeof(char*) * fields);
+ memset(*(bintbl + i), 0, sizeof(char*) * fields);
+ }
+ }
+}
+
+static void _free_binary_table(void){
+ unsigned rows, fields, i, j;
+ rows = db_num_rows(); fields = db_num_fields();
+
+ if(bintbl){
+ for(i = 0; i < rows; i++){
+ for(j = 0; j < fields; j++)
+ if(bintbl[i][j])
+ free(bintbl[i][j]);
+ free(bintbl[i]);
+ }
+ free(bintbl);
+ bintbl = NULL;
+ }
+
+}
+static void _set_binary_table(unsigned row, unsigned field){
+ unsigned char* tmp;
+ size_t result_size;
+ if(!bintbl[row][field]){
+ tmp = PQunescapeBytea(PQgetvalue(res, row, field), &result_size);
+ bintbl[row][field] = (char*)malloc(result_size + 1);
+ memcpy(bintbl[row][field], tmp, result_size);
+ PQfreemem(tmp); tmp = NULL;
+ bintbl[row][field][result_size] = '\0';
+ }
}
const char *db_get_result(unsigned row, unsigned field)
@@ -146,6 +195,11 @@ const char *db_get_result(unsigned row, unsigned field)
__FILE__, __func__, row, field);
return NULL;
}
+ if(PQftype(res, field) == BYTEAOID){
+ _create_binary_table();
+ _set_binary_table(row, field);
+ return bintbl[row][field];
+ }
return PQgetvalue(res, row, field);
}
@@ -179,7 +233,7 @@ int db_query(const char *the_query)
result set (i.e. it is a SELECT query)
the global res is
set to this temp_res result set */
-
+ _free_binary_table();
if (the_query != NULL) {
trace(TRACE_DEBUG, "%s,%s: "
"executing query [%s]", __FILE__, __func__,
@@ -242,7 +296,17 @@ unsigned long db_escape_direct(char *to,
{
return PQescapeString(to, from, length);
}
+unsigned long db_escape_binary(char *to,
+ const char *from, unsigned long length)
+{
+ size_t to_length;
+ unsigned char *esc_to;
+ esc_to = PQescapeBytea(from, length, &to_length);
+ strncpy(to, esc_to, to_length);
+ PQfreemem(esc_to);
+ return (unsigned long)(to_length - 1);
+}
int db_do_cleanup(const char **tables, int num_tables)
{
int result = 0;
@@ -275,6 +339,11 @@ u64_t db_get_length(unsigned row, unsigned field)
__FILE__, __func__, row, field);
return -1;
}
+ if(PQftype(res, field) == BYTEAOID){
+ _create_binary_table();
+ _set_binary_table(row, field);
+ return strlen(bintbl[row][field]);
+ }
return PQgetlength(res, row, field);
}
diff --git a/sql/postgresql/create_tables.pgsql b/sql/postgresql/create_tables.pgsql
index c8f1d14..db823c0 100644
--- a/sql/postgresql/create_tables.pgsql
+++ b/sql/postgresql/create_tables.pgsql
@@ -132,7 +132,7 @@ CREATE TABLE dbmail_messageblks (
messageblk_idnr INT8 DEFAULT nextval('dbmail_messageblk_idnr_seq'),
physmessage_id INT8 REFERENCES dbmail_physmessage(id)
ON DELETE CASCADE,
- messageblk TEXT NOT NULL,
+ messageblk BYTEA NOT NULL,
blocksize INT8 DEFAULT '0' NOT NULL,
is_header INT2 DEFAULT '0' NOT NULL,
PRIMARY KEY (messageblk_idnr)
_______________________________________________
DBmail mailing list
[email protected]
https://mailman.fastxs.nl/mailman/listinfo/dbmail