# HG changeset patch # User nrich@ii.net # Date 1229740642 0 # Node ID fd78e9cdc6e9921bf8f0518c3df251c445f37acf # Parent 7956401a0c5e1e78594e196493217153a081f38d * Added the connection:quote() method * modified DBI.Do to return the affected rows on success diff -r 7956401a0c5e -r fd78e9cdc6e9 DBI.lua --- 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 diff -r 7956401a0c5e -r fd78e9cdc6e9 dbd/common.h --- 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 diff -r 7956401a0c5e -r fd78e9cdc6e9 dbd/db2/connection.c --- 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} }; diff -r 7956401a0c5e -r fd78e9cdc6e9 dbd/mysql/connection.c --- 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} }; diff -r 7956401a0c5e -r fd78e9cdc6e9 dbd/oracle/connection.c --- 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} }; diff -r 7956401a0c5e -r fd78e9cdc6e9 dbd/postgresql/connection.c --- 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} }; diff -r 7956401a0c5e -r fd78e9cdc6e9 dbd/sqlite3/connection.c --- 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} };