]> saetta.ns0.it Git - gdadbcopy/commitdiff
Tables creation (no fields).
authorAndrea Zagli <azagli@libero.it>
Wed, 21 Nov 2012 21:12:51 +0000 (22:12 +0100)
committerAndrea Zagli <azagli@libero.it>
Wed, 21 Nov 2012 21:12:51 +0000 (22:12 +0100)
src/main.c
tests/db.db

index bb4a096e047d378fcd5b46adcc6130fcb03366f7..bc3b3565318585a5f5dc2e67bb06da2aad61e80a 100644 (file)
@@ -28,17 +28,6 @@ static gchar *db_cnc = NULL;
 static GdaConnection *gda_conn_ref_db;
 static GdaConnection *gda_conn_db;
 
-typedef struct
-{
-       gchar *name;
-       GHashTable *ht_fields;
-} Table;
-
-typedef struct
-{
-       gchar *name;
-} Field;
-
 static GOptionEntry entries[] =
 {
        { "ref-db-cnc", 0, 0, G_OPTION_ARG_STRING, &ref_db_cnc, "Reference database connection string", "CNC_STRING" },
@@ -71,7 +60,7 @@ update_metastore ()
 }
 
 static void
-read_fields (GdaConnection *gdacon, Table *table)
+read_fields (const gchar *table_name)
 {
        GError *error;
 
@@ -84,8 +73,6 @@ read_fields (GdaConnection *gdacon, Table *table)
        guint rows;
        guint row;
 
-       Field *field;
-
        error = NULL;
 
        mcontext.column_names = g_new (gchar *, 1);
@@ -94,9 +81,9 @@ read_fields (GdaConnection *gdacon, Table *table)
        mcontext.column_values[0] = gda_value_new (G_TYPE_STRING);
 
        g_value_take_string (mcontext.column_values[0],
-                            gda_sql_identifier_quote (table->name, gdacon, NULL, FALSE, FALSE));
+                            gda_sql_identifier_quote (table_name, gda_conn_ref_db, NULL, FALSE, FALSE));
 
