dbd/mysql/statement.c

Sat, 30 Jun 2012 00:37:19 +0200

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 30 Jun 2012 00:37:19 +0200
changeset 46
5ba1dd988961
parent 45
7c968f66bccd
permissions
-rw-r--r--

MySQL: Fix off-by-one in allocation and pass address of correct bind result buffer to mysql_stmt_fetch_column()

1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
1 #include "dbd_mysql.h"
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
2
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
3 static lua_push_type_t mysql_to_lua_push(unsigned int mysql_type) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
4 lua_push_type_t lua_type;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
5
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
6 switch(mysql_type) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
7 case MYSQL_TYPE_NULL:
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
8 lua_type = LUA_PUSH_NIL;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
9 break;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
10
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
11 case MYSQL_TYPE_TINY:
33
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
12 case MYSQL_TYPE_YEAR:
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
13 case MYSQL_TYPE_SHORT:
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
14 case MYSQL_TYPE_LONG:
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
15 lua_type = LUA_PUSH_INTEGER;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
16 break;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
17
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
18 case MYSQL_TYPE_DOUBLE:
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
19 case MYSQL_TYPE_LONGLONG:
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
20 lua_type = LUA_PUSH_NUMBER;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
21 break;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
22
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
23 default:
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
24 lua_type = LUA_PUSH_STRING;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
25 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
26
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
27 return lua_type;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
28 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
29
33
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
30 static size_t mysql_buffer_size(MYSQL_FIELD *field) {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
31 unsigned int mysql_type = field->type;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
32 size_t size = 0;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
33
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
34 switch (mysql_type) {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
35 case MYSQL_TYPE_TINY:
36
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
36 size = 1;
33
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
37 break;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
38 case MYSQL_TYPE_YEAR:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
39 case MYSQL_TYPE_SHORT:
36
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
40 size = 2;
33
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
41 break;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
42 case MYSQL_TYPE_INT24:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
43 size = 4;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
44 break;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
45 case MYSQL_TYPE_LONG:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
46 size = 4;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
47 break;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
48 case MYSQL_TYPE_LONGLONG:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
49 size = 8;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
50 break;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
51 case MYSQL_TYPE_FLOAT:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
52 size = 4;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
53 break;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
54 case MYSQL_TYPE_DOUBLE:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
55 size = 8;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
56 break;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
57 case MYSQL_TYPE_TIME:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
58 case MYSQL_TYPE_DATE:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
59 case MYSQL_TYPE_DATETIME:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
60 case MYSQL_TYPE_TIMESTAMP:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
61 size = sizeof(MYSQL_TIME);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
62 break;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
63 default:
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
64 size = field->length;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
65 }
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
66
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
67 return size;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
68 }
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
69
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
70 /*
21
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
71 * num_affected_rows = statement:affected()
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
72 */
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
73 static int statement_affected(lua_State *L) {
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
74 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
75
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
76 if (!statement->stmt) {
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
77 luaL_error(L, DBI_ERR_INVALID_STATEMENT);
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
78 }
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
79
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
80 lua_pushinteger(L, mysql_stmt_affected_rows(statement->stmt));
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
81
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
82 return 1;
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
83 }
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
84
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
85 /*
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
86 * success = statement:close()
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
87 */
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
88 static int statement_close(lua_State *L) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
89 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
90
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
91 if (statement->metadata) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
92 mysql_free_result(statement->metadata);
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
93 statement->metadata = NULL;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
94 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
95
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
96 if (statement->stmt) {
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
97 mysql_stmt_close(statement->stmt);
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
98 statement->stmt = NULL;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
99 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
100
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
101 lua_pushboolean(L, 1);
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
102 return 1;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
103 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
104
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
105 /*
30
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
106 * column_names = statement:columns()
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
107 */
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
108 static int statement_columns(lua_State *L) {
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
109 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
110
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
111 MYSQL_FIELD *fields;
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
112 int i;
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
113 int num_columns;
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
114 int d = 1;
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
115
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
116 if (!statement->stmt) {
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
117 luaL_error(L, DBI_ERR_INVALID_STATEMENT);
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
118 return 0;
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
119 }
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
120
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
121 fields = mysql_fetch_fields(statement->metadata);
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
122 num_columns = mysql_num_fields(statement->metadata);
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
123 lua_newtable(L);
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
124 for (i = 0; i < num_columns; i++) {
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
125 const char *name = fields[i].name;
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
126
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
127 LUA_PUSH_ARRAY_STRING(d, name);
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
128 }
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
129
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
130 return 1;
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
131 }
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
132
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
133
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
134 /*
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
135 * success,err = statement:execute(...)
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
136 */
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
137 static int statement_execute(lua_State *L) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
138 int n = lua_gettop(L);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
139 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
140 int num_bind_params = n - 1;
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
141 int expected_params;
14
98192b7d4e89 Add DB2 driver module.
nrich@ii.net
parents: 12
diff changeset
142
25
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
143 unsigned char *buffer = NULL;
14
98192b7d4e89 Add DB2 driver module.
nrich@ii.net
parents: 12
diff changeset
144 int offset = 0;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
145
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
146 MYSQL_BIND *bind = NULL;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
147 MYSQL_RES *metadata = NULL;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
148
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
149 char *error_message = NULL;
10
3aa8a37a3dd8 Added new typechecks and errors
nrich@ii.net
parents: 9
diff changeset
150 char *errstr = NULL;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
151
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
152 int p;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
153
36
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
154 if (statement->metadata) {
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
155 /*
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
156 * free existing metadata from any previous executions
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
157 */
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
158 mysql_free_result(statement->metadata);
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
159 statement->metadata = NULL;
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
160 }
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
161
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
162 if (!statement->stmt) {
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
163 lua_pushboolean(L, 0);
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
164 lua_pushstring(L, DBI_ERR_EXECUTE_INVALID);
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
165 return 2;
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
166 }
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
167
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
168 expected_params = mysql_stmt_param_count(statement->stmt);
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
169
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
170 if (expected_params != num_bind_params) {
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
171 /*
10
3aa8a37a3dd8 Added new typechecks and errors
nrich@ii.net
parents: 9
diff changeset
172 * mysql_stmt_bind_param does not handle this condition,
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
173 * and the client library will segfault if these do no match
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
174 */
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
175 lua_pushboolean(L, 0);
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
176 lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params);
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
177 return 2;
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
178 }
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
179
26
cf847efefdb5 Bugfix - fix allocation bugs
nrich@ii.net
parents: 25
diff changeset
180 if (num_bind_params > 0) {
25
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
181 bind = malloc(sizeof(MYSQL_BIND) * num_bind_params);
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
182 if (bind == NULL) {
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
183 luaL_error(L, "Could not alloc bind params\n");
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
184 }
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
185
26
cf847efefdb5 Bugfix - fix allocation bugs
nrich@ii.net
parents: 25
diff changeset
186 buffer = (unsigned char *)malloc(num_bind_params * sizeof(double));
25
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
187 memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params);
23
a4825c3e65e9 Bugfix: memory corruption possible after reallocs. Using a static buffer instead. Will need to check for overflows on static buffer.
nrich@ii.net
parents: 21
diff changeset
188 }
a4825c3e65e9 Bugfix: memory corruption possible after reallocs. Using a static buffer instead. Will need to check for overflows on static buffer.
nrich@ii.net
parents: 21
diff changeset
189
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
190 for (p = 2; p <= n; p++) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
191 int type = lua_type(L, p);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
192 int i = p - 2;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
193
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
194 const char *str = NULL;
7
4480ae002881 Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents: 6
diff changeset
195 size_t *str_len = NULL;
6
22046b996150 Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents: 4
diff changeset
196 double *num = NULL;
9
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
197 int *boolean = NULL;
10
3aa8a37a3dd8 Added new typechecks and errors
nrich@ii.net
parents: 9
diff changeset
198 char err[64];
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
199
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
200 switch(type) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
201 case LUA_TNIL:
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
202 bind[i].buffer_type = MYSQL_TYPE_NULL;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
203 bind[i].is_null = (my_bool*)1;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
204 break;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
205
9
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
206 case LUA_TBOOLEAN:
26
cf847efefdb5 Bugfix - fix allocation bugs
nrich@ii.net
parents: 25
diff changeset
207 boolean = (int *)(buffer + offset);
14
98192b7d4e89 Add DB2 driver module.
nrich@ii.net
parents: 12
diff changeset
208 offset += sizeof(int);
9
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
209 *boolean = lua_toboolean(L, p);
14
98192b7d4e89 Add DB2 driver module.
nrich@ii.net
parents: 12
diff changeset
210
9
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
211 bind[i].buffer_type = MYSQL_TYPE_LONG;
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
212 bind[i].is_null = (my_bool*)0;
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
213 bind[i].buffer = (char *)boolean;
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
214 bind[i].length = 0;
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
215 break;
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
216
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
217 case LUA_TNUMBER:
6
22046b996150 Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents: 4
diff changeset
218 /*
22046b996150 Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents: 4
diff changeset
219 * num needs to be it's own
22046b996150 Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents: 4
diff changeset
220 * memory here
22046b996150 Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents: 4
diff changeset
221 */
26
cf847efefdb5 Bugfix - fix allocation bugs
nrich@ii.net
parents: 25
diff changeset
222 num = (double *)(buffer + offset);
14
98192b7d4e89 Add DB2 driver module.
nrich@ii.net
parents: 12
diff changeset
223 offset += sizeof(double);
9
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
224 *num = lua_tonumber(L, p);
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
225
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
226 bind[i].buffer_type = MYSQL_TYPE_DOUBLE;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
227 bind[i].is_null = (my_bool*)0;
6
22046b996150 Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents: 4
diff changeset
228 bind[i].buffer = (char *)num;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
229 bind[i].length = 0;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
230 break;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
231
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
232 case LUA_TSTRING:
26
cf847efefdb5 Bugfix - fix allocation bugs
nrich@ii.net
parents: 25
diff changeset
233 str_len = (size_t *)(buffer + offset);
14
98192b7d4e89 Add DB2 driver module.
nrich@ii.net
parents: 12
diff changeset
234 offset += sizeof(size_t);
9
06eb2850703f Bugfix: fix connection in DBD.SQLite3.New
nrich@ii.net
parents: 7
diff changeset
235 str = lua_tolstring(L, p, str_len);
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
236
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
237 bind[i].buffer_type = MYSQL_TYPE_STRING;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
238 bind[i].is_null = (my_bool*)0;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
239 bind[i].buffer = (char *)str;
7
4480ae002881 Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents: 6
diff changeset
240 bind[i].length = str_len;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
241 break;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
242
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
243 default:
10
3aa8a37a3dd8 Added new typechecks and errors
nrich@ii.net
parents: 9
diff changeset
244 snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type));
3aa8a37a3dd8 Added new typechecks and errors
nrich@ii.net
parents: 9
diff changeset
245 errstr = err;
3aa8a37a3dd8 Added new typechecks and errors
nrich@ii.net
parents: 9
diff changeset
246 error_message = DBI_ERR_BINDING_PARAMS;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
247 goto cleanup;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
248 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
249 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
250
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
251 if (mysql_stmt_bind_param(statement->stmt, bind)) {
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
252 error_message = DBI_ERR_BINDING_PARAMS;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
253 goto cleanup;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
254 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
255
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
256 if (mysql_stmt_execute(statement->stmt)) {
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
257 error_message = DBI_ERR_BINDING_EXEC;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
258 goto cleanup;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
259 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
260
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
261 metadata = mysql_stmt_result_metadata(statement->stmt);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
262
21
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
263 if (metadata) {
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
264 mysql_stmt_store_result(statement->stmt);
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
265 }
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
266
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
267 cleanup:
23
a4825c3e65e9 Bugfix: memory corruption possible after reallocs. Using a static buffer instead. Will need to check for overflows on static buffer.
nrich@ii.net
parents: 21
diff changeset
268 if (bind) {
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
269 free(bind);
23
a4825c3e65e9 Bugfix: memory corruption possible after reallocs. Using a static buffer instead. Will need to check for overflows on static buffer.
nrich@ii.net
parents: 21
diff changeset
270 }
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
271
25
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
272 if (buffer) {
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
273 free(buffer);
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
274 }
2cc3feba9277 Cleanup.
nrich@ii.net
parents: 23
diff changeset
275
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
276 if (error_message) {
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
277 lua_pushboolean(L, 0);
10
3aa8a37a3dd8 Added new typechecks and errors
nrich@ii.net
parents: 9
diff changeset
278 lua_pushfstring(L, error_message, errstr ? errstr : mysql_stmt_error(statement->stmt));
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
279 return 2;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
280 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
281
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
282 statement->metadata = metadata;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
283
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
284 lua_pushboolean(L, 1);
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
285 return 1;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
286 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
287
11
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
288 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) {
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
289 int column_count, fetch_result_ok;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
290 MYSQL_BIND *bind = NULL;
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
291 unsigned long *real_length = NULL;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
292 const char *error_message = NULL;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
293
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
294 if (!statement->stmt) {
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
295 luaL_error(L, DBI_ERR_FETCH_INVALID);
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
296 return 0;
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
297 }
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
298
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
299 if (!statement->metadata) {
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
300 luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE);
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
301 return 0;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
302 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
303
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
304 column_count = mysql_num_fields(statement->metadata);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
305
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
306 if (column_count > 0) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
307 int i;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
308 MYSQL_FIELD *fields;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
309
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
310 real_length = calloc(column_count, sizeof(unsigned long));
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
311
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
312 bind = malloc(sizeof(MYSQL_BIND) * column_count);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
313 memset(bind, 0, sizeof(MYSQL_BIND) * column_count);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
314
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
315 fields = mysql_fetch_fields(statement->metadata);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
316
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
317 for (i = 0; i < column_count; i++) {
33
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
318 unsigned int length = mysql_buffer_size(&fields[i]);
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
319 if (length > sizeof(MYSQL_TIME)) {
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
320 bind[i].buffer = NULL;
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
321 bind[i].buffer_length = 0;
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
322 } else {
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
323 char *buffer = (char *)malloc(length);
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
324 memset(buffer, 0, length);
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
325
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
326 bind[i].buffer = buffer;
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
327 bind[i].buffer_length = length;
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
328 }
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
329
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
330 bind[i].buffer_type = fields[i].type;
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
331 bind[i].length = &real_length[i];
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
332 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
333
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
334 if (mysql_stmt_bind_result(statement->stmt, bind)) {
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
335 error_message = DBI_ERR_BINDING_RESULTS;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
336 goto cleanup;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
337 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
338
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
339 fetch_result_ok = mysql_stmt_fetch(statement->stmt);
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
340 if (fetch_result_ok == 0 || fetch_result_ok == MYSQL_DATA_TRUNCATED) {
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
341 int d = 1;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
342
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
343 lua_newtable(L);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
344 for (i = 0; i < column_count; i++) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
345 lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
346 const char *name = fields[i].name;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
347
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
348 if (bind[i].buffer == NULL) {
46
5ba1dd988961 MySQL: Fix off-by-one in allocation and pass address of correct bind result buffer to mysql_stmt_fetch_column()
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
349 char *buffer = (char *)calloc(real_length[i]+1, sizeof(char));
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
350 bind[i].buffer = buffer;
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
351 bind[i].buffer_length = real_length[i];
46
5ba1dd988961 MySQL: Fix off-by-one in allocation and pass address of correct bind result buffer to mysql_stmt_fetch_column()
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
352 mysql_stmt_fetch_column(statement->stmt, &bind[i], i, 0);
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
353 }
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
354
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
355 if (lua_push == LUA_PUSH_NIL) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
356 if (named_columns) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
357 LUA_PUSH_ATTRIB_NIL(name);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
358 } else {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
359 LUA_PUSH_ARRAY_NIL(d);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
360 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
361 } else if (lua_push == LUA_PUSH_INTEGER) {
36
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
362 if (fields[i].type == MYSQL_TYPE_YEAR || fields[i].type == MYSQL_TYPE_SHORT) {
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
363 if (named_columns) {
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
364 LUA_PUSH_ATTRIB_INT(name, *(short *)(bind[i].buffer));
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
365 } else {
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
366 LUA_PUSH_ARRAY_INT(d, *(short *)(bind[i].buffer));
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
367 }
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
368 } else if (fields[i].type == MYSQL_TYPE_TINY) {
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
369 if (named_columns) {
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
370 LUA_PUSH_ATTRIB_INT(name, (int)*(char *)(bind[i].buffer));
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
371 } else {
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
372 LUA_PUSH_ARRAY_INT(d, (int)*(char *)(bind[i].buffer));
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
373 }
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
374 } else {
36
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
375 if (named_columns) {
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
376 LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer));
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
377 } else {
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
378 LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer));
942bfe1843bc * Add new MySQL types
nrich@ii.net
parents: 33
diff changeset
379 }
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
380 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
381 } else if (lua_push == LUA_PUSH_NUMBER) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
382 if (named_columns) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
383 LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer));
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
384 } else {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
385 LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer));
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
386 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
387 } else if (lua_push == LUA_PUSH_STRING) {
33
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
388
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
389 if (fields[i].type == MYSQL_TYPE_TIMESTAMP || fields[i].type == MYSQL_TYPE_DATETIME) {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
390 char str[20];
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
391 struct st_mysql_time *t = bind[i].buffer;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
392
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
393 snprintf(str, 20, "%d-%02d-%02d %02d:%02d:%02d", t->year, t->month, t->day, t->hour, t->minute, t->second);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
394
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
395 if (named_columns) {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
396 LUA_PUSH_ATTRIB_STRING(name, str);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
397 } else {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
398 LUA_PUSH_ARRAY_STRING(d, str);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
399 }
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
400 } else if (fields[i].type == MYSQL_TYPE_TIME) {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
401 char str[9];
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
402 struct st_mysql_time *t = bind[i].buffer;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
403
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
404 snprintf(str, 9, "%02d:%02d:%02d", t->hour, t->minute, t->second);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
405
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
406 if (named_columns) {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
407 LUA_PUSH_ATTRIB_STRING(name, str);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
408 } else {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
409 LUA_PUSH_ARRAY_STRING(d, str);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
410 }
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
411 } else if (fields[i].type == MYSQL_TYPE_DATE) {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
412 char str[20];
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
413 struct st_mysql_time *t = bind[i].buffer;
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
414
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
415 snprintf(str, 11, "%d-%02d-%02d", t->year, t->month, t->day);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
416
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
417 if (named_columns) {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
418 LUA_PUSH_ATTRIB_STRING(name, str);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
419 } else {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
420 LUA_PUSH_ARRAY_STRING(d, str);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
421 }
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
422
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
423 } else {
33
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
424 if (named_columns) {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
425 LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
426 } else {
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
427 LUA_PUSH_ARRAY_STRING(d, bind[i].buffer);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
428 }
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
429 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
430 } else if (lua_push == LUA_PUSH_BOOLEAN) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
431 if (named_columns) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
432 LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer));
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
433 } else {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
434 LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer));
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
435 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
436 } else {
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
437 luaL_error(L, DBI_ERR_UNKNOWN_PUSH);
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
438 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
439 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
440 } else {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
441 lua_pushnil(L);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
442 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
443 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
444
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
445 cleanup:
45
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
446 free(real_length);
7c968f66bccd MySQL: Avoid allocating the full column size to receive results, for variable-length types check result size before allocation (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
447
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
448 if (bind) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
449 int i;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
450
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
451 for (i = 0; i < column_count; i++) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
452 free(bind[i].buffer);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
453 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
454
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
455 free(bind);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
456 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
457
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
458 if (error_message) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
459 luaL_error(L, error_message, mysql_stmt_error(statement->stmt));
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
460 return 0;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
461 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
462
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
463 return 1;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
464 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
465
11
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
466 static int next_iterator(lua_State *L) {
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
467 statement_t *statement = (statement_t *)luaL_checkudata(L, lua_upvalueindex(1), DBD_MYSQL_STATEMENT);
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
468 int named_columns = lua_toboolean(L, lua_upvalueindex(2));
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
469
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
470 return statement_fetch_impl(L, statement, named_columns);
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
471 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
472
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
473 /*
12
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
474 * table = statement:fetch(named_indexes)
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
475 */
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
476 static int statement_fetch(lua_State *L) {
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
477 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
478 int named_columns = lua_toboolean(L, 2);
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
479
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
480 return statement_fetch_impl(L, statement, named_columns);
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
481 }
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
482
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
483 /*
21
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
484 * num_rows = statement:rowcount()
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
485 */
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
486 static int statement_rowcount(lua_State *L) {
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
487 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
488
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
489 if (!statement->stmt) {
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
490 luaL_error(L, DBI_ERR_INVALID_STATEMENT);
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
491 }
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
492
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
493 lua_pushinteger(L, mysql_stmt_num_rows(statement->stmt));
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
494
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
495 return 1;
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
496 }
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
497
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
498 /*
12
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
499 * iterfunc = statement:rows(named_indexes)
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
500 */
12
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
501 static int statement_rows(lua_State *L) {
11
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
502 if (lua_gettop(L) == 1) {
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
503 lua_pushvalue(L, 1);
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
504 lua_pushboolean(L, 0);
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
505 } else {
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
506 lua_pushvalue(L, 1);
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
507 lua_pushboolean(L, lua_toboolean(L, 2));
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
508 }
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
509
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
510 lua_pushcclosure(L, next_iterator, 2);
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
511 return 1;
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
512 }
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
513
b3e05e361f46 Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents: 10
diff changeset
514 /*
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
515 * __gc
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
516 */
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
517 static int statement_gc(lua_State *L) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
518 /* always free the handle */
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
519 statement_close(L);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
520
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
521 return 0;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
522 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
523
32
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
524 /*
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
525 * __tostring
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
526 */
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
527 static int statement_tostring(lua_State *L) {
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
528 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
529
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
530 lua_pushfstring(L, "%s: %p", DBD_MYSQL_STATEMENT, statement);
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
531
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
532 return 1;
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
533 }
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
534
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
535 int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) {
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
536 unsigned long sql_len = strlen(sql_query);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
537
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
538 statement_t *statement = NULL;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
539
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
540 MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
541
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
542 if (!stmt) {
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
543 lua_pushnil(L);
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
544 lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql));
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
545 return 2;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
546 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
547
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
548 if (mysql_stmt_prepare(stmt, sql_query, sql_len)) {
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
549 lua_pushnil(L);
4
c50b0e6f25d6 Clean up error messages for consistency.
nrich@ii.net
parents: 3
diff changeset
550 lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt));
3
b61020ca4753 Cleanup and 'assert' error handling.
nrich@ii.net
parents: 2
diff changeset
551 return 2;
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
552 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
553
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
554 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t));
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
555 statement->mysql = conn->mysql;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
556 statement->stmt = stmt;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
557 statement->metadata = NULL;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
558
33
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
559 /*
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
560 mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (my_bool*)0);
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
561 */
6c64c45e7d8f * Fix MySQL date/time types
nrich@ii.net
parents: 32
diff changeset
562
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
563 luaL_getmetatable(L, DBD_MYSQL_STATEMENT);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
564 lua_setmetatable(L, -2);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
565
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
566 return 1;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
567 }
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
568
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
569 int dbd_mysql_statement(lua_State *L) {
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
570 static const luaL_Reg statement_methods[] = {
21
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
571 {"affected", statement_affected},
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
572 {"close", statement_close},
30
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 26
diff changeset
573 {"columns", statement_columns},
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
574 {"execute", statement_execute},
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
575 {"fetch", statement_fetch},
21
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 14
diff changeset
576 {"rowcount", statement_rowcount},
12
014ba3ab3903 Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents: 11
diff changeset
577 {"rows", statement_rows},
2
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
578 {NULL, NULL}
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
579 };
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
580
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
581 static const luaL_Reg statement_class_methods[] = {
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
582 {NULL, NULL}
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
583 };
c4f02fc67e5a Cleanup and commenting
nrich@ii.net
parents: 1
diff changeset
584
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
585 luaL_newmetatable(L, DBD_MYSQL_STATEMENT);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
586 luaL_register(L, 0, statement_methods);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
587 lua_pushvalue(L,-1);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
588 lua_setfield(L, -2, "__index");
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
589
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
590 lua_pushcfunction(L, statement_gc);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
591 lua_setfield(L, -2, "__gc");
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
592
32
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
593 lua_pushcfunction(L, statement_tostring);
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
594 lua_setfield(L, -2, "__tostring");
03ed0ca09837 Add __tostring method to connection and statement objects.
nrich@ii.net
parents: 30
diff changeset
595
1
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
596 luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods);
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
597
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
598 return 1;
408291a6eb3e Initial import.
nrich@ii.net
parents:
diff changeset
599 }

mercurial