dbd/mysql/connection.c

changeset 13
10c8c6f0da14
parent 4
c50b0e6f25d6
child 14
98192b7d4e89
equal deleted inserted replaced
12:014ba3ab3903 13:10c8c6f0da14
46 lua_pushnil(L); 46 lua_pushnil(L);
47 lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, mysql_error(conn->mysql)); 47 lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, mysql_error(conn->mysql));
48 return 2; 48 return 2;
49 } 49 }
50 50
51 /*
52 * by default turn off autocommit
53 */
54 mysql_autocommit(conn->mysql, 0);
55
51 luaL_getmetatable(L, DBD_MYSQL_CONNECTION); 56 luaL_getmetatable(L, DBD_MYSQL_CONNECTION);
52 lua_setmetatable(L, -2); 57 lua_setmetatable(L, -2);
53 58
59 return 1;
60 }
61
62 /*
63 * success = connection:autocommit(on)
64 */
65 static int connection_autocommit(lua_State *L) {
66 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION);
67 int on = lua_toboolean(L, 2);
68 int err = 0;
69
70 if (conn->mysql) {
71 err = mysql_autocommit(conn->mysql, on);
72 }
73
74 lua_pushboolean(L, !err);
54 return 1; 75 return 1;
55 } 76 }
56 77
57 /* 78 /*
58 * success = connection:close() 79 * success = connection:close()
66 disconnect = 1; 87 disconnect = 1;
67 conn->mysql = NULL; 88 conn->mysql = NULL;
68 } 89 }
69 90
70 lua_pushboolean(L, disconnect); 91 lua_pushboolean(L, disconnect);
92 return 1;
93 }
94
95 /*
96 * success = connection:commit()
97 */
98 static int connection_commit(lua_State *L) {
99 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION);
100 int err = 0;
101
102 if (conn->mysql) {
103 err = mysql_commit(conn->mysql);
104 }
105
106 lua_pushboolean(L, !err);
71 return 1; 107 return 1;
72 } 108 }
73 109
74 /* 110 /*
75 * ok = connection:ping() 111 * ok = connection:ping()
101 137
102 return 2; 138 return 2;
103 } 139 }
104 140
105 /* 141 /*
142 * success = connection:rollback()
143 */
144 static int connection_rollback(lua_State *L) {
145 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION);
146 int err = 0;
147
148 if (conn->mysql) {
149 err = mysql_rollback(conn->mysql);
150 }
151
152 lua_pushboolean(L, !err);
153 return 1;
154 }
155
156 /*
106 * __gc 157 * __gc
107 */ 158 */
108 static int connection_gc(lua_State *L) { 159 static int connection_gc(lua_State *L) {
109 /* always close the connection */ 160 /* always close the connection */
110 connection_close(L); 161 connection_close(L);
112 return 0; 163 return 0;
113 } 164 }
114 165
115 int dbd_mysql_connection(lua_State *L) { 166 int dbd_mysql_connection(lua_State *L) {
116 static const luaL_Reg connection_methods[] = { 167 static const luaL_Reg connection_methods[] = {
168 {"autocommit", connection_autocommit},
117 {"close", connection_close}, 169 {"close", connection_close},
170 {"commit", connection_commit},
118 {"ping", connection_ping}, 171 {"ping", connection_ping},
119 {"prepare", connection_prepare}, 172 {"prepare", connection_prepare},
173 {"rollback", connection_rollback},
120 {NULL, NULL} 174 {NULL, NULL}
121 }; 175 };
122 176
123 static const luaL_Reg connection_class_methods[] = { 177 static const luaL_Reg connection_class_methods[] = {
124 {"New", connection_new}, 178 {"New", connection_new},

mercurial