dbd/mysql/statement.c

changeset 30
8599f34c139b
parent 26
cf847efefdb5
child 32
03ed0ca09837
equal deleted inserted replaced
29:9b9d85320bc3 30:8599f34c139b
58 } 58 }
59 59
60 lua_pushboolean(L, 1); 60 lua_pushboolean(L, 1);
61 return 1; 61 return 1;
62 } 62 }
63
64 /*
65 * column_names = statement:columns()
66 */
67 static int statement_columns(lua_State *L) {
68 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
69
70 MYSQL_FIELD *fields;
71 int i;
72 int num_columns;
73 int d = 1;
74
75 if (!statement->stmt) {
76 luaL_error(L, DBI_ERR_INVALID_STATEMENT);
77 return 0;
78 }
79
80 fields = mysql_fetch_fields(statement->metadata);
81 num_columns = mysql_num_fields(statement->metadata);
82 lua_newtable(L);
83 for (i = 0; i < num_columns; i++) {
84 const char *name = fields[i].name;
85
86 LUA_PUSH_ARRAY_STRING(d, name);
87 }
88
89 return 1;
90 }
91
63 92
64 /* 93 /*
65 * success,err = statement:execute(...) 94 * success,err = statement:execute(...)
66 */ 95 */
67 static int statement_execute(lua_State *L) { 96 static int statement_execute(lua_State *L) {
219 248
220 if (!statement->metadata) { 249 if (!statement->metadata) {
221 luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE); 250 luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE);
222 return 0; 251 return 0;
223 } 252 }
224
225 if (!statement->metadata) {
226 lua_pushnil(L);
227 return 1;
228 }
229
230 253
231 column_count = mysql_num_fields(statement->metadata); 254 column_count = mysql_num_fields(statement->metadata);
232 255
233 if (column_count > 0) { 256 if (column_count > 0) {
234 int i; 257 int i;
411 434
412 int dbd_mysql_statement(lua_State *L) { 435 int dbd_mysql_statement(lua_State *L) {
413 static const luaL_Reg statement_methods[] = { 436 static const luaL_Reg statement_methods[] = {
414 {"affected", statement_affected}, 437 {"affected", statement_affected},
415 {"close", statement_close}, 438 {"close", statement_close},
439 {"columns", statement_columns},
416 {"execute", statement_execute}, 440 {"execute", statement_execute},
417 {"fetch", statement_fetch}, 441 {"fetch", statement_fetch},
418 {"rowcount", statement_rowcount}, 442 {"rowcount", statement_rowcount},
419 {"rows", statement_rows}, 443 {"rows", statement_rows},
420 {NULL, NULL} 444 {NULL, NULL}

mercurial