* 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"
{
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 */
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:
#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 ();