[libgda] GdaWebPStmt: ported to G_DECLARE/G_DEFINE



commit dfb5c61fe377f6eb618a43ff09cf37c48779d135
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Apr 5 15:00:47 2019 -0600

    GdaWebPStmt: ported to G_DECLARE/G_DEFINE

 providers/web/gda-web-blob-op.c  |   1 +
 providers/web/gda-web-blob-op.h  |   1 +
 providers/web/gda-web-provider.c |   6 +-
 providers/web/gda-web-provider.h |   1 +
 providers/web/gda-web-pstmt.c    | 158 ++++++++++++++++++++-------------------
 providers/web/gda-web-pstmt.h    |  21 ++----
 6 files changed, 94 insertions(+), 94 deletions(-)
---
diff --git a/providers/web/gda-web-blob-op.c b/providers/web/gda-web-blob-op.c
index ca7664eef..b91a437b3 100644
--- a/providers/web/gda-web-blob-op.c
+++ b/providers/web/gda-web-blob-op.c
@@ -2,6 +2,7 @@
  * Copyright (C) 2008 - 2014 Vivien Malerba <malerba gnome-db org>
  * Copyright (C) 2010 David King <davidk openismus com>
  * Copyright (C) 2011 Murray Cumming <murrayc murrayc com>
+ * Copyright (C) 2019 Daniel Espinosa <malerba gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
diff --git a/providers/web/gda-web-blob-op.h b/providers/web/gda-web-blob-op.h
index 66a227fd1..87b544573 100644
--- a/providers/web/gda-web-blob-op.h
+++ b/providers/web/gda-web-blob-op.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2008 - 2012 Vivien Malerba <malerba gnome-db org>
+ * Copyright (C) 2019 Daniel Espinosa <malerba gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
diff --git a/providers/web/gda-web-provider.c b/providers/web/gda-web-provider.c
index 759fd44f6..cf8d14dc3 100644
--- a/providers/web/gda-web-provider.c
+++ b/providers/web/gda-web-provider.c
@@ -2,6 +2,7 @@
  * Copyright (C) 2009 - 2015 Vivien Malerba <malerba gnome-db org>
  * Copyright (C) 2010 David King <davidk openismus com>
  * Copyright (C) 2011 - 2014 Murray Cumming <murrayc murrayc com>
+ * Copyright (C) 2019 Daniel Espinosa <malerba gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -1403,7 +1404,7 @@ gda_web_provider_statement_execute (GdaServerProvider *provider, GdaConnection *
                    ! g_ascii_strncasecmp (gda_pstmt_get_sql (_GDA_PSTMT (ps)), "describe", 8))
                        xmlSetProp (node, BAD_CAST "type", BAD_CAST "SELECT");
        }
-       xmlNewChild (cmdnode, NULL, BAD_CAST "preparehash", BAD_CAST (ps->pstmt_hash));
+       xmlNewChild (cmdnode, NULL, BAD_CAST "preparehash", BAD_CAST (gda_web_pstmt_get_pstmt_hash (ps)));
 
        /* bind statement's parameters */
        GSList *list;
@@ -1676,8 +1677,7 @@ gda_web_provider_statement_execute (GdaServerProvider *provider, GdaConnection *
                else if (!strcmp ((gchar*) node->name, "preparehash")) {
                        xmlChar *contents;
                        contents = xmlNodeGetContent (node);
-                       g_free (ps->pstmt_hash);
-                       ps->pstmt_hash = g_strdup ((gchar*) contents);
+                       gda_web_pstmt_set_pstmt_hash (ps, (gchar*) contents);
                        xmlFree (contents);
                }
        }
diff --git a/providers/web/gda-web-provider.h b/providers/web/gda-web-provider.h
index d098280ca..3b11aa051 100644
--- a/providers/web/gda-web-provider.h
+++ b/providers/web/gda-web-provider.h
@@ -4,6 +4,7 @@
  * Copyright (C) 2002 Tim Coleman <tim timcoleman com>
  * Copyright (C) 2003 Steve Fosdick <fozzy src gnome org>
  * Copyright (C) 2007 - 2012 Vivien Malerba <malerba gnome-db org>
