Tue, 01 Sep 2009 13:15:02 +0000
Add __tostring method to connection and statement objects.
1 | 1 | #include "dbd_mysql.h" |
2 | ||
3 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query); | |
4 | ||
2 | 5 | /* |
3 | 6 | * connection,err = DBD.MySQl.New(dbname, user, password, host, port) |
2 | 7 | */ |
1 | 8 | static int connection_new(lua_State *L) { |
9 | int n = lua_gettop(L); | |
10 | ||
11 | connection_t *conn = NULL; | |
12 | ||
13 | const char *host = NULL; | |
14 | const char *user = NULL; | |
15 | const char *password = NULL; | |
16 | const char *db = NULL; | |
17 | int port = 0; | |
18 | ||
19 | const char *unix_socket = NULL; /* TODO always NULL */ | |
20 | int client_flag = 0; /* TODO always 0, set flags from options table */ | |
21 | ||
22 | /* db, user, password, host, port */ | |
23 | switch (n) { | |
24 | case 5: | |
25 | if (lua_isnil(L, 5) == 0) | |
26 | port = luaL_checkint(L, 5); | |
27 | case 4: | |
28 | if (lua_isnil(L, 4) == 0) | |
29 | host = luaL_checkstring(L, 4); | |
30 | case 3: | |
31 | if (lua_isnil(L, 3) == 0) | |
32 | password = luaL_checkstring(L, 3); | |
33 | case 2: | |
34 | if (lua_isnil(L, 2) == 0) | |
35 | user = luaL_checkstring(L, 2); | |
36 | case 1: | |
14 | 37 | /* |
38 | * db is the only mandatory parameter | |
39 | */ | |
40 | db = luaL_checkstring(L, 1); | |
1 | 41 | } |
42 | ||
43 | conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t)); | |
44 | ||
45 | conn->mysql = mysql_init(NULL); | |
46 | ||
3 | 47 | if (!mysql_real_connect(conn->mysql, host, user, password, db, port, unix_socket, client_flag)) { |
1 | 48 | lua_pushnil(L); |
4 | 49 | lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, mysql_error(conn->mysql)); |
3 | 50 | return 2; |
1 | 51 | } |
52 | ||
13
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
53 | /* |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
54 | * by default turn off autocommit |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
55 | */ |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
56 | mysql_autocommit(conn->mysql, 0); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
57 | |
3 | 58 | luaL_getmetatable(L, DBD_MYSQL_CONNECTION); |
59 | lua_setmetatable(L, -2); | |
60 | ||
1 | 61 | return 1; |
62 | } | |
63 | ||
2 | 64 | /* |
13
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
65 | * success = connection:autocommit(on) |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
66 | */ |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
67 | static int connection_autocommit(lua_State *L) { |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
68 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
69 | int on = lua_toboolean(L, 2); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
70 | int err = 0; |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
71 | |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
72 | if (conn->mysql) { |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
73 | err = mysql_autocommit(conn->mysql, on); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
74 | } |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
75 | |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
76 | lua_pushboolean(L, !err); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
77 | return 1; |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
78 | } |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
79 | |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
80 | /* |
2 | 81 | * success = connection:close() |
82 | */ | |
1 | 83 | static int connection_close(lua_State *L) { |
84 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); | |
85 | int disconnect = 0; | |
86 | ||
87 | if (conn->mysql) { | |
88 | mysql_close(conn->mysql); | |
89 | disconnect = 1; | |
3 | 90 | conn->mysql = NULL; |
1 | 91 | } |
92 | ||
93 | lua_pushboolean(L, disconnect); | |
94 | return 1; | |
95 | } | |
96 | ||
13
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
97 | /* |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
98 | * success = connection:commit() |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
99 | */ |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
100 | static int connection_commit(lua_State *L) { |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
101 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
102 | int err = 0; |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
103 | |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
104 | if (conn->mysql) { |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
105 | err = mysql_commit(conn->mysql); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
106 | } |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
107 | |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
108 | lua_pushboolean(L, !err); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
109 | return 1; |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
110 | } |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
111 | |
2 | 112 | /* |
113 | * ok = connection:ping() | |
114 | */ | |
1 | 115 | static int connection_ping(lua_State *L) { |
116 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); | |
117 | int err = 1; | |
118 | ||
119 | if (conn->mysql) { | |
120 | err = mysql_ping(conn->mysql); | |
121 | } | |
122 | ||
123 | lua_pushboolean(L, !err); | |
124 | return 1; | |
125 | } | |
126 | ||
2 | 127 | /* |
3 | 128 | * statement,err = connection:prepare(sql_string) |
2 | 129 | */ |
1 | 130 | static int connection_prepare(lua_State *L) { |
131 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); | |
132 | ||
133 | if (conn->mysql) { | |
134 | return dbd_mysql_statement_create(L, conn, luaL_checkstring(L, 2)); | |
135 | } | |
136 | ||
137 | lua_pushnil(L); | |
4 | 138 | lua_pushstring(L, DBI_ERR_DB_UNAVAILABLE); |
3 | 139 | |
140 | return 2; | |
1 | 141 | } |
142 | ||
2 | 143 | /* |
22 | 144 | * quoted = connection:quote(str) |
145 | */ | |
146 | static int connection_quote(lua_State *L) { | |
147 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); | |
148 | size_t len; | |
149 | const char *from = luaL_checklstring(L, 2, &len); | |
150 | char *to = (char *)calloc(len*2+1, sizeof(char)); | |
151 | int quoted_len; | |
152 | ||
153 | if (!conn->mysql) { | |
154 | luaL_error(L, DBI_ERR_DB_UNAVAILABLE); | |
155 | } | |
156 | ||
157 | quoted_len = mysql_real_escape_string(conn->mysql, to, from, len); | |
158 | ||
159 | lua_pushlstring(L, to, quoted_len); | |
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:
22
diff
changeset
|
160 | free(to); |
22 | 161 | |
162 | return 1; | |
163 | } | |
164 | ||
165 | /* | |
13
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
166 | * success = connection:rollback() |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
167 | */ |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
168 | static int connection_rollback(lua_State *L) { |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
169 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
170 | int err = 0; |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
171 | |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
172 | if (conn->mysql) { |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
173 | err = mysql_rollback(conn->mysql); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
174 | } |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
175 | |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
176 | lua_pushboolean(L, !err); |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
177 | return 1; |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
178 | } |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
179 | |
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
180 | /* |
2 | 181 | * __gc |
182 | */ | |
1 | 183 | static int connection_gc(lua_State *L) { |
184 | /* always close the connection */ | |
185 | connection_close(L); | |
186 | ||
187 | return 0; | |
188 | } | |
189 | ||
32
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
190 | /* |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
191 | * __tostring |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
192 | */ |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
193 | static int connection_tostring(lua_State *L) { |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
194 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
195 | |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
196 | lua_pushfstring(L, "%s: %p", DBD_MYSQL_CONNECTION, conn); |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
197 | |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
198 | return 1; |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
199 | } |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
200 | |
2 | 201 | int dbd_mysql_connection(lua_State *L) { |
202 | static const luaL_Reg connection_methods[] = { | |
13
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
203 | {"autocommit", connection_autocommit}, |
2 | 204 | {"close", connection_close}, |
13
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
205 | {"commit", connection_commit}, |
2 | 206 | {"ping", connection_ping}, |
207 | {"prepare", connection_prepare}, | |
22 | 208 | {"quote", connection_quote}, |
13
10c8c6f0da14
Added connection:autocommit(), connection:commit(), and connection:rollback() to control transactions.
nrich@ii.net
parents:
4
diff
changeset
|
209 | {"rollback", connection_rollback}, |
2 | 210 | {NULL, NULL} |
211 | }; | |
1 | 212 | |
2 | 213 | static const luaL_Reg connection_class_methods[] = { |
214 | {"New", connection_new}, | |
215 | {NULL, NULL} | |
216 | }; | |
1 | 217 | |
218 | luaL_newmetatable(L, DBD_MYSQL_CONNECTION); | |
219 | luaL_register(L, 0, connection_methods); | |
220 | lua_pushvalue(L,-1); | |
221 | lua_setfield(L, -2, "__index"); | |
222 | ||
223 | lua_pushcfunction(L, connection_gc); | |
224 | lua_setfield(L, -2, "__gc"); | |
225 | ||
32
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
226 | lua_pushcfunction(L, connection_tostring); |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
227 | lua_setfield(L, -2, "__tostring"); |
03ed0ca09837
Add __tostring method to connection and statement objects.
nrich@ii.net
parents:
23
diff
changeset
|
228 | |
1 | 229 | luaL_register(L, DBD_MYSQL_CONNECTION, connection_class_methods); |
230 | ||
231 | return 1; | |
232 | } | |
233 |