Added the statement:affected() and statement:rowcount() methods.

Fri, 19 Dec 2008 09:17:16 +0000

author
nrich@ii.net
date
Fri, 19 Dec 2008 09:17:16 +0000
changeset 21
7956401a0c5e
parent 20
5ab0b30f8fbd
child 22
fd78e9cdc6e9

Added the statement:affected() and statement:rowcount() methods.

Makefile file | annotate | diff | comparison | revisions
dbd/common.h file | annotate | diff | comparison | revisions
dbd/db2/statement.c file | annotate | diff | comparison | revisions
dbd/mysql/statement.c file | annotate | diff | comparison | revisions
dbd/oracle/statement.c file | annotate | diff | comparison | revisions
dbd/postgresql/statement.c file | annotate | diff | comparison | revisions
dbd/sqlite3/dbd_sqlite3.h file | annotate | diff | comparison | revisions
dbd/sqlite3/statement.c file | annotate | diff | comparison | revisions
--- a/Makefile	Fri Dec 12 03:46:34 2008 +0000
+++ b/Makefile	Fri Dec 19 09:17:16 2008 +0000
@@ -24,23 +24,23 @@
 DB2_OBJS=$(OBJS) build/dbd_db2_main.o build/dbd_db2_connection.o build/dbd_db2_statement.o
 ORACLE_OBJS=$(OBJS) build/dbd_oracle_main.o build/dbd_oracle_connection.o build/dbd_oracle_statement.o
 
-free: dbdmysql dbdpsql dbdsqlite3
+free: mysql psql sqlite3
 
-all:  dbdmysql dbdpsql dbdsqlite3 dbddb2 dbdoracle
+all:  mysql psql sqlite3 db2 oracle
 
-dbdmysql: $(MYSQL_OBJS)
+mysql: $(MYSQL_OBJS)
 	$(CC) $(CFLAGS) $(MYSQL_OBJS) -o $(DBDMYSQL) $(MYSQL_LDFLAGS)
 
-dbdpsql: $(PSQL_OBJS)
+psql: $(PSQL_OBJS)
 	$(CC) $(CFLAGS) $(PSQL_OBJS) -o $(DBDPSQL) $(PSQL_LDFLAGS)
 
-dbdsqlite3: $(SQLITE3_OBJS)
+sqlite3: $(SQLITE3_OBJS)
 	$(CC) $(CFLAGS) $(SQLITE3_OBJS) -o $(DBDSQLITE3) $(SQLITE3_LDFLAGS)
 
-dbddb2: $(DB2_OBJS)
+db2: $(DB2_OBJS)
 	$(CC) $(CFLAGS) $(DB2_OBJS) -o $(DBDDB2) $(DB2_LDFLAGS)
 
-dbdoracle: $(ORACLE_OBJS)
+oracle: $(ORACLE_OBJS)
 	$(CC) $(CFLAGS) $(ORACLE_OBJS) -o $(DBDORACLE) $(ORACLE_LDFLAGS)
 
 clean:
