[libgda: 1/2] Some improvements in DDL module



commit cf523541d66e70ee774bfedaba1e9e04c06ee905
Author: Pavlo Solntsev <p sun fun gmail com>
Date:   Mon Sep 17 23:04:00 2018 +0000

    Some improvements in DDL module

 libgda/gda-ddl-column.c       |  21 ++--
 libgda/gda-ddl-creator.c      |  31 +++--
 libgda/gda-ddl-creator.h      |   8 +-
 libgda/gda-ddl-table.c        |  78 +++++++-----
 libgda/gda-ddl-table.h        |   6 +-
 tests/ddl/check-ddl-creator.c | 274 ++++++++++++++++++++++++++++++++++++++++--
 6 files changed, 349 insertions(+), 69 deletions(-)
---
diff --git a/libgda/gda-ddl-column.c b/libgda/gda-ddl-column.c
index ee1417850..1203c281e 100644
--- a/libgda/gda-ddl-column.c
+++ b/libgda/gda-ddl-column.c
@@ -640,6 +640,8 @@ gda_ddl_column_set_type (GdaDdlColumn *self,
   g_return_if_fail (self);
   GdaDdlColumnPrivate *priv = gda_ddl_column_get_instance_private (self);
   priv->m_gtype = type;
+  g_free (priv->mp_type);
+  priv->mp_type = g_strdup (gda_g_type_to_string (type));
 }
 
 /**
@@ -996,15 +998,18 @@ gda_ddl_column_prepare_create  (GdaDdlColumn *self,
   gchar *numstr = NULL;
   numstr = g_strdup_printf ("%d",priv->m_size);
 
-  if(!gda_server_operation_set_value_at(op,numstr,error,"/FIELDS_A/@COLUMN_SIZE/%d",order))
+  if (g_type_is_a (priv->m_gtype,G_TYPE_STRING))
     {
-      g_free (numstr);
-      return FALSE;
-    }
-  else
-    {
-      g_free (numstr);
-      numstr = NULL;
+      if(!gda_server_operation_set_value_at(op,numstr,error,"/FIELDS_A/@COLUMN_SIZE/%d",order))
+        {
+          g_free (numstr);
+          return FALSE;
+        }
+      else
+        {
+          g_free (numstr);
+          numstr = NULL;
+        }
     }
 
 /* We need to set scale only for numeric column type */
diff --git a/libgda/gda-ddl-creator.c b/libgda/gda-ddl-creator.c
index fef83d242..40ade0384 100644
--- a/libgda/gda-ddl-creator.c
+++ b/libgda/gda-ddl-creator.c
@@ -92,17 +92,25 @@ gda_ddl_creator_finalize (GObject *object)
   GdaDdlCreator *self = GDA_DDL_CREATOR(object);
   GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
 
+  g_free (priv->mp_schemaname);
+
+  G_OBJECT_CLASS (gda_ddl_creator_parent_class)->finalize (object);
+}
+
+static void
+gda_ddl_creator_dispose (GObject *object)
+{
+  GdaDdlCreator *self = GDA_DDL_CREATOR(object);
+  GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
+
   if (priv->mp_tables)
     g_list_free_full (priv->mp_tables, (GDestroyNotify) g_object_unref);
 
   if (priv->mp_views)
     g_list_free_full (priv->mp_views, (GDestroyNotify) g_object_unref);
 
-  g_free (priv->mp_schemaname);
-
   G_OBJECT_CLASS (gda_ddl_creator_parent_class)->finalize (object);
 }
