dovecot-2.2: lib-sql: Implemented sql_escape_blob()
dovecot at dovecot.org
dovecot at dovecot.org
Wed Sep 2 16:47:42 UTC 2015
details: http://hg.dovecot.org/dovecot-2.2/rev/113f4e32cb19
changeset: 19072:113f4e32cb19
user: Timo Sirainen <tss at iki.fi>
date: Wed Sep 02 19:43:26 2015 +0300
description:
lib-sql: Implemented sql_escape_blob()
diffstat:
src/lib-sql/driver-cassandra.c | 16 +++++++++++++++-
src/lib-sql/driver-mysql.c | 17 ++++++++++++++++-
src/lib-sql/driver-pgsql.c | 18 +++++++++++++++++-
src/lib-sql/driver-sqlite.c | 17 ++++++++++++++++-
src/lib-sql/driver-sqlpool.c | 23 ++++++++++++++++++++++-
src/lib-sql/sql-api-private.h | 2 ++
src/lib-sql/sql-api.c | 6 ++++++
src/lib-sql/sql-api.h | 3 +++
8 files changed, 97 insertions(+), 5 deletions(-)
diffs (244 lines):
diff -r 5119a85795f7 -r 113f4e32cb19 src/lib-sql/driver-cassandra.c
--- a/src/lib-sql/driver-cassandra.c Wed Sep 02 19:42:45 2015 +0300
+++ b/src/lib-sql/driver-cassandra.c Wed Sep 02 19:43:26 2015 +0300
@@ -2,6 +2,7 @@
#include "lib.h"
#include "array.h"
+#include "hex-binary.h"
#include "str.h"
#include "ioloop.h"
#include "write-full.h"
@@ -957,6 +958,17 @@
sql_transaction_add_query(_ctx, ctx->query_pool, query, affected_rows);
}
+static const char *
+driver_cassandra_escape_blob(struct sql_db *_db ATTR_UNUSED,
+ const unsigned char *data, size_t size)
+{
+ string_t *str = t_str_new(128);
+
+ str_append(str, "0x");
+ binary_to_hex_append(str, data, size);
+ return str_c(str);
+}
+
const struct sql_db driver_cassandra_db = {
.name = "cassandra",
.flags = SQL_DB_FLAG_POOLED,
@@ -976,7 +988,9 @@
driver_cassandra_transaction_commit_s,
driver_cassandra_transaction_rollback,
- driver_cassandra_update
+ driver_cassandra_update,
+
+ driver_cassandra_escape_blob
}
};
diff -r 5119a85795f7 -r 113f4e32cb19 src/lib-sql/driver-mysql.c
--- a/src/lib-sql/driver-mysql.c Wed Sep 02 19:42:45 2015 +0300
+++ b/src/lib-sql/driver-mysql.c Wed Sep 02 19:43:26 2015 +0300
@@ -3,6 +3,7 @@
#include "lib.h"
#include "ioloop.h"
#include "array.h"
+#include "hex-binary.h"
#include "str.h"
#include "net.h"
#include "sql-api-private.h"
@@ -619,6 +620,18 @@
query, affected_rows);
}
+static const char *
+driver_mysql_escape_blob(struct sql_db *_db ATTR_UNUSED,
+ const unsigned char *data, size_t size)
+{
+ string_t *str = t_str_new(128);
+
+ str_append(str, "HEX('");
+ binary_to_hex_append(str, data, size);
+ str_append_c(str, ')');
+ return str_c(str);
+}
+
const struct sql_db driver_mysql_db = {
.name = "mysql",
.flags = SQL_DB_FLAG_BLOCKING | SQL_DB_FLAG_POOLED,
@@ -638,7 +651,9 @@
driver_mysql_transaction_commit_s,
driver_mysql_transaction_rollback,
- driver_mysql_update
+ driver_mysql_update,
+
+ driver_mysql_escape_blob
}
};
diff -r 5119a85795f7 -r 113f4e32cb19 src/lib-sql/driver-pgsql.c
--- a/src/lib-sql/driver-pgsql.c Wed Sep 02 19:42:45 2015 +0300
+++ b/src/lib-sql/driver-pgsql.c Wed Sep 02 19:43:26 2015 +0300
@@ -3,6 +3,8 @@
#include "lib.h"
#include "array.h"
#include "ioloop.h"
+#include "hex-binary.h"
+#include "str.h"
#include "time-util.h"
#include "sql-api-private.h"
@@ -1066,6 +1068,18 @@
sql_transaction_add_query(_ctx, ctx->query_pool, query, affected_rows);
}
+static const char *
+driver_pgsql_escape_blob(struct sql_db *_db ATTR_UNUSED,
+ const unsigned char *data, size_t size)
+{
+ string_t *str = t_str_new(128);
+
+ str_append(str, "E'\\x");
+ binary_to_hex_append(str, data, size);
+ str_append_c(str, '\'');
+ return str_c(str);
+}
+
const struct sql_db driver_pgsql_db = {
.name = "pgsql",
.flags = SQL_DB_FLAG_POOLED,
@@ -1085,7 +1099,9 @@
driver_pgsql_transaction_commit_s,
driver_pgsql_transaction_rollback,
- driver_pgsql_update
+ driver_pgsql_update,
+
+ driver_pgsql_escape_blob
}
};
diff -r 5119a85795f7 -r 113f4e32cb19 src/lib-sql/driver-sqlite.c
--- a/src/lib-sql/driver-sqlite.c Wed Sep 02 19:42:45 2015 +0300
+++ b/src/lib-sql/driver-sqlite.c Wed Sep 02 19:43:26 2015 +0300
@@ -3,6 +3,7 @@
#include "lib.h"
#include "array.h"
#include "str.h"
+#include "hex-binary.h"
#include "sql-api-private.h"
#ifdef BUILD_SQLITE
@@ -392,6 +393,18 @@
*affected_rows = sqlite3_changes(db->sqlite);
}
+static const char *
+driver_sqlite_escape_blob(struct sql_db *_db ATTR_UNUSED,
+ const unsigned char *data, size_t size)
+{
+ string_t *str = t_str_new(128);
+
+ str_append(str, "x'");
+ binary_to_hex_append(str, data, size);
+ str_append_c(str, '\'');
+ return str_c(str);
+}
+
const struct sql_db driver_sqlite_db = {
.name = "sqlite",
.flags = SQL_DB_FLAG_BLOCKING,
@@ -410,7 +423,9 @@
driver_sqlite_transaction_commit,
driver_sqlite_transaction_commit_s,
driver_sqlite_transaction_rollback,
- driver_sqlite_update
+ driver_sqlite_update,
+
+ driver_sqlite_escape_blob
}
};
diff -r 5119a85795f7 -r 113f4e32cb19 src/lib-sql/driver-sqlpool.c
--- a/src/lib-sql/driver-sqlpool.c Wed Sep 02 19:42:45 2015 +0300
+++ b/src/lib-sql/driver-sqlpool.c Wed Sep 02 19:43:26 2015 +0300
@@ -790,6 +790,25 @@
query, affected_rows);
}
+static const char *
+driver_sqlpool_escape_blob(struct sql_db *_db,
+ const unsigned char *data, size_t size)
+{
+ struct sqlpool_db *db = (struct sqlpool_db *)_db;
+ const struct sqlpool_connection *conns;
+ unsigned int i, count;
+
+ /* use the first ready connection */
+ conns = array_get(&db->all_connections, &count);
+ for (i = 0; i < count; i++) {
+ if (SQL_DB_IS_READY(conns[i].db))
+ return sql_escape_blob(conns[i].db, data, size);
+ }
+ /* no ready connections. just use the first one (we're guaranteed
+ to always have one) */
+ return sql_escape_blob(conns[0].db, data, size);
+}
+
struct sql_db driver_sqlpool_db = {
"",
@@ -808,6 +827,8 @@
driver_sqlpool_transaction_commit_s,
driver_sqlpool_transaction_rollback,
- driver_sqlpool_update
+ driver_sqlpool_update,
+
+ driver_sqlpool_escape_blob
}
};
diff -r 5119a85795f7 -r 113f4e32cb19 src/lib-sql/sql-api-private.h
--- a/src/lib-sql/sql-api-private.h Wed Sep 02 19:42:45 2015 +0300
+++ b/src/lib-sql/sql-api-private.h Wed Sep 02 19:43:26 2015 +0300
@@ -76,6 +76,8 @@
void (*update)(struct sql_transaction_context *ctx, const char *query,
unsigned int *affected_rows);
+ const char *(*escape_blob)(struct sql_db *db,
+ const unsigned char *data, size_t size);
};
struct sql_db {
diff -r 5119a85795f7 -r 113f4e32cb19 src/lib-sql/sql-api.c
--- a/src/lib-sql/sql-api.c Wed Sep 02 19:42:45 2015 +0300
+++ b/src/lib-sql/sql-api.c Wed Sep 02 19:43:26 2015 +0300
@@ -126,6 +126,12 @@
return db->v.escape_string(db, string);
}
+const char *sql_escape_blob(struct sql_db *db,
+ const unsigned char *data, size_t size)
+{
+ return db->v.escape_blob(db, data, size);
+}
+
void sql_exec(struct sql_db *db, const char *query)
{
db->v.exec(db, query);
diff -r 5119a85795f7 -r 113f4e32cb19 src/lib-sql/sql-api.h
--- a/src/lib-sql/sql-api.h Wed Sep 02 19:42:45 2015 +0300
+++ b/src/lib-sql/sql-api.h Wed Sep 02 19:43:26 2015 +0300
@@ -70,6 +70,9 @@
/* Escape the given string if needed and return it. */
const char *sql_escape_string(struct sql_db *db, const char *string);
+/* Escape the given data as a string. */
+const char *sql_escape_blob(struct sql_db *db,
+ const unsigned char *data, size_t size);
/* Execute SQL query without waiting for results. */
void sql_exec(struct sql_db *db, const char *query);
More information about the dovecot-cvs
mailing list