]> saetta.ns0.it Git - libgdaex/commitdiff
Enabled alias editing for show fields.
authorAndrea Zagli <azagli@libero.it>
Sun, 8 May 2011 07:16:34 +0000 (09:16 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 8 May 2011 07:16:34 +0000 (09:16 +0200)
data/libgdaex/gui/libgdaex.ui
src/queryeditor.c
tests/query_editor.c

index cbd0925c2833e86f759980aae08aaf161e91f539..50f2683ebf5ecabdd25aaaa23a742a32762f0cca 100644 (file)
@@ -2,16 +2,6 @@
 <interface>
   <requires lib="gtk+" version="2.16"/>
   <!-- interface-naming-policy project-wide -->
-  <object class="GtkListStore" id="lstore_show">
-    <columns>
-      <!-- column-name table_name -->
-      <column type="gchararray"/>
-      <!-- column-name name -->
-      <column type="gchararray"/>
-      <!-- column-name name_visible -->
-      <column type="gchararray"/>
-    </columns>
-  </object>
   <object class="GtkTreeStore" id="tstore_where">
     <columns>
       <!-- column-name table_name -->
       <column type="gchararray"/>
     </columns>
   </object>
+  <object class="GtkListStore" id="lstore_show">
+    <columns>
+      <!-- column-name table_name -->
+      <column type="gchararray"/>
+      <!-- column-name name -->
+      <column type="gchararray"/>
+      <!-- column-name name_visible -->
+      <column type="gchararray"/>
+      <!-- column-name alias -->
+      <column type="gchararray"/>
+    </columns>
+  </object>
   <object class="GtkWindow" id="w_query_editor">
     <child>
       <object class="GtkHPaned" id="hpaned_query_editor">
                             <property name="can_focus">True</property>
                             <property name="model">lstore_show</property>
                             <property name="headers_clickable">False</property>
-                            <property name="search_column">0</property>
+                            <property name="rules_hint">True</property>
+                            <property name="search_column">2</property>
                             <child>
                               <object class="GtkTreeViewColumn" id="treeviewcolumn2">
                                 <property name="title">Field</property>
                                 </child>
                               </object>
                             </child>
+                            <child>
+                              <object class="GtkTreeViewColumn" id="treeviewcolumn10">
+                                <property name="title">Alias</property>
+                                <child>
+                                  <object class="GtkCellRendererText" id="cellrenderertext9"/>
+                                  <attributes>
+                                    <attribute name="text">3</attribute>
+                                  </attributes>
+                                </child>
+                              </object>
+                            </child>
                           </object>
                         </child>
                       </object>
                             <property name="can_focus">True</property>
                             <property name="model">tstore_where</property>
                             <property name="headers_clickable">False</property>
-                            <property name="search_column">0</property>
+                            <property name="rules_hint">True</property>
+                            <property name="search_column">2</property>
                             <child>
                               <object class="GtkTreeViewColumn" id="treeviewcolumn5">
                                 <property name="title">Field</property>
                             <property name="can_focus">True</property>
                             <property name="model">lstore_order</property>
                             <property name="headers_clickable">False</property>
-                            <property name="search_column">0</property>
+                            <property name="rules_hint">True</property>
+                            <property name="search_column">2</property>
                             <child>
                               <object class="GtkTreeViewColumn" id="treeviewcolumn3">
                                 <property name="title">Field</property>
index dc9045db36be3d48c9374c0de9f249c29771a978..00fdb4cfd53a1b4e83172d693d9c0aa4d293b8aa 100644 (file)
@@ -164,7 +164,8 @@ struct _GdaExQueryEditorPrivate
                GSList *relations;      /* GdaExQueryEditorRelation */
 
                /* for value choosing */
-               gboolean where_value;
+               guint editor_type; /* 1 - show; 2 - where; 3 - order */
+
                GtkListStore *lstore_where_type;
                GtkWidget *hbox;
                GtkWidget *not;
@@ -189,7 +190,8 @@ enum
        {
                COL_SHOW_TABLE_NAME,
                COL_SHOW_NAME,
-               COL_SHOW_VISIBLE_NAME
+               COL_SHOW_VISIBLE_NAME,
+               COL_SHOW_ALIAS
        };
 
 enum
@@ -494,6 +496,7 @@ const gchar
 
        gchar *table_name;
        gchar *field_name;
+       gchar *alias;
        gchar *asc_desc;
 
        GdaExQueryEditorTable *table;
@@ -523,6 +526,7 @@ const gchar
                                        gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_show), &iter,
                                                            COL_SHOW_TABLE_NAME, &table_name,
                                                            COL_SHOW_NAME, &field_name,