+ * Copyright (C) 2019 Daniel Espinosa <malerba gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
diff --git a/providers/web/gda-web-pstmt.c b/providers/web/gda-web-pstmt.c
index e31052227..fa5e16baa 100644
--- a/providers/web/gda-web-pstmt.c
+++ b/providers/web/gda-web-pstmt.c
@@ -25,108 +25,96 @@
 #include "gda-web-util.h"
 
 static void gda_web_pstmt_class_init (GdaWebPStmtClass *klass);
-static void gda_web_pstmt_init       (GdaWebPStmt *pstmt, GdaWebPStmtClass *klass);
+static void gda_web_pstmt_init       (GdaWebPStmt *pstmt);
+static void gda_web_pstmt_dispose    (GObject *object);
 static void gda_web_pstmt_finalize    (GObject *object);
 
-static GObjectClass *parent_class = NULL;
+typedef struct {
+       GWeakRef        cnc;
+       gchar          *pstmt_hash;
+} GdaWebPStmtPrivate;
 
-/**
- * gda_web_pstmt_get_type
- *
- * Returns: the #GType of GdaWebPStmt.
- */
-GType
-gda_web_pstmt_get_type (void)
-{
-       static GType type = 0;
-
-       if (G_UNLIKELY (type == 0)) {
-               static GMutex registering;
-               static const GTypeInfo info = {
-                       sizeof (GdaWebPStmtClass),
-                       (GBaseInitFunc) NULL,
-                       (GBaseFinalizeFunc) NULL,
-                       (GClassInitFunc) gda_web_pstmt_class_init,
-                       NULL,
-                       NULL,
-                       sizeof (GdaWebPStmt),
-                       0,
-                       (GInstanceInitFunc) gda_web_pstmt_init,
-                       0
-               };
-
-               g_mutex_lock (&registering);
-               if (type == 0)
-                       type = g_type_register_static (GDA_TYPE_PSTMT, "GdaWebPStmt", &info, 0);
-               g_mutex_unlock (&registering);
-       }
-       return type;
-}
+G_DEFINE_TYPE_WITH_PRIVATE (GdaWebPStmt, gda_web_pstmt, GDA_TYPE_PSTMT)
 
 static void 
 gda_web_pstmt_class_init (GdaWebPStmtClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
-       parent_class = g_type_class_peek_parent (klass);
-
        /* virtual functions */
+       object_class->dispose = gda_web_pstmt_dispose;
        object_class->finalize = gda_web_pstmt_finalize;
 }
 
 static void
-gda_web_pstmt_init (GdaWebPStmt *pstmt, G_GNUC_UNUSED GdaWebPStmtClass *klass)
+gda_web_pstmt_init (GdaWebPStmt *pstmt)
 {
        g_return_if_fail (GDA_IS_PSTMT (pstmt));
-       
-       pstmt->pstmt_hash = NULL;
+  GdaWebPStmtPrivate *priv = gda_web_pstmt_get_instance_private (pstmt);
+  g_weak_ref_init (&priv->cnc, NULL);
+       priv->pstmt_hash = NULL;
 }
 
 static void
