dbd/mysql/statement.c

changeset 4
c50b0e6f25d6
parent 3
b61020ca4753
child 6
22046b996150
equal deleted inserted replaced
3:b61020ca4753 4:c50b0e6f25d6
62 62
63 int p; 63 int p;
64 64
65 if (!statement->stmt) { 65 if (!statement->stmt) {
66 lua_pushboolean(L, 0); 66 lua_pushboolean(L, 0);
67 lua_pushstring(L, "execute called on a closed or invalid statement"); 67 lua_pushstring(L, DBI_ERR_EXECUTE_INVALID);
68 return 2; 68 return 2;
69 } 69 }
70 70
71 expected_params = mysql_stmt_param_count(statement->stmt); 71 expected_params = mysql_stmt_param_count(statement->stmt);
72 72
74 /* 74 /*
75 * mysql_stmt_bind_param does not handle this conndition, 75 * mysql_stmt_bind_param does not handle this conndition,
76 * and the client library will segfault if these do no match 76 * and the client library will segfault if these do no match
77 */ 77 */
78 lua_pushboolean(L, 0); 78 lua_pushboolean(L, 0);
79 lua_pushfstring(L, "Statement expected %d paramaters but received %d", expected_params, num_bind_params); 79 lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params);
80 return 2; 80 return 2;
81 } 81 }
82 82
83 bind = malloc(sizeof(MYSQL_BIND) * num_bind_params); 83 bind = malloc(sizeof(MYSQL_BIND) * num_bind_params);
84 memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params); 84 memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params);
115 bind[i].buffer = (char *)str; 115 bind[i].buffer = (char *)str;
116 bind[i].length = &str_len; 116 bind[i].length = &str_len;
117 break; 117 break;
118 118
119 default: 119 default:
120 error_message = "Binding unknown or unsupported type"; 120 error_message = DBI_ERR_BINDING_UNKNOWN;
121 goto cleanup; 121 goto cleanup;
122 } 122 }
123 } 123 }
124 124
125 if (mysql_stmt_bind_param(statement->stmt, bind)) { 125 if (mysql_stmt_bind_param(statement->stmt, bind)) {
126 error_message = "Error binding statement parameters: %s"; 126 error_message = DBI_ERR_BINDING_PARAMS;
127 goto cleanup; 127 goto cleanup;
128 } 128 }
129 129
130 if (mysql_stmt_execute(statement->stmt)) { 130 if (mysql_stmt_execute(statement->stmt)) {
131 error_message = "Error executing statement: %s"; 131 error_message = DBI_ERR_BINDING_EXEC;
132 goto cleanup; 132 goto cleanup;
133 } 133 }
134 134
135 metadata = mysql_stmt_result_metadata(statement->stmt); 135 metadata = mysql_stmt_result_metadata(statement->stmt);
136 136
155 int column_count; 155 int column_count;
156 MYSQL_BIND *bind = NULL; 156 MYSQL_BIND *bind = NULL;
157 const char *error_message = NULL; 157 const char *error_message = NULL;
158 158
159 if (!statement->stmt) { 159 if (!statement->stmt) {
160 luaL_error(L, "fetch called on a closed or invalid statement"); 160 luaL_error(L, DBI_ERR_FETCH_INVALID);
161 return 0; 161 return 0;
162 } 162 }
163 163
164 if (!statement->metadata) { 164 if (!statement->metadata) {
165 luaL_error(L, "fetch called before execute"); 165 luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE);
166 return 0; 166 return 0;
167 } 167 }
168 168
169 if (!statement->metadata) { 169 if (!statement->metadata) {
170 lua_pushnil(L); 170 lua_pushnil(L);
192 bind[i].buffer = buffer; 192 bind[i].buffer = buffer;
193 bind[i].buffer_length = length; 193 bind[i].buffer_length = length;
194 } 194 }
195 195
196 if (mysql_stmt_bind_result(statement->stmt, bind)) { 196 if (mysql_stmt_bind_result(statement->stmt, bind)) {
197 error_message = "Error binding results: %s"; 197 error_message = DBI_ERR_BINDING_RESULTS;
198 goto cleanup; 198 goto cleanup;
199 } 199 }
200 200
201 if (!mysql_stmt_fetch(statement->stmt)) { 201 if (!mysql_stmt_fetch(statement->stmt)) {
202 int d = 1; 202 int d = 1;
235 LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); 235 LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer));
236 } else { 236 } else {
237 LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); 237 LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer));
238 } 238 }
239 } else { 239 } else {
240 luaL_error(L, "Unknown push type in result set"); 240 luaL_error(L, DBI_ERR_UNKNOWN_PUSH);
241 } 241 }
242 } 242 }
243 } else { 243 } else {
244 lua_pushnil(L); 244 lua_pushnil(L);
245 } 245 }
295 295
296 MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); 296 MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql);
297 297
298 if (!stmt) { 298 if (!stmt) {
299 lua_pushnil(L); 299 lua_pushnil(L);
300 lua_pushfstring(L, "Error allocating statement handle: %s", mysql_error(conn->mysql)); 300 lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql));
301 return 2; 301 return 2;
302 } 302 }
303 303
304 if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { 304 if (mysql_stmt_prepare(stmt, sql_query, sql_len)) {
305 lua_pushnil(L); 305 lua_pushnil(L);
306 lua_pushfstring(L, "Error preparing statement handle: %s", mysql_stmt_error(stmt)); 306 lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt));
307 return 2; 307 return 2;
308 } 308 }
309 309
310 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); 310 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t));
311 statement->mysql = conn->mysql; 311 statement->mysql = conn->mysql;

mercurial