[libgda: 1/2] DDL: API simplification for GdaDdlCreator



commit 55d01b9add428ac8801094972c86e6f9b612339a
Author: Pavlo Solntsev <p sun fun gmail com>
Date:   Fri Oct 12 08:41:14 2018 -0500

    DDL: API simplification for GdaDdlCreator
    
    GdaConnection was added as a property to GdaDdlCreator
    New API was added to GdaConnection to create GdaDdlCreator and set
    "connection" property.
    Test was adjusted to meet new API.

 libgda/gda-connection.c         | 24 +++++++++++++++++
 libgda/gda-connection.h         |  3 ++-
 libgda/gda-ddl-creator.c        | 59 ++++++++++++++++++++++++++++-------------
 libgda/gda-ddl-creator.h        |  3 ---
 libgda/gda-ddl-view.h           |  2 +-
 tests/ddl/check-ddl-creator.c   | 21 +++++++++------
 tests/ddl/check-ddl-db-create.c |  7 ++---
 7 files changed, 84 insertions(+), 35 deletions(-)
---
diff --git a/libgda/gda-connection.c b/libgda/gda-connection.c
index dece5200e..95762a83c 100644
--- a/libgda/gda-connection.c
+++ b/libgda/gda-connection.c
@@ -7125,3 +7125,27 @@ gda_connection_operation_get_sql_identifier_at_path (GdaConnection *cnc, GdaServ
        return gda_sql_identifier_quote (str, cnc, gda_connection_get_provider (cnc), FALSE,
                                         cncoptions & GDA_CONNECTION_OPTIONS_SQL_IDENTIFIERS_CASE_SENSITIVE);
 }
+
+/**
+ * gda_connection_create_ddl_creator:
+ * @cnc: A #GdaConnection object to use
+ *
+ * A convenient method to create a new #GdaDdlCreator instance and set the current @cnc as a
+ * property.  If for some reason, this approach doesn't fit well, the same task can be achieved 
+ * by the following code:
+ *
+ * GdaDdlCreator *creator = gda_ddl_creator_new ();
+ * g_object_set (creator, "connection", cnc, NULL);
+ *
+ * Returns: (transfer full): A new instance of #GdaDdlCreator. The new object should be deallocated
+ * using g_object_unref().
+ *
+ * Since: 6.0
+ */
+GdaDdlCreator*
+gda_connection_create_ddl_creator (GdaConnection *cnc)
+{
+  g_return_val_if_fail (GDA_IS_CONNECTION (cnc),NULL);
+
+  return g_object_new (GDA_TYPE_DDL_CREATOR,"connection",cnc,NULL);
+}
diff --git a/libgda/gda-connection.h b/libgda/gda-connection.h
index a1282449d..86e3b3b31 100644
--- a/libgda/gda-connection.h
+++ b/libgda/gda-connection.h
@@ -41,6 +41,7 @@
 #include <libgda/gda-meta-store.h>
 #include <libgda/gda-server-operation.h>
 #include <libgda/gda-batch.h>
+#include <libgda/gda-ddl-creator.h>
 
 G_BEGIN_DECLS
 
@@ -393,7 +394,7 @@ GdaDataModel        *gda_connection_get_meta_store_data  (GdaConnection *cnc, Gd
                                                          GError **error, gint nb_filters, ...);
 GdaDataModel        *gda_connection_get_meta_store_data_v(GdaConnection *cnc, GdaConnectionMetaType 
meta_type,
                                                          GList* filters, GError **error);
-
+GdaDdlCreator       *gda_connection_create_ddl_creator (GdaConnection *cnc);
 G_END_DECLS
 
 #endif
diff --git a/libgda/gda-ddl-creator.c b/libgda/gda-ddl-creator.c
index 6a550d3c2..e1929ad6d 100644
--- a/libgda/gda-ddl-creator.c
+++ b/libgda/gda-ddl-creator.c
@@ -39,6 +39,7 @@ typedef struct
   GList *mp_tables; /* List of all tables that should be create, GdaDdlTable  */
   GList *mp_views; /* List of all views that should be created, GdaDdlView */
   gchar *mp_schemaname;
+  GdaConnection *cnc;
 } GdaDdlCreatorPrivate;
 
 /**
@@ -62,6 +63,7 @@ G_DEFINE_TYPE_WITH_PRIVATE (GdaDdlCreator, gda_ddl_creator, G_TYPE_OBJECT)
 enum {
   PROP_0,
   PROP_SCHEMA_NAME,
+  PROP_CNC,
   /*<private>*/
   N_PROPS
 };