+                                                           COL_SHOW_ALIAS, &alias,
                                                            -1);
 
                                        table = g_hash_table_lookup (priv->tables, table_name);
@@ -542,11 +546,14 @@ const gchar
                                                                                                    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);
+                                                       gda_sql_builder_select_add_field (sqlbuilder, field->decode_field_to_show,
+                                                                                                     field->decode_table2,
+                                                                                                     alias != NULL && g_strcmp0 (alias, "") != 0 ? alias : field->decode_field_alias);
                                                }
                                        else
                                                {
-                                                       gda_sql_builder_select_add_field (sqlbuilder, field->name, table->name, field->alias);
+                                                       gda_sql_builder_select_add_field (sqlbuilder, field->name, table->name,
+                                                                                         alias != NULL && g_strcmp0 (alias, "") != 0 ? alias : field->alias);
                                                        gda_sql_builder_select_add_target_id (sqlbuilder,
                                                                                              gda_sql_builder_add_id (sqlbuilder, table->name),
                                                                                              NULL);
@@ -755,6 +762,9 @@ xmlNode
        gchar *table_name;
        gchar *field_name;
 
+       GdaExQueryEditorTable *table;
+       GdaExQueryEditorField *field;
+
        xmlNode *node;
 
        ret = NULL;
@@ -767,6 +777,7 @@ xmlNode
 
        if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->lstore_show), &iter))
                {
+                       gchar *alias;
                        xmlNode *node_show;
 
                        node_show = xmlNewNode (NULL, "show");
@@ -777,12 +788,24 @@ xmlNode
                                        gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_show), &iter,
                                                            COL_SHOW_TABLE_NAME, &table_name,
                                                            COL_SHOW_NAME, &field_name,
+                                                           COL_SHOW_ALIAS, &alias,
                                                            -1);
 
+                                       table = g_hash_table_lookup (priv->tables, table_name);
+                                       field = g_hash_table_lookup (table->fields, field_name);
+
                                        node = xmlNewNode (NULL, "field");
                                        xmlAddChild (node_show, node);
                                        xmlNewProp (node, "table", table_name);
                                        xmlNewProp (node, "field", field_name);
+                                       if (alias != NULL && g_strcmp0 (alias, "") != 0)
+                                               {
+                                                       xmlNewProp (node, "alias", alias);
+                                               }
+                                       else
+                                               {
+                                                       xmlNewProp (node, "alias", field->alias);
+                                               }
                                } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->lstore_show), &iter));
                }
 
