From: Andrea Zagli Date: Sat, 30 Apr 2011 16:26:51 +0000 (+0200) Subject: Implemented GtkFormDecoder::decode. X-Git-Tag: 0.3.1~10 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=847cffe97306ef52242cdde9147b973eb6a07dfa;p=libgtkform Implemented GtkFormDecoder::decode. --- diff --git a/ui/gtkformdecoder.c b/ui/gtkformdecoder.c index 4ad506b..e3c2421 100644 --- a/ui/gtkformdecoder.c +++ b/ui/gtkformdecoder.c @@ -19,6 +19,14 @@ * Boston, MA 02111-1307, USA. */ +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#include + #include #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, ¶ms, 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: diff --git a/ui/test/Makefile.am b/ui/test/Makefile.am index 6430e5c..7a0133c 100644 --- a/ui/test/Makefile.am +++ b/ui/test/Makefile.am @@ -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 index 0000000..c0668b9 Binary files /dev/null and b/ui/test/database.db differ diff --git a/ui/test/decoder.c b/ui/test/decoder.c index 77d94f3..fe69842 100644 --- a/ui/test/decoder.c +++ b/ui/test/decoder.c @@ -18,21 +18,57 @@ #include +#include + #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 ();