]> saetta.ns0.it Git - gdadbcopy/commitdiff
Builded sql statement template for insert into.
authorAndrea Zagli <azagli@libero.it>
Thu, 22 Nov 2012 21:24:17 +0000 (22:24 +0100)
committerAndrea Zagli <azagli@libero.it>
Thu, 22 Nov 2012 21:24:17 +0000 (22:24 +0100)
src/main.c
tests/db.db
tests/refdb.db

index eb60f697bdef1baedf767298c36c532137321016..4b1bcd50cbe7ef0bf7cda02793c982b4b4add246 100644 (file)
@@ -35,6 +35,17 @@ static GOptionEntry entries[] =
        { NULL }
 };
 
+typedef struct
+{
+       gchar *name;
+
+       GdaSqlBuilder *sqlbuilder;
+
+       GSList *fields;
+} Table;
+
+GSList *tables;
+
 static void
 update_metastore ()
 {
@@ -60,7 +71,55 @@ update_metastore ()
 }
 
 static void
-read_fields (const gchar *table_name, GdaServerOperation *op)
+copy_data ()
+{
+       Table *table;
+
+       GSList *lst_tables;
+       GSList *lst_fields;
+
+       gchar *sql;
+       GdaDataModel *dm;
+       const GValue *gval;
+
+       GError *error;
+
+       GdaDataModelIter *iter;
+       GdaMetaTableColumn *column;
+
+       for (lst_tables = tables; lst_tables; lst_tables = lst_tables->next)
+               {
+                       table = (Table *)lst_tables->data;
+                       sql = g_strdup_printf ("SELECT * FROM %s",
+                                              table->name);
+                       error = NULL;
+                       dm = gda_execute_select_command (gda_conn_db,
+                                                        sql,
+                                                        &error);
+                       if (dm != NULL)
+                               {
+                                       iter = gda_data_model_create_iter (dm);
+                                       while (gda_data_model_iter_move_next (iter))
+                                               {
+                                                       for (lst_fields = table->fields; lst_fields; lst_fields = lst_fields->next)
+                                                               {
+                                                                       column = (GdaMetaTableColumn *)lst_fields->data;
+                                                                       gval = gda_data_model_iter_get_value_for_field (iter,
+                                                                                                                       column->column_name);
+                                                               }
+                                               }
+                               }
+                       else
+                               {
+                                       g_warning ("Error executing select command: %s",
+                                                  (error != NULL && error->message != NULL ? error->message : "no details."));
+                                       continue;
+                               }
+               }
+}
+
+static void
+read_fields (Table *table, GdaServerOperation *op)
 {
        GError *error;
 
@@ -81,7 +140,7 @@ read_fields (const gchar *table_name, GdaServerOperation *op)
        mcontext.column_values[0] = gda_value_new (G_TYPE_STRING);
 
        g_value_take_string (mcontext.column_values[0],
-                            gda_sql_identifier_quote (table_name, gda_conn_ref_db, NULL, FALSE, FALSE));
+                            gda_sql_identifier_quote (table->name, gda_conn_ref_db, NULL, FALSE, FALSE));
 
        error = NULL;
        if (!gda_connection_update_meta_store (gda_conn_ref_db, &mcontext, &error))
@@ -94,7 +153,7 @@ read_fields (const gchar *table_name, GdaServerOperation *op)
        error = NULL;
 
        gval = gda_value_new (G_TYPE_STRING);
-       g_value_take_string (gval, (gchar *)table_name);
+       g_value_take_string (gval, table->name);
 
        store = gda_connection_get_meta_store (gda_conn_ref_db);
        mstruct = gda_meta_struct_new (store, GDA_META_STRUCT_FEATURE_NONE);
