]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add close method needs testing
authorAnthony Minessale <anthony.minessale@gmail.com>
Fri, 10 Aug 2007 14:27:25 +0000 (14:27 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Fri, 10 Aug 2007 14:27:25 +0000 (14:27 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5583 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/mod/languages/mod_spidermonkey_core_db/mod_spidermonkey_core_db.c

index b5f4e3368cb66d3386b648dfd9b38e71a8d6406c..aba56b428a9ba9319ede2f30a2c8d5a9f5cfba30 100644 (file)
@@ -73,17 +73,33 @@ static JSBool db_construct(JSContext * cx, JSObject * obj, uintN argc, jsval * a
        return JS_FALSE;
 }
 
-static void db_destroy(JSContext * cx, JSObject * obj)
+static JSBool db_close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 {
        struct db_obj *dbo = JS_GetPrivate(cx, obj);
-
+       
        if (dbo) {
-               switch_memory_pool_t *pool = dbo->pool;
                if (dbo->stmt) {
                        switch_core_db_finalize(dbo->stmt);
                        dbo->stmt = NULL;
                }
-               switch_core_db_close(dbo->db);
+               if (dbo->db) {
+                       switch_core_db_close(dbo->db);
+                       dbo->db = NULL;
+               }
+       }
+
+       return JS_TRUE;
+}
+
+static void db_destroy(JSContext *cx, JSObject *obj)
+{
+       struct db_obj *dbo = JS_GetPrivate(cx, obj);
+
+       if (dbo) {
+               switch_memory_pool_t *pool = dbo->pool;
+               jsval rval = JS_TRUE;
+
+               db_close(cx, obj, 0, NULL, &rval);
                switch_core_destroy_memory_pool(&pool);
                pool = NULL;
        }
@@ -119,6 +135,10 @@ static JSBool db_exec(JSContext * cx, JSObject * obj, uintN argc, jsval * argv,
        struct db_obj *dbo = JS_GetPrivate(cx, obj);
        *rval = INT_TO_JSVAL(0);
 
+       if (!dbo->db) {
+               return JS_FALSE;
+       }
+
        if (argc > 0) {
                char *sql = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
                char *err = NULL;
@@ -153,6 +173,10 @@ static JSBool db_next(JSContext * cx, JSObject * obj, uintN argc, jsval * argv,
        struct db_obj *dbo = JS_GetPrivate(cx, obj);
        *rval = BOOLEAN_TO_JSVAL(JS_FALSE);
 
+       if (!dbo->db) {
+               return JS_FALSE;
+       }
+
        if (dbo->stmt) {
                int running = 1;
                while (running < 5000) {
@@ -180,6 +204,10 @@ static JSBool db_fetch(JSContext * cx, JSObject * obj, uintN argc, jsval * argv,
        char code[1024];
        int x;
 
+       if (!dbo->db) {
+               return JS_FALSE;
+       }
+
        snprintf(code, sizeof(code), "~var _dB_RoW_DaTa_ = {}");
        eval_some_js(code, dbo->cx, dbo->obj, rval);
        if (*rval == JS_FALSE) {
@@ -207,6 +235,10 @@ static JSBool db_prepare(JSContext * cx, JSObject * obj, uintN argc, jsval * arg
 
        *rval = BOOLEAN_TO_JSVAL(JS_FALSE);
 
+       if (!dbo->db) {
+               return JS_FALSE;
+       }
+
        if (dbo->stmt) {
                switch_core_db_finalize(dbo->stmt);
                dbo->stmt = NULL;
@@ -229,6 +261,7 @@ enum db_tinyid {
 
 static JSFunctionSpec db_methods[] = {
        {"exec", db_exec, 1},
+       {"close", db_close, 0},
        {"next", db_next, 0},
        {"fetch", db_fetch, 1},
        {"prepare", db_prepare, 0},