@@ -109,6 +111,9 @@ gda_ddl_creator_dispose (GObject *object)
   if (priv->mp_views)
     g_list_free_full (priv->mp_views, (GDestroyNotify) g_object_unref);
 
+  if (priv->cnc)
+    g_object_unref (priv->cnc);
+
   G_OBJECT_CLASS (gda_ddl_creator_parent_class)->finalize (object);
 }
 static void
@@ -125,6 +130,9 @@ gda_ddl_creator_get_property (GObject    *object,
       case PROP_SCHEMA_NAME:
         g_value_set_string (value, priv->mp_schemaname);
       break;
+      case PROP_CNC:
+        g_value_set_object (value, G_OBJECT(priv->cnc));
+        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -145,6 +153,12 @@ gda_ddl_creator_set_property (GObject      *object,
         g_free(priv->mp_schemaname);
         priv->mp_schemaname = g_value_dup_string (value);
       break;
+      case PROP_CNC:
+        if (priv->cnc)
+          g_object_unref (priv->cnc);
+
+        priv->cnc = GDA_CONNECTION (g_object_ref (G_OBJECT (g_value_get_object (value))));
+      break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -167,6 +181,13 @@ gda_ddl_creator_class_init (GdaDdlCreatorClass *klass)
                          NULL,
                          G_PARAM_READWRITE);
 
+  properties[PROP_CNC] =
+    g_param_spec_object ("connection",
+                         "Connection",
+                         "Connection to use",
+                         GDA_TYPE_CONNECTION,
+                         G_PARAM_READWRITE);
+
   g_object_class_install_properties (object_class,N_PROPS,properties);
 }
 
@@ -178,6 +199,7 @@ gda_ddl_creator_init (GdaDdlCreator *self)
   priv->mp_tables = NULL;
   priv->mp_views = NULL;
   priv->mp_schemaname = NULL;
+  priv->cnc = NULL;
 }
 
 static gboolean
