[planner: 12/40] unknown type name 'GdaCommand'.




commit 719eb7a55d3ee657a768105444d06c5ef2137c30
Author: Ahmed Baïzid <ahmed baizid org>
Date:   Thu Jun 18 14:49:13 2015 +0200

    unknown type name 'GdaCommand'.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=584556

 libplanner/mrp-sql.c     |  83 +++++++++++++++++--------------
 src/planner-sql-plugin.c | 127 ++++++++++++++++++++++++++++++++---------------
 2 files changed, 135 insertions(+), 75 deletions(-)
---
diff --git a/libplanner/mrp-sql.c b/libplanner/mrp-sql.c
index 8b0121f6..500ffd29 100644
--- a/libplanner/mrp-sql.c
+++ b/libplanner/mrp-sql.c
@@ -27,6 +27,7 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <libgda/libgda.h>
+#include <libgda/sql-parser/gda-sql-parser.h>
 #include <libplanner/planner.h>
 #include <libplanner/mrp-private.h>
 #include "mrp-storage-sql.h"
@@ -226,46 +227,56 @@ sql_quote_and_escape_string(SQLData   *data,
        *string = quoted_string;
 }
 
+/*
+ * sql_execute_command:
+ * @cnc: an opened connection
+ * @sql: a query statement that must not begin with "SELECT"
+ *
+ * Execute a single non-SELECT SQL statement over an opened connection.
+ *
+ * src/planner-sql-plugin.c duplicates the code.
+ *
+ * Returns: %TRUE on success, %FALSE on failure.
+ *
+ * Deprecated: 0.14.6: Use gda_connection_execute_non_select_command() instead.
+ */
 static gboolean
