dbd/mysql/statement.c

changeset 30
8599f34c139b
parent 26
cf847efefdb5
child 32
03ed0ca09837
--- a/dbd/mysql/statement.c	Tue Feb 17 00:23:00 2009 +0000
+++ b/dbd/mysql/statement.c	Fri Apr 17 23:46:12 2009 +0000
@@ -62,6 +62,35 @@
 }
 
 /*
+ * column_names = statement:columns()
+ */
+static int statement_columns(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
+
+    MYSQL_FIELD *fields;
+    int i;
+    int num_columns;
+    int d = 1;
+
+    if (!statement->stmt) {
+        luaL_error(L, DBI_ERR_INVALID_STATEMENT);
+        return 0;
+    }
+
+    fields = mysql_fetch_fields(statement->metadata);
+    num_columns = mysql_num_fields(statement->metadata);
+    lua_newtable(L);
+    for (i = 0; i < num_columns; i++) {
+	const char *name = fields[i].name;
+
+        LUA_PUSH_ARRAY_STRING(d, name);
+    }
+
+    return 1;
+}
+
+
+/*
  * success,err = statement:execute(...)
  */
 static int statement_execute(lua_State *L) {
@@ -222,12 +251,6 @@
 	return 0;
     }
 
-    if (!statement->metadata) {
-	lua_pushnil(L);
-	return 1;
-    }
-
-
     column_count = mysql_num_fields(statement->metadata);
 
     if (column_count > 0) {
@@ -413,6 +436,7 @@
     static const luaL_Reg statement_methods[] = {
         {"affected", statement_affected},
 	{"close", statement_close},
+	{"columns", statement_columns},
 	{"execute", statement_execute},
 	{"fetch", statement_fetch},
 	{"rowcount", statement_rowcount},

mercurial