Skip to content

Instantly share code, notes, and snippets.

@neomantra
Created December 30, 2011 18:00
Show Gist options
  • Save neomantra/1540828 to your computer and use it in GitHub Desktop.
Save neomantra/1540828 to your computer and use it in GitHub Desktop.
luamongo run_command
/*
* res,err = db:run_command(dbname, lua_table or json_str, options)
*/
static int dbclient_run_command(lua_State *L) {
DBClientBase *dbclient = userdata_to_dbclient(L, 1);
const char *ns = luaL_checkstring(L, 2);
int options = luaL_tointeger(L, 4); // if it is invalid it returns 0
BSONObj command; // arg 3
try {
int type = lua_type(L, 3);
if (type == LUA_TSTRING) {
const char *jsonstr = luaL_checkstring(L, 3);
command = fromjson(jsonstr);
} else if (type == LUA_TTABLE) {
lua_to_bson(L, 3, command);
} else {
throw(LUAMONGO_REQUIRES_JSON_OR_TABLE);
}
BSONObj retval;
bool success = dbclient->runCommand(ns, command, retval, options);
if ( !success )
throw "run_command failed";
bson_to_lua(L, retval );
return 1;
} catch (std::exception &e) {
lua_pushboolean(L, 0);
lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_CONNECTION,
"run_command", e.what());
return 2;
} catch (const char *err) {
lua_pushboolean(L, 0);
lua_pushstring(L, err);
return 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment