[libgda: 1/2] connection and provider properties for GdaServerOperation



commit c4fbbaa6c86ca19a6c0b343f897d009d2c19d448
Author: Pavlo Solntsev <p sun fun gmail com>
Date:   Thu Jul 19 03:52:27 2018 +0000

    connection and provider properties for GdaServerOperation

 libgda/gda-server-operation.c              | 57 +++++++++++++++++++
 libgda/gda-server-operation.h              |  2 +
 libgda/sqlite/gda-sqlite-provider.c        | 60 +++++++++-----------
 providers/firebird/gda-firebird-provider.c | 22 ++++----
 providers/mysql/gda-mysql-provider.c       | 24 ++++----
 providers/oracle/gda-oracle-provider.c     | 24 ++++----
 providers/postgres/gda-postgres-provider.c | 24 ++++----
 tests/Makefile.am                          | 10 +++-
 tests/meson.build                          | 23 ++++++++
 tests/test-server-operation.c              | 90 ++++++++++++++++++++++++++++++
 10 files changed, 254 insertions(+), 82 deletions(-)
---
diff --git a/libgda/gda-server-operation.c b/libgda/gda-server-operation.c
index 0fe399cc0..87bb979ad 100644
--- a/libgda/gda-server-operation.c
+++ b/libgda/gda-server-operation.c
@@ -3521,3 +3521,60 @@ gda_server_operation_perform_drop_table (GdaServerOperation *op, GError **error)
                return FALSE;
        }
 }
+
+/**
+ * gda_server_operation_perform:
+ * @op: a #GdaServerOperation object
+ * @error: (allow-none): a place to store an error, or %NULL
+ *
+ * Performs the operation described by @op. Note that @op is not destroyed by this method
+ * and can be reused.
+ *
+ * Returns: %TRUE if no error occurred
+ */
+gboolean
+gda_server_operation_perform (GdaServerOperation *op, GError **error)
+{
+  GdaServerProvider *provider;
+  GdaConnection *cnc;
+
+  g_return_val_if_fail (op,FALSE);
+  g_return_val_if_fail (GDA_IS_SERVER_OPERATION (op), FALSE);
+
+  g_object_get (op,"provider",&provider,"connection",&cnc,NULL);
+
+  g_return_val_if_fail (GDA_IS_SERVER_PROVIDER (provider), FALSE);
+  g_return_val_if_fail (GDA_IS_CONNECTION (cnc), FALSE);
+
+  return gda_server_provider_perform_operation (provider,cnc,op,error);
+}
+
+/**
+ * gda_server_operation_render:
+ * @op: a #GdaServerOperation object
+ * @error: (allow-none): a place to store an error, or %NULL
+ *
+ * Creates an SQL statement (possibly using some specific extensions of the DBMS) corresponding to the
+ * @op operation. Note that the returned string may actually contain more than one SQL statement.
+ *
+ * This function's purpose is mainly informative to get the actual SQL code which would be executed to 
perform
+ * the operation; to actually perform the operation, use gda_server_operation_perform().
+ *
+ * Returns: (transfer full) (allow-none): a new string, or %NULL if an error occurred or operation cannot be 
rendered as SQL.
+ */
+gchar *
+gda_server_operation_render (GdaServerOperation *op, GError **error)
+{
+  GdaServerProvider *provider;
+  GdaConnection *cnc;
+
+  g_return_val_if_fail (op,NULL);
+  g_return_val_if_fail (GDA_IS_SERVER_OPERATION(op),NULL);
+
+  g_object_get (op,"provider",&provider,"connection",&cnc,NULL);
+
+       g_return_val_if_fail (GDA_IS_SERVER_PROVIDER (provider), NULL);
+       g_return_val_if_fail (GDA_IS_CONNECTION(cnc), NULL);
+
+       return (gchar*) gda_server_provider_render_operation(provider,cnc,op,error);
+}
diff --git a/libgda/gda-server-operation.h b/libgda/gda-server-operation.h
index 692abfba4..ee9476c3e 100644
--- a/libgda/gda-server-operation.h
+++ b/libgda/gda-server-operation.h
@@ -248,6 +248,8 @@ GdaServerOperation *gda_server_operation_prepare_create_database       (const gc
 gboolean            gda_server_operation_perform_create_database       (GdaServerOperation *op, const gchar 
*provider, GError **error);
 GdaServerOperation *gda_server_operation_prepare_drop_database         (const gchar *provider, const gchar 
*db_name, GError **error);
 gboolean            gda_server_operation_perform_drop_database         (GdaServerOperation *op, const gchar 
*provider, GError **error);
+gboolean            gda_server_operation_perform                       (GdaServerOperation *op, GError 
**error);
+gchar              *gda_server_operation_render                        (GdaServerOperation *op, GError 
**error);
 
 /*
  * Tables creation and destruction
diff --git a/libgda/sqlite/gda-sqlite-provider.c b/libgda/sqlite/gda-sqlite-provider.c
index cc2e6245d..d03a91c8f 100644
--- a/libgda/sqlite/gda-sqlite-provider.c
+++ b/libgda/sqlite/gda-sqlite-provider.c
@@ -1159,42 +1159,36 @@ gda_sqlite_provider_supports_operation (G_GNUC_UNUSED GdaServerProvider *provide
  * Create operation request
  */
 static GdaServerOperation *
-gda_sqlite_provider_create_operation (GdaServerProvider *provider, G_GNUC_UNUSED GdaConnection *cnc,
+gda_sqlite_provider_create_operation (GdaServerProvider *provider, GdaConnection *cnc,
                                      GdaServerOperationType type,
                                      G_GNUC_UNUSED GdaSet *options, GError **error)
 {
-        gchar *file;
-        GdaServerOperation *op;
-        gchar *str;
-       gchar *dir;
-        file = g_strdup_printf (PNAME "_specs_%s", gda_server_operation_op_type_to_string (type));
-        str = g_utf8_strdown (file, -1);
-        g_free (file);
-
-       gchar *tmp;
-       tmp = g_strdup_printf ("%s.xml", str);
-       dir = gda_gbr_get_file_path (GDA_DATA_DIR, LIBGDA_ABI_NAME, NULL);
-        file = gda_server_provider_find_file (provider, dir, tmp);
-       g_free (dir);
-       g_free (tmp);
-
-       if (file) {
-               g_free (str);
-               op = gda_server_operation_new (type, file);
-               g_free (file);
-       }
-       else {
-               gchar *lpname;
-               lpname = g_utf8_strdown (PNAME, -1);
-               file = g_strdup_printf ("/spec/%s/%s.raw.xml", lpname, str);
-               g_free (str);
-               g_free (lpname);
-               op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, "op-type", type,
-                                                        "spec-resource", file, NULL));
-               g_free (file);
-       }
-
-        return op;
+  gchar *file;
+  GdaServerOperation *op;
+  gchar *str;
+  
+  file = g_strdup_printf (PNAME "_specs_%s", gda_server_operation_op_type_to_string (type));
+  str = g_utf8_strdown (file, -1);
+ 
+  g_free (file);
+  file = NULL;
+
+  gchar *lpname;
+  lpname = g_utf8_strdown (PNAME, -1);
+  file = g_strdup_printf ("/spec/%s/%s.raw.xml", lpname, str);
+  g_free (lpname);
+
+  op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, 
+                                           "op-type", type, 
+                                           "spec-resource", file, 
+                                           "provider", provider, 
+                                           "connection", cnc,
+                                           NULL));
+
+  g_free (file);
+  g_free (str);
+
+  return op;
 }
 
 /*
diff --git a/providers/firebird/gda-firebird-provider.c b/providers/firebird/gda-firebird-provider.c
index 7ed843694..232bbde25 100644
--- a/providers/firebird/gda-firebird-provider.c
+++ b/providers/firebird/gda-firebird-provider.c
@@ -623,17 +623,17 @@ gda_firebird_provider_create_operation (GdaServerProvider *provider, GdaConnecti
        g_free (dir);
         g_free (str);
 
-       if (file) {
-               op = gda_server_operation_new (type, file);
-               g_free (file);
-       }
-       else {
-               file = g_strdup_printf ("/spec/firebird/%s.raw.xml", stype);
-               op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, "op-type", type,
-                                                        "spec-resource", file, NULL));
-               g_free (file);
-        }
-       g_free (stype);
+  if (!file)
+    file = g_strdup_printf ("/spec/firebird/%s.raw.xml", stype);
+        
+  op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, 
+                                           "op-type", type, 
+                                           "spec-resource", file, 
+                                           "connection", cnc,
+                                           "provider", provider,
+                                           NULL));
+  g_free (stype);
+  g_free (file);
 
         return op;
 }
diff --git a/providers/mysql/gda-mysql-provider.c b/providers/mysql/gda-mysql-provider.c
index a87d920e7..149d03b1e 100644
--- a/providers/mysql/gda-mysql-provider.c
+++ b/providers/mysql/gda-mysql-provider.c
@@ -832,18 +832,18 @@ gda_mysql_provider_create_operation (GdaServerProvider       *provider,
        g_free (dir);
        g_free (tmp);
 
-        if (file) {
-               g_free (str);
-               op = gda_server_operation_new (type, file);
-               g_free (file);
-       }
-       else {
-               file = g_strdup_printf ("/spec/mysql/%s.raw.xml", str);
-               g_free (str);
-               op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, "op-type", type,
-                                                        "spec-resource", file, NULL));
-               g_free (file);
-        }
+  if (!file)
+    file = g_strdup_printf ("/spec/mysql/%s.raw.xml", str);
+
+  op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, 
+                                           "op-type", type, 
+                                           "spec-resource", file, 
+                                           "connection",cnc,
+                                           "provider",provider,
+                                           NULL));
+
+  g_free (str);
+  g_free (file);
 
         return op;
 }
diff --git a/providers/oracle/gda-oracle-provider.c b/providers/oracle/gda-oracle-provider.c
index a1cdb3643..de8b3ce8f 100644
--- a/providers/oracle/gda-oracle-provider.c
+++ b/providers/oracle/gda-oracle-provider.c
@@ -892,18 +892,18 @@ gda_oracle_provider_create_operation (GdaServerProvider *provider, GdaConnection
        g_free (dir);
        g_free (tmp);
 
-        if (file) {
-               g_free (str);
-               op = gda_server_operation_new (type, file);
-               g_free (file);
-        }
-       else {
-               file = g_strdup_printf ("/spec/oracle/%s.raw.xml", str);
-               g_free (str);
-               op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, "op-type", type,
-                                                        "spec-resource", file, NULL));
-               g_free (file);
-       }
+  if (!file) 
+    file = g_strdup_printf ("/spec/oracle/%s.raw.xml", str);
+
+  op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, 
+                                           "op-type", type, 
+                                           "spec-resource", file, 
+                                           "connection", cnc,
+                                           "provider", provider,
+                                           NULL));
+
+  g_free (str);
+  g_free (file);
 
         return op;
 }
diff --git a/providers/postgres/gda-postgres-provider.c b/providers/postgres/gda-postgres-provider.c
index d79714efc..b81e1d7f4 100644
--- a/providers/postgres/gda-postgres-provider.c
+++ b/providers/postgres/gda-postgres-provider.c
@@ -860,18 +860,18 @@ gda_postgres_provider_create_operation (GdaServerProvider *provider, GdaConnecti
        g_free (dir);
        g_free (tmp);
 
-       if (file) {
-               g_free (str);
-               op = gda_server_operation_new (type, file);
-               g_free (file);
-       }
-       else {
-               file = g_strdup_printf ("/spec/postgres/%s.raw.xml", str);
-               g_free (str);
-               op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, "op-type", type,
-                                                        "spec-resource", file, NULL));
-               g_free (file);
-        }
+  if (!file) 
+    file = g_strdup_printf ("/spec/postgres/%s.raw.xml", str);
+
+  op = GDA_SERVER_OPERATION (g_object_new (GDA_TYPE_SERVER_OPERATION, 
+                                           "op-type", type, 
+                                           "spec-resource", file, 
+                                           "connection",cnc,
+                                           "provider",provider,
+                                           NULL));
+      
+  g_free (str);
+  g_free (file);
 
         return op;
 }
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5769ec1a1..7c80272f6 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,8 +1,8 @@
 noinst_LTLIBRARIES = libgda-test-6.0.la
 
 TESTS_ENVIRONMENT = GDA_TOP_SRC_DIR="$(abs_top_srcdir)" GDA_TOP_BUILD_DIR="$(abs_top_builddir)"
-TESTS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes test-sql-builder 
test-connection-string-split test-input-parsers test-quark-list test-sql-renderer
-check_PROGRAMS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes 
test-sql-builder test-connection-string-split test-input-parsers test-quark-list test-sql-renderer
+TESTS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes test-sql-builder 
test-connection-string-split test-input-parsers test-quark-list test-sql-renderer test-server-operation 
+check_PROGRAMS = test-ddl-creator test-bin-converter test-sql-identifier test-identifiers-quotes 
test-sql-builder test-connection-string-split test-input-parsers test-quark-list test-sql-renderer 
test-server-operation
 
 if HAVE_UI
 UI_EXTENSION = ui
@@ -102,5 +102,11 @@ test_sql_renderer_LDADD = \
        libgda-test-6.0.la \
         $(COREDEPS_LIBS)
 
+test_server_operation_SOURCES = \
+        test-server-operation.c
+
+test_server_operation_LDADD = \
+        $(top_builddir)/libgda/libgda-6.0.la \
+        $(COREDEPS_LIBS)
 
 EXTRA_DIST = dbstruct.xml
diff --git a/tests/meson.build b/tests/meson.build
index 6e14d3a8f..723a9d3d5 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -229,6 +229,29 @@ test('SqlRenderer', tsqlr,
                ]
        )
 
+tso = executable('test-server-operation',
+       ['test-server-operation.c'] ,
+       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('ServerOperation', tso,
+       env: [
+               'GDA_TOP_SRC_DIR='+meson.source_root(),
+               'GDA_TOP_BUILD_DIR='+meson.build_root()
+               ]
+       )
+
 subdir('data-models')
 subdir('meta-store')
 subdir('multi-threading')
diff --git a/tests/test-server-operation.c b/tests/test-server-operation.c
new file mode 100644
index 000000000..81d57b722
--- /dev/null
+++ b/tests/test-server-operation.c
@@ -0,0 +1,90 @@
+/* check-server-operation.c
+ *
+ * Copyright (C) 2018 Pavlo Solntsev <p sun fun 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 {
+    GdaServerOperation *op;
+    GdaConnection *cnc;
+    GdaServerProvider *provider;
+} CheckOPObject;
+
+static void
+test_server_operation_provider (CheckOPObject *self,
+                                gconstpointer user_data)
+{
+  GdaServerProvider *server = NULL;
+  GdaConnection *cnc = NULL;
+
+  g_object_get (self->op,"provider",&server,"connection",&cnc,NULL);
+
+  g_assert_nonnull (server);
+  g_assert_nonnull (cnc);
+
+  g_assert_true (GDA_IS_SERVER_PROVIDER(server));
+  g_assert_true (GDA_IS_CONNECTION(cnc));
+}
+
+static void
+test_server_operation_start (CheckOPObject *self,
+                             gconstpointer user_data)
+{
+  self->cnc = gda_connection_open_from_string ("SQLite", "DB_DIR=.;DB_NAME=op_test_db", NULL,
+                                               GDA_CONNECTION_OPTIONS_NONE, NULL);
+  g_assert_nonnull (self->cnc);
+
+  self->provider = gda_connection_get_provider (self->cnc);
+
+  self->op = gda_server_provider_create_operation (self->provider,
+                                                   self->cnc,
+                                                   GDA_SERVER_OPERATION_CREATE_TABLE,
+                                                   NULL,
+                                                   NULL); 
+  g_assert_nonnull (self->op);
+}
+
+static void
+test_server_operation_finish (CheckOPObject *self,
+                              gconstpointer user_data)
+{
+  g_object_unref (self->op);
+  gda_connection_close (self->cnc,NULL);
+}
+
+gint 
+main(gint argc, gchar *argv[])
+{
+  setlocale (LC_ALL,"");
+
+  g_test_init (&argc,&argv,NULL);
+
+  g_test_add ("/test-server-operation/provider",
+              CheckOPObject,
+              NULL,
+              test_server_operation_start,
+              test_server_operation_provider,
+              test_server_operation_finish);
+
+  return g_test_run();
+}
+


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