1 #include "dbd_mysql.h" |
1 #include "dbd_mysql.h" |
2 |
2 |
3 int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query); |
3 int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query); |
4 |
4 |
|
5 /* |
|
6 * connection = DBD.MySQl.New(dbname, user, password, host, port) |
|
7 */ |
5 static int connection_new(lua_State *L) { |
8 static int connection_new(lua_State *L) { |
6 int n = lua_gettop(L); |
9 int n = lua_gettop(L); |
7 |
10 |
8 connection_t *conn = NULL; |
11 connection_t *conn = NULL; |
9 |
12 |
61 |
67 |
62 lua_pushboolean(L, disconnect); |
68 lua_pushboolean(L, disconnect); |
63 return 1; |
69 return 1; |
64 } |
70 } |
65 |
71 |
|
72 /* |
|
73 * ok = connection:ping() |
|
74 */ |
66 static int connection_ping(lua_State *L) { |
75 static int connection_ping(lua_State *L) { |
67 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
76 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
68 int err = 1; |
77 int err = 1; |
69 |
78 |
70 if (conn->mysql) { |
79 if (conn->mysql) { |
73 |
82 |
74 lua_pushboolean(L, !err); |
83 lua_pushboolean(L, !err); |
75 return 1; |
84 return 1; |
76 } |
85 } |
77 |
86 |
|
87 /* |
|
88 * statement = connection:prepare(sql_string) |
|
89 */ |
78 static int connection_prepare(lua_State *L) { |
90 static int connection_prepare(lua_State *L) { |
79 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
91 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION); |
80 |
92 |
81 if (conn->mysql) { |
93 if (conn->mysql) { |
82 return dbd_mysql_statement_create(L, conn, luaL_checkstring(L, 2)); |
94 return dbd_mysql_statement_create(L, conn, luaL_checkstring(L, 2)); |
84 |
96 |
85 lua_pushnil(L); |
97 lua_pushnil(L); |
86 return 1; |
98 return 1; |
87 } |
99 } |
88 |
100 |
89 |
101 /* |
|
102 * __gc |
|
103 */ |
90 static int connection_gc(lua_State *L) { |
104 static int connection_gc(lua_State *L) { |
91 /* always close the connection */ |
105 /* always close the connection */ |
92 connection_close(L); |
106 connection_close(L); |
93 |
107 |
94 return 0; |
108 return 0; |
95 } |
109 } |
96 |
110 |
97 static const luaL_Reg connection_methods[] = { |
111 int dbd_mysql_connection(lua_State *L) { |
98 {"close", connection_close}, |
112 static const luaL_Reg connection_methods[] = { |
99 {"ping", connection_ping}, |
113 {"close", connection_close}, |
100 {"prepare", connection_prepare}, |
114 {"ping", connection_ping}, |
101 {NULL, NULL} |
115 {"prepare", connection_prepare}, |
102 }; |
116 {NULL, NULL} |
|
117 }; |
103 |
118 |
104 static const luaL_Reg connection_class_methods[] = { |
119 static const luaL_Reg connection_class_methods[] = { |
105 {"New", connection_new}, |
120 {"New", connection_new}, |
106 {NULL, NULL} |
121 {NULL, NULL} |
107 }; |
122 }; |
108 |
123 |
109 int dbd_mysql_connection(lua_State *L) { |
|
110 luaL_newmetatable(L, DBD_MYSQL_CONNECTION); |
124 luaL_newmetatable(L, DBD_MYSQL_CONNECTION); |
111 luaL_register(L, 0, connection_methods); |
125 luaL_register(L, 0, connection_methods); |
112 lua_pushvalue(L,-1); |
126 lua_pushvalue(L,-1); |
113 lua_setfield(L, -2, "__index"); |
127 lua_setfield(L, -2, "__index"); |
114 |
128 |