/mongo_connection.cpp

https://github.com/xiaogongzhu/luamongo · C++ · 133 lines · 89 code · 28 blank · 16 comment · 4 complexity · bcd6888abb24a8a67a961f48d589a0a1 MD5 · raw file

  1. #include <client/dbclient.h>
  2. extern "C" {
  3. #include <lua.h>
  4. #include <lauxlib.h>
  5. #include <lualib.h>
  6. #if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
  7. #include <compat-5.1.h>
  8. #endif
  9. };
  10. #include "common.h"
  11. using namespace mongo;
  12. extern const luaL_Reg dbclient_methods[];
  13. namespace {
  14. inline DBClientConnection* userdata_to_connection(lua_State* L, int index) {
  15. void *ud = luaL_checkudata(L, index, LUAMONGO_CONNECTION);
  16. DBClientConnection *connection = *((DBClientConnection **)ud);
  17. return connection;
  18. }
  19. } // anonymous namespace
  20. /*
  21. * db,err = mongo.Connection.New({})
  22. * accepts an optional table of features:
  23. * auto_reconnect (default = false)
  24. * rw_timeout (default = 0) (mongo >= v1.5)
  25. */
  26. static int connection_new(lua_State *L) {
  27. int resultcount = 1;
  28. try {
  29. bool auto_reconnect;
  30. int rw_timeout;
  31. if (lua_type(L,1) == LUA_TTABLE) {
  32. // extract arguments from table
  33. lua_getfield(L, 1, "auto_reconnect");
  34. auto_reconnect = lua_toboolean(L, -1);
  35. lua_getfield(L, 1, "rw_timeout");
  36. int rw_timeout = lua_tointeger(L, -1);
  37. lua_pop(L, 2);
  38. } else {
  39. auto_reconnect = false;
  40. rw_timeout = 0;
  41. }
  42. DBClientConnection **connection = (DBClientConnection **)lua_newuserdata(L, sizeof(DBClientConnection *));
  43. *connection = new DBClientConnection(auto_reconnect, 0, rw_timeout);
  44. luaL_getmetatable(L, LUAMONGO_CONNECTION);
  45. lua_setmetatable(L, -2);
  46. } catch (std::exception &e) {
  47. lua_pushnil(L);
  48. lua_pushfstring(L, LUAMONGO_ERR_CONNECTION_FAILED, e.what());
  49. resultcount = 2;
  50. }
  51. return resultcount;
  52. }
  53. /*
  54. * ok,err = connection:connect(connection_str)
  55. */
  56. static int connection_connect(lua_State *L) {
  57. DBClientConnection *connection = userdata_to_connection(L, 1);
  58. const char *connectstr = luaL_checkstring(L, 2);
  59. try {
  60. connection->connect(connectstr);
  61. } catch (std::exception &e) {
  62. lua_pushnil(L);
  63. lua_pushfstring(L, LUAMONGO_ERR_CONNECT_FAILED, connectstr, e.what());
  64. return 2;
  65. }
  66. lua_pushboolean(L, 1);
  67. return 1;
  68. }
  69. /*
  70. * __gc
  71. */
  72. static int connection_gc(lua_State *L) {
  73. DBClientConnection *connection = userdata_to_connection(L, 1);
  74. delete connection;
  75. return 0;
  76. }
  77. /*
  78. * __tostring
  79. */
  80. static int connection_tostring(lua_State *L) {
  81. DBClientConnection *connection = userdata_to_connection(L, 1);
  82. lua_pushfstring(L, "%s: %s", LUAMONGO_CONNECTION, connection->toString().c_str());
  83. return 1;
  84. }
  85. int mongo_connection_register(lua_State *L) {
  86. static const luaL_Reg connection_methods[] = {
  87. {"connect", connection_connect},
  88. {NULL, NULL}
  89. };
  90. static const luaL_Reg connection_class_methods[] = {
  91. {"New", connection_new},
  92. {NULL, NULL}
  93. };
  94. luaL_newmetatable(L, LUAMONGO_CONNECTION);
  95. luaL_register(L, NULL, dbclient_methods);
  96. luaL_register(L, NULL, connection_methods);
  97. lua_pushvalue(L,-1);
  98. lua_setfield(L, -2, "__index");
  99. lua_pushcfunction(L, connection_gc);
  100. lua_setfield(L, -2, "__gc");
  101. lua_pushcfunction(L, connection_tostring);
  102. lua_setfield(L, -2, "__tostring");
  103. luaL_register(L, LUAMONGO_CONNECTION, connection_class_methods);
  104. return 1;
  105. }