-
 static void
 gda_ddl_creator_get_property (GObject    *object,
                               guint       prop_id,
@@ -148,6 +156,7 @@ gda_ddl_creator_class_init (GdaDdlCreatorClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
   object_class->finalize = gda_ddl_creator_finalize;
+  object_class->dispose = gda_ddl_creator_dispose;
   object_class->get_property = gda_ddl_creator_get_property;
   object_class->set_property = gda_ddl_creator_set_property;
 
@@ -371,7 +380,6 @@ gda_ddl_creator_parse_file_from_path (GdaDdlCreator *self,
                    GDA_DDL_CREATOR_ERROR,
                    GDA_DDL_CREATOR_DOC_NULL,
                    _("xmlDoc object can't be created from xmfile name '%s'"), xmlfile);
-      g_print ("Step %d\n",__LINE__);
       goto on_error;
     }
 
@@ -424,7 +432,6 @@ gda_ddl_creator_validate_file_from_path (const gchar *xmlfile,
                    GDA_DDL_CREATOR_ERROR,
                    GDA_DDL_CREATOR_DOC_NULL,
                    _("xmlDoc object can't be created from xmfile name '%s'"), xmlfile);
-      g_print ("Step %d\n",__LINE__);
       goto on_error;
     }
 
@@ -448,7 +455,7 @@ on_error:
  *
  * Since: 6.0
  */
-const GList*
+GList*
 gda_ddl_creator_get_tables (GdaDdlCreator *self)
 {
   GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
@@ -463,7 +470,7 @@ gda_ddl_creator_get_tables (GdaDdlCreator *self)
  *
  * Since: 6.0
  */
-const GList*
+GList*
 gda_ddl_creator_get_views (GdaDdlCreator *self)
 {
   GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
@@ -627,13 +634,13 @@ on_error:
  */
 void
 gda_ddl_creator_append_table (GdaDdlCreator *self,
-                              const GdaDdlTable *table)
+                              GdaDdlTable *table)
 {
   g_return_if_fail (self);
 
   GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
 
-  priv->mp_tables = g_list_append (priv->mp_tables,(gpointer)table);
+  priv->mp_tables = g_list_append (priv->mp_tables,g_object_ref (table));
 }
 
 /**
@@ -647,13 +654,13 @@ gda_ddl_creator_append_table (GdaDdlCreator *self,
  */
 void
 gda_ddl_creator_append_view (GdaDdlCreator *self,
-                             const GdaDdlView *view)
+                             GdaDdlView *view)
 {
   g_return_if_fail (self);
 
   GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
 
-  priv->mp_views = g_list_append (priv->mp_views,(gpointer)view);
+  priv->mp_views = g_list_append (priv->mp_views,g_object_ref (view));
 }
 
 /**
@@ -740,7 +747,7 @@ gda_ddl_creator_perform_operation (GdaDdlCreator *self,
         }
       else
         {
-          if(!gda_ddl_table_create (it->data,cnc,error))
+          if(!gda_ddl_table_create (it->data,cnc,TRUE,error))
             goto on_error;
         }
     } /* End of for loop */
diff --git a/libgda/gda-ddl-creator.h b/libgda/gda-ddl-creator.h
index 0042a7345..8325e7240 100644
--- a/libgda/gda-ddl-creator.h
+++ b/libgda/gda-ddl-creator.h
@@ -81,18 +81,18 @@ gboolean         gda_ddl_creator_parse_file (GdaDdlCreator *self,
                                              GFile *xmlfile,
                                              GError **error);
 
