dbd/mysql/statement.c

changeset 3
b61020ca4753
parent 2
c4f02fc67e5a
child 4
c50b0e6f25d6
equal deleted inserted replaced
2:c4f02fc67e5a 3:b61020ca4753
32 static int statement_close(lua_State *L) { 32 static int statement_close(lua_State *L) {
33 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); 33 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
34 34
35 if (statement->metadata) { 35 if (statement->metadata) {
36 mysql_free_result(statement->metadata); 36 mysql_free_result(statement->metadata);
37 statement->metadata = NULL;
37 } 38 }
38 39
39 if (statement->stmt) { 40 if (statement->stmt) {
40 mysql_stmt_close(statement->stmt); 41 mysql_stmt_close(statement->stmt);
41 } 42 statement->stmt = NULL;
42 43 }
44
45 lua_pushboolean(L, 1);
43 return 1; 46 return 1;
44 } 47 }
45 48
46 /* 49 /*
47 * success = statement:execute(...) 50 * success,err = statement:execute(...)
48 */ 51 */
49 static int statement_execute(lua_State *L) { 52 static int statement_execute(lua_State *L) {
50 int n = lua_gettop(L); 53 int n = lua_gettop(L);
51 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); 54 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
52 int num_bind_params = n - 1; 55 int num_bind_params = n - 1;
56 int expected_params;
53 57
54 MYSQL_BIND *bind = NULL; 58 MYSQL_BIND *bind = NULL;
55 MYSQL_RES *metadata = NULL; 59 MYSQL_RES *metadata = NULL;
56 60
57 char *error_message = NULL; 61 char *error_message = NULL;
58 62
59 int p; 63 int p;
64
65 if (!statement->stmt) {
66 lua_pushboolean(L, 0);
67 lua_pushstring(L, "execute called on a closed or invalid statement");
68 return 2;
69 }
70
71 expected_params = mysql_stmt_param_count(statement->stmt);
72
73 if (expected_params != num_bind_params) {
74 /*
75 * mysql_stmt_bind_param does not handle this conndition,
76 * and the client library will segfault if these do no match
77 */
78 lua_pushboolean(L, 0);
79 lua_pushfstring(L, "Statement expected %d paramaters but received %d", expected_params, num_bind_params);
80 return 2;
81 }
60 82
61 bind = malloc(sizeof(MYSQL_BIND) * num_bind_params); 83 bind = malloc(sizeof(MYSQL_BIND) * num_bind_params);
62 memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params); 84 memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params);
63 85
64 for (p = 2; p <= n; p++) { 86 for (p = 2; p <= n; p++) {
115 cleanup: 137 cleanup:
116 if (bind) 138 if (bind)
117 free(bind); 139 free(bind);
118 140
119 if (error_message) { 141 if (error_message) {
120 luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); 142 lua_pushboolean(L, 0);
121 return 0; 143 lua_pushfstring(L, error_message, mysql_stmt_error(statement->stmt));
144 return 2;
122 } 145 }
123 146
124 statement->metadata = metadata; 147 statement->metadata = metadata;
125 148
149 lua_pushboolean(L, 1);
126 return 1; 150 return 1;
127 } 151 }
128 152
129 static int statement_fetch_impl(lua_State *L, int named_columns) { 153 static int statement_fetch_impl(lua_State *L, int named_columns) {
130 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); 154 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
131 int column_count; 155 int column_count;
132 MYSQL_BIND *bind = NULL; 156 MYSQL_BIND *bind = NULL;
133 const char *error_message = NULL; 157 const char *error_message = NULL;
134 158
135 if (!statement->stmt) { 159 if (!statement->stmt) {
160 luaL_error(L, "fetch called on a closed or invalid statement");
161 return 0;
162 }
163
164 if (!statement->metadata) {
136 luaL_error(L, "fetch called before execute"); 165 luaL_error(L, "fetch called before execute");
137 lua_pushnil(L); 166 return 0;
138 return 1;
139 } 167 }
140 168
141 if (!statement->metadata) { 169 if (!statement->metadata) {
142 lua_pushnil(L); 170 lua_pushnil(L);
143 return 1; 171 return 1;
266 statement_t *statement = NULL; 294 statement_t *statement = NULL;
267 295
268 MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); 296 MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql);
269 297
270 if (!stmt) { 298 if (!stmt) {
271 luaL_error(L, "Error allocating statement handle: %s", mysql_error(conn->mysql)); 299 lua_pushnil(L);
272 return 0; 300 lua_pushfstring(L, "Error allocating statement handle: %s", mysql_error(conn->mysql));
301 return 2;
273 } 302 }
274 303
275 if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { 304 if (mysql_stmt_prepare(stmt, sql_query, sql_len)) {
276 luaL_error(L, "Error preparing statement handle: %s", mysql_stmt_error(stmt)); 305 lua_pushnil(L);
277 return 0; 306 lua_pushfstring(L, "Error preparing statement handle: %s", mysql_stmt_error(stmt));
307 return 2;
278 } 308 }
279 309
280 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); 310 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t));
281 statement->mysql = conn->mysql; 311 statement->mysql = conn->mysql;
282 statement->stmt = stmt; 312 statement->stmt = stmt;

mercurial