]> saetta.ns0.it Git - libgdaex/commitdiff
Begin of implementation of decoded fields.
authorAndrea Zagli <azagli@libero.it>
Wed, 2 Mar 2011 18:04:36 +0000 (19:04 +0100)
committerAndrea Zagli <azagli@libero.it>
Wed, 2 Mar 2011 18:04:36 +0000 (19:04 +0100)
src/queryeditor.c
src/queryeditor.h
tests/query_editor.c

index cb7c6a1169fb782c290542d1e1ce9225a375f803..d9140d2d16eb6a11052e23815321b17af79a31f7 100644 (file)
        #include <config.h>
 #endif
 
+#include <stdarg.h>
+
 #include "queryeditor.h"
 
 typedef struct
        {
                gchar *name;
                gchar *name_visible;
+               gboolean visible;
+
                GHashTable *fields;     /* GdaExQueryEditorField */
        } GdaExQueryEditorTable;
 
+typedef struct
+       {
+               gchar *table1;
+               gchar *table2;
+
+               GSList *fields1;
+               GSList *fields2;
+       } GdaExQueryEditorRelation;
+
 static void gdaex_query_editor_class_init (GdaExQueryEditorClass *class);
 static void gdaex_query_editor_init (GdaExQueryEditor *gdaex_query_editor);
 
@@ -41,6 +54,11 @@ static void gdaex_query_editor_get_property (GObject *object,
                                GValue *value,
                                GParamSpec *pspec);
 
