[libgda] postgresql: fixes on DDL



commit d03432ea3472dee3ae2352adf067921a15297505
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Thu Dec 13 17:20:46 2018 -0600

    postgresql: fixes on DDL
    
    * CREATE TABLE operation now supports IF NOT EXISTS
    * Fixed GdaDdlTable to support database default types
      for GType on column
    * Fixed gda_server_perform_operation() to set errors
    * Added a test case for GdaDdlCreator using PostgreSQL

 libgda/gda-ddl-column.c                            |  39 +++--
 libgda/gda-ddl-column.h                            |   2 +-
 libgda/gda-ddl-creator.c                           |  59 +++----
 libgda/gda-ddl-fkey.c                              |  44 ++---
 libgda/gda-ddl-table.c                             |  82 ++++-----
 libgda/gda-server-operation.c                      |   7 +-
 libgda/gda-server-provider.c                       |   4 +-
 providers/postgres/gda-postgres-ddl.c              |   4 +
 .../postgres/postgres_specs_create_table.xml.in    |   4 +
 providers/reuseable/postgres/gda-postgres-meta.c   |   2 +-
 tests/ddl/check-ddl-creator-postgresql.c           | 183 +++++++++++++++++++++
 tests/ddl/meson.build                              |  27 +++
 12 files changed, 343 insertions(+), 114 deletions(-)
---
diff --git a/libgda/gda-ddl-column.c b/libgda/gda-ddl-column.c
index 1203c281e..dafbd27f4 100644
--- a/libgda/gda-ddl-column.c
+++ b/libgda/gda-ddl-column.c
@@ -23,6 +23,7 @@
 #include "gda-util.h"
 #include "gda-ddl-buildable.h"
 #include "gda-ddl-base.h"
+#include "gda-server-provider.h"
 
 G_DEFINE_QUARK (gda-ddl-column-error, gda_ddl_column_error)
 