-const GList     *gda_ddl_creator_get_tables    (GdaDdlCreator *self);
-const GList     *gda_ddl_creator_get_views     (GdaDdlCreator *self);
+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,
-                                               const GdaDdlTable *table);
+                                               GdaDdlTable *table);
 
 void             gda_ddl_creator_append_view (GdaDdlCreator *self,
-                                              const GdaDdlView *view);
+                                              GdaDdlView *view);
 
 gboolean         gda_ddl_creator_perform_operation (GdaDdlCreator *self,
                                                     GdaConnection *cnc,
diff --git a/libgda/gda-ddl-table.c b/libgda/gda-ddl-table.c
index c735489af..407854571 100644
--- a/libgda/gda-ddl-table.c
+++ b/libgda/gda-ddl-table.c
@@ -105,14 +105,23 @@ gda_ddl_table_finalize (GObject *object)
   GdaDdlTable *self = (GdaDdlTable *)object;
   GdaDdlTablePrivate *priv = gda_ddl_table_get_instance_private (self);
 
+  g_free (priv->mp_comment);
+
+  G_OBJECT_CLASS (gda_ddl_table_parent_class)->finalize (object);
+}
+
+static void
+gda_ddl_table_dispose (GObject *object)
+{
+  GdaDdlTable *self = (GdaDdlTable *)object;
+  GdaDdlTablePrivate *priv = gda_ddl_table_get_instance_private (self);
+
   if (priv->mp_fkeys)
     g_list_free_full (priv->mp_fkeys, (GDestroyNotify) g_object_unref);
   if (priv->mp_columns)
     g_list_free_full (priv->mp_columns, (GDestroyNotify)g_object_unref);
 
-  g_free (priv->mp_comment);
-
-  G_OBJECT_CLASS (gda_ddl_table_parent_class)->finalize (object);
+  G_OBJECT_CLASS (gda_ddl_table_parent_class)->dispose (object);
 }
 
 static void