-gda_web_pstmt_finalize (GObject *object)
+gda_web_pstmt_dispose (GObject *object)
 {
        GdaWebPStmt *pstmt = (GdaWebPStmt *) object;
 
        g_return_if_fail (GDA_IS_PSTMT (pstmt));
+  GdaWebPStmtPrivate *priv = gda_web_pstmt_get_instance_private (pstmt);
 
-       if (pstmt->pstmt_hash) {
+       if (priv->pstmt_hash) {
+    GdaConnection *cnc = NULL;
                WebConnectionData *cdata;
-               cdata = (WebConnectionData*) gda_connection_internal_get_provider_data_error (pstmt->cnc, 
NULL);
-               if (!cdata) 
-                       goto next;
-
-               /* send command to deallocate prepared statement */
-               xmlDocPtr doc;
-               xmlNodePtr root, cmdnode;
-               gchar *token;
-               doc = xmlNewDoc (BAD_CAST "1.0");
-               root = xmlNewNode (NULL, BAD_CAST "request");
-               xmlDocSetRootElement (doc, root);
-               token = _gda_web_compute_token (cdata);
-               xmlNewChild (root, NULL, BAD_CAST "token", BAD_CAST token);
-               g_free (token);
-               cmdnode = xmlNewChild (root, NULL, BAD_CAST "cmd", BAD_CAST "UNPREPARE");
-               xmlNewChild (cmdnode, NULL, BAD_CAST "preparehash", BAD_CAST pstmt->pstmt_hash);
-
-               xmlChar *cmde;
-               xmlDocPtr replydoc;
-               int size;
-               gchar status;
-               
-               xmlDocDumpMemory (doc, &cmde, &size);
-               xmlFreeDoc (doc);
-               replydoc = _gda_web_send_message_to_frontend (pstmt->cnc, cdata, MESSAGE_UNPREPARE, (gchar*) 
cmde,
-                                                             cdata->key, &status);
-               xmlFree (cmde);         
-               if (replydoc)
-                       xmlFreeDoc (replydoc);
+    cnc = g_weak_ref_get (&priv->cnc);
+    if (cnc != NULL) {
+                 cdata = (WebConnectionData*) gda_connection_internal_get_provider_data_error (cnc, NULL);
+                 if (!cdata)
+                         goto next;
+
+                 /* send command to deallocate prepared statement */
+                 xmlDocPtr doc;
+                 xmlNodePtr root, cmdnode;
+                 gchar *token;
+                 doc = xmlNewDoc (BAD_CAST "1.0");
+                 root = xmlNewNode (NULL, BAD_CAST "request");
+                 xmlDocSetRootElement (doc, root);
+                 token = _gda_web_compute_token (cdata);
+                 xmlNewChild (root, NULL, BAD_CAST "token", BAD_CAST token);
+                 g_free (token);
+                 cmdnode = xmlNewChild (root, NULL, BAD_CAST "cmd", BAD_CAST "UNPREPARE");
+                 xmlNewChild (cmdnode, NULL, BAD_CAST "preparehash", BAD_CAST priv->pstmt_hash);
+
+                 xmlChar *cmde;
+                 xmlDocPtr replydoc;
+                 int size;
+                 gchar status;
+
+                 xmlDocDumpMemory (doc, &cmde, &size);
+                 xmlFreeDoc (doc);
+                 replydoc = _gda_web_send_message_to_frontend (cnc, cdata, MESSAGE_UNPREPARE, (gchar*) cmde,
+                                                               cdata->key, &status);
+                 xmlFree (cmde);
+                 if (replydoc)
+                         xmlFreeDoc (replydoc);
+    }
                
        next:
                /* free memory */
-               g_free (pstmt->pstmt_hash);
+               g_free (priv->pstmt_hash);
        }
 
        /* chain to parent class */
-       parent_class->finalize (object);
+       G_OBJECT_CLASS (gda_web_pstmt_parent_class)->finalize (object);
+}
+
+static void
+gda_web_pstmt_finalize (GObject *object)
+{
+       GdaWebPStmt *pstmt = (GdaWebPStmt *) object;
+
+       g_return_if_fail (GDA_IS_PSTMT (pstmt));
+  GdaWebPStmtPrivate *priv = gda_web_pstmt_get_instance_private (pstmt);
+  g_weak_ref_clear (&priv->cnc);
 }
 
 GdaWebPStmt *
@@ -135,9 +123,27 @@ gda_web_pstmt_new (GdaConnection *cnc, const gchar *pstmt_hash)
        GdaWebPStmt *pstmt;
        g_return_val_if_fail (pstmt_hash && *pstmt_hash, NULL);
 
-        pstmt = (GdaWebPStmt *) g_object_new (GDA_TYPE_WEB_PSTMT, NULL);
-       pstmt->cnc = cnc;
-        pstmt->pstmt_hash = g_strdup (pstmt_hash);
+  pstmt = (GdaWebPStmt *) g_object_new (GDA_TYPE_WEB_PSTMT, NULL);
+  GdaWebPStmtPrivate *priv = gda_web_pstmt_get_instance_private (pstmt);
+       g_weak_ref_set (&priv->cnc, cnc);
+  priv->pstmt_hash = g_strdup (pstmt_hash);
 
-        return pstmt;
+  return pstmt;
+}
+
+gchar*
+gda_web_pstmt_get_pstmt_hash   (GdaWebPStmt *stmt)
+{
+  GdaWebPStmtPrivate *priv = gda_web_pstmt_get_instance_private (stmt);
+  return priv->pstmt_hash;
+}
+void
+gda_web_pstmt_set_pstmt_hash   (GdaWebPStmt *stmt, const gchar *pstmt_hash)
+{
+  GdaWebPStmtPrivate *priv = gda_web_pstmt_get_instance_private (stmt);
+  if (priv->pstmt_hash != NULL) {
+    g_free (priv->pstmt_hash);
+    priv->pstmt_hash = NULL;
+  }
+  priv->pstmt_hash = g_strdup (pstmt_hash);
 }