@@ -105,10 +164,12 @@ read_fields (const gchar *table_name, GdaServerOperation *op)
        gda_value_free (gval);
        if (dbo != NULL)
                {
-                       GdaMetaTable *table = GDA_META_TABLE (dbo);
+                       GdaMetaTable *meta_table = GDA_META_TABLE (dbo);
+
+                       table->fields = meta_table->columns;
 
                        i = 0;
-                       for (list = table->columns; list; list = list->next, i++)
+                       for (list = meta_table->columns; list; list = list->next, i++)
                                {
                                        column = (GdaMetaTableColumn *)list->data;
                                        g_message ("\t\tField: %s - %s - %d - %d - %d - %s",
@@ -119,6 +180,15 @@ read_fields (const gchar *table_name, GdaServerOperation *op)
                                                column->nullok,
                                                column->default_value);
 
+                                       /* sql builder */
+                                       gda_sql_builder_add_field_value_id (table->sqlbuilder,
+                                                                           gda_sql_builder_add_id (table->sqlbuilder, column->column_name),
+                                                                           gda_sql_builder_add_param (table->sqlbuilder,
+                                                                                                      g_strdup_printf ("p_%s", column->column_name),
+                                                                                                      column->gtype,
+                                                                                                      FALSE));
+
+                                       /* server operation */
                                        error = NULL;
                                        if (!gda_server_operation_set_value_at (op,
                                                                                column->column_name,
@@ -155,9 +225,10 @@ read_tables ()
        guint rows;
        guint row;
 
-       gchar *table_name;
+       Table *table;
 
        GdaServerOperation *op;
+       GdaStatement *stmt;
 
        error = NULL;
        dm_tables = gda_connection_get_meta_store_data (gda_conn_ref_db,
@@ -169,8 +240,14 @@ read_tables ()
                        rows = gda_data_model_get_n_rows (dm_tables);
                        for (row = 0; row < rows; row++)
                                {
-                                       table_name = gda_value_stringify (gda_data_model_get_value_at (dm_tables, 0, row, NULL));
-                                       g_message ("\tTable: %s", table_name);
+                                       table = g_new0 (Table, 1);
+                                       tables = g_slist_append (tables, table);
+                                       table->name = gda_value_stringify (gda_data_model_get_value_at (dm_tables, 0, row, NULL));
+                                       g_message ("\tTable: %s", table->name);
+
+                                       /* sql builder for insert into */
+                                       table->sqlbuilder = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
+                                       gda_sql_builder_set_table (table->sqlbuilder, table->name);
 
                                        /* starting server operation */
                                        error = NULL;
@@ -184,7 +261,7 @@ read_tables ()
                                                }
 
                                        error = NULL;
-                                       if (!gda_server_operation_set_value_at (op, table_name, &error, 
+                                       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.",
@@ -192,7 +269,7 @@ read_tables ()
                                                        continue;
                                                }
 
-                                       read_fields (table_name, op);
+                                       read_fields (table, op);
 
                                        error = NULL;
                                        if (!gda_connection_perform_operation (gda_conn_db, op, &error))
@@ -202,6 +279,12 @@ read_tables ()
                                                        continue;
                                                }
 
+                                       error = NULL;
+                                       stmt = gda_sql_builder_get_statement (table->sqlbuilder, &error);
+                                       error = NULL;
+                                       g_message ("\t\tSql: %s",
+                                                  gda_statement_to_sql (stmt, NULL, &error));
+
                                        g_object_unref (op);
                                }
                }
@@ -266,7 +349,11 @@ main (int argc, char *argv[])
        update_metastore ();
 
        g_message ("Reading tables of reference database (%s).", ref_db_cnc);
-       read_tables (gda_conn_ref_db);
+       tables = g_slist_alloc ();
+       read_tables ();
+
+       /*g_message ("Copying data.");
+       copy_data ();*/
 
        g_object_unref (gda_conn_ref_db);
        g_object_unref (gda_conn_db);
index 6b88bd9cb1841cc069c831cf19be577b87b4828f..c4808c7d4e014244f84aa0215f0459ca1eca476c 100644 (file)
Binary files a/tests/db.db and b/tests/db.db differ
index 81a6e21fec7986cb58e9e4f99691f8c2ac396241..932e681acccb2d3d9b928af5e5f0bad7678254fc 100644 (file)
Binary files a/tests/refdb.db and b/tests/refdb.db differ