@@ -165,6 +174,7 @@ gda_ddl_table_class_init (GdaDdlTableClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
   object_class->finalize = gda_ddl_table_finalize;
+  object_class->dispose = gda_ddl_table_dispose;
   object_class->get_property = gda_ddl_table_get_property;
   object_class->set_property = gda_ddl_table_set_property;
 
@@ -241,7 +251,7 @@ gda_ddl_table_parse_node (GdaDdlBuildable *buildable,
               g_object_set (G_OBJECT(self),"comment", (char *)comment,NULL);
               xmlFree (comment);
             }
-        } 
+        }
       else if (!g_strcmp0 ((gchar *) it->name, "column"))
         {
           GdaDdlColumn *column = gda_ddl_column_new ();
@@ -286,8 +296,8 @@ gda_ddl_table_write_node (GdaDdlBuildable *buildable,
   xmlNewProp (node,BAD_CAST gdaddltablenodes[GDA_DDL_TABLE_NAME],
               BAD_CAST gda_ddl_base_get_name (GDA_DDL_BASE(self)));
 
-  xmlNewProp (node,BAD_CAST gdaddltablenodes[GDA_DDL_TABLE_TEMP], 
-              BAD_CAST GDA_BOOL_TO_STR (priv->m_istemp)); 
+  xmlNewProp (node,BAD_CAST gdaddltablenodes[GDA_DDL_TABLE_TEMP],
+              BAD_CAST GDA_BOOL_TO_STR (priv->m_istemp));
 
   xmlNewChild (node,NULL,
                (xmlChar*)gdaddltablenodes[GDA_DDL_TABLE_COMMENT],
@@ -412,7 +422,7 @@ gda_ddl_table_is_valid (GdaDdlTable *self)
  *
  * Since: 6.0
  */
-const GList*
+GList*
 gda_ddl_table_get_columns (GdaDdlTable *self)
 {
   g_return_val_if_fail (self, NULL);
@@ -433,7 +443,7 @@ gda_ddl_table_get_columns (GdaDdlTable *self)
  *
  * Since: 6.0
  */
-const GList*
+GList*
 gda_ddl_table_get_fkeys (GdaDdlTable *self)
 {
   g_return_val_if_fail (self, NULL);
@@ -465,7 +475,7 @@ gda_ddl_table_is_temp (GdaDdlTable *self)
  * gda_ddl_table_prepare_create:
  * @self: a #GdaDdlTable instance
  * @op: an instance of #GdaServerOperation to populate.
- * @error: error container 
+ * @error: error container
  *
  * Populate @op with information stored in @self. This method sets @op to execute CREATE_TABLE
  * operation.
@@ -475,6 +485,7 @@ gda_ddl_table_is_temp (GdaDdlTable *self)
 gboolean
 gda_ddl_table_prepare_create (GdaDdlTable *self,
                               GdaServerOperation *op,
+                              gboolean ifnotexists,
                               GError **error)
 {
   GdaDdlTablePrivate *priv = gda_ddl_table_get_instance_private (self);
@@ -498,11 +509,11 @@ gda_ddl_table_prepare_create (GdaDdlTable *self,
     return FALSE;
 
   if (!gda_server_operation_set_value_at(op,
-                                         priv->mp_comment,
+                                         GDA_BOOL_TO_STR(ifnotexists),
                                          error,
                                          "/TABLE_DEF_P/TABLE_IFNOTEXISTS"))
     return FALSE;
-  
+
   GList *it = NULL;
   gint i = 0; /* column order counter */
 
@@ -528,7 +539,7 @@ _gda_ddl_compare_column_meta(GdaMetaTableColumn *a,GdaDdlColumn *b)
     return 0;
 
   const gchar *namea = a->column_name;
-  const gchar *nameb = gda_ddl_column_get_name(b); 
+  const gchar *nameb = gda_ddl_column_get_name(b);
 
   return g_strcmp0(namea,nameb);
 }
@@ -538,14 +549,14 @@ _gda_ddl_compare_column_meta(GdaMetaTableColumn *a,GdaDdlColumn *b)
  * @self: a #GdaDdlTable instance
  * @obj: The corresponding meta object to take data from
  * @cnc: opened connection
- * @error: error container 
+ * @error: error container
  *
  * With this method object @obj in the database available through @cnc will be updated using
- * ADD_COLUMN operation with information stored in @self. 
- * 
+ * ADD_COLUMN operation with information stored in @self.
+ *
  * Returns: %TRUE if no error and %FALSE otherwise
  */
-gboolean        
+gboolean
 gda_ddl_table_update (GdaDdlTable *self,
                       GdaMetaTable *obj,
                       GdaConnection *cnc,
@@ -554,12 +565,12 @@ gda_ddl_table_update (GdaDdlTable *self,
   g_return_val_if_fail (self,FALSE);
   g_return_val_if_fail (obj,FALSE);
   g_return_val_if_fail (cnc,FALSE);
- 
+
   if (!gda_connection_is_opened(cnc))
     return FALSE;
 
   GdaDdlTablePrivate *priv = gda_ddl_table_get_instance_private (self);
-  
+
   if (!obj->columns)
     {
       g_set_error (error,GDA_DDL_TABLE_ERROR,GDA_DDL_TABLE_COLUMN_EMPTY,_("Empty column list"));
@@ -575,7 +586,7 @@ gda_ddl_table_update (GdaDdlTable *self,
   dbcolumns = obj->columns;
 
   gda_lockable_lock((GdaLockable*)cnc);
- 
+
   provider = gda_connection_get_provider (cnc);
 
   op = gda_server_provider_create_operation(provider,
@@ -585,13 +596,13 @@ gda_ddl_table_update (GdaDdlTable *self,
                                             error);
   if (!op)
     goto on_error;
- 
+
   if(!gda_server_operation_set_value_at(op,
                                         gda_ddl_base_get_full_name(GDA_DDL_BASE(self)),
                                         error,
                                         "/COLUMN_DEF_P/TABLE_NAME"))
     goto on_error;
-  
+
   gint newcolumncount = 0;
   for (it = priv->mp_columns;it;it=it->next)
     {
@@ -602,8 +613,8 @@ gda_ddl_table_update (GdaDdlTable *self,
       if (res) /* If object is present we need go to next element */
         continue;
       else
-        newcolumncount++; /* We need to count new column. See below */ 
-          
+        newcolumncount++; /* We need to count new column. See below */
+
       if(!gda_ddl_column_prepare_add(it->data,op,error))
         {
           g_slist_free (res);
@@ -629,10 +640,11 @@ on_error:
  * gda_ddl_table_create:
  * @self: a #GdaDdlTable object
  * @cnc: a #GdaConnection object
+ * @ifnotexists: Set to %TRUE if table should be created with "IFNOTEXISTS" option
  * @error: container for error storage
  *
  * Execute a full set of steps to create tabe in the database.
- * This method is called with "IFNOTEXISTS" option. 
+ * This method is called with "IFNOTEXISTS" option.
  *
  * Returns: %TRUE if successful, %FALSE otherwise
  *
@@ -641,6 +653,7 @@ on_error:
 gboolean
 gda_ddl_table_create (GdaDdlTable *self,
                       GdaConnection *cnc,
+                      gboolean ifnotexists,
                       GError **error)
 {
   g_return_val_if_fail (self,FALSE);
@@ -648,12 +661,12 @@ gda_ddl_table_create (GdaDdlTable *self,
 
   if (!gda_connection_is_opened(cnc))
     return FALSE;
-  
+
   gda_lockable_lock(GDA_LOCKABLE(cnc));
 
   GdaServerProvider *provider = NULL;
   GdaServerOperation *op = NULL;
- 
+
   provider = gda_connection_get_provider (cnc);
 
   op = gda_server_provider_create_operation(provider,
@@ -664,7 +677,8 @@ gda_ddl_table_create (GdaDdlTable *self,
   if (!op)
     goto on_error;
 
-  if (!gda_ddl_table_prepare_create(self,op,error))
+
+  if (!gda_ddl_table_prepare_create(self,op,ifnotexists,error))
     goto on_error;
 
   if(!gda_server_provider_perform_operation(provider,cnc,op,error))
@@ -712,7 +726,7 @@ gda_ddl_table_new_from_meta (GdaMetaDbObject *obj)
   for (it = metatable->columns; it; it = it->next)
     {
       GdaDdlColumn *column = gda_ddl_column_new_from_meta (GDA_META_TABLE_COLUMN(it->data));
-      
+
       gda_ddl_table_append_column(table,column);
     }
 
@@ -723,10 +737,10 @@ gda_ddl_table_new_from_meta (GdaMetaDbObject *obj)
         continue;
 
       GdaDdlFkey *fkey = gda_ddl_fkey_new_from_meta (GDA_META_TABLE_FOREIGN_KEY(it->data));
-      
+
       gda_ddl_table_append_fkey (table,fkey);
 
-    } 
+    }
 
   return table;
 }
@@ -748,7 +762,7 @@ gda_ddl_table_append_column (GdaDdlTable *self,
 
   GdaDdlTablePrivate *priv = gda_ddl_table_get_instance_private (self);
 
-  priv->mp_columns = g_list_append (priv->mp_columns,column);
+  priv->mp_columns = g_list_append (priv->mp_columns,g_object_ref(column));
 }
 
 /**
@@ -768,6 +782,6 @@ gda_ddl_table_append_fkey (GdaDdlTable *self,
 
   GdaDdlTablePrivate *priv = gda_ddl_table_get_instance_private (self);
 
-  priv->mp_fkeys = g_list_append (priv->mp_fkeys,fkey);
+  priv->mp_fkeys = g_list_append (priv->mp_fkeys,g_object_ref(fkey));
 }
 
diff --git a/libgda/gda-ddl-table.h b/libgda/gda-ddl-table.h
index 2e3a0fe48..06a1c0737 100644
--- a/libgda/gda-ddl-table.h
+++ b/libgda/gda-ddl-table.h
@@ -53,8 +53,8 @@ GQuark gda_ddl_table_error_quark(void);
 
 GdaDdlTable*    gda_ddl_table_new               (void);
 gboolean        gda_ddl_table_is_valid          (GdaDdlTable *self);
-const GList*    gda_ddl_table_get_columns       (GdaDdlTable *self);
-const GList*    gda_ddl_table_get_fkeys         (GdaDdlTable *self);
+GList*          gda_ddl_table_get_columns       (GdaDdlTable *self);
+GList*          gda_ddl_table_get_fkeys         (GdaDdlTable *self);
 
 void            gda_ddl_table_append_column     (GdaDdlTable *self,
                                                  GdaDdlColumn *column);
@@ -65,6 +65,7 @@ void            gda_ddl_table_set_temp          (GdaDdlTable *self,
 
 gboolean        gda_ddl_table_prepare_create (GdaDdlTable *self,
                                               GdaServerOperation *op,
+                                              gboolean ifnotexists,
                                               GError **error);
 
 gboolean        gda_ddl_table_update          (GdaDdlTable *self,
@@ -74,6 +75,7 @@ gboolean        gda_ddl_table_update          (GdaDdlTable *self,
 
 gboolean        gda_ddl_table_create          (GdaDdlTable *self,
                                                GdaConnection *cnc,
+                                               gboolean ifnotexists,
                                                GError **error);
 
 GdaDdlTable    *gda_ddl_table_new_from_meta    (GdaMetaDbObject *obj);
diff --git a/tests/ddl/check-ddl-creator.c b/tests/ddl/check-ddl-creator.c
index ee1bbf2e3..d0d4dcb3d 100644
--- a/tests/ddl/check-ddl-creator.c
+++ b/tests/ddl/check-ddl-creator.c
@@ -24,10 +24,20 @@
 #include <libgda/libgda.h>
 #include <libgda/gda-ddl-creator.h>
 #include <libgda/gda-ddl-base.h>
+#include <sql-parser/gda-sql-parser.h>
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 
+enum {
+  COLUMN_ID = 0,
+  COLUMN_NAME,
+  COLUMN_STATE,
+  COLUMN_TIME,
+  COLUMN_TIMESTAMP
+};
+
+
 typedef struct {
     GdaDdlCreator *creator;
     gchar *xmlfile;
@@ -35,6 +45,17 @@ typedef struct {
     GFile *file;
 } CheckDdlObject;
 
+typedef struct {
+    GdaDdlCreator *creator;
+    GdaConnection *cnc;
+    GdaDdlColumn *column_id;
+    GdaDdlColumn *column_name;
+    GdaDdlColumn *column_ctime;
+    GdaDdlColumn *column_ts;
+    GdaDdlColumn *column_state;
+    GdaDdlTable *table;
+} DdlCreatorCnc;
+
 static void
 test_ddl_creator_start (CheckDdlObject *self,
                      gconstpointer user_data)
@@ -74,6 +95,70 @@ test_ddl_creator_start (CheckDdlObject *self,
   g_print ("GFile is %s\n",g_file_get_path(self->file));
 }
 
+static void
+test_ddl_creator_start_db (DdlCreatorCnc *self,
+                           gconstpointer user_data)
+{
+  gda_init();
+
+  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",
+                                              NULL,
+                                              GDA_CONNECTION_OPTIONS_NONE,
+                                              NULL);
+  g_assert_nonnull (self->cnc);
+
+  gboolean open_res = gda_connection_open (self->cnc, NULL);
+
+  g_assert_true (open_res);
+
+  self->table = gda_ddl_table_new ();
+  gda_ddl_base_set_name (GDA_DDL_BASE(self->table),"dntypes");
+
+  self->column_id = gda_ddl_column_new ();
+  gda_ddl_column_set_name (self->column_id,"id");
+  gda_ddl_column_set_type (self->column_id, G_TYPE_INT);
+  gda_ddl_column_set_autoinc (self->column_id, TRUE);
+  gda_ddl_column_set_pkey (self->column_id, TRUE);
+
+  gda_ddl_table_append_column (self->table,self->column_id);
+
+  self->column_name = gda_ddl_column_new ();
+  gda_ddl_column_set_name (self->column_name,"name");
+  gda_ddl_column_set_type (self->column_name, G_TYPE_STRING);
+  gda_ddl_column_set_size (self->column_name, 50);
+
+  gda_ddl_table_append_column (self->table,self->column_name);
+
+  self->column_ctime = gda_ddl_column_new ();
+  gda_ddl_column_set_name (self->column_ctime,"create_time");
+  gda_ddl_column_set_type (self->column_ctime, GDA_TYPE_TIME);
+
+  gda_ddl_table_append_column (self->table,self->column_ctime);
+
+  self->column_state = gda_ddl_column_new ();
+  gda_ddl_column_set_name (self->column_state,"state");
+  gda_ddl_column_set_type (self->column_state, G_TYPE_BOOLEAN);
+
+  gda_ddl_table_append_column (self->table,self->column_state);
+
+  self->column_ts = gda_ddl_column_new ();
+  gda_ddl_column_set_name (self->column_ts,"mytimestamp");
+  gda_ddl_column_set_type (self->column_ts, G_TYPE_DATE_TIME);
+
+  gda_ddl_table_append_column (self->table,self->column_ts);
+
+  gda_ddl_creator_append_table (self->creator, self->table);
+
+  open_res = gda_ddl_creator_perform_operation (self->creator,self->cnc,NULL);
+
+  g_assert_true (open_res);
+}
+
 static void
 test_ddl_creator_finish (CheckDdlObject *self,
                       gconstpointer user_data)
@@ -85,6 +170,20 @@ test_ddl_creator_finish (CheckDdlObject *self,
   g_object_unref (self->cnc);
 }
 
+static void
+test_ddl_creator_finish_db (DdlCreatorCnc *self,
+                            gconstpointer user_data)
+{
+  gda_connection_close(self->cnc,NULL);
+  g_object_unref (self->cnc);
+  g_object_unref (self->creator);
+  g_object_unref (self->column_id);
+  g_object_unref (self->column_name);
+  g_object_unref (self->column_ctime);
+  g_object_unref (self->column_ts);
+  g_object_unref (self->table);
+}
+
 static void
 test_ddl_creator_parse_xml_path (CheckDdlObject *self,
                                 gconstpointer user_data)
@@ -139,14 +238,167 @@ test_ddl_creator_create_db (CheckDdlObject *self,
   g_assert_true (resop);
 }
 
-/* static void */
-/* test_ddl_creator_parse_cnc (CheckDdlObject *self, */
-/*                            gconstpointer user_data) */
-/* { */
-/*   gboolean res = gda_ddl_creator_parse_cnc(self->creator,self->cnc,NULL); */
+static void
+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;
+
+  value_name = gda_value_new (G_TYPE_STRING);
+  g_assert_nonnull (value_name);
+  g_value_set_string (value_name,"First");
+
+  value_state = gda_value_new (G_TYPE_BOOLEAN);
+  g_assert_nonnull (value_state);
+  g_value_set_boolean (value_state,TRUE);
+
+  value_ctime = gda_value_new_time_from_timet (time(NULL));
+  g_assert_nonnull (value_ctime);
+
+  value_timest = gda_value_new_date_time_from_timet (time(NULL));
+  g_assert_nonnull (value_timest);
+
+  open_res = gda_connection_insert_row_into_table (self->cnc,"dntypes",NULL,
+                                                   "name",value_name,
+                                                   "state",value_state,
+                                                   "create_time",value_ctime,
+                                                   "mytimestamp",value_timest,
+                                                   NULL);
+
+  GdaDataModel *model = NULL;
+  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);
+
+  GList *tables = gda_ddl_creator_get_tables (creator);
+  g_assert_nonnull (tables);
+  gint raw = 0;
+  gint column_count = 0;
+
+  for (GList *it = tables; it; it = it->next)
+    {
+      g_assert_cmpstr (dntypes,==,gda_ddl_base_get_name (GDA_DDL_BASE(it->data)));
+      GList *columns = gda_ddl_table_get_columns (GDA_DDL_TABLE(it->data));
+      g_assert_nonnull (columns);
+
+      column_count = 0;
+
+      for (GList *jt = columns;jt;jt=jt->next)
+        {
+          GdaDdlColumn *column = GDA_DDL_COLUMN (jt->data);
+          GType column_type = gda_ddl_column_get_gtype (column);
+          g_assert_true (column_type != G_TYPE_NONE);
+          
+          if (!g_strcmp0 ("id",gda_ddl_column_get_name (column))) 
+            {
+              GError *error = NULL;
+              const GValue *value = gda_data_model_get_typed_value_at (model,
+                                                                       column_count++,
+                                                                       raw,
+                                                                       G_TYPE_INT,
+                                                                       FALSE,
+                                                                       &error);
+
+              if (!value)
+                g_print ("value_int error: %s\n",error && error->message ? error->message : "No default");
+
+              g_assert_nonnull (value);
+
+              GType ggtype = G_VALUE_TYPE(value);
 
-/*   g_assert_true (res); */
-/* } */
+              g_print ("for ID type is %s\n",g_type_name (ggtype));
+              
+            }
+          
+          if (!g_strcmp0 ("name",gda_ddl_column_get_name (column))) 
+            {
+              const GValue *value = gda_data_model_get_typed_value_at (model,
+                                                                       column_count++,
+                                                                       raw,
+                                                                       G_TYPE_STRING,
+                                                                       FALSE,
+                                                                       NULL);
+
+              g_assert_nonnull (value);
+
+              GType ggtype = G_VALUE_TYPE(value);
+
+              g_print ("for NAME type is %s\n",g_type_name (ggtype));
+              
+            }
+
+          if (!g_strcmp0 ("state",gda_ddl_column_get_name (column))) 
+            {
+              const GValue *value = gda_data_model_get_typed_value_at (model,
+                                                                           column_count++,
+                                                                           raw,
+                                                                           G_TYPE_BOOLEAN,
+                                                                           FALSE,
+                                                                           NULL);
+
+              g_assert_nonnull (value);
+
+              GType ggtype = G_VALUE_TYPE(value);
+              
+              g_assert_true (TRUE && g_value_get_boolean (value));
+            }
+
+          if (!g_strcmp0 ("create_time",gda_ddl_column_get_name (column))) 
+            {
+              const GValue *value = gda_data_model_get_typed_value_at (model,
+                                                                           column_count++,
+                                                                           raw,
+                                                                           GDA_TYPE_TIME,
+                                                                           FALSE,
+                                                                           NULL);
+
+              g_assert_nonnull (value);
+
+              GType ggtype = G_VALUE_TYPE(value);
+
+              g_print ("for created_time type is %s\n",g_type_name (ggtype));
+              
+            }
+
+          if (!g_strcmp0 ("mytimestamp",gda_ddl_column_get_name (column))) 
+            {
+              const GValue *value = gda_data_model_get_typed_value_at (model,
+                                                                           column_count++,
+                                                                           raw,
+                                                                           G_TYPE_DATE_TIME,
+                                                                           FALSE,
+                                                                           NULL);
+
+              g_assert_nonnull (value);
+
+              GType ggtype = G_VALUE_TYPE(value);
+              GDateTime *dt = (GDateTime*)g_value_get_boxed (value);
+
+              g_print ("for dt type is %s\n",g_type_name (ggtype));
+              g_print ("YYYY-MM-DD: %d-%d-%d\n",g_date_time_get_year (dt),
+                                                g_date_time_get_month (dt),
+                                                g_date_time_get_day_of_month (dt));
+               
+            }
+        }
+      raw++;
+    }
+
+  g_object_unref (creator);
+  g_assert_true (open_res);
+
+  gda_value_free (value_name);
+  gda_value_free (value_state);
+  gda_value_free (value_ctime);
+  gda_value_free (value_timest);
+  g_object_unref (model);
+}
 
 gint
 main (gint   argc,
@@ -183,12 +435,12 @@ main (gint   argc,
               test_ddl_creator_start,
               test_ddl_creator_validate_xml,
               test_ddl_creator_finish);
-  /*g_test_add ("/test-ddl/creator-parse-cnc",
-              CheckDdlObject,
+  g_test_add ("/test-ddl/creator-parse-cnc",
+              DdlCreatorCnc, 
               NULL,
-              test_ddl_creator_start,
+              test_ddl_creator_start_db,
               test_ddl_creator_parse_cnc,
-              test_ddl_creator_finish);*/
+              test_ddl_creator_finish_db);
  
   return g_test_run();
 }


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