dbd/sqlite3/statement.c

changeset 10
3aa8a37a3dd8
parent 9
06eb2850703f
child 11
b3e05e361f46
equal deleted inserted replaced
9:06eb2850703f 10:3aa8a37a3dd8
67 */ 67 */
68 int statement_execute(lua_State *L) { 68 int statement_execute(lua_State *L) {
69 int n = lua_gettop(L); 69 int n = lua_gettop(L);
70 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); 70 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
71 int p; 71 int p;
72 int err = 0; 72 int errflag = 0;
73 73 const char *errstr = NULL;
74 int expected_params;
75 int num_bind_params = n - 1;
74 76
75 if (!statement->stmt) { 77 if (!statement->stmt) {
76 lua_pushboolean(L, 0); 78 lua_pushboolean(L, 0);
77 lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); 79 lua_pushstring(L, DBI_ERR_EXECUTE_INVALID);
78 return 2; 80 return 2;
87 lua_pushboolean(L, 0); 89 lua_pushboolean(L, 0);
88 lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); 90 lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite));
89 return 2; 91 return 2;
90 } 92 }
91 93
94 expected_params = sqlite3_bind_parameter_count(statement->stmt);
95 if (expected_params != num_bind_params) {
96 /*
97 * sqlite3_reset does not handle this condition,
98 * and the client library will fill unset params
99 * with NULLs
100 */
101 lua_pushboolean(L, 0);
102 lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params);
103 return 2;
104 }
105
92 for (p = 2; p <= n; p++) { 106 for (p = 2; p <= n; p++) {
93 int i = p - 1; 107 int i = p - 1;
94
95 int type = lua_type(L, p); 108 int type = lua_type(L, p);
109 char err[64];
96 110
97 switch(type) { 111 switch(type) {
98 case LUA_TNIL: 112 case LUA_TNIL:
99 err = sqlite3_bind_null(statement->stmt, i) != SQLITE_OK; 113 errflag = sqlite3_bind_null(statement->stmt, i) != SQLITE_OK;
100 break; 114 break;
101 case LUA_TNUMBER: 115 case LUA_TNUMBER:
102 err = sqlite3_bind_double(statement->stmt, i, lua_tonumber(L, p)) != SQLITE_OK; 116 errflag = sqlite3_bind_double(statement->stmt, i, lua_tonumber(L, p)) != SQLITE_OK;
103 break; 117 break;
104 case LUA_TSTRING: 118 case LUA_TSTRING:
105 err = sqlite3_bind_text(statement->stmt, i, lua_tostring(L, p), -1, SQLITE_STATIC) != SQLITE_OK; 119 errflag = sqlite3_bind_text(statement->stmt, i, lua_tostring(L, p), -1, SQLITE_STATIC) != SQLITE_OK;
106 break; 120 break;
107 case LUA_TBOOLEAN: 121 case LUA_TBOOLEAN:
108 err = sqlite3_bind_int(statement->stmt, i, lua_toboolean(L, p)) != SQLITE_OK; 122 errflag = sqlite3_bind_int(statement->stmt, i, lua_toboolean(L, p)) != SQLITE_OK;
109 break; 123 break;
110 default: 124 default:
111 /* 125 /*
112 * Unknown/unsupported value type 126 * Unknown/unsupported value type
113 */ 127 */
114 err = 1; 128 errflag = 1;
129 snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type));
130 errstr = err;
115 } 131 }
116 132
117 133 if (errflag)
118 if (err)
119 break; 134 break;
120 } 135 }
121 136
122 if (err) { 137 if (errflag) {
123 lua_pushboolean(L, 0); 138 lua_pushboolean(L, 0);
124 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, sqlite3_errmsg(statement->sqlite)); 139 if (errstr)
140 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errstr);
141 else
142 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, sqlite3_errmsg(statement->sqlite));
143
125 return 2; 144 return 2;
126 } 145 }
127 146
128 if (!step(statement)) { 147 if (!step(statement)) {
129 lua_pushboolean(L, 0); 148 lua_pushboolean(L, 0);

mercurial