[libgda] GdaPostgresPStmt: ported to G_DECLARE/G_DEFINE



commit 56bebb473cdb25a3b9b2af3dfcf0436e5442d98d
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Apr 5 12:12:45 2019 -0600

    GdaPostgresPStmt: ported to G_DECLARE/G_DEFINE

 providers/postgres/gda-postgres-provider.c |   8 +-
 providers/postgres/gda-postgres-pstmt.c    | 133 ++++++++++++++++-------------
 providers/postgres/gda-postgres-pstmt.h    |  27 ++----
 3 files changed, 83 insertions(+), 85 deletions(-)
---
diff --git a/providers/postgres/gda-postgres-provider.c b/providers/postgres/gda-postgres-provider.c
index 7fa065c0d..046bde131 100644
--- a/providers/postgres/gda-postgres-provider.c
+++ b/providers/postgres/gda-postgres-provider.c
@@ -1632,7 +1632,7 @@ gda_postgres_provider_statement_prepare (GdaServerProvider *provider, GdaConnect
         gda_pstmt_set_param_ids (_GDA_PSTMT (ps), param_ids);
         gda_pstmt_set_sql (_GDA_PSTMT (ps), sql);
        if (sql_can_cause_date_format_change (sql))
-               ps->date_format_change = TRUE;
+               gda_postgres_pstmt_set_date_format_change (ps, TRUE);
 
        gda_connection_add_prepared_statement (cnc, stmt, (GdaPStmt *) ps);
        g_object_unref (ps);
@@ -1691,7 +1691,7 @@ prepare_stmt_simple (PostgresConnectionData *cdata, const gchar *sql, GError **e
                gda_pstmt_set_param_ids (_GDA_PSTMT (ps), NULL);
                gda_pstmt_set_sql (_GDA_PSTMT (ps), sql);
                if (sql_can_cause_date_format_change (sql))
-                       ps->date_format_change = TRUE;
+                 gda_postgres_pstmt_set_date_format_change (ps, TRUE);
 
                return ps;
        }
@@ -2277,9 +2277,9 @@ gda_postgres_provider_statement_execute (GdaServerProvider *provider, GdaConnect
                date_format_change = sql_can_cause_date_format_change (esql);
        }
        else {
-               pg_res = PQexecPrepared (cdata->pconn, ps->prep_name, nb_params, (const char * const *) 
param_values,
+               pg_res = PQexecPrepared (cdata->pconn, gda_postgres_pstmt_get_prep_name (ps), nb_params, 
(const char * const *) param_values,
                                         param_lengths, param_formats, 0);
-               date_format_change = ps->date_format_change;
+               date_format_change = gda_postgres_pstmt_get_date_format_change (ps);
        }
 
        params_freev (param_values, param_mem, nb_params);
diff --git a/providers/postgres/gda-postgres-pstmt.c b/providers/postgres/gda-postgres-pstmt.c
index 468024b56..0f7c57108 100644
--- a/providers/postgres/gda-postgres-pstmt.c
+++ b/providers/postgres/gda-postgres-pstmt.c
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2008 - 2013 Vivien Malerba <malerba gnome-db org>
  * Copyright (C) 2010 David King <davidk openismus com>
+ * Copyright (C) 2019 Daniel Espinsa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -24,85 +25,75 @@
 #include "gda-postgres-util.h"
 
 static void gda_postgres_pstmt_class_init (GdaPostgresPStmtClass *klass);
-static void gda_postgres_pstmt_init       (GdaPostgresPStmt *pstmt, GdaPostgresPStmtClass *klass);
-static void gda_postgres_pstmt_finalize    (GObject *object);
-
-static GObjectClass *parent_class = NULL;
-
-/**
- * gda_postgres_pstmt_get_type
- *
- * Returns: the #GType of GdaPostgresPStmt.
- */
-GType
-gda_postgres_pstmt_get_type (void)
-{
-       static GType type = 0;
-
-       if (G_UNLIKELY (type == 0)) {
-               static GMutex registering;
-               static const GTypeInfo info = {
-                       sizeof (GdaPostgresPStmtClass),
-                       (GBaseInitFunc) NULL,
-                       (GBaseFinalizeFunc) NULL,
-                       (GClassInitFunc) gda_postgres_pstmt_class_init,
-                       NULL,
-                       NULL,
-                       sizeof (GdaPostgresPStmt),
-                       0,
-                       (GInstanceInitFunc) gda_postgres_pstmt_init,
-                       0
-               };
-
-               g_mutex_lock (&registering);
-               if (type == 0)
-                       type = g_type_register_static (GDA_TYPE_PSTMT, "GdaPostgresPStmt", &info, 0);
-               g_mutex_unlock (&registering);
-       }
-       return type;
-}
+static void gda_postgres_pstmt_init       (GdaPostgresPStmt *pstmt);
+static void gda_postgres_pstmt_dispose    (GObject *object);
+
+typedef struct {
+       /* PostgreSQL identifies a prepared statement by its name, which we'll keep here,
+        * along with a pointer to a GdaConnection and a PGconn because when the prepared
+        * statement is destroyed, we need to call "DEALLOCATE..."
+        */
+       GWeakRef        cnc;
+       PGconn         *pconn;
+       gchar          *prep_name;
+       gboolean        date_format_change; /* TRUE if this statement may incur a date format change */
+  gboolean        deallocated;
+} GdaPostgresPStmtPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (GdaPostgresPStmt, gda_postgres_pstmt, GDA_TYPE_PSTMT)
 
 static void 
 gda_postgres_pstmt_class_init (GdaPostgresPStmtClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
-       parent_class = g_type_class_peek_parent (klass);
-
        /* virtual functions */
-       object_class->finalize = gda_postgres_pstmt_finalize;
+       object_class->dispose = gda_postgres_pstmt_dispose;
 }
 
 static void
-gda_postgres_pstmt_init (GdaPostgresPStmt *pstmt, G_GNUC_UNUSED GdaPostgresPStmtClass *klass)
+gda_postgres_pstmt_init (GdaPostgresPStmt *pstmt)
 {
        g_return_if_fail (GDA_IS_PSTMT (pstmt));
+  GdaPostgresPStmtPrivate *priv = gda_postgres_pstmt_get_instance_private (pstmt);
        
-       pstmt->prep_name = NULL;
-       pstmt->date_format_change = FALSE;
+  g_weak_ref_init (&priv->cnc, NULL);
+  priv->pconn = NULL;
+       priv->prep_name = NULL;
+       priv->date_format_change = FALSE;
+  priv->deallocated = FALSE;
 }
 
 static void
-gda_postgres_pstmt_finalize (GObject *object)
+gda_postgres_pstmt_dispose (GObject *object)
 {
        GdaPostgresPStmt *pstmt = (GdaPostgresPStmt *) object;
 
        g_return_if_fail (GDA_IS_PSTMT (pstmt));
-
-       /* deallocate statement */
-       gchar *sql;
-       PGresult *pg_res;
-
-       sql = g_strdup_printf ("DEALLOCATE %s", pstmt->prep_name);
-       pg_res = _gda_postgres_PQexec_wrap (pstmt->cnc, pstmt->pconn, sql);
-       g_free (sql);
-       if (pg_res) 
-               PQclear (pg_res);
-       
+  GdaPostgresPStmtPrivate *priv = gda_postgres_pstmt_get_instance_private (pstmt);
+
+  if (!priv->deallocated) {
+    GdaConnection *cnc = NULL;
+
+    cnc = g_weak_ref_get (&priv->cnc);
+    if (cnc != NULL) {
+      /* deallocate statement */
+           gchar *sql;
+           PGresult *pg_res;
+
+           sql = g_strdup_printf ("DEALLOCATE %s", priv->prep_name);
+           pg_res = _gda_postgres_PQexec_wrap (cnc, priv->pconn, sql);
+           g_free (sql);
+           if (pg_res)
+                   PQclear (pg_res);
+      g_object_unref (cnc);
+    }
+    priv->deallocated = TRUE;
+  }
        /* free memory */
-       g_free (pstmt->prep_name);
+       g_clear_pointer (&priv->prep_name, g_free);
 
        /* chain to parent class */
-       parent_class->finalize (object);
+       G_OBJECT_CLASS (gda_postgres_pstmt_parent_class)->dispose (object);
 }
 
 GdaPostgresPStmt *
@@ -111,9 +102,31 @@ gda_postgres_pstmt_new (GdaConnection *cnc, PGconn *pconn, const gchar *prep_nam
        GdaPostgresPStmt *pstmt;
 
        pstmt = (GdaPostgresPStmt *) g_object_new (GDA_TYPE_POSTGRES_PSTMT, NULL);
-       pstmt->prep_name = g_strdup (prep_name);
-       pstmt->cnc = cnc;
-       pstmt->pconn = pconn;
+  GdaPostgresPStmtPrivate *priv = gda_postgres_pstmt_get_instance_private (pstmt);
+       priv->prep_name = g_strdup (prep_name);
+       g_weak_ref_set (&priv->cnc, cnc);
+  // FIXME: PGconn should be get from the GdaConnection
+       priv->pconn = pconn;
 
        return pstmt;
 }
+
+
+const gchar*
+gda_postgres_pstmt_get_prep_name (GdaPostgresPStmt *pstmt)
+{
+  GdaPostgresPStmtPrivate *priv = gda_postgres_pstmt_get_instance_private (pstmt);
+  return priv->prep_name;
+}
+gboolean
+gda_postgres_pstmt_get_date_format_change (GdaPostgresPStmt *pstmt)
+{
+  GdaPostgresPStmtPrivate *priv = gda_postgres_pstmt_get_instance_private (pstmt);
+  return priv->date_format_change;
+}
+void
+gda_postgres_pstmt_set_date_format_change (GdaPostgresPStmt *pstmt, gboolean change)
+{
+  GdaPostgresPStmtPrivate *priv = gda_postgres_pstmt_get_instance_private (pstmt);
+  priv->date_format_change = change;
+}
diff --git a/providers/postgres/gda-postgres-pstmt.h b/providers/postgres/gda-postgres-pstmt.h
index 9a7604fd3..bbef90119 100644
--- a/providers/postgres/gda-postgres-pstmt.h
+++ b/providers/postgres/gda-postgres-pstmt.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2008 - 2013 Vivien Malerba <malerba gnome-db org>
+ * Copyright (C) 2019 Daniel Espinsa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -26,33 +27,17 @@
 G_BEGIN_DECLS
 
 #define GDA_TYPE_POSTGRES_PSTMT            (gda_postgres_pstmt_get_type())
-#define GDA_POSTGRES_PSTMT(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GDA_TYPE_POSTGRES_PSTMT, 
GdaPostgresPStmt))
-#define GDA_POSTGRES_PSTMT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, GDA_TYPE_POSTGRES_PSTMT, 
GdaPostgresPStmtClass))
-#define GDA_IS_POSTGRES_PSTMT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE(obj, GDA_TYPE_POSTGRES_PSTMT))
-#define GDA_IS_POSTGRES_PSTMT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GDA_TYPE_POSTGRES_PSTMT))
-
-typedef struct _GdaPostgresPStmt        GdaPostgresPStmt;
-typedef struct _GdaPostgresPStmtClass   GdaPostgresPStmtClass;
-
-struct _GdaPostgresPStmt {
-       GdaPStmt        object;
-       
-       /* PostgreSQL identifies a prepared statement by its name, which we'll keep here,
-        * along with a pointer to a GdaConnection and a PGconn because when the prepared
-        * statement is destroyed, we need to call "DEALLOCATE..."
-        */
-       GdaConnection  *cnc;
-       PGconn         *pconn;
-       gchar          *prep_name;
-       gboolean        date_format_change; /* TRUE if this statement may incur a date format change */
-};
+
+G_DECLARE_DERIVABLE_TYPE (GdaPostgresPStmt, gda_postgres_pstmt, GDA, POSTGRES_PSTMT, GdaPStmt)
 
 struct _GdaPostgresPStmtClass {
        GdaPStmtClass  parent_class;
 };
 
-GType             gda_postgres_pstmt_get_type  (void) G_GNUC_CONST;
 GdaPostgresPStmt *gda_postgres_pstmt_new       (GdaConnection *cnc, PGconn *pconn, const gchar *prep_name);
+const gchar      *gda_postgres_pstmt_get_prep_name (GdaPostgresPStmt *pstmt);
+gboolean          gda_postgres_pstmt_get_date_format_change (GdaPostgresPStmt *pstmt);
+void              gda_postgres_pstmt_set_date_format_change (GdaPostgresPStmt *pstmt, gboolean change);
 
 G_END_DECLS
 


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