-
Notifications
You must be signed in to change notification settings - Fork 58
/
mongo_connection.cpp
419 lines (360 loc) · 11.7 KB
/
mongo_connection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <iostream>
#include <client/dbclient.h>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
#include <compat-5.1.h>
#endif
};
#include "utils.h"
#include "common.h"
using namespace mongo;
extern int cursor_create(lua_State *L, DBClientConnection *connection, const char *ns, const Query &query);
extern void lua_to_bson(lua_State *L, int stackpos, BSONObj &obj);
extern void bson_to_lua(lua_State *L, const BSONObj &obj);
namespace {
inline DBClientConnection* userdata_to_connection(lua_State* L, int index) {
void *ud = 0;
ud = luaL_checkudata(L, index, LUAMONGO_CONNECTION);
DBClientConnection *connection = *((DBClientConnection **)ud);
return connection;
}
} // anonymous namespace
/*
* db,err = mongo.Connection.New({})
* accepts an optional table of features:
* auto_reconnect (default = false)
* rw_timeout (default = 0) (mongo >= v1.5)
*/
static int connection_new(lua_State *L) {
int resultcount = 1;
try {
bool auto_reconnect;
int rw_timeout;
if (lua_type(L,1) == LUA_TTABLE) {
// extract arguments from table
lua_getfield(L, 1, "auto_reconnect");
auto_reconnect = lua_toboolean(L, -1);
lua_getfield(L, 1, "rw_timeout");
int rw_timeout = lua_tointeger(L, -1);
lua_pop(L, 2);
} else {
auto_reconnect = false;
rw_timeout = 0;
}
DBClientConnection **connection = (DBClientConnection **)lua_newuserdata(L, sizeof(DBClientConnection *));
#if defined(MONGO_1_5)
*connection = new DBClientConnection(auto_reconnect, 0, rw_timeout);
#else
*connection = new DBClientConnection(auto_reconnect, 0);
#endif // defined(MONGO_1_5)
luaL_getmetatable(L, LUAMONGO_CONNECTION);
lua_setmetatable(L, -2);
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_CONNECTION_FAILED, e.what());
resultcount = 2;
}
return resultcount;
}
/*
* ok,err = db:connect(connection_str)
*/
static int connection_connect(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *connectstr = luaL_checkstring(L, 2);
try {
connection->connect(connectstr);
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_CONNECT_FAILED, connectstr, e.what());
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
/*
* ok,err = db:auth({})
* accepts a table of parameters:
* dbname database to authenticate (required)
* username username to authenticate against (required)
* password password to authenticate against (required)
* digestPassword set to true if password is pre-digested (default = true)
*
*/
static int connection_auth(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
luaL_checktype(L, 2, LUA_TTABLE);
lua_getfield(L, 2, "dbname");
const char *dbname = luaL_checkstring(L, -1);
lua_getfield(L, 2, "username");
const char *username = luaL_checkstring(L, -1);
lua_getfield(L, 2, "password");
const char *password = luaL_checkstring(L, -1);
lua_getfield(L, 2, "digestPassword");
bool digestPassword = lua_isnil(L, -1) ? true : lua_toboolean(L, -1);
lua_pop(L, 4);
std::string errmsg;
bool success = connection->auth(dbname, username, password, errmsg, digestPassword);
if (!success) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_CONNECTION_FAILED, errmsg.c_str());
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
/*
* is_failed = db:is_failed()
*/
static int connection_is_failed(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
bool is_failed = connection->isFailed();
lua_pushboolean(L, is_failed);
return 1;
}
/*
* addr = db:get_server_address()
*/
static int connection_get_server_address(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
std::string address = connection->getServerAddress();
lua_pushstring(L, address.c_str());
return 1;
}
/*
* count,err = db:count(ns)
*/
static int connection_count(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *ns = luaL_checkstring(L, 2);
int count = 0;
try {
count = connection->count(ns);
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_COUNT_FAILED, e.what());
return 2;
}
lua_pushinteger(L, count);
return 1;
}
/*
* ok,err = db:insert(ns, lua_table or json_str)
*/
static int connection_insert(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *ns = luaL_checkstring(L, 2);
try {
int type = lua_type(L, 3);
if (type == LUA_TSTRING) {
const char *jsonstr = luaL_checkstring(L, 3);
connection->insert(ns, fromjson(jsonstr));
} else if (type == LUA_TTABLE) {
BSONObj data;
lua_to_bson(L, 3, data);
connection->insert(ns, data);
} else {
throw(LUAMONGO_REQUIRES_JSON_OR_TABLE);
}
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_INSERT_FAILED, e.what());
return 2;
} catch (const char *err) {
lua_pushnil(L);
lua_pushstring(L, err);
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
/*
* cursor,err = db:query(ns, lua_table or json_str or query_obj)
* cursor,err = db:query(ns, lua_table or json_str or query_obj, lua_table or json_str)
* cursor,err = db:query(ns, lua_table or json_str or query_obj, fieldname, sort_ascending)
*/
static int connection_query(lua_State *L) {
int n = lua_gettop(L);
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *ns = luaL_checkstring(L, 2);
Query query;
if (n >= 3) {
try {
int type = lua_type(L, 3);
if (type == LUA_TSTRING) {
query = fromjson(luaL_checkstring(L, 3));
} else if (type == LUA_TTABLE) {
BSONObj obj;
lua_to_bson(L, 3, obj);
query = obj;
} else if (type == LUA_TUSERDATA) {
void *uq = 0;
uq = luaL_checkudata(L, 3, LUAMONGO_QUERY);
query = *(*((Query **)uq));
} else {
throw(LUAMONGO_REQUIRES_JSON_OR_TABLE);
}
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_QUERY_FAILED, e.what());
return 2;
} catch (const char *err) {
lua_pushnil(L);
lua_pushstring(L, err);
return 2;
}
}
if (n == 4) {
try {
int type = lua_type(L, 4);
if (type == LUA_TSTRING) {
query.sort(fromjson(luaL_checkstring(L, 4)));
} else if (type == LUA_TTABLE) {
BSONObj obj;
lua_to_bson(L, 4, obj);
query.sort(obj);
} else {
throw(LUAMONGO_REQUIRES_JSON_OR_TABLE);
}
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_QUERY_FAILED, e.what());
return 2;
} catch (const char *err) {
lua_pushnil(L);
lua_pushstring(L, err);
return 2;
}
} else if (n == 5) {
const char *field = luaL_checkstring(L, 4);
int asc = lua_toboolean(L, 5) ? 1 : -1;
try {
query.sort(field, asc);
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_QUERY_FAILED, e.what());
return 2;
}
}
return cursor_create(L, connection, ns, query);
}
/*
* ok,err = db:remove(ns, lua_table or json_str)
*/
static int connection_remove(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *ns = luaL_checkstring(L, 2);
try {
int type = lua_type(L, 3);
bool justOne = lua_toboolean(L, 4);
if (type == LUA_TSTRING) {
const char *jsonstr = luaL_checkstring(L, 3);
connection->remove(ns, fromjson(jsonstr), justOne);
} else if (type == LUA_TTABLE) {
BSONObj data;
lua_to_bson(L, 3, data);
connection->remove(ns, data, justOne);
} else {
throw(LUAMONGO_REQUIRES_JSON_OR_TABLE);
}
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_REMOVE_FAILED, e.what());
return 2;
} catch (const char *err) {
lua_pushnil(L);
lua_pushstring(L, err);
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
/*
* ok,err = db:update(ns, lua_table or json_str, lua_table or json_str, upsert, multi)
*/
static int connection_update(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *ns = luaL_checkstring(L, 2);
try {
int type_query = lua_type(L, 3);
int type_obj = lua_type(L, 4);
bool upsert = lua_toboolean(L, 5);
bool multi = lua_toboolean(L, 6);
BSONObj query;
BSONObj obj;
if (type_query == LUA_TSTRING) {
const char *jsonstr = luaL_checkstring(L, 3);
query = fromjson(jsonstr);
} else if (type_query == LUA_TTABLE) {
lua_to_bson(L, 3, query);
} else {
throw(LUAMONGO_REQUIRES_JSON_OR_TABLE);
}
if (type_obj == LUA_TSTRING) {
const char *jsonstr = luaL_checkstring(L, 4);
obj = fromjson(jsonstr);
} else if (type_obj == LUA_TTABLE) {
lua_to_bson(L, 4, obj);
} else {
throw(LUAMONGO_REQUIRES_JSON_OR_TABLE);
}
connection->update(ns, query, obj, upsert, multi);
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_UPDATE_FAILED, e.what());
return 2;
} catch (const char *err) {
lua_pushnil(L);
lua_pushstring(L, err);
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
/*
* __gc
*/
static int connection_gc(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
delete connection;
return 0;
}
/*
* __tostring
*/
static int connection_tostring(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
lua_pushfstring(L, "%s: %s", LUAMONGO_CONNECTION, connection->toString().c_str());
return 1;
}
int mongo_connection_register(lua_State *L) {
static const luaL_Reg connection_methods[] = {
{"connect", connection_connect},
{"auth", connection_auth},
{"is_failed", connection_is_failed},
{"get_server_address", connection_get_server_address},
{"count", connection_count},
{"insert", connection_insert},
{"query", connection_query},
{"remove", connection_remove},
{"update", connection_update},
{NULL, NULL}
};
static const luaL_Reg connection_class_methods[] = {
{"New", connection_new},
{NULL, NULL}
};
luaL_newmetatable(L, LUAMONGO_CONNECTION);
luaL_register(L, 0, connection_methods);
lua_pushvalue(L,-1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, connection_gc);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, connection_tostring);
lua_setfield(L, -2, "__tostring");
luaL_register(L, LUAMONGO_CONNECTION, connection_class_methods);
return 1;
}