*
*/
+#include <string.h>
+
#include <sql-parser/gda-sql-parser.h>
#include "datasources.h"
gboolean is_key;
guint id_field;
+ gchar *ids_tables_saved;
+ gchar *ids_fields_saved;
+
GdaMetaContext mcontext = {"_columns", 1, NULL, NULL};
g_return_if_fail (IS_DATASOURCES (datasources));
{
gtk_widget_hide_all (dialog);
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_WARNING,
- GTK_BUTTONS_OK,
- "Unable to update the metastore: %s",
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to update the metastore: %s",
error != NULL && error->message != NULL ? error->message : "no details");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
{
gtk_widget_hide_all (dialog);
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_WARNING,
- GTK_BUTTONS_OK,
- priv->error_update_metastore);
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ priv->error_update_metastore);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return;
}
+ ids_tables_saved = g_strdup ("");
+ ids_fields_saved = g_strdup ("");
+
/* updating tables */
error = NULL;
dm_tables = gda_connection_get_meta_store_data (priv->gdacon,
- GDA_CONNECTION_META_TABLES,
- &error,
- 0);
+ GDA_CONNECTION_META_TABLES,
+ &error,
+ 0);
if (dm_tables != NULL && error == NULL)
{
gdouble fract;
{
gtk_widget_hide_all (dialog);
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_WARNING,
- GTK_BUTTONS_OK,
- "Unable to get tables list.\n%s",
- (error != NULL && error->message != NULL ? error->message : "No details."));
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to get tables list.\n%s",
+ error != NULL && error->message != NULL ? error->message : "No details.");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
break;
new_id = 0;
sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM %stables",
- priv->commons->prefix);
+ priv->commons->prefix);
error = NULL;
stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, &error);
if (stmt == NULL || error != NULL)
{
gtk_widget_hide_all (dialog);
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_WARNING,
- GTK_BUTTONS_OK,
- "Unable to get tables list.\n%s",
- (error != NULL && error->message != NULL ? error->message : "No details."));
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to get tables list.\n%s",
+ error != NULL && error->message != NULL ? error->message : "No details.");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
break;
new_id++;
sql = g_strdup_printf ("INSERT INTO %stables"
- " (id, name, id_datasources)"
- " VALUES (%d, '%s', %d)",
- priv->commons->prefix,
- new_id,
- table_name,
- id_ds);
+ " (id, name, id_datasources)"
+ " VALUES (%d, '%s', %d)",
+ priv->commons->prefix,
+ new_id,
+ table_name,
+ id_ds);
error = NULL;
stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, &error);
if (stmt == NULL || error != NULL)
{
gtk_widget_hide_all (dialog);
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_WARNING,
- GTK_BUTTONS_OK,
- "Unable to get tables list.\n%s",
- (error != NULL && error->message != NULL ? error->message : "No details."));
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to get tables list.\n%s",
+ error != NULL && error->message != NULL ? error->message : "No details.");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
break;
{
gtk_widget_hide_all (dialog);
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_WARNING,
- GTK_BUTTONS_OK,
- "Unable to get tables list.\n%s",
- (error != NULL && error->message != NULL ? error->message : "No details."));
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to get tables list.\n%s",
+ error != NULL && error->message != NULL ? error->message : "No details.");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
break;
{
gtk_widget_hide_all (dialog);
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_WARNING,
- GTK_BUTTONS_OK,
- "Unable to get tables list.\n%s",
- (error != NULL && error->message != NULL ? error->message : "No details."));
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to get tables list.\n%s",
+ error != NULL && error->message != NULL ? error->message : "No details.");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
break;
g_object_unref (stmt);
g_object_unref (dm);
+ ids_tables_saved = g_strconcat (ids_tables_saved, g_strdup_printf ("%d,", new_id), NULL);
+
/* updating fields */
error = NULL;
mcontext.column_names = g_new (gchar *, 1);
mcontext.column_values = g_new (GValue *, 1);
mcontext.column_values[0] = gda_value_new (G_TYPE_STRING);
g_value_take_string (mcontext.column_values[0],
- gda_sql_identifier_quote (table_name, priv->gdacon, NULL, FALSE, FALSE));
+ gda_sql_identifier_quote (table_name, priv->gdacon, NULL, FALSE, FALSE));
if (!gda_connection_update_meta_store (priv->gdacon, &mcontext, &error))
{
gtk_widget_hide_all (dialog);
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_WARNING,
- GTK_BUTTONS_OK,
- "Unable to get fields list.\n%s",
- (error != NULL && error->message != NULL ? error->message : "No details."));
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to get fields list.\n%s",
+ error != NULL && error->message != NULL ? error->message : "No details.");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
break;
gval = gda_value_new (G_TYPE_STRING);
g_value_take_string (gval, table_name);
dm_fields = gda_connection_get_meta_store_data (priv->gdacon,
- GDA_CONNECTION_META_FIELDS,
- &error,
- 1,
- "name", gval);
+ GDA_CONNECTION_META_FIELDS,
+ &error,
+ 1,
+ "name", gval);
if (dm_fields != NULL)
{
rows_fields = gda_data_model_get_n_rows (dm_fields);
{
field_name = gda_value_stringify (gda_data_model_get_value_at (dm_fields, 0, row_fields, NULL));
+ /* check if already exists */
+ sql = g_strdup_printf ("SELECT id FROM %sfields WHERE id_tables = %d AND name = '%s'",
+ priv->commons->prefix,
+ new_id,
+ field_name);
+ error = NULL;
+ stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, &error);
+ if (stmt == NULL || error != NULL)
+ {
+ gtk_widget_hide_all (dialog);
+ dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to get tables list.\n%s",
+ error != NULL && error->message != NULL ? error->message : "No details.");
+ gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
+ break;
+ }
+
+ g_free (sql);
+
+ error = NULL;
+ dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error);
+ g_object_unref (stmt);
+ if (dm != NULL && error == NULL && gda_data_model_get_n_rows (dm) > 0)
+ {
+ ids_fields_saved = g_strconcat (ids_fields_saved, g_strdup_printf ("%d,", g_value_get_int (gda_data_model_get_value_at (dm, 0, 0, NULL))), NULL);
+ }
+ else if (error != NULL)
+ {
+ gtk_widget_hide_all (dialog);
+ dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to get tables list.\n%s",
+ error != NULL && error->message != NULL ? error->message : "No details.");
+ gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
+ break;
+ }
+ else
+ {
/* find if field is the primary key */
sql = g_strdup_printf ("SELECT column_name"
- " FROM _key_column_usage AS c"
- " INNER JOIN _table_constraints AS t"
- " ON c.table_catalog = t.table_catalog"
- " AND c.table_schema = t.table_schema"
- " AND c.table_name = t.table_name"
- " WHERE t.table_name = '%s'"
- " AND t.constraint_type = 'PRIMARY KEY'"
- " AND c.column_name = '%s'",
- table_name,
- field_name);
+ " FROM _key_column_usage AS c"
+ " INNER JOIN _table_constraints AS t"
+ " ON c.table_catalog = t.table_catalog"
+ " AND c.table_schema = t.table_schema"
+ " AND c.table_name = t.table_name"
+ " WHERE t.table_name = '%s'"
+ " AND t.constraint_type = 'PRIMARY KEY'"
+ " AND c.column_name = '%s'",
+ table_name,
+ field_name);
error = NULL;
dm = gda_meta_store_extract (gda_connection_get_meta_store (priv->gdacon),
- sql,
- &error,
- NULL);
+ sql,
+ &error,
+ NULL);
if (dm == NULL || error != NULL)
{
g_warning ("Unable to get the primary key: %s.",
- error != NULL && error->message != NULL ? error->message : "no details");
+ error != NULL && error->message != NULL ? error->message : "no details");
}
is_key = (dm != NULL && gda_data_model_get_n_rows (dm) > 0);
id_field = 0;
error = NULL;
sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM %sfields",
- priv->commons->prefix);
+ priv->commons->prefix);
stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
g_free (sql);
dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error);
else if (error != NULL)
{
g_warning ("Error getting new id field: %s",
- (error != NULL && error->message != NULL ? error->message : "No details."));
+ error != NULL && error->message != NULL ? error->message : "No details.");
}
id_field++;
error = NULL;
sql = g_strdup_printf ("INSERT INTO %sfields"
- " (id, id_tables, name, is_key)"
- " VALUES (%d, %d, '%s', %s)",
- priv->commons->prefix,
- id_field,
- new_id,
- field_name,
- is_key ? "TRUE" : "FALSE");
+ " (id, id_tables, name, is_key)"
+ " VALUES (%d, %d, '%s', %s)",
+ priv->commons->prefix,
+ id_field,
+ new_id,
+ field_name,
+ is_key ? "TRUE" : "FALSE");
stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
g_free (sql);
gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error);
if (error != NULL)
{
g_warning ("Error saving new field: %s",
- (error->message != NULL ? error->message : "No details."));
+ error->message != NULL ? error->message : "No details.");
+ }
+
+ ids_fields_saved = g_strconcat (ids_fields_saved, g_strdup_printf ("%d,", id_field), NULL);
}
}
}
{
gtk_widget_hide_all (dialog);
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_WARNING,
- GTK_BUTTONS_OK,
- "Unable to get tables list.\n%s",
- (error != NULL && error->message != NULL ? error->message : "No details."));
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "Unable to get tables list.\n%s",
+ error != NULL && error->message != NULL ? error->message : "No details.");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return;
}
-
+
+ /* removing tables that aren't still in db */
+ if (g_strcmp0 (ids_tables_saved, "") != 0)
+ {
+ ids_tables_saved[strlen (ids_tables_saved) - 1] = '\0';
+ sql = g_strdup_printf ("DELETE FROM %stables WHERE id NOT IN (%s)",
+ priv->commons->prefix,
+ ids_tables_saved);
+ stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
+ g_free (sql);
+ gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error);
+ g_object_unref (stmt);
+ }
+
+ /* removing fields that aren't still in db */
+ if (g_strcmp0 (ids_fields_saved, "") != 0)
+ {
+ ids_fields_saved[strlen (ids_fields_saved) - 1] = '\0';
+ sql = g_strdup_printf ("DELETE FROM %sfields WHERE id NOT IN (%s)",
+ priv->commons->prefix,
+ ids_fields_saved);
+ stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
+ g_free (sql);
+ gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error);
+ g_object_unref (stmt);
+ }
+
gtk_widget_hide_all (dialog);
+
datasources_load (datasources);
}
else