dbd/mysql/statement.c

changeset 14
98192b7d4e89
parent 12
014ba3ab3903
child 21
7956401a0c5e
equal deleted inserted replaced
13:10c8c6f0da14 14:98192b7d4e89
52 static int statement_execute(lua_State *L) { 52 static int statement_execute(lua_State *L) {
53 int n = lua_gettop(L); 53 int n = lua_gettop(L);
54 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);
55 int num_bind_params = n - 1; 55 int num_bind_params = n - 1;
56 int expected_params; 56 int expected_params;
57
58 unsigned char *buffer = NULL;
59 int offset = 0;
57 60
58 MYSQL_BIND *bind = NULL; 61 MYSQL_BIND *bind = NULL;
59 MYSQL_RES *metadata = NULL; 62 MYSQL_RES *metadata = NULL;
60 63
61 char *error_message = NULL; 64 char *error_message = NULL;
99 bind[i].buffer_type = MYSQL_TYPE_NULL; 102 bind[i].buffer_type = MYSQL_TYPE_NULL;
100 bind[i].is_null = (my_bool*)1; 103 bind[i].is_null = (my_bool*)1;
101 break; 104 break;
102 105
103 case LUA_TBOOLEAN: 106 case LUA_TBOOLEAN:
104 boolean = (int *)malloc(sizeof(int)); 107 buffer = realloc(buffer, offset + sizeof(int));
108 boolean = (int *)buffer + offset;
109 offset += sizeof(int);
105 *boolean = lua_toboolean(L, p); 110 *boolean = lua_toboolean(L, p);
111
106 bind[i].buffer_type = MYSQL_TYPE_LONG; 112 bind[i].buffer_type = MYSQL_TYPE_LONG;
107 bind[i].is_null = (my_bool*)0; 113 bind[i].is_null = (my_bool*)0;
108 bind[i].buffer = (char *)boolean; 114 bind[i].buffer = (char *)boolean;
109 bind[i].length = 0; 115 bind[i].length = 0;
110 break; 116 break;
112 case LUA_TNUMBER: 118 case LUA_TNUMBER:
113 /* 119 /*
114 * num needs to be it's own 120 * num needs to be it's own
115 * memory here 121 * memory here
116 */ 122 */
117 num = (double *)malloc(sizeof(double)); 123 buffer = realloc(buffer, offset + sizeof(double));
124 num = (double *)buffer + offset;
125 offset += sizeof(double);
118 *num = lua_tonumber(L, p); 126 *num = lua_tonumber(L, p);
119 127
120 bind[i].buffer_type = MYSQL_TYPE_DOUBLE; 128 bind[i].buffer_type = MYSQL_TYPE_DOUBLE;
121 bind[i].is_null = (my_bool*)0; 129 bind[i].is_null = (my_bool*)0;
122 bind[i].buffer = (char *)num; 130 bind[i].buffer = (char *)num;
123 bind[i].length = 0; 131 bind[i].length = 0;
124 break; 132 break;
125 133
126 case LUA_TSTRING: 134 case LUA_TSTRING:
127 str_len = malloc(sizeof(size_t)); 135 buffer = realloc(buffer, offset + sizeof(size_t));
136 str_len = (size_t *)buffer + offset;
137 offset += sizeof(size_t);
128 str = lua_tolstring(L, p, str_len); 138 str = lua_tolstring(L, p, str_len);
129 139
130 bind[i].buffer_type = MYSQL_TYPE_STRING; 140 bind[i].buffer_type = MYSQL_TYPE_STRING;
131 bind[i].is_null = (my_bool*)0; 141 bind[i].is_null = (my_bool*)0;
132 bind[i].buffer = (char *)str; 142 bind[i].buffer = (char *)str;
152 } 162 }
153 163
154 metadata = mysql_stmt_result_metadata(statement->stmt); 164 metadata = mysql_stmt_result_metadata(statement->stmt);
155 165
156 cleanup: 166 cleanup:
157 if (bind) { 167 /*
158 int i; 168 * free the buffer with a resize to 0
159 169 */
160 for (i = 0; i < num_bind_params; i++) { 170 realloc(buffer, 0);
161 /* 171
162 * Free the memory associated with 172 if (bind)
163 * the allocation of double and string
164 * bind params. If the interface are
165 * extended with other types they
166 * will need to be added here
167 */
168 if (bind[i].buffer_type == MYSQL_TYPE_DOUBLE || bind[i].buffer_type == MYSQL_TYPE_LONG) {
169 if (bind[i].buffer)
170 free(bind[i].buffer);
171 } else if (bind[i].buffer_type == MYSQL_TYPE_STRING) {
172 if (bind[i].length)
173 free(bind[i].length);
174 }
175 }
176
177 free(bind); 173 free(bind);
178 }
179 174
180 if (error_message) { 175 if (error_message) {
181 lua_pushboolean(L, 0); 176 lua_pushboolean(L, 0);
182 lua_pushfstring(L, error_message, errstr ? errstr : mysql_stmt_error(statement->stmt)); 177 lua_pushfstring(L, error_message, errstr ? errstr : mysql_stmt_error(statement->stmt));
183 return 2; 178 return 2;

mercurial