]> saetta.ns0.it Git - libgtkform/commitdiff
Implemented GtkFormDecoder::decode.
authorAndrea Zagli <azagli@libero.it>
Sat, 30 Apr 2011 16:26:51 +0000 (18:26 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 30 Apr 2011 16:26:51 +0000 (18:26 +0200)
ui/gtkformdecoder.c
ui/test/Makefile.am
ui/test/database.db [new file with mode: 0644]
ui/test/decoder.c

index 4ad506bb563b2872d5b1c262f976951187a56b2b..e3c24211a113c2303be249a79359c4c80d7a9a9e 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+#ifdef HAVE_CONFIG_H
+       #include <config.h>
+#endif
+
+#include <glib/gi18n-lib.h>
+
+#include <sql-parser/gda-sql-parser.h>
+
 #include <libgdaex.h>
 
 #include "gtkformdecoder.h"
@@ -204,10 +212,85 @@ gtk_form_decoder_decode (GtkFormDecoder *decoder)
 {
        GtkFormDecoderPrivate *priv;
 
+       GObject *res;
+       GdaConnection *gdacon;
+       GdaSqlParser *parser;
+       GdaStatement *stmt;
+       GdaSet *params;
+       GSList *hs;
+       GdaHolder *h;
+       GValue *gval;
+       GError *error;
+
        g_return_if_fail (GTK_IS_FORM_DECODER (decoder));
 
        priv = GTK_FORM_DECODER_GET_PRIVATE (decoder);
 
+       if (IS_GDAEX (priv->gdaex))
+               {
+                       gdacon = (GdaConnection *)gdaex_get_gdaconnection (priv->gdaex);
+                       parser = gda_connection_create_parser (gdacon);
+                       if (parser == NULL)
+                               {
+                                       parser = gda_sql_parser_new ();
+                               }
+
+                       error == NULL;
+                       stmt = gda_sql_parser_parse_string (parser, priv->sql, NULL, &error);
+                       if (stmt == NULL)
+                               {
+                                       g_warning (_("Error on parsing sql string: %s\n%s"), priv->sql,
+                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                               }
+                       else
+                               {
+                                       gda_statement_get_parameters (stmt, &params, NULL);
+                                       if (params != NULL)
+                                               {
+                                                       hs = params->holders;
+                                                       while (hs != NULL)
+                                                               {
+                                                                       h = (GdaHolder *)hs->data;
+
+                                                                       if (GDA_IS_HOLDER (h))
+                                                                               {
+                                                                                       gval = gda_value_new_from_string (gtk_label_get_text (GTK_LABEL (priv->lbl_key)), gda_holder_get_g_type (h));
+
+                                                                                       error = NULL;
+                                                                                       if (!gda_holder_set_value (h, gval, &error))
+                                                                                               {
+                                                                                                       g_warning (_("Error on setting holder's value: %s"),
+                                                                                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                                                                                               }
+                                                                               }
+                                                                       hs = g_slist_next (hs);
+                                                               }
+                                               }
+
+                                       error = NULL;
+                                       res = gda_connection_statement_execute (gdacon, stmt, params,
+                                                                               GDA_STATEMENT_MODEL_RANDOM_ACCESS,
+                                                                               NULL, &error);
+                                       if (res == NULL)
+                                               {
+                                                       g_warning (_("Error on executing decoding: %s"),
+                                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                                               }
+                                       else if (GDA_IS_DATA_MODEL (res) && gda_data_model_get_n_rows ((GdaDataModel *)res) == 1)
+                                               {
+                                                       gtk_entry_set_text (GTK_ENTRY (priv->txt_decoded),
+                                                                           gdaex_data_model_get_value_stringify_at ((GdaDataModel *)res, 0, 0));
+                                               }
+                                       else
+                                               {
+                                                       gtk_entry_set_text (GTK_ENTRY (priv->txt_decoded), "");
+                                               }
+                               }
+               }
+       else
+               {
+                       g_warning (_("No GdaEx object inside GtkFormDecoder."));
+               }
 }
 
 /* CALLBACKS */
@@ -224,7 +307,19 @@ gtk_form_decoder_set_property (GObject *object, guint property_id, const GValue
                                break;
 
                        case PROP_SQL:
-                               priv->sql = g_strstrip (g_value_dup_string (value));
+                               if (G_VALUE_HOLDS_STRING (value))
+                                       {
+                                               priv->sql = g_value_dup_string (value);
+                                               if (priv->sql != NULL)
+                                                       {
+                                                               g_strstrip (priv->sql);
+                                                       }
+                                       }
+                               else
+                                       {
+                                               g_free (priv->sql);
+                                               priv->sql = NULL;
+                                       }
                                break;
 
                        default:
index 6430e5cb5881544e271c94bec4bc656e52505e07..7a0133c03f995b85c45941b5a98ee3eecaf15814 100644 (file)
@@ -15,4 +15,4 @@ test_SOURCES = decoder.c
 LDADD = $(top_builddir)/src/libgtkform.la \
         $(top_builddir)/ui/libgtkformui.la
 
-EXTRA_DIST = 
+EXTRA_DIST = database.db
diff --git a/ui/test/database.db b/ui/test/database.db
new file mode 100644 (file)
index 0000000..c0668b9
Binary files /dev/null and b/ui/test/database.db differ
index 77d94f3acaec7e5ef7af4d06695cc52ea4970ade..fe69842808fe3e9fafc4dd73e776dce9925386e8 100644 (file)
 
 #include <gtk/gtk.h>
 
+#include <libgdaex.h>
+
 #include "gtkformdecoder.h"
 
 GtkWidget *w;
 GtkWidget *decoder;
 
+GdaEx *gdaex;
+
+static void
+on_btn_browse_clicked (gpointer instance, gpointer user_data)
+{
+       gtk_form_decoder_set_key (GTK_FORM_DECODER (decoder), "1");
+}
+
+gboolean
+on_w_delete_event (GtkWidget *widget,
+                   GdkEvent *event,
+                   gpointer user_data)
+{
+       return FALSE;
+}
+
 int
 main (int argc, char **argv)
 {
        gtk_init (&argc, &argv);
 
+       gdaex = gdaex_new_from_string ("SQLite://DB_DIR=" GUIDIR ";DB_NAME=database.db");
+       if (gdaex == NULL)
+               {
+                       return 0;
+               }
+
        w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       g_signal_connect (w, "delete-event", G_CALLBACK (on_w_delete_event), NULL);
+       g_signal_connect (w, "destroy", gtk_main_quit, NULL);
 
        decoder = gtk_form_decoder_new ();
        gtk_container_add (GTK_CONTAINER (w), decoder);
 
+       g_object_set (G_OBJECT (decoder),
+                     "gdaex", gdaex,
+                     "sql", "SELECT name"
+                            " FROM customers"
+                            " WHERE id = ##key0::gint",
+                     NULL);
+
+       g_signal_connect (G_OBJECT (decoder), "btn_browse_clicked",
+                         G_CALLBACK (on_btn_browse_clicked), NULL);
+
        gtk_widget_show_all (w);
 
        gtk_main ();