-sql_execute_command (GdaConnection *con, gchar *command)
+sql_execute_command (GdaConnection *cnc, gchar *sql)
 {
-       GdaCommand *cmd;
-       GList      *list;
-       GError     *error = NULL;
-
-       cmd = gda_command_new (command, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
-       list = gda_connection_execute_command (con, cmd, NULL, &error);
-       gda_command_free (cmd);
-
-       while (list) {
-               if (list->data) {
-                       g_object_unref (list->data);
-               }
-               list = list->next;
-       }
-       g_list_free (list);
-
-       if(error != NULL) {
-               g_error_free (error);
-               return FALSE;
-       }
-       else
-       {
-               return TRUE;
-       }
+    GError *error = NULL;
+    gda_connection_execute_non_select_command (cnc, sql, &error);
+    if (error) {
+        g_warning (error->message);
+        g_clear_error (&error);
+        return FALSE;
+    }
+    return TRUE;
 }
 
+/*
+ * sql_execute_query:
+ * @cnc: an opened connection
+ * @sql: a query statement that must begin with "SELECT"
+ *
+ * Execute an SQL SELECT command over an opened connection.
+ *
+ * src/planner-sql-plugin.c duplicates the code.
+ *
+ * Returns: (transfer full): a new #GdaDataModel if successful, %NULL otherwise
+ *
+ * Deprecated: 0.14.6: Use gda_connection_execute_select_command() instead.
+ */
 static GdaDataModel *
-sql_execute_query (GdaConnection *con, gchar *query)
+sql_execute_query (GdaConnection *cnc, gchar *sql)
 {
-       GdaCommand    *cmd;
-       GdaDataModel  *result;
-
-       cmd = gda_command_new (query, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
-       result = gda_connection_execute_select_command (con, cmd, NULL, NULL);
-       gda_command_free (cmd);
-
-       return result;
+    GdaDataModel *result;
+    GError *error = NULL;
+    result = gda_connection_execute_select_command (cnc, sql, &error);
+    if (error) {
+        g_warning (error->message);
+        g_clear_error (&error);
+    }
+    return result;
 }
 
 static const gchar *
@@ -1265,7 +1276,7 @@ sql_read_day_types (SQLData *data)
        g_object_unref (model);
        model = NULL;
 
-       sql_execute_query (data->con, "CLOSE mycursor");
+       sql_execute_command (data->con, "CLOSE mycursor");
 
        return TRUE;
 
@@ -1587,7 +1598,7 @@ sql_read_groups (SQLData *data)
        g_object_unref (model);
        model = NULL;
 
-       sql_execute_query (data->con, "CLOSE mycursor");
+       sql_execute_command (data->con, "CLOSE mycursor");
 
        return TRUE;
 
diff --git a/src/planner-sql-plugin.c b/src/planner-sql-plugin.c
index 5d322b83..834dc51e 100644
--- a/src/planner-sql-plugin.c
+++ b/src/planner-sql-plugin.c
@@ -30,6 +30,7 @@
 #include <glade/glade.h>
 #include <gtk/gtk.h>
 #include <libgda/libgda.h>
+#include <libgda/sql-parser/gda-sql-parser.h>
 #include <libplanner/mrp-paths.h>
 
 #include "planner-conf.h"
@@ -73,10 +74,10 @@ static void          sql_plugin_open                (GtkAction      *action,
                                                     gpointer        user_data);
 static void          sql_plugin_save                (GtkAction      *action,
                                                     gpointer        user_data);
-static GdaDataModel *sql_execute_query              (GdaConnection  *con,
-                                                    gchar          *query);
-static gboolean      sql_execute_command            (GdaConnection  *con,
-                                                    gchar          *command);
+static GdaDataModel *sql_execute_query              (GdaConnection  *cnc,
+                                                    gchar          *sql);
+static gboolean      sql_execute_command            (GdaConnection  *cnc,
+                                                    gchar          *sql);
 static const gchar * sql_get_last_error             (GdaConnection  *connection);
 void                 plugin_init                    (PlannerPlugin  *plugin);
 void                 plugin_exit                    (PlannerPlugin  *plugin);
@@ -98,46 +99,94 @@ static const GtkActionEntry entries[] = {
          G_CALLBACK (sql_plugin_save) }
 };
 
+/*
+ * sql_execute_batch:
+ * @cnc: a opened connection
+ * @sql: an SQL batch
+ *
+ * Executes all the statements contained in an SQL script over an opened connection.
+ *
+ * Returns: %TRUE on success, %FALSE on failure.
+ */
 static gboolean
-sql_execute_command (GdaConnection *con, gchar *command)
+sql_execute_batch (GdaConnection *cnc, gchar *sql)
 {
-       GdaCommand *cmd;
-       GList      *list;
-       GError     *error = NULL;
-
-       cmd = gda_command_new (command, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
-       list = gda_connection_execute_command (con, cmd, NULL, &error);
-       gda_command_free (cmd);
-
-       while (list) {
-               if (list->data) {
-                       g_object_unref (list->data);
-               }
-               list = list->next;
-       }
-       g_list_free (list);
+    GdaSqlParser *parser;
+    GdaBatch *batch;
+    GSList *list;
+    GError *error = NULL;
+    parser = gda_sql_parser_new ();
+    batch = gda_sql_parser_parse_string_as_batch (parser, sql, NULL, &error);
+    if (error) {
+        g_warning (error->message);
+        g_clear_error (&error);
+        g_object_unref (parser);
+        return FALSE;
+    }
+    list = gda_connection_batch_execute (cnc, batch, NULL, GDA_STATEMENT_MODEL_RANDOM_ACCESS, &error);
+    if (error) {
+        g_warning (error->message);
+        g_clear_error (&error);
+        g_object_unref (batch);
+        g_object_unref (parser);
+        return FALSE;
+    }
+    g_slist_free_full (list, g_object_unref);
+    g_object_unref (batch);
+    g_object_unref (parser);
+    return TRUE;
+}
 
-       if(error != NULL) {
-               g_error_free (error);
-               return FALSE;
-       }
-       else
-       {
-               return TRUE;
-       }
+/*
+ * sql_execute_command:
+ * @cnc: an opened connection
+ * @sql: a query statement that must not begin with "SELECT"
+ *
+ * Execute a single non-SELECT SQL statement over an opened connection.
+ *
+ * libplanner/mrp-sql.c duplicates the code.
+ *
+ * Returns: %TRUE on success, %FALSE on failure.
+ *
+ * Deprecated: 0.14.6: Use gda_connection_execute_non_select_command() instead.
+ */
+static gboolean
+sql_execute_command (GdaConnection *cnc, gchar *sql)
+{
+    GError *error = NULL;
+    gda_connection_execute_non_select_command (cnc, sql, &error);
+    if (error) {
+        g_warning (error->message);
+        g_clear_error (&error);
+        return FALSE;
+    }
+    return TRUE;
 }
 
+/*
+ * sql_execute_query:
+ * @cnc: an opened connection
+ * @sql: a query statement that must begin with "SELECT"
+ *
+ * Execute an SQL SELECT command over an opened connection.
+ *
+ * libplanner/mrp-sql.c duplicates the code.
+ *
+ * Returns: (transfer full): a new #GdaDataModel if successful, %NULL otherwise
+ *
+ * Deprecated: 0.14.6: Use gda_connection_execute_select_command() instead.
+ */
 static GdaDataModel *
-sql_execute_query (GdaConnection *con, gchar *query)
+sql_execute_query (GdaConnection *cnc, gchar *sql)
 {
-       GdaCommand    *cmd;
-       GdaDataModel  *result;
-
-       cmd = gda_command_new (query, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
-       result = gda_connection_execute_select_command (con, cmd, NULL, NULL);
-       gda_command_free (cmd);
-
-       return result;
+    GdaDataModel *result;
+    GError *error = NULL;
+    result = gda_connection_execute_select_command (cnc, sql, &error);
+    if (error) {
+        g_warning (error->message);
+        g_clear_error (&error);
+    }
+    return result;
 }
 
 static const gchar *
@@ -534,7 +583,7 @@ check_database_tables (GdaConnection *conn,
                gtk_widget_destroy (dialog);
                if (result == GTK_RESPONSE_YES) {
                        g_file_get_contents (upgrade_file, &contents, NULL, NULL);
-                       success = sql_execute_command (conn, contents);
+                       success = sql_execute_batch (conn, contents);
                        g_free (contents);
                        if (!success) {
                                dialog = gtk_message_dialog_new (window,
@@ -574,7 +623,7 @@ check_database_tables (GdaConnection *conn,
                gchar  *contents;
 
                g_file_get_contents (database_file, &contents, NULL, NULL);
-               success = sql_execute_command (conn, contents);
+               success = sql_execute_batch (conn, contents);
                g_free (contents);
                if (!success) {
                        dialog = gtk_message_dialog_new (window,


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