@@ -984,23 +985,33 @@ gda_ddl_column_prepare_create  (GdaDdlColumn *self,
                                 guint order,
                                 GError **error)
 {
+  GdaConnection *cnc;
+  const gchar *strtype;
+
   g_return_val_if_fail(self,FALSE);
   g_return_val_if_fail(op,FALSE);
 
   GdaDdlColumnPrivate *priv = gda_ddl_column_get_instance_private (self);
 
-  if(!gda_server_operation_set_value_at(op,priv->mp_name,error,"/FIELDS_A/@COLUMN_NAME/%d",order))
+  if(!gda_server_operation_set_value_at (op, priv->mp_name, error, "/FIELDS_A/@COLUMN_NAME/%d", order))
     return FALSE;
-
-  if(!gda_server_operation_set_value_at(op,priv->mp_type,error,"/FIELDS_A/@COLUMN_TYPE/%d",order))
+  cnc = (GdaConnection*) g_object_get_data (G_OBJECT (op), "connection");
+  if (cnc == NULL) {
+    g_set_error (error, GDA_DDL_COLUMN_ERROR, GDA_DDL_COLUMN_ERROR_TYPE,
+                 _("Internal error: Operation should be prepared, setting a connection data"));
+    return FALSE;
+  }
+  strtype = gda_server_provider_get_default_dbms_type (gda_connection_get_provider (cnc),
+                                                       cnc, priv->m_gtype);
+  if(!gda_server_operation_set_value_at (op, strtype, error, "/FIELDS_A/@COLUMN_TYPE/%d", order))
     return FALSE;
 
   gchar *numstr = NULL;
-  numstr = g_strdup_printf ("%d",priv->m_size);
+  numstr = g_strdup_printf ("%d", priv->m_size);
 
-  if (g_type_is_a (priv->m_gtype,G_TYPE_STRING))
+  if (g_type_is_a (priv->m_gtype, G_TYPE_STRING))
     {
-      if(!gda_server_operation_set_value_at(op,numstr,error,"/FIELDS_A/@COLUMN_SIZE/%d",order))
+      if(!gda_server_operation_set_value_at (op, numstr, error, "/FIELDS_A/@COLUMN_SIZE/%d", order))
         {
           g_free (numstr);
           return FALSE;
@@ -1031,28 +1042,28 @@ gda_ddl_column_prepare_create  (GdaDdlColumn *self,
         }
     }
 
-  if(!gda_server_operation_set_value_at(op,GDA_BOOL_TO_STR (priv->m_nnul), error,
+  if(!gda_server_operation_set_value_at (op, GDA_BOOL_TO_STR (priv->m_nnul), error,
                                         "/FIELDS_A/@COLUMN_NNUL/%d",order))
     return FALSE;
 
-  if(!gda_server_operation_set_value_at(op,GDA_BOOL_TO_STR (priv->m_autoinc),error,
+  if(!gda_server_operation_set_value_at (op, GDA_BOOL_TO_STR (priv->m_autoinc), error,
                                         "/FIELDS_A/@COLUMN_AUTOINC/%d",order))
     return FALSE;
 
-  if(!gda_server_operation_set_value_at(op,GDA_BOOL_TO_STR (priv->m_unique),error,
+  if(!gda_server_operation_set_value_at (op, GDA_BOOL_TO_STR (priv->m_unique), error,
                                         "/FIELDS_A/@COLUMN_UNIQUE/%d",order))
     return FALSE;
 
-  if(!gda_server_operation_set_value_at(op,GDA_BOOL_TO_STR (priv->m_pkey),error,
+  if(!gda_server_operation_set_value_at (op, GDA_BOOL_TO_STR (priv->m_pkey), error,
                                         "/FIELDS_A/@COLUMN_PKEY/%d",order))
     return FALSE;
 
-  if(!gda_server_operation_set_value_at(op,priv->mp_default,error,
-                                        "/FIELDS_A/@COLUMN_DEFAULT/%d",order))
+  if(!gda_server_operation_set_value_at (op,priv->mp_default, error,
+                                        "/FIELDS_A/@COLUMN_DEFAULT/%d", order))
     return FALSE;
 
-  if(!gda_server_operation_set_value_at(op,priv->mp_check,error,
-                                        "/FIELDS_A/@COLUMN_CHECK/%d",order))
+  if(!gda_server_operation_set_value_at (op,priv->mp_check,error,
+                                        "/FIELDS_A/@COLUMN_CHECK/%d", order))
     return FALSE;
 
   return TRUE;
diff --git a/libgda/gda-ddl-column.h b/libgda/gda-ddl-column.h
index 3748ec105..6187ce937 100644
--- a/libgda/gda-ddl-column.h
+++ b/libgda/gda-ddl-column.h
@@ -46,7 +46,7 @@ struct _GdaDdlColumnClass
  */
 typedef  enum {
   GDA_DDL_COLUMN_ERROR_TYPE
-}GdaDdlColumnError;
+} GdaDdlColumnError;
 
 #define GDA_DDL_COLUMN_ERROR gda_ddl_column_error_quark()
 GQuark gda_ddl_column_error_quark (void);
diff --git a/libgda/gda-ddl-creator.c b/libgda/gda-ddl-creator.c
index e1929ad6d..1d0d8f345 100644
--- a/libgda/gda-ddl-creator.c
+++ b/libgda/gda-ddl-creator.c
@@ -573,7 +573,6 @@ gda_ddl_creator_get_view (GdaDdlCreator *self,
 /** 
  * gda_ddl_creator_parse_cnc:
  * @self: a #GdaDdlCreator instance 
- * @cnc: Connection to parse
  * @error: error storage object
  *
  * Parse cnc to populate @self object. This method should be called every time after database was
@@ -688,7 +687,6 @@ gda_ddl_creator_append_view (GdaDdlCreator *self,
 /**
  * gda_ddl_creator_perform_operation:
  * @self: a #GdaDdlCreator object
- * @cnc: a connection object to work with
  * @error: object to store error
  *
  * After population @self with all data this method may be
@@ -715,7 +713,8 @@ gboolean
 gda_ddl_creator_perform_operation (GdaDdlCreator *self,
                                    GError **error)
 {
-  g_return_val_if_fail (self,FALSE);
+  g_return_val_if_fail (self, FALSE);
+  gboolean st = TRUE;
 
   GdaDdlCreatorPrivate *priv = gda_ddl_creator_get_instance_private (self);
 
@@ -724,7 +723,7 @@ gda_ddl_creator_perform_operation (GdaDdlCreator *self,
       g_set_error (error,
                    GDA_DDL_CREATOR_ERROR,
                    GDA_DDL_CREATOR_CONNECTION_CLOSED,
-                   _("Connection is not open"));
+                   _("Connection is not opened"));
       return FALSE;
     }
 
@@ -755,48 +754,44 @@ gda_ddl_creator_perform_operation (GdaDdlCreator *self,
        * Other databases may also ignore schema and reference objects only by database and
        * name, e.g. db_name.table_name 
        * */
-      g_value_set_string (catalog,gda_ddl_base_get_catalog(it->data));
-      g_value_set_string (schema ,gda_ddl_base_get_schema (it->data));
-      g_value_set_string (name   ,gda_ddl_base_get_name   (it->data));
+      g_value_set_string (catalog, gda_ddl_base_get_catalog(it->data));
+      g_value_set_string (schema , gda_ddl_base_get_schema (it->data));
+      g_value_set_string (name   , gda_ddl_base_get_name   (it->data));
 
-      mobj = gda_meta_struct_complement (mstruct,GDA_META_DB_TABLE,catalog,schema,name,NULL);
+      mobj = gda_meta_struct_complement (mstruct, GDA_META_DB_TABLE, catalog, schema,name, NULL);
 
-      if (mobj)
-        {
-          if(!gda_ddl_table_update (it->data,GDA_META_TABLE(mobj),priv->cnc,error))
-            goto on_error;
+      if (mobj) {
+        st = gda_ddl_table_update (it->data,GDA_META_TABLE(mobj), priv->cnc, error);
+        if (!st) {
+          break;
         }
-      else
-        {
-          if(!gda_ddl_table_create (it->data,priv->cnc,TRUE,error))
-            goto on_error;
+      }
+      else {
+        if(st = gda_ddl_table_create (it->data,priv->cnc, TRUE, error))
+        if (!st) {
+          break;
         }
+      }
     } /* End of for loop */
 
   gda_value_free (catalog);
   gda_value_free (schema);
   gda_value_free (name);
 
-/*TODO: add update option for views */
-  for (it = priv->mp_views; it; it = it->next)
-    {
-      if(!gda_ddl_view_create (it->data,priv->cnc,error))
-        goto on_error;
+  if (st) {
+  /*TODO: add update option for views */
+    for (it = priv->mp_views; it; it = it->next) {
+        st = gda_ddl_view_create (it->data, priv->cnc, error);
+        if (!st) {
+          break;
+        }
     } /* End of for loop */
+  }
 
   g_object_unref (mstruct);
-  gda_lockable_unlock ((GdaLockable*)priv->cnc);
+  gda_lockable_unlock ((GdaLockable*) priv->cnc);
 
-  return TRUE;
-
-on_error:
-  gda_value_free (catalog);
-  gda_value_free (schema);
-  gda_value_free (name);
-  g_object_unref (mstruct);
-  gda_lockable_unlock ((GdaLockable*)priv->cnc);
-
-  return FALSE;
+  return st;
 }
 
 /**
diff --git a/libgda/gda-ddl-fkey.c b/libgda/gda-ddl-fkey.c
index a3c41e9af..e2f5a503e 100644
--- a/libgda/gda-ddl-fkey.c
+++ b/libgda/gda-ddl-fkey.c
@@ -615,22 +615,22 @@ gda_ddl_fkey_prepare_create  (GdaDdlFkey *self,
 {
   GdaDdlFkeyPrivate *priv = gda_ddl_fkey_get_instance_private (self);
 
-  if (!gda_server_operation_set_value_at(op,
-                                         priv->mp_ref_table,
-                                         error,
-                                         "/FKEY_S/%d/FKEY_REF_TABLE",i))
+  if (!gda_server_operation_set_value_at (op,
+                                          priv->mp_ref_table,
+                                          error,
+                                          "/FKEY_S/%d/FKEY_REF_TABLE", i))
     return FALSE;
 
-  if (!gda_server_operation_set_value_at(op,
-                                         OnAction[priv->m_ondelete],
-                                         error,
-                                         "/FKEY_S/%d/FKEY_ONDELETE",i))
+  if (!gda_server_operation_set_value_at (op,
+                                          OnAction[priv->m_ondelete],
+                                          error,
+                                          "/FKEY_S/%d/FKEY_ONDELETE", i))
     return FALSE;
 
-  if (!gda_server_operation_set_value_at(op,
-                                         OnAction[priv->m_onupdate],
-                                         error,
-                                         "/FKEY_S/%d/FKEY_ONUPDATE",i))
+  if (!gda_server_operation_set_value_at (op,
+                                          OnAction[priv->m_onupdate],
+                                          error,
+                                          "/FKEY_S/%d/FKEY_ONUPDATE", i))
     return FALSE;
 
   GList *itfield = NULL;
@@ -642,18 +642,18 @@ gda_ddl_fkey_prepare_create  (GdaDdlFkey *self,
 
   for (;itfield && itreffield;)
     {
-      if (!gda_server_operation_set_value_at(op,
-                                             itfield->data,
-                                             error,
-                                             "/FKEY_S/%d/FKEY_FIELDS_A/@FK_FIELD/%d",
-                                             i,fkeycount))
+      if (!gda_server_operation_set_value_at (op,
+                                              itfield->data,
+                                              error,
+                                              "/FKEY_S/%d/FKEY_FIELDS_A/@FK_FIELD/%d",
+                                              i, fkeycount))
         return FALSE;
 
-      if (!gda_server_operation_set_value_at(op,
-                                             itreffield->data,
-                                             error,
-                                             "/FKEY_S/%d/FKEY_FIELDS_A/@FK_REF_PK_FIELD/%d",
-                                             i,fkeycount))
+      if (!gda_server_operation_set_value_at (op,
+                                              itreffield->data,
+                                              error,
+                                              "/FKEY_S/%d/FKEY_FIELDS_A/@FK_REF_PK_FIELD/%d",
+                                              i, fkeycount))
         return FALSE;
 
       fkeycount++;
diff --git a/libgda/gda-ddl-table.c b/libgda/gda-ddl-table.c
index c19f09676..62b1fc79e 100644
--- a/libgda/gda-ddl-table.c
+++ b/libgda/gda-ddl-table.c
@@ -490,40 +490,40 @@ gda_ddl_table_prepare_create (GdaDdlTable *self,
 {
   GdaDdlTablePrivate *priv = gda_ddl_table_get_instance_private (self);
 
-  if (!gda_server_operation_set_value_at(op,
-                                         gda_ddl_base_get_name(GDA_DDL_BASE(self)),
-                                         error,
-                                         "/TABLE_DEF_P/TABLE_NAME"))
+  if (!gda_server_operation_set_value_at (op,
+                                          gda_ddl_base_get_name (GDA_DDL_BASE(self)),
+                                          error,
+                                          "/TABLE_DEF_P/TABLE_NAME"))
     return FALSE;
 
-  if (!gda_server_operation_set_value_at(op,
-                                         GDA_BOOL_TO_STR(priv->m_istemp),
-                                         error,
-                                         "/TABLE_DEF_P/TABLE_TEMP"))
+  if (!gda_server_operation_set_value_at (op,
+                                          GDA_BOOL_TO_STR(priv->m_istemp),
+                                          error,
+                                          "/TABLE_DEF_P/TABLE_TEMP"))
     return FALSE;
 
-  if (!gda_server_operation_set_value_at(op,
-                                         priv->mp_comment,
-                                         error,
-                                         "/TABLE_DEF_P/TABLE_COMMENT"))
+  if (!gda_server_operation_set_value_at (op,
+                                          priv->mp_comment,
+                                          error,
+                                          "/TABLE_DEF_P/TABLE_COMMENT"))
     return FALSE;
 
-  if (!gda_server_operation_set_value_at(op,
-                                         GDA_BOOL_TO_STR(ifnotexists),
-                                         error,
-                                         "/TABLE_DEF_P/TABLE_IFNOTEXISTS"))
+  if (!gda_server_operation_set_value_at (op,
+                                          GDA_BOOL_TO_STR (ifnotexists),
+                                          error,
+                                          "/TABLE_DEF_P/TABLE_IFNOTEXISTS"))
     return FALSE;
 
   GList *it = NULL;
   gint i = 0; /* column order counter */
 
   for (it = priv->mp_columns;it;it=it->next)
-    if(!gda_ddl_column_prepare_create (GDA_DDL_COLUMN(it->data),op,i++,error))
+    if(!gda_ddl_column_prepare_create (GDA_DDL_COLUMN (it->data), op, i++, error))
       return FALSE;
 
   i = 0;
   for (it = priv->mp_fkeys;it;it=it->next)
-    if(!gda_ddl_fkey_prepare_create (GDA_DDL_FKEY(it->data),op,i++,error))
+    if(!gda_ddl_fkey_prepare_create (GDA_DDL_FKEY(it->data), op, i++, error))
       return FALSE;
 
   return TRUE;
@@ -657,16 +657,22 @@ gda_ddl_table_create (GdaDdlTable *self,
                       gboolean ifnotexists,
                       GError **error)
 {
-  g_return_val_if_fail (self,FALSE);
-  g_return_val_if_fail (cnc,FALSE);
-
-  if (!gda_connection_is_opened(cnc))
-    return FALSE;
+  g_return_val_if_fail (self, FALSE);
+  g_return_val_if_fail (cnc, FALSE);
 
+ if (!gda_connection_is_opened (cnc))
+    {
+      g_set_error (error,
+                   GDA_DDL_CREATOR_ERROR,
+                   GDA_DDL_CREATOR_CONNECTION_CLOSED,
+                   _("Connection is not opened"));
+      return FALSE;
+    }
   gda_lockable_lock(GDA_LOCKABLE(cnc));
 
   GdaServerProvider *provider = NULL;
   GdaServerOperation *op = NULL;
+  gboolean res = FALSE;
 
   provider = gda_connection_get_provider (cnc);
 
@@ -675,25 +681,21 @@ gda_ddl_table_create (GdaDdlTable *self,
                                             GDA_SERVER_OPERATION_CREATE_TABLE,
                                             NULL,
                                             error);
-  if (!op)
-    goto on_error;
-
-
-  if (!gda_ddl_table_prepare_create(self,op,ifnotexists,error))
-    goto on_error;
-
-  if(!gda_server_provider_perform_operation(provider,cnc,op,error))
-    goto on_error;
-
-  g_object_unref (op);
+  if (op) {
+    g_object_set_data_full (G_OBJECT (op), "connection", g_object_ref (cnc), g_object_unref);
+    if (gda_ddl_table_prepare_create(self, op, ifnotexists, error)) {
+#ifdef GDA_DEBUG_NO
+      gchar* str = gda_server_operation_render (op, error);
+      g_message ("Operation: %s", str);
+      g_free (str);
+#endif
+      res = gda_server_provider_perform_operation(provider, cnc, op, error);
+    }
+    g_object_unref (op);
+  }
   gda_lockable_unlock(GDA_LOCKABLE(cnc));
 
-  return TRUE;
-
-on_error:
-  g_object_unref (op);
-  gda_lockable_unlock(GDA_LOCKABLE(cnc));
-  return FALSE;
+  return res;
 }
 
 /**
diff --git a/libgda/gda-server-operation.c b/libgda/gda-server-operation.c
index 2362d945f..6e4a3372a 100644
--- a/libgda/gda-server-operation.c
+++ b/libgda/gda-server-operation.c
@@ -2328,6 +2328,10 @@ gda_server_operation_set_value_at_path (GdaServerOperation *op, const gchar *val
 
        g_free (extension);
        g_free (colname);
+  if (!allok && (*error) == NULL) {
+    g_warning ("gda_server_operation_set_value_at_path: No Error set");
+  }
+
        return allok;
 }
 
@@ -2583,8 +2587,7 @@ gda_server_operation_perform_create_database (GdaServerOperation *op, const gcha
        if (prov)
                return gda_server_provider_perform_operation (prov, NULL, op, error);
        else {
-               g_warning ("Could not find operation's associated provider, "
-                          "did you use gda_server_operation_prepare_create_database() ?");
+               g_warning (_("Could not find operation's associated provider, did you use 
gda_server_operation_prepare_create_database() ?"));
                return FALSE;
        }
 }
diff --git a/libgda/gda-server-provider.c b/libgda/gda-server-provider.c
index 8b7079571..faec60a9b 100644
--- a/libgda/gda-server-provider.c
+++ b/libgda/gda-server-provider.c
@@ -1158,7 +1158,7 @@ gda_server_provider_perform_operation (GdaServerProvider *provider, GdaConnectio
 
        gpointer retval = NULL;
        gda_worker_do_job (worker, context, 0, &retval, NULL,
-                          (GdaWorkerFunc) worker_perform_operation, (gpointer) &data, NULL, NULL, NULL);
+                          (GdaWorkerFunc) worker_perform_operation, (gpointer) &data, NULL, NULL, error);
        if (context)
                g_main_context_unref (context);
 
@@ -1439,7 +1439,7 @@ gda_server_provider_get_default_dbms_type (GdaServerProvider *provider, GdaConne
                cdata = gda_connection_internal_get_provider_data_error (cnc, NULL);
                if (!cdata) {
                        gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */
-                       g_warning ("Internal error: connection reported as opened, yet no provider's data has 
been setted");
+                       g_warning (_("Internal error: connection reported as opened, yet no provider's data 
has been setted"));
                        return FALSE;
                }
                worker = gda_worker_ref (cdata->worker);
diff --git a/providers/postgres/gda-postgres-ddl.c b/providers/postgres/gda-postgres-ddl.c
index 9af18d4ef..2ecdf55e1 100644
--- a/providers/postgres/gda-postgres-ddl.c
+++ b/providers/postgres/gda-postgres-ddl.c
@@ -115,6 +115,7 @@ gda_postgres_render_CREATE_TABLE (GdaServerProvider *provider, GdaConnection *cn
 {
        GString *string;
        const GValue *value;
+       const GValue *valuex;
        const GValue *value1 = NULL;
        gboolean hasfields = FALSE;
        gint nrows;
@@ -130,6 +131,9 @@ gda_postgres_render_CREATE_TABLE (GdaServerProvider *provider, GdaConnection *cn
        if (value && G_VALUE_HOLDS (value, G_TYPE_BOOLEAN) && g_value_get_boolean (value))
                g_string_append (string, "TEMP ");
        g_string_append (string, "TABLE ");
+       valuex = gda_server_operation_get_value_at (op, "/TABLE_DEF_P/TABLE_IFNOTEXISTS");
+       if (valuex && G_VALUE_HOLDS (valuex, G_TYPE_BOOLEAN) && g_value_get_boolean (valuex))
+               g_string_append (string, "IF NOT EXISTS ");
 
        tmp = gda_connection_operation_get_sql_identifier_at (cnc, op,
                                                          "/TABLE_DEF_P/TABLE_NAME", error);
diff --git a/providers/postgres/postgres_specs_create_table.xml.in 
b/providers/postgres/postgres_specs_create_table.xml.in
index 4f16c5f03..ee8008106 100644
--- a/providers/postgres/postgres_specs_create_table.xml.in
+++ b/providers/postgres/postgres_specs_create_table.xml.in
@@ -11,6 +11,9 @@
     <parameter id="TABLE_TEMP" _name="Temporary table" gdatype="gboolean" nullok="TRUE">
       <gda_value>FALSE</gda_value>
     </parameter>
+    <parameter id="TABLE_IFNOTEXISTS" _name="Only if it does not yet exist" _descr="Create table only if it 
does not yet exist" gdatype="gboolean" nullok="TRUE">
+      <gda_value>FALSE</gda_value>
+    </parameter>
     <parameter id="TABLE_COMMENT" _name="Temporary table" gdatype="gchararray" nullok="TRUE"/>
   </parameters>
 
@@ -143,3 +146,4 @@
     </gda_array>
   </sources>
 </serv_op>
+
diff --git a/providers/reuseable/postgres/gda-postgres-meta.c 
b/providers/reuseable/postgres/gda-postgres-meta.c
index 5cf9c24b6..b35bb5df3 100644
--- a/providers/reuseable/postgres/gda-postgres-meta.c
+++ b/providers/reuseable/postgres/gda-postgres-meta.c
@@ -921,7 +921,7 @@ gboolean
 _gda_postgres_meta__tables_views (G_GNUC_UNUSED GdaServerProvider *prov, GdaConnection *cnc,
                                  GdaMetaStore *store, GdaMetaContext *context, GError **error)
 {
-  g_warning ("Updating Tables Views meta data");
+  g_message ("Updating Tables Views meta data");
        GdaDataModel *tables_model, *views_model;
        gboolean retval = TRUE;
 
diff --git a/tests/ddl/check-ddl-creator-postgresql.c b/tests/ddl/check-ddl-creator-postgresql.c
new file mode 100644
index 000000000..56df63ad8
--- /dev/null
+++ b/tests/ddl/check-ddl-creator-postgresql.c
@@ -0,0 +1,183 @@
+/* check-ddl-creator-postgresql.c
+ *
+ * Copyright 2018 Pavlo Solntsev <p sun fun gmail com>
+ * Copyright 2018 Daniel Espinosa <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
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <locale.h>
+#include <libgda/libgda.h>
+
+typedef struct {
+  GdaDdlCreator *creator;
+  gchar *xmlfile;
+  GdaConnection *cnc;
+  gboolean started_db;
+} CheckDdlObject;
+
+static void
+test_ddl_creator_start (CheckDdlObject *self,
+                     gconstpointer user_data)
+{
+  gda_init();
+  self->xmlfile = NULL;
+  self->creator = NULL;
+  self->cnc = NULL;
+  self->started_db = FALSE;
+
+  const gchar *topsrcdir = g_getenv ("GDA_TOP_SRC_DIR");
+
+  g_print ("ENV: %s\n",topsrcdir);
+  g_assert_nonnull (topsrcdir);
+
+  self->xmlfile = g_build_filename(topsrcdir,
+                                   "tests",
+                                   "ddl",
+                                   "ddl-db.xml",NULL);
+
+  g_assert_nonnull (self->xmlfile);
+
+  self->cnc = gda_connection_new_from_string("Postgresql",
+                                             "DB_NAME=test;HOST=localhost;USERNAME=test;PASSWORD=test1",
+                                             NULL,
+                                             GDA_CONNECTION_OPTIONS_NONE,
+                                             NULL);
+  if (self->cnc == NULL) {
+    g_print ("Postgres test not run, please setup a database 'test', owned by 'test' role with password 
'test1' at localhost\n");
+    g_print ("Test Skip.\n");
+    return;
+  }
+
+  g_assert_nonnull (self->cnc);
+
+  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);
+
+  /* Create DataBase structure */
+
+  GError *error = NULL;
+  gboolean res = FALSE;
+  GdaDdlTable *table = NULL;
+  GdaDdlColumn *column_id;
+  GdaDdlColumn *column_name;
+  GdaDdlColumn *column_ctime;
+  GdaDdlColumn *column_ts;
+  GdaDdlColumn *column_state;
+
+  if (self->cnc == NULL) {
+    return;
+  }
+
+  g_assert_nonnull (self->creator);
+
+  table = gda_ddl_table_new ();
+  gda_ddl_base_set_name (GDA_DDL_BASE(table),"dntypes");
+
+  column_id = gda_ddl_column_new ();
+  gda_ddl_column_set_name (column_id,"id");
+  gda_ddl_column_set_type (column_id, G_TYPE_INT);
+  gda_ddl_column_set_autoinc (column_id, TRUE);
+  gda_ddl_column_set_pkey (column_id, TRUE);
+
+  gda_ddl_table_append_column (table,column_id);
+
+  column_name = gda_ddl_column_new ();
+  gda_ddl_column_set_name (column_name,"name");
+  gda_ddl_column_set_type (column_name, G_TYPE_STRING);
+  gda_ddl_column_set_size (column_name, 50);
+
+  gda_ddl_table_append_column (table,column_name);
+
+  column_ctime = gda_ddl_column_new ();
+  gda_ddl_column_set_name (column_ctime,"create_time");
+  gda_ddl_column_set_type (column_ctime, GDA_TYPE_TIME);
+
+  gda_ddl_table_append_column (table,column_ctime);
+
+  column_state = gda_ddl_column_new ();
+  gda_ddl_column_set_name (column_state,"state");
+  gda_ddl_column_set_type (column_state, G_TYPE_BOOLEAN);
+
+  gda_ddl_table_append_column (table,column_state);
+
+  column_ts = gda_ddl_column_new ();
+  gda_ddl_column_set_name (column_ts,"mytimestamp");
+  gda_ddl_column_set_type (column_ts, G_TYPE_DATE_TIME);
+
+  gda_ddl_table_append_column (table,column_ts);
+
+  gda_ddl_creator_append_table (self->creator, table);
+  res = gda_ddl_table_create (table, self->cnc, TRUE, &error);
+  if (!res) {
+    g_warning ("Error Creating table: %s", error->message);
+  }
+  g_assert_true (res);
+}
+
+static void
+test_ddl_creator_finish (CheckDdlObject *self,
+                      gconstpointer user_data)
+{
+  gda_connection_close(self->cnc,NULL);
+  g_free (self->xmlfile);
+  g_object_unref (self->creator);
+  g_object_unref (self->cnc);
+}
+
+
+static void
+test_tables (CheckDdlObject *self,
+             gconstpointer user_data)
+{
+  g_message ("Testing Tables...");
+  if (self->cnc == NULL) {
+      return;
+  }
+  GList *tables = gda_ddl_creator_get_tables (self->creator);
+  g_assert (tables != NULL);
+  g_assert (g_list_length (tables) != 0);
+  g_assert (g_list_length (tables) == 1);
+  GList *lt = NULL;
+  for (lt = tables; lt; lt = tables->next) {
+    GdaDdlTable *table = (GdaDdlTable *) lt->data;
+    g_message ("Table found: %s", gda_ddl_base_get_full_name (GDA_DDL_BASE (table)));
+  }
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  setlocale (LC_ALL,"");
+
+  g_test_init (&argc,&argv,NULL);
+
+  g_test_add ("/test-ddl-postgresql/meta-tables",
+              CheckDdlObject,
+              NULL,
+              test_ddl_creator_start,
+              test_tables,
+              test_ddl_creator_finish);
+
+  return g_test_run();
+}
diff --git a/tests/ddl/meson.build b/tests/ddl/meson.build
index 7319fe77f..dbd8a5ff5 100644
--- a/tests/ddl/meson.build
+++ b/tests/ddl/meson.build
@@ -142,3 +142,30 @@ test('ddlcreate', ddlcreate,
                'GDA_TOP_BUILD_DIR='+meson.build_root()
                ]
        )
+
+ddl_creator_pg_sources = files([
+       'check-ddl-creator-postgresql.c'
+       ])
+
+tcpg = executable('check-ddl-creator-postgresql',
+       ddl_creator_pg_sources,
+       c_args: [
+               '-include',
+               meson.build_root() + '/config.h',
+               '-DCHECK_FILES="'+meson.source_root()+'"',
+               '-DTOP_BUILD_DIR="'+meson.build_root()+'"'
+               ],
+       link_with: libgda,
+       dependencies: [
+               libgda_dep,
+               inc_rooth_dep,
+               inc_sqliteh_dep
+               ],
+       install: false
+       )
+test('ddlcreator-postgresql', tcpg,
+       env: [
+               'GDA_TOP_SRC_DIR='+meson.source_root(),
+               'GDA_TOP_BUILD_DIR='+meson.build_root()
+               ]
+       )
\ No newline at end of file


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