From d3dacd8cc0c5309b1b3565c8ce49c008c43622ee Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 15 Aug 2010 11:22:04 +0200 Subject: [PATCH] Completed tables and fields management. --- data/audit-gui/gui/audit-gui.gui | 6 +- src/datasources.c | 21 ++-- src/table.c | 161 ++++++++++++++++++++++++++++--- 3 files changed, 163 insertions(+), 25 deletions(-) diff --git a/data/audit-gui/gui/audit-gui.gui b/data/audit-gui/gui/audit-gui.gui index 6bacfac..3f5eec8 100644 --- a/data/audit-gui/gui/audit-gui.gui +++ b/data/audit-gui/gui/audit-gui.gui @@ -392,6 +392,8 @@ + + @@ -511,7 +513,7 @@ - 0 + 1 @@ -522,7 +524,7 @@ - 1 + 2 diff --git a/src/datasources.c b/src/datasources.c index e433a8d..b3ed705 100644 --- a/src/datasources.c +++ b/src/datasources.c @@ -175,10 +175,12 @@ datasources_load_fields (Datasources *datasources, guint id_table, GtkTreeIter * DatasourcesPrivate *priv = DATASOURCES_GET_PRIVATE (datasources); sql = g_strdup_printf ("SELECT id, name" - " FROM fields" + " FROM %sfields" " WHERE status <> 'D'" " AND id_tables = %d" - " ORDER BY name", id_table); + " ORDER BY name", + priv->commons->prefix, + id_table); stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error); g_object_unref (stmt); @@ -216,10 +218,12 @@ datasources_load_tables (Datasources *datasources, guint id_ds, GtkTreeIter *ite DatasourcesPrivate *priv = DATASOURCES_GET_PRIVATE (datasources); sql = g_strdup_printf ("SELECT id, name" - " FROM tables" + " FROM %stables" " WHERE status <> 'D'" " AND id_datasources = %d" - " ORDER BY name", id_ds); + " ORDER BY name", + priv->commons->prefix, + id_ds); stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error); g_object_unref (stmt); @@ -261,9 +265,10 @@ datasources_load (Datasources *datasources) gtk_tree_store_clear (priv->tstore_datasources); sql = g_strdup_printf ("SELECT id, name, cnc_string" - " FROM datasources" + " FROM %sdatasources" " WHERE status <> 'D'" - " ORDER BY name, cnc_string"); + " ORDER BY name, cnc_string", + priv->commons->prefix); stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error); g_object_unref (stmt); @@ -521,7 +526,7 @@ datasources_on_btn_delete_clicked (GtkButton *button, if (id != 0) { stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, - g_strdup_printf ("UPDATE datasources SET status = 'D' WHERE id = %d", id), + g_strdup_printf ("UPDATE %sdatasources SET status = 'D' WHERE id = %d", priv->commons->prefix, id), NULL, NULL); } else @@ -532,7 +537,7 @@ datasources_on_btn_delete_clicked (GtkButton *button, if (id != 0) { stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, - g_strdup_printf ("UPDATE tables SET status = 'D' WHERE id = %d", id), + g_strdup_printf ("UPDATE %stables SET status = 'D' WHERE id = %d", priv->commons->prefix, id), NULL, NULL); } diff --git a/src/table.c b/src/table.c index c9eddef..0b8dd51 100644 --- a/src/table.c +++ b/src/table.c @@ -17,6 +17,7 @@ * */ +#include #include #include "table.h" @@ -291,7 +292,8 @@ table_load (Table *table) GdaStatement *stmt; GdaDataModel *dm; - sql = g_strdup_printf ("SELECT name FROM tables WHERE id = %d", + sql = g_strdup_printf ("SELECT name FROM %stables WHERE id = %d", + priv->commons->prefix, priv->id); stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error); @@ -317,24 +319,49 @@ static void table_save (Table *table) { GError *error; + GtkTreeIter iter; + gchar *sql; GdaStatement *stmt; GdaDataModel *dm; GtkWidget *dialog; - gchar *cncstring; + guint new_id; + + gchar *table_name; + guint id_field; + gchar *field_name; + gboolean choosen; + gchar *id_saved; TableClass *klass = TABLE_GET_CLASS (table); TablePrivate *priv = TABLE_GET_PRIVATE (table); + if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (gtk_builder_get_object (priv->commons->gtkbuilder, "combobox1")), &iter)) + { + gtk_tree_model_get (GTK_TREE_MODEL (gtk_combo_box_get_model (GTK_COMBO_BOX (gtk_builder_get_object (priv->commons->gtkbuilder, "combobox1")))), &iter, + 0, &table_name, + -1); + } + else + { + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "You must select a table."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return; + } + if (priv->id == 0) { /* find the new id */ - guint new_id; - new_id = 0; - sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM tables"); + sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM %stables", + priv->commons->prefix); stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error); g_object_unref (stmt); @@ -344,19 +371,22 @@ table_save (Table *table) } new_id++; - sql = g_strdup_printf ("INSERT INTO tables (id, name)" + sql = g_strdup_printf ("INSERT INTO %stables" + " (id, name)" " VALUES (%d, '%s')", + priv->commons->prefix, new_id, - gtk_entry_get_text (GTK_ENTRY (gtk_builder_get_object (priv->commons->gtkbuilder, "entry1")))); + table_name); stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); } else { - sql = g_strdup_printf ("UPDATE tables" + sql = g_strdup_printf ("UPDATE %stables" " SET" " name = '%s'" " WHERE id = %d", - gtk_entry_get_text (GTK_ENTRY (gtk_builder_get_object (priv->commons->gtkbuilder, "entry1"))), + priv->commons->prefix, + table_name, priv->id); stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); } @@ -366,13 +396,92 @@ table_save (Table *table) { g_object_unref (stmt); - g_signal_emit (table, klass->updated_signal_id, 0); - if (priv->id == 0) { - priv->id = strtol (gtk_label_get_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label7"))), NULL, 10); + priv->id = new_id; + gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label7")), g_strdup_printf ("%d", new_id)); + } + + /* saving fields */ + id_saved = g_strdup (""); + if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->lstore_fields), &iter)) + { + do + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_fields), &iter, + 0, &id_field, + 1, &field_name, + 2, &choosen, + -1); + + if (choosen) + { + if (id_field == 0) + { + error = NULL; + sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM %sfields" + " WHERE id_tables = %d", + priv->commons->prefix, + priv->id); + stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); + dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error); + if (dm != NULL && gda_data_model_get_n_rows (dm) == 1) + { + id_field = g_value_get_int (gda_data_model_get_value_at (dm, 0, 0, NULL)); + } + else + { + g_warning ("Error getting new id field: %s", + (error != NULL && error->message != NULL ? error->message : "No details.")); + } + id_field++; + g_object_unref (stmt); + + error = NULL; + sql = g_strdup_printf ("INSERT INTO %sfields" + " (id, id_tables, name)" + " VALUES (%d, %d, '%s')", + priv->commons->prefix, + id_field, + priv->id, + field_name); + stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); + gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error); + if (error != NULL) + { + g_warning ("Error save new field: %s", + (error->message != NULL ? error->message : "No details.")); + } + g_object_unref (stmt); + } + id_saved = g_strconcat (id_saved, g_strdup_printf ("%d,", id_field), NULL); + } + } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->lstore_fields), &iter)); } + if (g_strcmp0 (id_saved, "") != 0) + { + /* removing fields not saved */ + error = NULL; + id_saved[strlen (id_saved) - 1] = '\0'; + sql = g_strdup_printf ("DELETE FROM %sfields" + " WHERE id_tables = %d" + " AND id NOT IN (%s)", + priv->commons->prefix, + priv->id, + id_saved); + stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); + gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error); + if (error != NULL) + { + g_warning ("Error removing old fields: %s", + (error->message != NULL ? error->message : "No details.")); + } + g_object_unref (stmt); + } + + g_signal_emit (table, klass->updated_signal_id, 0); + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, @@ -443,12 +552,12 @@ fixed_toggled (GtkCellRendererToggle *cell, /* get toggled iter */ gtk_tree_model_get_iter (model, &iter, path); - gtk_tree_model_get (model, &iter, 1, &choosen, -1); + gtk_tree_model_get (model, &iter, 2, &choosen, -1); choosen ^= 1; /* set new value */ - gtk_list_store_set (GTK_LIST_STORE (model), &iter, 1, choosen, -1); + gtk_list_store_set (GTK_LIST_STORE (model), &iter, 2, choosen, -1); /* clean up */ gtk_tree_path_free (path); @@ -464,6 +573,8 @@ table_on_cb_tables_changed (GtkComboBox *widget, gchar *table_name; GdaDataModel *dm; + GdaStatement *stmt; + GdaDataModel *dm_fields; GtkTreeIter iter; @@ -472,6 +583,9 @@ table_on_cb_tables_changed (GtkComboBox *widget, guint rows; guint row; + gchar *field_name; + gchar *sql; + TablePrivate *priv = TABLE_GET_PRIVATE ((Table *)user_data); gtk_list_store_clear (GTK_LIST_STORE (priv->lstore_fields)); @@ -517,10 +631,27 @@ table_on_cb_tables_changed (GtkComboBox *widget, rows = gda_data_model_get_n_rows (dm); for (row = 0; row < rows; row++) { + field_name = gda_value_stringify (gda_data_model_get_value_at (dm, 0, row, NULL)); gtk_list_store_append (priv->lstore_fields, &iter); gtk_list_store_set (priv->lstore_fields, &iter, - 0, gda_value_stringify (gda_data_model_get_value_at (dm, 0, row, NULL)), + 1, field_name, -1); + + /* check if field is choosen for libaudit */ + error = NULL; + sql = g_strdup_printf ("SELECT id FROM %sfields WHERE name = '%s' AND status <> 'D'", + priv->commons->prefix, + field_name); + stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); + dm_fields = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error); + if (dm_fields != NULL && gda_data_model_get_n_rows (dm_fields) == 1) + { + gtk_list_store_set (priv->lstore_fields, &iter, + 0, g_value_get_int (gda_data_model_get_value_at (dm_fields, 0, 0, NULL)), + 2, TRUE, + -1); + } + g_object_unref (stmt); } } -- 2.49.0