-       if (!gda_connection_update_meta_store (gdacon, &mcontext, &error))
+       if (!gda_connection_update_meta_store (gda_conn_ref_db, &mcontext, &error))
                {
                        g_warning ("Unable to update database metastore: %s",
                                   (error != NULL && error->message != NULL ? error->message : "no details."));
@@ -105,8 +92,8 @@ read_fields (GdaConnection *gdacon, Table *table)
 
        error = NULL;
        gval = gda_value_new (G_TYPE_STRING);
-       g_value_take_string (gval, table->name);
-       dm_fields = gda_connection_get_meta_store_data (gdacon,
+       g_value_take_string (gval, (gchar *)table_name);
+       dm_fields = gda_connection_get_meta_store_data (gda_conn_ref_db,
                                                        GDA_CONNECTION_META_FIELDS,
                                                        &error,
                                                        1,
@@ -116,15 +103,11 @@ read_fields (GdaConnection *gdacon, Table *table)
                        rows = gda_data_model_get_n_rows (dm_fields);
                        for (row = 0; row < rows; row++)
                                {
-                                       field = g_new0 (Field, 1);
-                                       field->name = gda_value_stringify (gda_data_model_get_value_at (dm_fields, 0, row, NULL));
-                                       g_message ("\t\tField: %s - %s - %s - %d - %d", field->name,
+                                       g_message ("\t\tField: %s - %s - %s - %d - %d", gda_value_stringify (gda_data_model_get_value_at (dm_fields, 0, row, NULL)),
                                                gda_value_stringify (gda_data_model_get_value_at (dm_fields, 1, row, NULL)),
                                                gda_value_stringify (gda_data_model_get_value_at (dm_fields, 2, row, NULL)),
                                                g_value_get_int (gda_data_model_get_value_at (dm_fields, 3, row, NULL)),
                                                g_value_get_int (gda_data_model_get_value_at (dm_fields, 4, row, NULL)));
-
-                                       g_hash_table_insert (table->ht_fields, field->name, field);
                                }
                }
        if (dm_fields != NULL)
@@ -134,7 +117,7 @@ read_fields (GdaConnection *gdacon, Table *table)
 }
 
 static void
-read_tables (GdaConnection *gdacon, GHashTable *ht)
+read_tables ()
 {
        GError *error;
 
@@ -143,10 +126,12 @@ read_tables (GdaConnection *gdacon, GHashTable *ht)
        guint rows;
        guint row;
 
-       Table *table;
+       gchar *table_name;
+
+       GdaServerOperation *op;
 
        error = NULL;
-       dm_tables = gda_connection_get_meta_store_data (gdacon,
+       dm_tables = gda_connection_get_meta_store_data (gda_conn_ref_db,
                                                        GDA_CONNECTION_META_TABLES,
                                                        &error,
                                                        0);
@@ -155,15 +140,55 @@ read_tables (GdaConnection *gdacon, GHashTable *ht)
                        rows = gda_data_model_get_n_rows (dm_tables);
                        for (row = 0; row < rows; row++)
                                {
-                                       table = g_new0 (Table, 1);
-                                       table->ht_fields = g_hash_table_new (g_str_hash, g_str_equal);
-
-                                       table->name = gda_value_stringify (gda_data_model_get_value_at (dm_tables, 0, row, NULL));
-                                       g_message ("\tTable: %s", table->name);
-
-                                       g_hash_table_insert (ht, table->name, table);
-
-                                       read_fields (gdacon, table);
+                                       table_name = gda_value_stringify (gda_data_model_get_value_at (dm_tables, 0, row, NULL));
+                                       g_message ("\tTable: %s", table_name);
+
+                                       /* starting server operation */
+                                       error = NULL;
+                                       op = gda_connection_create_operation (gda_conn_db, GDA_SERVER_OPERATION_CREATE_TABLE, NULL, &error);
+                                       if (op == NULL
+                                           || error != NULL)
+                                               {
+                                                       g_warning ("Error on creating GdaServerOperation: %s.",
+                                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                                                       continue;
+                                               }
+
+                                       error = NULL;
+                                       if (!gda_server_operation_set_value_at (op, table_name, &error, 
+                                                                               "/TABLE_DEF_P/TABLE_NAME"))
+                                               {
+                                                       g_warning ("Error on setting GdaServerOperation's value: %s.",
+                                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                                                       continue;
+                                               }
+
+                                       /*read_fields (gdacon, table_name);*/
+                                       if (!gda_server_operation_set_value_at (op, "id", &error, 
+                                                                               "/FIELDS_A/@COLUMN_NAME/0"))
+                                               {
+                                                       g_warning ("Error on setting GdaServerOperation's value: %s.",
+                                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                                                       continue;
+                                               }
+                                       if (!gda_server_operation_set_value_at (op, "INTEGER", &error, 
+                                                                               "/FIELDS_A/@COLUMN_TYPE/0"))
+                                               {
+                                                       g_warning ("Error on setting GdaServerOperation's value: %s.",
+                                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                                                       continue;
+                                               }
+
+
+                                       error = NULL;
+                                       if (!gda_connection_perform_operation (gda_conn_db, op, &error))
+                                               {
+                                                       g_warning ("Error on performing GdaServerOperation: %s.",
+                                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                                                       continue;
+                                               }
+
+                                       g_object_unref (op);
                                }
                }
        if (dm_tables != NULL)
@@ -178,8 +203,6 @@ main (int argc, char *argv[])
        GError *error;
        GOptionContext *context;
 
-       GHashTable *ht_ref_db_tables;
-
        g_type_init ();
 
        error = NULL;
@@ -229,8 +252,7 @@ main (int argc, char *argv[])
        update_metastore ();
 
        g_message ("Reading tables of reference database (%s).", ref_db_cnc);
-       ht_ref_db_tables = g_hash_table_new (g_str_hash, g_str_equal);
-       read_tables (gda_conn_ref_db, ht_ref_db_tables);
+       read_tables (gda_conn_ref_db);
 
        g_object_unref (gda_conn_ref_db);
        g_object_unref (gda_conn_db);
index 4c2a966fbfe0a63f38d25a0f784942c2d7023ece..66bc769b6a3320a6265120f2fc0a5e3a7c7efa6e 100644 (file)
Binary files a/tests/db.db and b/tests/db.db differ