--- a/dbd/common.h	Fri Dec 12 03:46:34 2008 +0000
+++ b/dbd/common.h	Fri Dec 19 09:17:16 2008 +0000
@@ -119,6 +119,8 @@
 #define DBI_ERR_ALLOC_RESULT	    "Error allocating result set: %s"
 #define DBI_ERR_DESC_RESULT	    "Error describing result set: %s"
 #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"
 
 /*
  * convert string to lower case
--- a/dbd/db2/statement.c	Fri Dec 12 03:46:34 2008 +0000
+++ b/dbd/db2/statement.c	Fri Dec 19 09:17:16 2008 +0000
@@ -22,6 +22,26 @@
 }
 
 /*
+ * num_affected_rows = statement:affected()
+ */
+static int statement_affected(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_DB2_STATEMENT);
+    SQLRETURN rc = SQL_SUCCESS;
+    SQLINTEGER affected;
+
+    if (!statement->stmt) {
+        luaL_error(L, DBI_ERR_INVALID_STATEMENT);
+    }
+
+    rc = SQLRowCount(statement->stmt, &affected);
+
+
+    lua_pushinteger(L, affected);
+
+    return 1;
+}
+
+/*
  * success = statement:close()
  */
 static int statement_close(lua_State *L) {
@@ -335,6 +355,15 @@
 }
 
 /*
+ * num_rows = statement:rowcount()
+ */
+static int statement_rowcount(lua_State *L) {
+    luaL_error(L, DBI_ERR_NOT_IMPLEMENTED, DBD_DB2_STATEMENT, "rowcount");
+
+    return 0;
+}
+
+/*
  * iterfunc = statement:rows(named_indexes)
  */
 static int statement_rows(lua_State *L) {
@@ -381,8 +410,8 @@
 
     /*
      * turn off deferred prepare
-     * statements will be sent to the server at prepare timr,
-     * and therefor we can catch errors then rather 
+     * statements will be sent to the server at prepare time,
+     * and therefore we can catch errors now rather 
      * than at execute time
      */
     rc = SQLSetStmtAttr(stmt,SQL_ATTR_DEFERRED_PREPARE,(SQLPOINTER)SQL_DEFERRED_PREPARE_OFF,0);
@@ -410,9 +439,11 @@
 
 int dbd_db2_statement(lua_State *L) {
     static const luaL_Reg statement_methods[] = {
+	{"affected", statement_affected},
 	{"close", statement_close},
 	{"execute", statement_execute},
 	{"fetch", statement_fetch},
+	{"rowcount", statement_rowcount},
 	{"rows", statement_rows},
 	{NULL, NULL}
     };
--- a/dbd/mysql/statement.c	Fri Dec 12 03:46:34 2008 +0000
+++ b/dbd/mysql/statement.c	Fri Dec 19 09:17:16 2008 +0000
@@ -27,6 +27,21 @@
 } 
 
 /*
+ * num_affected_rows = statement:affected()
+ */
+static int statement_affected(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
+
+    if (!statement->stmt) {
+        luaL_error(L, DBI_ERR_INVALID_STATEMENT);
+    }
+
+    lua_pushinteger(L, mysql_stmt_affected_rows(statement->stmt));
+
+    return 1;
+}
+
+/*
  * success = statement:close()
  */
 static int statement_close(lua_State *L) {
@@ -163,6 +178,10 @@
 
     metadata = mysql_stmt_result_metadata(statement->stmt);
 
+    if (metadata) {
+        mysql_stmt_store_result(statement->stmt);
+    }
+
 cleanup:
     /*
      * free the buffer with a resize to 0
@@ -315,9 +334,23 @@
 }
 
 /*
+ * num_rows = statement:rowcount()
+ */
+static int statement_rowcount(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
+
+    if (!statement->stmt) {
+        luaL_error(L, DBI_ERR_INVALID_STATEMENT);
+    }
+
+    lua_pushinteger(L, mysql_stmt_num_rows(statement->stmt));
+
+    return 1;
+}
+
+/*
  * iterfunc = statement:rows(named_indexes)
  */
-
 static int statement_rows(lua_State *L) {
     if (lua_gettop(L) == 1) {			
 	lua_pushvalue(L, 1);
@@ -373,9 +406,11 @@
 
 int dbd_mysql_statement(lua_State *L) {
     static const luaL_Reg statement_methods[] = {
+        {"affected", statement_affected},
 	{"close", statement_close},
 	{"execute", statement_execute},
 	{"fetch", statement_fetch},
+	{"rowcount", statement_rowcount},
 	{"rows", statement_rows},
 	{NULL, NULL}
     };
--- a/dbd/oracle/statement.c	Fri Dec 12 03:46:34 2008 +0000
+++ b/dbd/oracle/statement.c	Fri Dec 19 09:17:16 2008 +0000
@@ -25,6 +25,35 @@
 }
 
 /*
+ * num_affected_rows = statement:affected()
+ */
+static int statement_affected(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_ORACLE_STATEMENT);
+    int affected;
+    int rc; 
+
+    if (!statement->stmt) {
+        luaL_error(L, DBI_ERR_INVALID_STATEMENT);
+    }
+
+    /* 
+     * get number of affected rows 
+     */
+    rc = OCIAttrGet(
+	(dvoid *)statement->stmt, 
+	(ub4)OCI_HTYPE_STMT,
+        (dvoid *)&affected, 
+	(ub4 *)0, 
+	(ub4)OCI_ATTR_ROW_COUNT,
+        statement->conn->err
+    );
+
+    lua_pushinteger(L, affected);
+
+    return 1;
+}
+
+/*
  * success = statement:close()
  */
 int statement_close(lua_State *L) {
@@ -351,6 +380,15 @@
 }
 
 /*
+ * num_rows = statement:rowcount()
+ */
+static int statement_rowcount(lua_State *L) {
+    luaL_error(L, DBI_ERR_NOT_IMPLEMENTED, DBD_ORACLE_STATEMENT, "rowcount");
+
+    return 0;
+}
+
+/*
  * iterfunc = statement:rows(named_indexes)
  */
 static int statement_rows(lua_State *L) {
@@ -405,9 +443,11 @@
 
 int dbd_oracle_statement(lua_State *L) {
     static const luaL_Reg statement_methods[] = {
+	{"affected", statement_affected},
 	{"close", statement_close},
 	{"execute", statement_execute},
 	{"fetch", statement_fetch},
+	{"rowcount", statement_rowcount},
 	{"rows", statement_rows},
 	{NULL, NULL}
     };
--- a/dbd/postgresql/statement.c	Fri Dec 12 03:46:34 2008 +0000
+++ b/dbd/postgresql/statement.c	Fri Dec 19 09:17:16 2008 +0000
@@ -26,6 +26,20 @@
     return lua_type;
 }
 
+/*
+ * num_affected_rows = statement:affected()
+ */
+static int statement_affected(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT);
+
+    if (!statement->result) {
+        luaL_error(L, DBI_ERR_INVALID_STATEMENT);
+    }
+
+    lua_pushinteger(L, atoi(PQcmdTuples(statement->result)));
+
+    return 1;
+}
 
 /*
  * success = statement:close()
@@ -243,6 +257,21 @@
 }
 
 /*
+ * num_rows = statement:rowcount()
+ */
+static int statement_rowcount(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT);
+
+    if (!statement->result) {
+        luaL_error(L, DBI_ERR_INVALID_STATEMENT);
+    }
+
+    lua_pushinteger(L, PQntuples(statement->result));
+
+    return 1;
+}
+
+/*
  * iterfunc = statement:rows(named_indexes)
  */
 static int statement_rows(lua_State *L) {
@@ -322,9 +351,11 @@
 
 int dbd_postgresql_statement(lua_State *L) {
     static const luaL_Reg statement_methods[] = {
+	{"affected", statement_affected},
 	{"close", statement_close},
 	{"execute", statement_execute},
 	{"fetch", statement_fetch},
+	{"rowcount", statement_rowcount},
 	{"rows", statement_rows},
 	{NULL, NULL}
     };
--- a/dbd/sqlite3/dbd_sqlite3.h	Fri Dec 12 03:46:34 2008 +0000
+++ b/dbd/sqlite3/dbd_sqlite3.h	Fri Dec 19 09:17:16 2008 +0000
@@ -19,5 +19,6 @@
     sqlite3_stmt *stmt;
     sqlite3 *sqlite;
     int more_data;
+    int affected;
 } statement_t;
 
--- a/dbd/sqlite3/statement.c	Fri Dec 12 03:46:34 2008 +0000
+++ b/dbd/sqlite3/statement.c	Fri Dec 19 09:17:16 2008 +0000
@@ -44,9 +44,24 @@
 }
 
 /*
+ * num_affected_rows = statement:affected()
+ */
+static int statement_affected(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
+
+    if (!statement->stmt) {
+        luaL_error(L, DBI_ERR_INVALID_STATEMENT);
+    }
+
+    lua_pushinteger(L, statement->affected);
+ 
+    return 1;   
+}
+
+/*
  * success = statement:close()
  */
-int statement_close(lua_State *L) {
+static int statement_close(lua_State *L) {
     statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
     int ok = 0;
 
@@ -65,7 +80,7 @@
 /*
  * success,err = statement:execute(...)
  */
-int statement_execute(lua_State *L) {
+static int statement_execute(lua_State *L) {
     int n = lua_gettop(L);
     statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
     int p;
@@ -150,6 +165,8 @@
 	return 2;
     }
 
+    statement->affected = sqlite3_changes(statement->sqlite);
+
     lua_pushboolean(L, 1);
     return 1;
 }
@@ -281,6 +298,15 @@
 }
 
 /*
+ * num_rows = statement:rowcount()
+ */
+static int statement_rowcount(lua_State *L) {
+    luaL_error(L, DBI_ERR_NOT_IMPLEMENTED, DBD_SQLITE_STATEMENT, "rowcount");
+
+    return 0;
+}
+
+/*
  * __gc
  */
 static int statement_gc(lua_State *L) {
@@ -297,6 +323,7 @@
     statement->sqlite = conn->sqlite;
     statement->stmt = NULL;
     statement->more_data = 0;
+    statement->affected = 0;
 
     if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) {
 	lua_pushnil(L);
@@ -311,10 +338,12 @@
 
 int dbd_sqlite3_statement(lua_State *L) {
     static const luaL_Reg statement_methods[] = {
+	{"affected", statement_affected},
 	{"close", statement_close},
 	{"execute", statement_execute},
 	{"fetch", statement_fetch},
 	{"rows", statement_rows},
+	{"rowcount", statement_rowcount},
 	{NULL, NULL}
     };
 

mercurial