]> saetta.ns0.it Git - zakaudit/gui/commitdiff
Identification of the primary key fields.
authorAndrea Zagli <azagli@libero.it>
Wed, 1 Jun 2011 12:29:43 +0000 (14:29 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 1 Jun 2011 12:29:43 +0000 (14:29 +0200)
src/table.c

index e21f0b13398faefe2e4d540d86819c174c2b64bd..f427c601082bc408bb384b6360d234fa752b6c70 100644 (file)
@@ -426,6 +426,8 @@ table_save (Table *table)
 
        guint new_id;
 
+       gboolean is_key;
+
        gchar *table_name;
        guint id_field;
        gchar *field_name;
@@ -489,6 +491,7 @@ table_save (Table *table)
                                               priv->id);
                        stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
                }
+       g_free (sql);
 
        error = NULL;
        if (gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error) == 1)
@@ -517,6 +520,30 @@ table_save (Table *table)
                                                                {
                                                                        if (id_field == 0)
                                                                                {
+                                                                                       /* 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 = (gda_data_model_get_n_rows (dm) > 0);
+
                                                                                        error = NULL;
                                                                                        sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM %sfields",
                                                                                                               priv->commons->prefix);
@@ -532,16 +559,20 @@ table_save (Table *table)
                                                                                                                   (error != NULL && error->message != NULL ? error->message : "No details."));
                                                                                                }
                                                                                        id_field++;
+
+                                                                                       g_free (sql);
                                                                                        g_object_unref (stmt);
+                                                                                       g_object_unref (dm);
 
                                                                                        error = NULL;
                                                                                        sql = g_strdup_printf ("INSERT INTO %sfields"
-                                                                                                              " (id, id_tables, name)"
-                                                                                                             " VALUES (%d, %d, '%s')",
+                                                                                                              " (id, id_tables, name, is_key)"
+                                                                                                             " VALUES (%d, %d, '%s', %s)",
                                                                                                               priv->commons->prefix,
                                                                                                               id_field,
                                                                                                               priv->id,
-                                                                                                              field_name);
+                                                                                                              field_name,
+                                                                                                              is_key ? "TRUE" : "FALSE");
                                                                                        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)
@@ -549,7 +580,10 @@ table_save (Table *table)
                                                                                                        g_warning ("Error saving new field: %s",
                                                                                                                   (error->message != NULL ? error->message : "No details."));
                                                                                                }
+
+                                                                                       g_free (sql);
                                                                                        g_object_unref (stmt);
+                                                                                       g_object_unref (dm);
                                                                                }
                                                                        id_saved = g_strconcat (id_saved, g_strdup_printf ("%d,", id_field), NULL);
                                                                }
@@ -573,6 +607,7 @@ table_save (Table *table)
                                                   (error->message != NULL ? error->message : "No details."));
                                }
                        g_object_unref (stmt);
+                       g_free (sql);
 
                        g_signal_emit (table, klass->updated_signal_id, 0);