@@ -901,6 +924,7 @@ gdaex_query_editor_load_choices_from_xml (GdaExQueryEditor *qe, xmlNode *root,
        gchar *table_name;
        gchar *field_name;
        gchar *name_visible;
+       gchar *alias;
        GValue *v_table_field;
 
        GdaExQueryEditorTable *table;
@@ -956,11 +980,18 @@ gdaex_query_editor_load_choices_from_xml (GdaExQueryEditor *qe, xmlNode *root,
                                                        {
                                                                if (!gdaex_query_editor_model_has_value (GTK_TREE_MODEL (priv->lstore_show), COL_SHOW_VISIBLE_NAME, v_table_field))
                                                                        {
+                                                                               alias = xmlGetProp (node_field, "alias");
+                                                                               if (alias == NULL)
+                                                                                       {
+                                                                                               alias = g_strdup ("");
+                                                                                       }
+
                                                                                gtk_list_store_append (priv->lstore_show, &iter);
                                                                                gtk_list_store_set (priv->lstore_show, &iter,
                                                                                                    COL_SHOW_TABLE_NAME, field->table_name,
                                                                                                    COL_SHOW_NAME, field_name,
                                                                                                    COL_SHOW_VISIBLE_NAME, name_visible,
+                                                                                                   COL_SHOW_ALIAS, alias,
                                                                                                    -1);
                                                                        }
                                                        }
@@ -1545,13 +1576,19 @@ gdaex_query_editor_on_btn_cancel_clicked (GtkButton *button,
        gdaex_query_editor_remove_child_from_vbx_values (qe);
        gtk_widget_hide (priv->vbx_values_container);
 
-       if (priv->where_value)
+       switch (priv->editor_type)
                {
-                       gtk_tree_selection_unselect_all (priv->sel_where);
-               }
-       else
-               {
-                       gtk_tree_selection_unselect_all (priv->sel_order);
+                       case 1:
+                               gtk_tree_selection_unselect_all (priv->sel_show);
+                               break;
+
+                       case 2:
+                               gtk_tree_selection_unselect_all (priv->sel_where);
+                               break;
+
+                       case 3:
+                               gtk_tree_selection_unselect_all (priv->sel_order);
+                               break;
                }
 }
 
@@ -1563,65 +1600,117 @@ gdaex_query_editor_on_btn_save_clicked (GtkButton *button,
        GdaExQueryEditorPrivate *priv;
 
        GtkTreeIter iter;
+
+       gchar *val1;
+       gchar *val2;
        gchar *asc_desc;
 
        qe = (GdaExQueryEditor *)user_data;
        priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (qe);
 
-       if (priv->where_value)
+       switch (priv->editor_type)
                {
-                       if (gtk_tree_selection_get_selected (priv->sel_where, NULL, &iter))
-                               {
-                                       GtkTreeIter iter_val;
-                                       guint where_type;
+                       case 1:
+                               if (gtk_tree_selection_get_selected (priv->sel_show, NULL, &iter))
+                                       {
+                                               val1 = (gchar *)gtk_entry_get_text (GTK_ENTRY (priv->txt1));
+                                               if (val1 == NULL)
+                                                       {
+                                                               val1 = g_strdup ("");
+                                                       }
+                                               else
+                                                       {
+                                                               g_strstrip (val1);
+                                                       }
+                                               gtk_list_store_set (priv->lstore_show, &iter,
+                                                                   COL_SHOW_ALIAS, val1,
+                                                                   -1);
+
+                                               gtk_tree_selection_unselect_all (priv->sel_show);
+                                       }
+                               break;
+
+                       case 2:
+                               if (gtk_tree_selection_get_selected (priv->sel_where, NULL, &iter))
+                                       {
+                                               GtkTreeIter iter_val;
+                                               guint where_type;
                                        
-                                       GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->cb_where_type));
-                                       if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->cb_where_type), &iter_val))
-                                               {
-                                                       GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (priv->hpaned_main)),
-                                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
-                                                                                                        GTK_MESSAGE_WARNING,
-                                                                                                        GTK_BUTTONS_OK,
-                                                                                                        "You must select a condition's type before.");
-                                                       gtk_dialog_run (GTK_DIALOG (dialog));
-                                                       gtk_widget_destroy (dialog);
-                                                       return;
-                                               }
+                                               GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->cb_where_type));
+                                               if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->cb_where_type), &iter_val))
+                                                       {
+                                                               GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (priv->hpaned_main)),
+                                                                                                           GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                                           GTK_MESSAGE_WARNING,
+                                                                                                           GTK_BUTTONS_OK,
+                                                                                                           "You must select a condition's type before.");
+                                                               gtk_dialog_run (GTK_DIALOG (dialog));
+                                                               gtk_widget_destroy (dialog);
+                                                               return;
+                                                       }
 
-                                       gtk_tree_model_get (model, &iter_val,
-                                                           0, &where_type,
-                                                           -1);
+                                               gtk_tree_model_get (model, &iter_val,
+                                                                   0, &where_type,
+                                                                   -1);
 
-                                       gtk_tree_store_set (priv->tstore_where, &iter,
-                                                           COL_WHERE_CONDITION_NOT, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->not)),
-                                                           COL_WHERE_CONDITION_TYPE, where_type,
-                                                           COL_WHERE_CONDITION_TYPE_VISIBLE, gdaex_query_editor_get_where_type_str_from_type (where_type),
-                                                           COL_WHERE_CONDITION_FROM, gtk_entry_get_text (GTK_ENTRY (priv->txt1)),
-                                                           COL_WHERE_CONDITION_TO, priv->txt2 != NULL ? gtk_entry_get_text (GTK_ENTRY (priv->txt2)) : "",
-                                                           -1);
+                                               val1 = (gchar *)gtk_entry_get_text (GTK_ENTRY (priv->txt1));
+                                               if (val1 == NULL)
+                                                       {
+                                                               val1 = g_strdup ("");
+                                                       }
+                                               else
+                                                       {
+                                                               g_strstrip (val1);
+                                                       }
 
