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);
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);
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);
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
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);
}
*
*/
+#include <string.h>
#include <sql-parser/gda-sql-parser.h>
#include "table.h"
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);
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);
}
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);
}
{
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,
/* 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);
gchar *table_name;
GdaDataModel *dm;
+ GdaStatement *stmt;
+ GdaDataModel *dm_fields;
GtkTreeIter iter;
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));
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);
}
}