+static gboolean _gdaex_query_editor_add_table (GdaExQueryEditor *qe,
+                              const gchar *table_name,
+                              const gchar *table_name_visibile,
+                              gboolean is_visible);
+
 static void gdaex_query_editor_refresh_gui (GdaExQueryEditor *qe);
 static void gdaex_query_editor_refresh_gui_add_fields (GdaExQueryEditor *qe,
                                                        GdaExQueryEditorTable *table,
@@ -123,6 +141,8 @@ struct _GdaExQueryEditorPrivate
 
                GHashTable *tables;     /* GdaExQueryEditorTable */
 
+               GSList *relations;      /* GdaExQueryEditorRelation */
+
                /* for value choosing */
                GtkWidget *hbox;
                GtkWidget *lbl;
@@ -198,6 +218,7 @@ gdaex_query_editor_init (GdaExQueryEditor *gdaex_query_editor)
 
        priv->dialog = NULL;
        priv->hpaned_main = NULL;
+       priv->relations = NULL;
 }
 
 /**
@@ -336,25 +357,7 @@ gdaex_query_editor_add_table (GdaExQueryEditor *qe,
                               const gchar *table_name,
                               const gchar *table_name_visibile)
 {
-       gboolean ret;
-
-       GdaExQueryEditorPrivate *priv;
-       GdaExQueryEditorTable *table;
-
-       g_return_val_if_fail (GDAEX_IS_QUERY_EDITOR (qe), FALSE);
-
-       priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (qe);
-
-       table = g_new0 (GdaExQueryEditorTable, 1);
-       table->name = g_strstrip (g_strdup (table_name));
-       table->name_visible = g_strstrip (g_strdup (table_name_visibile));
-       table->fields = g_hash_table_new (g_str_hash, g_str_equal);
-
-       g_hash_table_insert (priv->tables, table->name, table);
-
-       ret = TRUE;
-
-       return ret;
+       return _gdaex_query_editor_add_table (qe, table_name, table_name_visibile, TRUE);
 }
 
 gboolean
@@ -388,6 +391,63 @@ gdaex_query_editor_table_add_field (GdaExQueryEditor *qe,
        return ret;
 }
 
+/**
+ * gdaex_query_editor_add_relation:
+ * @table1: relation's left part.
+ * @table2: relation's right part.
+ * @...: couples of fields name, one from @table1 and one from @table2.; 
+ *       must be terminated with a #NULL value.
+ *
+ */
+gboolean
+gdaex_query_editor_add_relation (GdaExQueryEditor *qe,
+                                 const gchar *table1,
+                                 const gchar *table2,
+                                 ...)
+{
+       GdaExQueryEditorPrivate *priv;
+
+       GdaExQueryEditorRelation *relation;
+
+       va_list fields;
+       gchar *field_name1;
+       gchar *field_name2;
+
+       g_return_val_if_fail (GDAEX_IS_QUERY_EDITOR (qe), FALSE);
+
+       priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (qe);
+
+       relation = g_new0 (GdaExQueryEditorRelation, 1);
+
+       relation->table1 = g_strdup (table1);
+       /* TODO if table1 doesn't exists, create with visible = FALSE */
+
+       relation->table2 = g_strdup (table2);
+       /* TODO if table2 doesn't exists, create with visible = FALSE */
+
+       va_start (fields, table2);
+
+       while ((field_name1 = va_arg (fields, gchar *)) != NULL
+              && (field_name2 = va_arg (fields, gchar *)) != NULL)
+               {
+                       if (field_name2 != NULL)
+                               {
+                                       relation->fields1 = g_slist_append (relation->fields1, g_strdup (field_name1));
+                                       relation->fields2 = g_slist_append (relation->fields1, g_strdup (field_name2));
+                               }
+                       else
+                               {
+                                       break;
+                               }
+               }
+
+       va_end (fields);
+
+       priv->relations = g_slist_append (priv->relations, relation);
+
+       return TRUE;
+}
+
 const gchar
 *gdaex_query_editor_get_sql (GdaExQueryEditor *qe)
 {
@@ -429,10 +489,30 @@ const gchar
                                        table = g_hash_table_lookup (priv->tables, table_name);
                                        field = g_hash_table_lookup (table->fields, field_name);
 
-                                       gda_sql_builder_select_add_field (sqlbuilder, field->name, table->name, NULL);
-                                       gda_sql_builder_select_add_target_id (sqlbuilder,
-                                                                             gda_sql_builder_add_id (sqlbuilder, table->name),
-                                                                             NULL);
+                                       if (field->decode_table2 != NULL)
+                                               {
+                                                       /* TODO alias for table2 must change based on tables count */
+                                                       guint id_target1 = gda_sql_builder_select_add_target_id (sqlbuilder,
+                                                                                                         gda_sql_builder_add_id (sqlbuilder, table->name),
+                                                                                                         NULL);
+                                                       guint id_target2 = gda_sql_builder_select_add_target_id (sqlbuilder,
+                                                                                                         gda_sql_builder_add_id (sqlbuilder, field->decode_table2),
+                                                                                                         "t2");
+                                                       guint id_join1 = gda_sql_builder_add_id (sqlbuilder, g_strconcat (field->table_name, ".", field->name, NULL));
+                                                       guint id_join2 = gda_sql_builder_add_id (sqlbuilder, g_strconcat ("t2.", field->decode_field2, NULL));
+                                                       guint join_cond = gda_sql_builder_add_cond (sqlbuilder, GDA_SQL_OPERATOR_TYPE_EQ,
+                                                                                                   id_join1, id_join2, 0);
+                                                       gda_sql_builder_select_join_targets (sqlbuilder, id_target1, id_target2,
+                                                                                            GDA_SQL_SELECT_JOIN_INNER, join_cond);
+                                                       gda_sql_builder_select_add_field (sqlbuilder, field->decode_field_to_show, field->decode_table2, field->decode_field_alias);
+                                               }
+                                       else
+                                               {
+                                                       gda_sql_builder_select_add_field (sqlbuilder, field->name, table->name, field->alias);
+                                                       gda_sql_builder_select_add_target_id (sqlbuilder,
+                                                                                             gda_sql_builder_add_id (sqlbuilder, table->name),
+                                                                                             NULL);
+                                               }
                                } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->lstore_show), &iter));
                }
 
@@ -519,6 +599,30 @@ gdaex_query_editor_get_property (GObject *object,
                }
 }
 