-                                       gtk_tree_selection_unselect_all (priv->sel_where);
-                               }
-               }
-       else
-               {
-                       if (gtk_tree_selection_get_selected (priv->sel_order, NULL, &iter))
-                               {
-                                       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->opt_asc)))
-                                               {
-                                                       asc_desc = g_strdup ("ASC");
-                                               }
-                                       else
-                                               {
-                                                       asc_desc = g_strdup ("DESC");
-                                               }
+                                               if (priv->txt2 != NULL)
+                                                       {
+                                                               val2 = (gchar *)gtk_entry_get_text (GTK_ENTRY (priv->txt2));
+                                                               if (val2 == NULL)
+                                                                       {
+                                                                               val2 = g_strdup ("");
+                                                                       }
+                                                               else
+                                                                       {
+                                                                               g_strstrip (val2);
+                                                                       }
+                                                       }
+                                               else
+                                                       {
+                                                               val2 = g_strdup ("");
+                                                       }
 
-                                       gtk_list_store_set (priv->lstore_order, &iter,
-                                                           COL_ORDER_ORDER, asc_desc,
-                                                           -1);
+                                               gtk_tree_store_set (priv->tstore_where, &iter,
+                                                                       COL_WHERE_CONDITION_NOT, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->not)),
+                                                                       COL_WHERE_CONDITION_TYPE, where_type,
+                                                                       COL_WHERE_CONDITION_TYPE_VISIBLE, gdaex_query_editor_get_where_type_str_from_type (where_type),
+                                                                       COL_WHERE_CONDITION_FROM, val1,
+                                                                       COL_WHERE_CONDITION_TO, val2,
+                                                                       -1);
 
-                                       gtk_tree_selection_unselect_all (priv->sel_order);
-                               }
+                                               gtk_tree_selection_unselect_all (priv->sel_where);
+                                       }
+                               break;
+
+                       case 3:
+                               if (gtk_tree_selection_get_selected (priv->sel_order, NULL, &iter))
+                                       {
+                                               if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->opt_asc)))
+                                                       {
+                                                               asc_desc = g_strdup ("ASC");
+                                                       }
+                                               else
+                                                       {
+                                                               asc_desc = g_strdup ("DESC");
+                                                       }
+
+                                               gtk_list_store_set (priv->lstore_order, &iter,
+                                                                   COL_ORDER_ORDER, asc_desc,
+                                                                   -1);
+
+                                               gtk_tree_selection_unselect_all (priv->sel_order);
+                                       }
+                               break;
                }
 
        gtk_widget_hide (priv->vbx_values_container);
@@ -1742,16 +1831,23 @@ gdaex_query_editor_on_sel_show_changed (GtkTreeSelection *treeselection,
 
        gchar *table_name;
        gchar *field_name;
+       gchar *alias;
+
        GdaExQueryEditorTable *table;
        GdaExQueryEditorField *field;
 
+       GtkWidget *tbl;
+       GtkWidget *lbl;
+
        GdaExQueryEditor *qe = (GdaExQueryEditor *)user_data;
        GdaExQueryEditorPrivate *priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (qe);
 
+       gtk_widget_hide (priv->vbx_values_container);
+       gdaex_query_editor_remove_child_from_vbx_values (qe);
+
        if (gtk_tree_selection_get_selected (priv->sel_show, NULL, &iter))
                {
-                       gdaex_query_editor_remove_child_from_vbx_values (qe);
-                       gtk_widget_hide (priv->vbx_values_container);
+                       priv->editor_type = 1;
 
                        gtk_tree_selection_unselect_all (priv->sel_where);
                        gtk_tree_selection_unselect_all (priv->sel_order);
@@ -1759,12 +1855,35 @@ gdaex_query_editor_on_sel_show_changed (GtkTreeSelection *treeselection,
                        gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_show), &iter,
                                            COL_SHOW_TABLE_NAME, &table_name,
                                            COL_SHOW_NAME, &field_name,
+                                           COL_SHOW_ALIAS, &alias,
                                            -1);
 
                        table = g_hash_table_lookup (priv->tables, table_name);
                        field = g_hash_table_lookup (table->fields, field_name);
 
                        gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (priv->gtkbuilder, "button4")), !field->always_showed);