@@ -554,30 +576,30 @@ gda_ddl_creator_get_view (GdaDdlCreator *self,
  * @cnc: Connection to parse
  * @error: error storage object
  *
- * Parse cnc to populate @self object. 
+ * Parse cnc to populate @self object. This method should be called every time after database was
+ * modified or @self was just created.
  *
  * Returns: Returns %TRUE if succeeded, %FALSE otherwise.
  */
 gboolean
 gda_ddl_creator_parse_cnc (GdaDdlCreator *self,
-                           GdaConnection *cnc,
                            GError **error)
 {
   g_return_val_if_fail (self,FALSE);
-  g_return_val_if_fail (cnc,FALSE);
 
-  if (!gda_connection_is_opened (cnc))
+  GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
+
+  if (!gda_connection_is_opened (priv->cnc))
     return FALSE;
 
-  if(!gda_connection_update_meta_store (cnc, NULL, error))
+  if(!gda_connection_update_meta_store (priv->cnc, NULL, error))
     return FALSE;
 
-  GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
 
   GdaMetaStore *mstore = NULL;
   GdaMetaStruct *mstruct = NULL;
 
-  mstore = gda_connection_get_meta_store (cnc);
+  mstore = gda_connection_get_meta_store (priv->cnc);
   mstruct = (GdaMetaStruct*) g_object_new (GDA_TYPE_META_STRUCT, "meta-store", mstore, "features", 
GDA_META_STRUCT_FEATURE_ALL, NULL);
 
   GSList *dblist = NULL;
@@ -691,13 +713,13 @@ gda_ddl_creator_append_view (GdaDdlCreator *self,
  */
 gboolean
 gda_ddl_creator_perform_operation (GdaDdlCreator *self,
-                                   GdaConnection *cnc,
                                    GError **error)
 {
   g_return_val_if_fail (self,FALSE);
-  g_return_val_if_fail (cnc,FALSE);
 
-  if (!gda_connection_is_opened (cnc))
+  GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
+
+  if (!gda_connection_is_opened (priv->cnc))
     {
       g_set_error (error,
                    GDA_DDL_CREATOR_ERROR,
@@ -706,17 +728,16 @@ gda_ddl_creator_perform_operation (GdaDdlCreator *self,
       return FALSE;
     }
 
-  gda_lockable_lock ((GdaLockable*)cnc);
+  gda_lockable_lock ((GdaLockable*)priv->cnc);
 // We need to get MetaData
-  if(!gda_connection_update_meta_store (cnc, NULL, error))
+  if(!gda_connection_update_meta_store (priv->cnc, NULL, error))
     return FALSE;
 
-  GdaMetaStore *mstore = gda_connection_get_meta_store (cnc);
+  GdaMetaStore *mstore = gda_connection_get_meta_store (priv->cnc);
   GdaMetaStruct *mstruct = (GdaMetaStruct*) g_object_new (GDA_TYPE_META_STRUCT, "meta-store", mstore, 
"features", GDA_META_STRUCT_FEATURE_ALL, NULL);
 
   // We need information about catalog, schema, name for each object we would
   // like to check
-  GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
 
   GdaMetaDbObject *mobj = NULL;
   GValue *catalog = NULL;
@@ -742,12 +763,12 @@ gda_ddl_creator_perform_operation (GdaDdlCreator *self,
 
       if (mobj)
         {
-          if(!gda_ddl_table_update (it->data,GDA_META_TABLE(mobj),cnc,error))
+          if(!gda_ddl_table_update (it->data,GDA_META_TABLE(mobj),priv->cnc,error))
             goto on_error;
         }
       else
         {
-          if(!gda_ddl_table_create (it->data,cnc,TRUE,error))
+          if(!gda_ddl_table_create (it->data,priv->cnc,TRUE,error))
             goto on_error;
         }
     } /* End of for loop */
@@ -759,12 +780,12 @@ gda_ddl_creator_perform_operation (GdaDdlCreator *self,
 /*TODO: add update option for views */
   for (it = priv->mp_views; it; it = it->next)
     {
-      if(!gda_ddl_view_create (it->data,cnc,error))
+      if(!gda_ddl_view_create (it->data,priv->cnc,error))
         goto on_error;
     } /* End of for loop */
 
   g_object_unref (mstruct);
-  gda_lockable_unlock ((GdaLockable*)cnc);
+  gda_lockable_unlock ((GdaLockable*)priv->cnc);
 
   return TRUE;
 
@@ -773,7 +794,7 @@ on_error:
   gda_value_free (schema);
   gda_value_free (name);
   g_object_unref (mstruct);
-  gda_lockable_unlock ((GdaLockable*)cnc);
+  gda_lockable_unlock ((GdaLockable*)priv->cnc);
 
   return FALSE;
 }
diff --git a/libgda/gda-ddl-creator.h b/libgda/gda-ddl-creator.h
index 8325e7240..38383db3a 100644
--- a/libgda/gda-ddl-creator.h
+++ b/libgda/gda-ddl-creator.h
@@ -23,7 +23,6 @@
 
 #include <glib-object.h>
 #include <gmodule.h>
-#include "gda-connection.h"
 #include "gda-ddl-table.h"
 #include "gda-ddl-view.h"
 #include "gda-server-operation.h"
@@ -85,7 +84,6 @@ GList           *gda_ddl_creator_get_tables   (GdaDdlCreator *self);
 GList           *gda_ddl_creator_get_views     (GdaDdlCreator *self);
 
 gboolean         gda_ddl_creator_parse_cnc (GdaDdlCreator *self,
-                                            GdaConnection *cnc,
                                             GError **error);
 
 void             gda_ddl_creator_append_table (GdaDdlCreator *self,
@@ -95,7 +93,6 @@ void             gda_ddl_creator_append_view (GdaDdlCreator *self,
                                               GdaDdlView *view);
 
 gboolean         gda_ddl_creator_perform_operation (GdaDdlCreator *self,
-                                                    GdaConnection *cnc,
                                                     GError **error);
 
 gboolean         gda_ddl_creator_write_to_file (GdaDdlCreator *self,
diff --git a/libgda/gda-ddl-view.h b/libgda/gda-ddl-view.h
index bb1efd3b0..af715a3f8 100644
--- a/libgda/gda-ddl-view.h
+++ b/libgda/gda-ddl-view.h
@@ -25,8 +25,8 @@
 #include <gmodule.h>
 #include "gda-ddl-base.h"
 #include "gda-ddl-buildable.h"
-#include "gda-connection.h"
 #include "gda-meta-struct.h"
+#include "gda-server-operation.h"
 
 
 G_BEGIN_DECLS
diff --git a/tests/ddl/check-ddl-creator.c b/tests/ddl/check-ddl-creator.c
index d0d4dcb3d..710a56b9b 100644
--- a/tests/ddl/check-ddl-creator.c
+++ b/tests/ddl/check-ddl-creator.c
@@ -78,8 +78,6 @@ test_ddl_creator_start (CheckDdlObject *self,
 
   g_assert_nonnull (self->xmlfile);
 
-  self->creator = gda_ddl_creator_new();
-  
   self->cnc = gda_connection_new_from_string("SQLite",
                                              "DB_DIR=.;DB_NAME=ddl_test",
                                              NULL,
@@ -91,6 +89,10 @@ test_ddl_creator_start (CheckDdlObject *self,
   gboolean openres = gda_connection_open(self->cnc,NULL);
   g_assert_true (openres);
 
+  self->creator = gda_connection_create_ddl_creator (self->cnc);
+
+  g_assert_nonnull (self->creator);
+
   self->file = g_file_new_for_path (self->xmlfile); 
   g_print ("GFile is %s\n",g_file_get_path(self->file));
 }
@@ -103,7 +105,6 @@ test_ddl_creator_start_db (DdlCreatorCnc *self,
 
   self->cnc = NULL;
   self->creator = NULL;
-  self->creator = gda_ddl_creator_new ();
  
   self->cnc = gda_connection_new_from_string ("SQLite",
                                               "DB_DIR=.;DB_NAME=ddl_types",
@@ -116,6 +117,10 @@ test_ddl_creator_start_db (DdlCreatorCnc *self,
 
   g_assert_true (open_res);
 
+  self->creator = gda_connection_create_ddl_creator (self->cnc);
+
+  g_assert_nonnull (self->creator);
+
   self->table = gda_ddl_table_new ();
   gda_ddl_base_set_name (GDA_DDL_BASE(self->table),"dntypes");
 
@@ -154,7 +159,7 @@ test_ddl_creator_start_db (DdlCreatorCnc *self,
 
   gda_ddl_creator_append_table (self->creator, self->table);
 
-  open_res = gda_ddl_creator_perform_operation (self->creator,self->cnc,NULL);
+  open_res = gda_ddl_creator_perform_operation (self->creator,NULL);
 
   g_assert_true (open_res);
 }
@@ -229,7 +234,6 @@ test_ddl_creator_create_db (CheckDdlObject *self,
 
   GError *error = NULL;
   gboolean resop = gda_ddl_creator_perform_operation(self->creator,
-                                                     self->cnc,
                                                      &error);  
 
   if (!resop)
@@ -243,7 +247,6 @@ test_ddl_creator_parse_cnc (DdlCreatorCnc *self,
                             gconstpointer user_data)
 {
   gboolean open_res;
-  const gchar* name_str = "First";
   const gchar* dntypes = "dntypes";
 
   GValue *value_name,*value_state,*value_ctime,*value_timest;
@@ -273,8 +276,10 @@ test_ddl_creator_parse_cnc (DdlCreatorCnc *self,
   model = gda_connection_execute_select_command (self->cnc,"SELECT * FROM dntypes",NULL);
   g_assert_nonnull (model);
 
-  GdaDdlCreator *creator = gda_ddl_creator_new ();
-  open_res = gda_ddl_creator_parse_cnc (creator,self->cnc,NULL);
+  GdaDdlCreator *creator = gda_connection_create_ddl_creator (self->cnc);
+  open_res = gda_ddl_creator_parse_cnc (creator,NULL);
+
+  g_assert_true (open_res);
 
   GList *tables = gda_ddl_creator_get_tables (creator);
   g_assert_nonnull (tables);
diff --git a/tests/ddl/check-ddl-db-create.c b/tests/ddl/check-ddl-db-create.c
index 15fa10926..6d807a3f4 100644
--- a/tests/ddl/check-ddl-db-create.c
+++ b/tests/ddl/check-ddl-db-create.c
@@ -57,8 +57,6 @@ test_ddl_db_create_start(CheckCreatedb *self,
 
   g_assert_nonnull (self->xmlfile);
 
-  self->creator = gda_ddl_creator_new();
-
   self->cnc = gda_connection_new_from_string ("SQLite",
                                               "DB_DIR=.;DB_NAME=ddl_test_create",
                                               NULL,
@@ -70,6 +68,10 @@ test_ddl_db_create_start(CheckCreatedb *self,
   gboolean res = gda_connection_open (self->cnc,NULL);
   g_assert_true (res);
 
+  self->creator = gda_connection_create_ddl_creator (self->cnc);
+
+  g_assert_nonnull (self->creator);
+
   res = gda_ddl_creator_validate_file_from_path (self->xmlfile,NULL);
   g_assert_true (res);
 
@@ -80,7 +82,6 @@ test_ddl_db_create_start(CheckCreatedb *self,
   g_assert_true (res);
   
   res = gda_ddl_creator_perform_operation (self->creator,
-                                           self->cnc,
                                            NULL);
   g_assert_true (res);
 }


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