+static gboolean
+_gdaex_query_editor_add_table (GdaExQueryEditor *qe,
+                              const gchar *table_name,
+                              const gchar *table_name_visibile,
+                              gboolean is_visible)
+{
+       GdaExQueryEditorPrivate *priv;
+       GdaExQueryEditorTable *table;
+
+       g_return_val_if_fail (GDAEX_IS_QUERY_EDITOR (qe), FALSE);
+
+       priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (qe);
+
+       table = g_new0 (GdaExQueryEditorTable, 1);
+       table->name = g_strstrip (g_strdup (table_name));
+       table->name_visible = g_strstrip (g_strdup (table_name_visibile));
+       table->fields = g_hash_table_new (g_str_hash, g_str_equal);
+       table->visible = is_visible;
+
+       g_hash_table_insert (priv->tables, table->name, table);
+
+       return TRUE;
+}
+
 static void
 gdaex_query_editor_refresh_gui (GdaExQueryEditor *qe)
 {
@@ -540,11 +644,14 @@ gdaex_query_editor_refresh_gui (GdaExQueryEditor *qe)
                {
                        table = (GdaExQueryEditorTable *)value;
 
-                       gtk_tree_store_append (priv->tstore_fields, &iter, NULL);
-                       gtk_tree_store_set (priv->tstore_fields, &iter,
-                                           COL_FIELDS_NAME, table->name,
-                                           COL_FIELDS_VISIBLE_NAME, table->name_visible,
-                                           -1);
+                       if (table->visible)
+                               {
+                                       gtk_tree_store_append (priv->tstore_fields, &iter, NULL);
+                                       gtk_tree_store_set (priv->tstore_fields, &iter,
+                                                           COL_FIELDS_NAME, table->name,
+                                                           COL_FIELDS_VISIBLE_NAME, table->name_visible,
+                                                           -1);
+                               }
 
                        gdaex_query_editor_refresh_gui_add_fields (qe, table, &iter);
                }
index 8d6824649aac01284cb18fff8ae10e1fe8c552e5..85f35d554caa48c348cced400e90f8756b119ae6 100644 (file)
@@ -65,12 +65,22 @@ typedef struct
                gchar *name;
                gchar *name_visible;
                gchar *description;
+               gchar *alias;
                /* - tipo di campo (string, integer, double, date, datetime) */
                /* - sceglibile per la parte show (es. le chiavi esterne non ha senno che lo siano) */
                /* - sempre presente nelle query, quindi non sceglibile per la parte show */
                /* - sceglibile per la parte where */
                /* - condizioni where che è possibile applicare (es. i campi id_* non ha senso che abbiano un between, */
                /*                                             ma ci deve essere la possibilità di fare ricerche anche sulle decodifiche) */
+               gchar *decode_table2;
+               /* TODO 
+               GSList *decode_fields1;
+               GSList *decode_fields2;
+               */
+               /* TODO to test */
+               gchar *decode_field2;
+               gchar *decode_field_to_show;
+               gchar *decode_field_alias;
        } GdaExQueryEditorField;
 
 gboolean gdaex_query_editor_add_table (GdaExQueryEditor *qe,
@@ -80,8 +90,9 @@ gboolean gdaex_query_editor_table_add_field (GdaExQueryEditor *qe,
                                              const gchar *table_name,
                                              GdaExQueryEditorField field);
 gboolean gdaex_query_editor_add_relation (GdaExQueryEditor *qe,
-                                          const gchar *table_name1, GdaExQueryEditorField field1,
-                                          const gchar *table_name2, GdaExQueryEditorField field2);
+                                          const gchar *table1,
+                                          const gchar *table2,
+                                          ...);
 
 const gchar *gdaex_query_editor_get_sql (GdaExQueryEditor *qe);
 
index f857a94508e39151621489b0861f6724db790efc..f56152dafd58fe78347ac479c4abeb950ae8fc01 100644 (file)
@@ -82,6 +82,19 @@ main (int argc, char *argv[])
        gdaex_query_editor_table_add_field (qe, "clients", *field);
        g_free (field);
 
+       field = g_new0 (GdaExQueryEditorField, 1);
+       field->name = g_strdup ("id_cities");
+       field->name_visible = g_strdup ("City");
+       field->description = g_strdup ("The client's city");
+       field->decode_table2 = g_strdup ("cities");
+       /*field->decode_fields1 = g_slist_append (field->decode_fields1, "id_cities");
+       field->decode_fields2 = g_slist_append (field->decode_fields2, "id");*/
+       field->decode_field2 = g_strdup ("id");
+       field->decode_field_to_show = g_strdup ("name");
+       field->decode_field_alias = g_strdup ("city_name");
+       gdaex_query_editor_table_add_field (qe, "clients", *field);
+       g_free (field);
+
        dialog = gdaex_query_editor_get_dialog (qe);
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);