Make dbh:connected accessible from Lua - thanks Grmt

This commit is contained in:
Leon de Rooij 2011-01-03 12:20:06 +01:00
parent 45a32dda3d
commit 09e6fd3f4e
4 changed files with 40 additions and 7 deletions

View File

@ -82,12 +82,13 @@ class Session : public CoreSession {
class Dbh {
private:
switch_cache_db_handle_t *dbh;
bool connected;
bool m_connected;
static int query_callback(void *pArg, int argc, char **argv, char **cargv);
public:
Dbh(char *dsn, char *user = NULL, char *pass = NULL);
~Dbh();
bool release();
bool connected();
bool query(char *sql, SWIGLUA_FN lua_fun);
};

View File

@ -318,9 +318,9 @@ Dbh::Dbh(char *dsn, char *user, char *pass)
options.odbc_options.pass = pass;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) == SWITCH_STATUS_SUCCESS) {
connected = true;
m_connected = true;
} else {
connected = false;
m_connected = false;
}
}
@ -331,14 +331,19 @@ Dbh::~Dbh()
bool Dbh::release()
{
if (connected) {
if (m_connected) {
switch_cache_db_release_db_handle(&dbh);
connected = false;
m_connected = false;
return true;
}
return false;
}
bool Dbh::connected()
{
return m_connected;
}
int Dbh::query_callback(void *pArg, int argc, char **argv, char **cargv)
{
SWIGLUA_FN *lua_fun = (SWIGLUA_FN *)pArg;
@ -366,7 +371,7 @@ int Dbh::query_callback(void *pArg, int argc, char **argv, char **cargv)
bool Dbh::query(char *sql, SWIGLUA_FN lua_fun)
{
if (connected) {
if (m_connected) {
if (lua_fun.L) {
if (switch_cache_db_execute_sql_callback(dbh, sql, query_callback, &lua_fun, NULL) == SWITCH_STATUS_SUCCESS) {
return true;

View File

@ -55,12 +55,13 @@ namespace LUA {
class Dbh {
protected:
switch_cache_db_handle_t *dbh;
bool connected;
bool m_connected;
static int query_callback(void *pArg, int argc, char **argv, char **cargv);
public:
Dbh(char *dsn, char *user = NULL, char *pass = NULL);
~Dbh();
bool release();
bool connected();
bool query(char *sql, SWIGLUA_FN lua_fun);
};
}

View File

@ -7229,6 +7229,31 @@ fail:
}
static int _wrap_Dbh_connected(lua_State* L) {
int SWIG_arg = -1;
LUA::Dbh *arg1 = (LUA::Dbh *) 0 ;
bool result;
SWIG_check_num_args("connected",1,1)
if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("connected",1,"LUA::Dbh *");
if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_LUA__Dbh,0))){
SWIG_fail_ptr("Dbh_connected",1,SWIGTYPE_p_LUA__Dbh);
}
result = (bool)(arg1)->connected();
SWIG_arg=0;
lua_pushboolean(L,(int)(result==true)); SWIG_arg++;
return SWIG_arg;
if(0) SWIG_fail;
fail:
lua_error(L);
return SWIG_arg;
}
static int _wrap_Dbh_query(lua_State* L) {
int SWIG_arg = -1;
LUA::Dbh *arg1 = (LUA::Dbh *) 0 ;
@ -7277,6 +7302,7 @@ delete arg1;
}
static swig_lua_method swig_LUA_Dbh_methods[] = {
{"release", _wrap_Dbh_release},
{"connected", _wrap_Dbh_connected},
{"query", _wrap_Dbh_query},
{0,0}
};