|
1 #include "dbd_mysql.h" |
|
2 |
|
3 int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query); |
|
4 |
|
5 static int connection_new(lua_State *L) { |
|
6 int n = lua_gettop(L); |
|
7 |
|
8 connection_t *conn = NULL; |
|
9 |
|
10 const char *host = NULL; |
|
11 const char *user = NULL; |
|
12 const char *password = NULL; |
|
13 const char *db = NULL; |
|
14 int port = 0; |
|
15 |
|
16 const char *unix_socket = NULL; /* TODO always NULL */ |
|
17 int client_flag = 0; /* TODO always 0, set flags from options table */ |
|
18 |
|
19 /* db, user, password, host, port */ |
|
20 switch (n) { |
|
21 case 5: |
|
22 if (lua_isnil(L, 5) == 0) |
|
23 port = luaL_checkint(L, 5); |
|
24 case 4: |
|
25 if (lua_isnil(L, 4) == 0) |
|
26 host = luaL_checkstring(L, 4); |
|
27 case 3: |
|
28 if (lua_isnil(L, 3) == 0) |
|
29 password = luaL_checkstring(L, 3); |
|
30 case 2: |
|
31 if (lua_isnil(L, 2) == 0) |
|
32 user = luaL_checkstring(L, 2); |
|
33 case 1: |
|
34 if (lua_isnil(L, 1) == 0) |
|
35 db = luaL_checkstring(L, 1); |
|
36 } |
|
37 |
|
38 conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t)); |
|
39 |
|
40 conn->mysql = mysql_init(NULL); |
|
41 |
|
42 if (mysql_real_connect(conn->mysql, host, user, password, db, port, unix_socket, client_flag)) { |
|
43 luaL_getmetatable(L, DBD_MYSQL_CONNECTION); |
|
44 lua_setmetatable(L, -2); |
|
45 } else { |
|
46 luaL_error(L, "Failed to connect to database: %s", mysql_error(conn->mysql)); |
|
47 lua_pushnil(L); |
|
48 } |
|
49 |
|
50 return 1; |
|
51 } |
|
52 |
|
53 static int connection_close(lua_State *L) { |
|
54 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
|
55 int disconnect = 0; |
|
56 |
|
57 if (conn->mysql) { |
|
58 mysql_close(conn->mysql); |
|
59 disconnect = 1; |
|
60 } |
|
61 |
|
62 lua_pushboolean(L, disconnect); |
|
63 return 1; |
|
64 } |
|
65 |
|
66 static int connection_ping(lua_State *L) { |
|
67 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
|
68 int err = 1; |
|
69 |
|
70 if (conn->mysql) { |
|
71 err = mysql_ping(conn->mysql); |
|
72 } |
|
73 |
|
74 lua_pushboolean(L, !err); |
|
75 return 1; |
|
76 } |
|
77 |
|
78 static int connection_prepare(lua_State *L) { |
|
79 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
|
80 |
|
81 if (conn->mysql) { |
|
82 return dbd_mysql_statement_create(L, conn, luaL_checkstring(L, 2)); |
|
83 } |
|
84 |
|
85 lua_pushnil(L); |
|
86 return 1; |
|
87 } |
|
88 |
|
89 |
|
90 static int connection_gc(lua_State *L) { |
|
91 /* always close the connection */ |
|
92 connection_close(L); |
|
93 |
|
94 return 0; |
|
95 } |
|
96 |
|
97 static const luaL_Reg connection_methods[] = { |
|
98 {"close", connection_close}, |
|
99 {"ping", connection_ping}, |
|
100 {"prepare", connection_prepare}, |
|
101 {NULL, NULL} |
|
102 }; |
|
103 |
|
104 static const luaL_Reg connection_class_methods[] = { |
|
105 {"New", connection_new}, |
|
106 {NULL, NULL} |
|
107 }; |
|
108 |
|
109 int dbd_mysql_connection(lua_State *L) { |
|
110 luaL_newmetatable(L, DBD_MYSQL_CONNECTION); |
|
111 luaL_register(L, 0, connection_methods); |
|
112 lua_pushvalue(L,-1); |
|
113 lua_setfield(L, -2, "__index"); |
|
114 |
|
115 lua_pushcfunction(L, connection_gc); |
|
116 lua_setfield(L, -2, "__gc"); |
|
117 |
|
118 luaL_register(L, DBD_MYSQL_CONNECTION, connection_class_methods); |
|
119 |
|
120 return 1; |
|
121 } |
|
122 |