* Added the connection:quote() method

Sat, 20 Dec 2008 02:37:22 +0000

author
nrich@ii.net
date
Sat, 20 Dec 2008 02:37:22 +0000
changeset 22
fd78e9cdc6e9
parent 21
7956401a0c5e
child 23
a4825c3e65e9

* Added the connection:quote() method
* modified DBI.Do to return the affected rows on success

DBI.lua file | annotate | diff | comparison | revisions
dbd/common.h file | annotate | diff | comparison | revisions
dbd/db2/connection.c file | annotate | diff | comparison | revisions
dbd/mysql/connection.c file | annotate | diff | comparison | revisions
dbd/oracle/connection.c file | annotate | diff | comparison | revisions
dbd/postgresql/connection.c file | annotate | diff | comparison | revisions
dbd/sqlite3/connection.c file | annotate | diff | comparison | revisions
--- a/DBI.lua	Fri Dec 19 09:17:16 2008 +0000
+++ b/DBI.lua	Sat Dec 20 02:37:22 2008 +0000
@@ -67,5 +67,11 @@
 	return false, err
     end
 
-    return sth:execute(...)
+    local ok, err = sth:execute(...)
+
+    if not ok then
+        return false, err
+    end
+
+    return sth:affected()
 end
--- a/dbd/common.h	Fri Dec 19 09:17:16 2008 +0000
+++ b/dbd/common.h	Sat Dec 20 02:37:22 2008 +0000
@@ -121,6 +121,7 @@
 #define DBI_ERR_BINDING_TYPE_ERR    "Unknown or unsupported type `%s'"
 #define DBI_ERR_INVALID_STATEMENT   "Invalid statement handle"
 #define DBI_ERR_NOT_IMPLEMENTED     "Method %s.%s is not implemented"
+#define DBI_ERR_QUOTING_STR         "Error quoting string: %s"
 
 /*
  * convert string to lower case
--- a/dbd/db2/connection.c	Fri Dec 19 09:17:16 2008 +0000
+++ b/dbd/db2/connection.c	Sat Dec 20 02:37:22 2008 +0000
@@ -197,6 +197,14 @@
 }
 
 /*
+ * quoted = connection:quote(str)
+ */
+static int connection_quote(lua_State *L) {
+    luaL_error(L, DBI_ERR_NOT_IMPLEMENTED, DBD_DB2_CONNECTION, "quote");
+    return 0;
+}
+
+/*
  * success = connection:rollback()
  */
 static int connection_rollback(lua_State *L) {
@@ -228,6 +236,7 @@
 	{"commit", connection_commit},
 	{"ping", connection_ping},
 	{"prepare", connection_prepare},
+	{"quote", connection_quote},
 	{"rollback", connection_rollback},
 	{NULL, NULL}
     };
--- a/dbd/mysql/connection.c	Fri Dec 19 09:17:16 2008 +0000
+++ b/dbd/mysql/connection.c	Sat Dec 20 02:37:22 2008 +0000
@@ -141,6 +141,27 @@
 }
 
 /*
+ * quoted = connection:quote(str)
+ */
+static int connection_quote(lua_State *L) {
+    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION);
+    size_t len;
+    const char *from = luaL_checklstring(L, 2, &len);
+    char *to = (char *)calloc(len*2+1, sizeof(char));
+    int quoted_len;
+
+    if (!conn->mysql) {
+        luaL_error(L, DBI_ERR_DB_UNAVAILABLE);
+    }
+
+    quoted_len = mysql_real_escape_string(conn->mysql, to, from, len);
+
+    lua_pushlstring(L, to, quoted_len);
+
+    return 1;
+}
+
+/*
  * success = connection:rollback()
  */
 static int connection_rollback(lua_State *L) {
@@ -172,6 +193,7 @@
 	{"commit", connection_commit},
 	{"ping", connection_ping},
 	{"prepare", connection_prepare},
+	{"quote", connection_quote},
 	{"rollback", connection_rollback},
 	{NULL, NULL}
     };
--- a/dbd/oracle/connection.c	Fri Dec 19 09:17:16 2008 +0000
+++ b/dbd/oracle/connection.c	Sat Dec 20 02:37:22 2008 +0000
@@ -184,6 +184,14 @@
 }
 
 /*
+ * quoted = connection:quote(str)
+ */
+static int connection_quote(lua_State *L) {
+    luaL_error(L, DBI_ERR_NOT_IMPLEMENTED, DBD_ORACLE_CONNECTION, "quote");
+    return 0;
+}
+
+/*
  * success = connection:rollback()
  */
 static int connection_rollback(lua_State *L) {
@@ -218,6 +226,7 @@
 	{"commit", connection_commit},
 	{"ping", connection_ping},
 	{"prepare", connection_prepare},
+	{"quote", connection_quote},
 	{"rollback", connection_rollback},
 	{NULL, NULL}
     };
--- a/dbd/postgresql/connection.c	Fri Dec 19 09:17:16 2008 +0000
+++ b/dbd/postgresql/connection.c	Sat Dec 20 02:37:22 2008 +0000
@@ -199,6 +199,35 @@
 }
 
 /*
+ * quoted = connection:quote(str)
+ */
+static int connection_quote(lua_State *L) {
+    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION);
+    size_t len;
+    const char *from = luaL_checklstring(L, 2, &len);
+    char *to = (char *)calloc(len*2+1, sizeof(char));
+    int err = 0;
+    int quoted_len;
+
+    if (!conn->postgresql) {
+        luaL_error(L, DBI_ERR_DB_UNAVAILABLE);
+    }
+
+    quoted_len = PQescapeStringConn(conn->postgresql, to, from, len, &err);
+
+    if (err) {
+        free(to);
+        
+       luaL_error(L, DBI_ERR_QUOTING_STR, PQerrorMessage(conn->postgresql));
+    }
+
+    lua_pushlstring(L, to, quoted_len);
+    free(to);
+
+    return 1;
+}
+
+/*
  * success = connection:rollback()
  */
 static int connection_rollback(lua_State *L) {
@@ -235,6 +264,7 @@
 	{"commit", connection_commit},
 	{"ping", connection_ping},
 	{"prepare", connection_prepare},
+	{"quote", connection_quote},
 	{"rollback", connection_rollback},
 	{NULL, NULL}
     };
--- a/dbd/sqlite3/connection.c	Fri Dec 19 09:17:16 2008 +0000
+++ b/dbd/sqlite3/connection.c	Sat Dec 20 02:37:22 2008 +0000
@@ -148,6 +148,27 @@
 }
 
 /*
+ * quoted = connection:quote(str)
+ */
+static int connection_quote(lua_State *L) {
+    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION);
+    size_t len;
+    const char *from = luaL_checklstring(L, 2, &len);
+    char *to;
+
+    if (!conn->sqlite) {
+        luaL_error(L, DBI_ERR_DB_UNAVAILABLE);
+    }
+
+    to = sqlite3_mprintf("%q", from);
+
+    lua_pushstring(L, to);
+    sqlite3_free(to);
+
+    return 1;
+}
+
+/*
  * success = connection:rollback()
  */
 static int connection_rollback(lua_State *L) {
@@ -187,6 +208,7 @@
 	{"commit", connection_commit},
 	{"ping", connection_ping},
 	{"prepare", connection_prepare},
+	{"quote", connection_quote},
 	{"rollback", connection_rollback},
 	{NULL, NULL}
     };

mercurial