From: Andrea Zagli Date: Thu, 2 Jun 2011 09:08:11 +0000 (+0200) Subject: Going forward with the development of "add all tables". X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=1c6989449e0fb1db81f6ca88b7c2681e026b071a;p=zakaudit%2Fgui Going forward with the development of "add all tables". TODO: check if fields exist; delete tables and fields not in db. --- diff --git a/src/datasources.c b/src/datasources.c index 9bbf444..acc5a75 100644 --- a/src/datasources.c +++ b/src/datasources.c @@ -74,6 +74,12 @@ struct _DatasourcesPrivate GtkWidget *widget; GtkTreeSelection *selection; GtkTreeStore *tstore_datasources; + + guint update_metastore_datasource_id; + gboolean update_metastore_ended; + gchar *error_update_metastore; + + GdaConnection *gdacon; }; G_DEFINE_TYPE (Datasources, datasources, G_TYPE_OBJECT) @@ -162,6 +168,41 @@ GtkWidget return priv->widget; } +static gpointer +datasources_update_metastore (gpointer data) +{ + Datasources *datasources; + DatasourcesPrivate *priv; + + GError *error; + + GdaMetaContext mcontext = {"_tables", 0, NULL, NULL}; + + datasources = (Datasources *)data; + priv = DATASOURCES_GET_PRIVATE (datasources); + + error = NULL; + priv->gdacon = datasources_get_gdaconnection (priv->commons, priv->update_metastore_datasource_id, &error); + if (priv->gdacon == NULL || error != NULL) + { + priv->error_update_metastore = g_strdup_printf ("Unable to update metastore.\n%s", + (error != NULL && error->message != NULL ? error->message : "No details.")); + priv->update_metastore_ended = TRUE; + return NULL; + } + + error = NULL; + if (!gda_connection_update_meta_store (priv->gdacon, &mcontext, &error)) + { + priv->error_update_metastore = g_strdup_printf ("Unable to update metastore.\n%s", + (error != NULL && error->message != NULL ? error->message : "No details.")); + priv->update_metastore_ended = TRUE; + return NULL; + } + + priv->update_metastore_ended = TRUE; +} + /** * datasources_add_all_tables: * @datasources: @@ -179,6 +220,31 @@ datasources_add_all_tables (Datasources *datasources) guint depth; GtkTreePath *path; + GError *error; + GThread *thr; + + GdaStatement *stmt; + GdaDataModel *dm_tables; + GdaDataModel *dm_fields; + GdaDataModel *dm; + + guint rows; + guint row; + + gchar *table_name; + + gchar *sql; + guint new_id; + + GValue *gval; + guint rows_fields; + guint row_fields; + gchar *field_name; + gboolean is_key; + guint id_field; + + GdaMetaContext mcontext = {"_columns", 1, NULL, NULL}; + g_return_if_fail (IS_DATASOURCES (datasources)); priv = DATASOURCES_GET_PRIVATE (datasources); @@ -211,34 +277,328 @@ datasources_add_all_tables (Datasources *datasources) if (id_ds != 0) { dialog = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "w_progress")); + + /* updating meta store */ + priv->update_metastore_datasource_id = id_ds; + priv->update_metastore_ended = FALSE; + priv->error_update_metastore = NULL; + + error = NULL; + thr = g_thread_create (datasources_update_metastore, + (gpointer)datasources, + FALSE, + &error); + if (thr == 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 update the metastore: %s", + error != NULL && error->message != NULL ? error->message : "no details"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return; + } + gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label10")), - "Updating data store"); + "Updating meta store"); gtk_widget_show_all (dialog); gtk_widget_show_now (dialog); - while (gtk_events_pending ()) gtk_main_iteration (); - g_usleep (3000000); - gtk_progress_bar_pulse (GTK_PROGRESS_BAR (gtk_builder_get_object (priv->commons->gtkbuilder, "progressbar1"))); - while (gtk_events_pending ()) gtk_main_iteration (); - g_usleep (3000000); - gtk_progress_bar_pulse (GTK_PROGRESS_BAR (gtk_builder_get_object (priv->commons->gtkbuilder, "progressbar1"))); - while (gtk_events_pending ()) gtk_main_iteration (); - g_usleep (3000000); - gtk_progress_bar_pulse (GTK_PROGRESS_BAR (gtk_builder_get_object (priv->commons->gtkbuilder, "progressbar1"))); - while (gtk_events_pending ()) gtk_main_iteration (); - g_usleep (3000000); - gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label10")), - "Adding table «table_name»"); - while (gtk_events_pending ()) gtk_main_iteration (); - g_usleep (3000000); - gtk_progress_bar_pulse (GTK_PROGRESS_BAR (gtk_builder_get_object (priv->commons->gtkbuilder, "progressbar1"))); - while (gtk_events_pending ()) gtk_main_iteration (); - g_usleep (3000000); - gtk_progress_bar_pulse (GTK_PROGRESS_BAR (gtk_builder_get_object (priv->commons->gtkbuilder, "progressbar1"))); - while (gtk_events_pending ()) gtk_main_iteration (); - g_usleep (3000000); - gtk_progress_bar_pulse (GTK_PROGRESS_BAR (gtk_builder_get_object (priv->commons->gtkbuilder, "progressbar1"))); - while (gtk_events_pending ()) gtk_main_iteration (); + + while (!priv->update_metastore_ended) + { + while (gtk_events_pending ()) gtk_main_iteration (); + g_usleep (500000); + gtk_progress_bar_pulse (GTK_PROGRESS_BAR (gtk_builder_get_object (priv->commons->gtkbuilder, "progressbar1"))); + } + + if (priv->error_update_metastore != 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, + priv->error_update_metastore); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return; + } + + /* updating tables */ + error = NULL; + dm_tables = gda_connection_get_meta_store_data (priv->gdacon, + GDA_CONNECTION_META_TABLES, + &error, + 0); + if (dm_tables != NULL && error == NULL) + { + gdouble fract; + gdouble fract_tot; + + rows = gda_data_model_get_n_rows (dm_tables); + + fract = 1.0 / rows; + fract_tot = 0.0; + + for (row = 0; row < rows; row++) + { + table_name = gda_value_stringify (gda_data_model_get_value_at (dm_tables, 0, row, NULL)); + + if (!table_is_a_system_table (priv->commons, table_name)) + { + fract_tot += fract; + gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label10")), + g_strdup_printf ("Adding table «%s»", table_name)); + gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (gtk_builder_get_object (priv->commons->gtkbuilder, "progressbar1")), fract_tot); + while (gtk_events_pending ()) gtk_main_iteration (); + + /* check if already exists */ + sql = g_strdup_printf ("SELECT id FROM %stables WHERE name = '%s'", + priv->commons->prefix, + table_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 || gda_data_model_get_n_rows (dm) < 1) + { + /* find the new id */ + g_object_unref (dm); + + new_id = 0; + sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM %stables", + 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_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 && gda_data_model_get_n_rows (dm) > 0) + { + new_id = g_value_get_int (gda_data_model_get_value_at (dm, 0, 0, NULL)); + } + 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); + 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; + if (gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error) != 1 + || 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 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 + { + error = NULL; + new_id = g_value_get_int (gda_data_model_get_value_at (dm, 0, 0, &error)); + } + g_object_unref (stmt); + g_object_unref (dm); + + /* updating fields */ + error = NULL; + mcontext.column_names = g_new (gchar *, 1); + mcontext.column_names[0] = "table_name"; + 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)); + 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_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + break; + } + + error = NULL; + 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); + if (dm_fields != NULL) + { + rows_fields = gda_data_model_get_n_rows (dm_fields); + for (row_fields = 0; row_fields < rows_fields; row_fields++) + { + field_name = gda_value_stringify (gda_data_model_get_value_at (dm_fields, 0, row_fields, NULL)); + + /* 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); + error = NULL; + dm = gda_meta_store_extract (gda_connection_get_meta_store (priv->gdacon), + 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"); + } + 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); + 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); + g_object_unref (stmt); + if (dm != NULL && gda_data_model_get_n_rows (dm) > 0) + { + id_field = g_value_get_int (gda_data_model_get_value_at (dm, 0, 0, NULL)); + } + else if (error != NULL) + { + g_warning ("Error getting new id field: %s", + (error != NULL && error->message != NULL ? error->message : "No details.")); + } + id_field++; + + g_object_unref (dm); + + 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"); + 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); + if (error != NULL) + { + g_warning ("Error saving new field: %s", + (error->message != NULL ? error->message : "No details.")); + } + } + } + } + } + g_object_unref (dm_tables); + } + else + { + 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); + return; + } + gtk_widget_hide_all (dialog); + datasources_load (datasources); } else { @@ -263,6 +623,93 @@ datasources_add_all_tables (Datasources *datasources) } } +#define DATASOURCES_GETGDACONNECTION_ERROR datasources_getgdaconnection_error_quark () +GQuark +datasources_getgdaconnection_error_quark (void) +{ + return g_quark_from_static_string ("datasources-getgdaconnection-error-quark"); +} + +GdaConnection +*datasources_get_gdaconnection (Commons *commons, guint id, GError **error) +{ + gchar *sql; + GError *_error; + GdaStatement *stmt; + GdaDataModel *dm; + gchar *cnc; + + GdaConnection *gdacon; + + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + /* open connection to the db to inspect */ + sql = g_strdup_printf ("SELECT cnc_string" + " FROM %sdatasources" + " WHERE id = %d AND status <> 'D'", + commons->prefix, + id); + + _error = NULL; + stmt = gda_sql_parser_parse_string (commons->gdaparser, sql, NULL, &_error); + if (stmt == NULL || _error != NULL) + { + g_set_error (error, + DATASOURCES_GETGDACONNECTION_ERROR, + DATASOURCES_GETGDACONNECTION_ERROR_NOT_PARSE, + "Unable to parse sql string.\n%s", + (_error != NULL && _error->message != NULL ? _error->message : "No details.")); + return NULL; + } + + _error = NULL; + dm = gda_connection_statement_execute_select (commons->gdacon, stmt, NULL, &_error); + g_object_unref (stmt); + if (dm != NULL && _error == NULL) + { + if (gda_data_model_get_n_rows (dm) < 1) + { + g_set_error (error, + DATASOURCES_GETGDACONNECTION_ERROR, + DATASOURCES_GETGDACONNECTION_ERROR_NOT_FOUND, + "Datasource with id «%d» not found.", + id); + return NULL; + } + else + { + cnc = gda_value_stringify (gda_data_model_get_value_at (dm, 0, 0, NULL)); + g_object_unref (dm); + } + } + else + { + g_set_error (error, + DATASOURCES_GETGDACONNECTION_ERROR, + DATASOURCES_GETGDACONNECTION_ERROR_NOT_PARSE, + "Unable to execute sql.\n%s", + (_error != NULL && _error->message != NULL ? _error->message : "No details.")); + return NULL; + } + + _error = NULL; + gdacon = gda_connection_open_from_string (NULL, cnc, NULL, + GDA_CONNECTION_OPTIONS_NONE, + &_error); + g_free (cnc); + if (gdacon == NULL || _error != NULL) + { + g_set_error (error, + DATASOURCES_GETGDACONNECTION_ERROR, + DATASOURCES_GETGDACONNECTION_ERROR_NO_CONNECTION, + "Unable to to open the connection.\n%s", + (_error != NULL && _error->message != NULL ? _error->message : "No details.")); + return NULL; + } + + return gdacon; +} + /* PRIVATE */ static void datasources_load_fields (Datasources *datasources, guint id_table, GtkTreeIter *iter_parent) diff --git a/src/datasources.h b/src/datasources.h index 4431eec..48799a8 100644 --- a/src/datasources.h +++ b/src/datasources.h @@ -59,6 +59,14 @@ GtkWidget *datasources_get_widget (Datasources *datasources); void datasources_add_all_tables (Datasources *datasources); +enum + { + DATASOURCES_GETGDACONNECTION_ERROR_NOT_FOUND, + DATASOURCES_GETGDACONNECTION_ERROR_NOT_PARSE, + DATASOURCES_GETGDACONNECTION_ERROR_NO_CONNECTION, + } DatasourcesGetGdaConnectionError; +GdaConnection *datasources_get_gdaconnection (Commons *commons, guint id, GError **error); + G_END_DECLS diff --git a/src/table.c b/src/table.c index f427c60..2c5296a 100644 --- a/src/table.c +++ b/src/table.c @@ -21,6 +21,7 @@ #include #include "table.h" +#include "datasources.h" static void table_class_init (TableClass *klass); static void table_init (Table *table); @@ -185,28 +186,26 @@ GtkWidget return priv->w; } -/* PRIVATE */ -static gboolean -table_is_a_system_table (Table *table, const gchar *table_name) +gboolean +table_is_a_system_table (Commons *commons, const gchar *table_name) { gboolean ret; gchar *real_table_name; - TablePrivate *priv = TABLE_GET_PRIVATE (table); - real_table_name = g_strstrip (g_strdelimit (g_strdup (table_name), "\"`", ' ')); - ret = g_strcmp0 (real_table_name, g_strdup_printf ("%sdatasources", priv->commons->prefix)) == 0 - || g_strcmp0 (real_table_name, g_strdup_printf ("%stables", priv->commons->prefix)) == 0 - || g_strcmp0 (real_table_name, g_strdup_printf ("%sfields", priv->commons->prefix)) == 0 - || g_strcmp0 (real_table_name, g_strdup_printf ("%sactions", priv->commons->prefix)) == 0 - || g_strcmp0 (real_table_name, g_strdup_printf ("%svalues", priv->commons->prefix)) == 0; + ret = g_strcmp0 (real_table_name, g_strdup_printf ("%sdatasources", commons->prefix)) == 0 + || g_strcmp0 (real_table_name, g_strdup_printf ("%stables", commons->prefix)) == 0 + || g_strcmp0 (real_table_name, g_strdup_printf ("%sfields", commons->prefix)) == 0 + || g_strcmp0 (real_table_name, g_strdup_printf ("%sactions", commons->prefix)) == 0 + || g_strcmp0 (real_table_name, g_strdup_printf ("%svalues", commons->prefix)) == 0; g_free (real_table_name); return ret; } +/* PRIVATE */ static void table_load_db_tables (Table *table) { @@ -235,53 +234,10 @@ table_load_db_tables (Table *table) gtk_list_store_clear (GTK_LIST_STORE (model)); - /* open connection to the db to inspect */ - sql = g_strdup_printf ("SELECT cnc_string" - " FROM %sdatasources" - " WHERE id = %d AND status <> 'D'", - priv->commons->prefix, - strtol (gtk_label_get_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label8"))), NULL, 10)); - - error = NULL; - stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, &error); - if (stmt == NULL || error != NULL) - { - dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), - 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; - } - - 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) - { - cnc = gda_value_stringify (gda_data_model_get_value_at (dm, 0, 0, NULL)); - g_object_unref (dm); - } - else - { - dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), - 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; - } - error = NULL; - priv->gdacon = gda_connection_open_from_string (NULL, cnc, NULL, - GDA_CONNECTION_OPTIONS_NONE, - &error); + priv->gdacon = datasources_get_gdaconnection (priv->commons, + strtol (gtk_label_get_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label8"))), NULL, 10), + &error); if (priv->gdacon == NULL || error != NULL) { dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), @@ -321,7 +277,7 @@ table_load_db_tables (Table *table) { table_name = gda_value_stringify (gda_data_model_get_value_at (dm, 0, row, NULL)); - if (!table_is_a_system_table (table, table_name)) + if (!table_is_a_system_table (priv->commons, table_name)) { gtk_list_store_append (GTK_LIST_STORE (model), &iter); gtk_list_store_set (GTK_LIST_STORE (model), &iter, @@ -542,7 +498,7 @@ table_save (Table *table) g_warning ("Unable to get the primary key: %s.", error != NULL && error->message != NULL ? error->message : "no details"); } - is_key = (gda_data_model_get_n_rows (dm) > 0); + is_key = (dm != NULL && gda_data_model_get_n_rows (dm) > 0); error = NULL; sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM %sfields", diff --git a/src/table.h b/src/table.h index f201723..b6a584d 100644 --- a/src/table.h +++ b/src/table.h @@ -59,6 +59,8 @@ Table *table_new (Commons *commons, guint id, guint id_datasources); GtkWidget *table_get_widget (Table *table); +gboolean table_is_a_system_table (Commons *commons, const gchar *table_name); + G_END_DECLS