+
+                       priv->hbox = gtk_hbox_new (TRUE, 0);
+
+                       tbl = gtk_table_new (2, 2, FALSE);
+                       gtk_table_set_row_spacings (GTK_TABLE (tbl), 5);
+                       gtk_table_set_col_spacings (GTK_TABLE (tbl), 5);
+                       gtk_box_pack_start (GTK_BOX (priv->hbox), tbl, TRUE, TRUE, 0);
+
+                       lbl = gtk_label_new ("Alias");
+                       gtk_table_attach (GTK_TABLE (tbl), lbl, 1, 2, 0, 1, 0, 0, 0, 0);
+
+                       lbl = gtk_label_new (g_strconcat (table->name_visible, " - ", field->name_visible, NULL));
+                       gtk_table_attach (GTK_TABLE (tbl), lbl, 0, 1, 1, 2, 0, 0, 0, 0);
+
+                       priv->txt1 = gtk_entry_new ();
+                       gtk_entry_set_text (GTK_ENTRY (priv->txt1), alias);
+                       gtk_table_attach (GTK_TABLE (tbl), priv->txt1, 1, 2, 1, 2, 0, 0, 0, 0);
+
+                       gtk_box_pack_start (GTK_BOX (priv->vbx_values), priv->hbox, FALSE, FALSE, 0);
+
+                       gtk_widget_show_all (priv->vbx_values);
+                       gtk_widget_show (priv->vbx_values_container);
                }
 }
 
@@ -1894,7 +2013,7 @@ gdaex_query_editor_on_sel_where_changed (GtkTreeSelection *treeselection,
 
        if (gtk_tree_selection_get_selected (priv->sel_where, NULL, &iter))
                {
-                       priv->where_value = TRUE;
+                       priv->editor_type = 2;
 
                        gtk_tree_selection_unselect_all (priv->sel_show);
                        gtk_tree_selection_unselect_all (priv->sel_order);
@@ -2225,7 +2344,7 @@ gdaex_query_editor_on_sel_order_changed (GtkTreeSelection *treeselection,
 
        if (gtk_tree_selection_get_selected (priv->sel_order, NULL, &iter))
                {
-                       priv->where_value = FALSE;
+                       priv->editor_type = 3;
 
                        gtk_tree_selection_unselect_all (priv->sel_show);
                        gtk_tree_selection_unselect_all (priv->sel_where);
index 1d4879cf1bd5479db0227f8fde6b40c6bad64d14..9631a009e347f0a39b35d5037cc03e54df8226e7 100644 (file)
@@ -98,10 +98,10 @@ on_btn_get_sql_clicked (GtkButton *button,
        gint size;
 
        GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (w),
-                                                GTK_DIALOG_DESTROY_WITH_PARENT,
-                                                GTK_MESSAGE_INFO,
-                                                GTK_BUTTONS_OK,
-                                                gdaex_query_editor_get_sql ((GdaExQueryEditor *)user_data));
+                                                   GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                   GTK_MESSAGE_INFO,
+                                                   GTK_BUTTONS_OK,
+                                                   gdaex_query_editor_get_sql ((GdaExQueryEditor *)user_data));
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);;
 
@@ -111,10 +111,10 @@ on_btn_get_sql_clicked (GtkButton *button,
        xmlDocDumpMemory (doc, &buf, &size);
 
        dialog = gtk_message_dialog_new (GTK_WINDOW (w),
-                                                GTK_DIALOG_DESTROY_WITH_PARENT,
-                                                GTK_MESSAGE_INFO,
-                                                GTK_BUTTONS_OK,
-                                                buf);
+                                                   GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                   GTK_MESSAGE_INFO,
+                                                   GTK_BUTTONS_OK,
+                                                   buf);
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);;
 }
@@ -201,6 +201,7 @@ main (int argc, char *argv[])
        field->name = g_strdup ("age");
        field->name_visible = g_strdup ("Age");
        field->description = g_strdup ("The client's age");
+       field->alias = g_strdup ("client_age");
        field->type = GDAEX_QE_FIELD_TYPE_INTEGER;
        field->for_show = TRUE;
        field->for_where = TRUE;