diff --git a/providers/web/gda-web-pstmt.h b/providers/web/gda-web-pstmt.h
index 4dd56d815..3ef07deaa 100644
--- a/providers/web/gda-web-pstmt.h
+++ b/providers/web/gda-web-pstmt.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2008 - 2012 Vivien Malerba <malerba gnome-db org>
+ * Copyright (C) 2019 Daniel Espinosa <malerba gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -27,27 +28,17 @@
 G_BEGIN_DECLS
 
 #define GDA_TYPE_WEB_PSTMT            (gda_web_pstmt_get_type())
-#define GDA_WEB_PSTMT(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GDA_TYPE_WEB_PSTMT, GdaWebPStmt))
-#define GDA_WEB_PSTMT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, GDA_TYPE_WEB_PSTMT, GdaWebPStmtClass))
-#define GDA_IS_WEB_PSTMT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE(obj, GDA_TYPE_WEB_PSTMT))
-#define GDA_IS_WEB_PSTMT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GDA_TYPE_WEB_PSTMT))
 
-typedef struct _GdaWebPStmt        GdaWebPStmt;
-typedef struct _GdaWebPStmtClass   GdaWebPStmtClass;
-
-struct _GdaWebPStmt {
-       GdaPStmt        object;
-
-       GdaConnection  *cnc;
-       gchar          *pstmt_hash;
-};
+G_DECLARE_DERIVABLE_TYPE(GdaWebPStmt, gda_web_pstmt, GDA, WEB_PSTMT, GdaPStmt)
 
 struct _GdaWebPStmtClass {
        GdaPStmtClass  parent_class;
 };
 
-GType         gda_web_pstmt_get_type  (void) G_GNUC_CONST;
-GdaWebPStmt  *gda_web_pstmt_new (GdaConnection *cnc, const gchar *pstmt_hash);
+GdaWebPStmt  *gda_web_pstmt_new              (GdaConnection *cnc, const gchar *pstmt_hash);
+gchar        *gda_web_pstmt_get_pstmt_hash   (GdaWebPStmt *stmt);
+void          gda_web_pstmt_set_pstmt_hash   (GdaWebPStmt *stmt, const gchar *pstmt_hash);
+void          gda_web_pstmt_free_pstmt_hash  (GdaWebPStmt *stmt);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]