]> saetta.ns0.it Git - gdadbcopy/commitdiff
Added command line argument --only-data (closes #526).
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 28 Dec 2012 07:33:04 +0000 (08:33 +0100)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 28 Dec 2012 07:33:04 +0000 (08:33 +0100)
src/main.c

index 3052d20feed9aa4782f51397d29ca95dda68842c..7410ba0edc1a8492e91211b8bec6db31a4614bf8 100644 (file)
@@ -29,6 +29,7 @@ static gchar **tables_to_copy = NULL;
 static gboolean drop_tables = FALSE;
 static gchar *output_file = NULL;
 static gboolean only_schema = FALSE;
+static gboolean only_data = FALSE;
 static gboolean explicit = FALSE;
 static gboolean verbose = FALSE;
 
@@ -47,7 +48,8 @@ static GOptionEntry entries[] =
        { "table", 0, 0, G_OPTION_ARG_STRING_ARRAY, &tables_to_copy, "The table/view's name to copy (can be used multiple times)", "TABLE_NAME" },
        { "drop-tables", 0, 0, G_OPTION_ARG_NONE, &drop_tables, "Drop tables before creating new ones", NULL },
        { "output-file", 0, 0, G_OPTION_ARG_FILENAME, &output_file, "Ouput the whole operation in an xmlfile", "FILENAME" },
-       { "only-schema", 0, 0, G_OPTION_ARG_NONE, &only_schema, "Read only the schema: it doesn't copy data", NULL },
+       { "only-schema", 0, 0, G_OPTION_ARG_NONE, &only_schema, "Read only the tables/views schema: it doesn't copy data", NULL },
+       { "only-data", 0, 0, G_OPTION_ARG_NONE, &only_data, "Read only the data: it doesn't create tables/views", NULL },
        { "explicit", 0, 0, G_OPTION_ARG_NONE, &explicit, "Read schema from sql statement, instead from GdaMetaStore (at least one --table must be provided)", NULL },
        { "verbose", 0, 0, G_OPTION_ARG_NONE, &verbose, "Verbose", NULL },
        { NULL }
@@ -229,14 +231,9 @@ copy_data ()
                }
 }
 
-static gboolean
-field_server_operation (Table *table,
-                        GdaServerOperation *op,
-                        GdaMetaTableColumn *column,
-                        guint idx_column)
+static void
+field_create_sqlbuilder (Table *table, GdaMetaTableColumn *column)
 {
-       GError *error;
-
        gchar *column_name;
 
        /* sql builder */
@@ -248,6 +245,15 @@ field_server_operation (Table *table,
                                                                       column->gtype,
                                                                       column->nullok));
        g_free (column_name);
+}
+
+static gboolean
+field_server_operation (Table *table,
+                        GdaServerOperation *op,
+                        GdaMetaTableColumn *column,
+                        guint idx_column)
+{
+       GError *error;
 
        /* server operation */
        error = NULL;
@@ -362,7 +368,12 @@ read_fields_explicit (Table *table, GdaServerOperation *op, GdaDataModel *dm)
                                                  mt_column->default_value);
                                }
 
-                       field_server_operation (table, op, mt_column, col);
+                       field_create_sqlbuilder (table, mt_column);
+
+                       if (!only_data)
+                               {
+                                       field_server_operation (table, op, mt_column, col);
+                               }
                }
 }
 
@@ -452,7 +463,12 @@ read_fields (Table *table, GdaServerOperation *op)
                                                                  column->default_value);
                                                }
 
-                                       field_server_operation (table, op, column, i);
+                                       field_create_sqlbuilder (table, column);
+
+                                       if (!only_data)
+                                               {
+                                                       field_server_operation (table, op, column, i);
+                                               }
                                }
                        }
                        else
@@ -533,6 +549,14 @@ drop_table (Table *table)
                }
 }
 
+static void
+table_create_sqlbuilder (Table *table)
+{
+       /* sql builder for insert into */
+       table->sqlbuilder = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
+       gda_sql_builder_set_table (table->sqlbuilder, table->name);
+}
+
 static GdaServerOperation
 *table_start_server_operation (Table *table)
 {
@@ -540,10 +564,6 @@ static GdaServerOperation
 
        GdaServerOperation *op;
 
-       /* sql builder for insert into */
-       table->sqlbuilder = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
-       gda_sql_builder_set_table (table->sqlbuilder, table->name);
-
        if (!provider_support_operation (gda_conn_db, GDA_SERVER_OPERATION_CREATE_TABLE))
                {
                        g_warning ("Provider doesn't support GDA_SERVER_OPERATION_CREATE_TABLE.");
@@ -630,6 +650,8 @@ read_tables_explicit ()
                        Table *table;
                        GdaServerOperation *op;
 
+                       op = NULL;
+
                        g_printf ("* Reading tables on reference database declared on command line.\n");
 
                        parser = gda_connection_create_parser (gda_conn_ref_db);
@@ -660,15 +682,20 @@ read_tables_explicit ()
                                                                        g_printf ("\tTable: %s\n",
                                                                                  table->name);
 
-                                                                       if (drop_tables)
+                                                                       if (!only_data && drop_tables)
                                                                                {
                                                                                        drop_table (table);
                                                                                }
 
-                                                                       op = table_start_server_operation (table);
-                                                                       if (op == NULL)
+                                                                       table_create_sqlbuilder (table);
+
+                                                                       if (!only_data)
                                                                                {
-                                                                                       continue;
+                                                                                       op = table_start_server_operation (table);
+                                                                                       if (op == NULL)
+                                                                                               {
+                                                                                                       continue;
+                                                                                               }
                                                                                }
                                                                }
 
@@ -676,12 +703,15 @@ read_tables_explicit ()
 
                                                        if (cols > 0)
                                                                {
-                                                                       if (output_file != NULL)
+                                                                       if (!only_data && output_file != NULL)
                                                                                {
                                                                                        output_server_operation (op);
                                                                                }
 
-                                                                       table_exec_server_operation (op);
+                                                                       if (!only_data)
+                                                                               {
+                                                                                       table_exec_server_operation (op);
+                                                                               }
 
                                                                        if (verbose)
                                                                                {
@@ -693,7 +723,10 @@ read_tables_explicit ()
                                                                                        g_object_unref (stmt);
                                                                                }
 
-                                                                       g_object_unref (op);
+                                                                       if (op != NULL)
+                                                                               {
+                                                                                       g_object_unref (op);
+                                                                               }
 
                                                                        tables = g_slist_append (tables, table);
                                                                }
@@ -752,25 +785,33 @@ read_tables ()
                                        g_printf ("\tTable: %s\n",
                                                  table->name);
 
-                                       if (drop_tables)
+                                       if (!only_data && drop_tables)
                                                {
                                                        drop_table (table);
                                                }
 
-                                       op = table_start_server_operation (table);
-                                       if (op == NULL)
+                                       table_create_sqlbuilder (table);
+
+                                       if (!only_data)
                                                {
-                                                       continue;
+                                                       op = table_start_server_operation (table);
+                                                       if (op == NULL)
+                                                               {
+                                                                       continue;
+                                                               }
                                                }
 
                                        read_fields (table, op);
 
-                                       if (output_file != NULL)
+                                       if (!only_data && output_file != NULL)
                                                {
                                                        output_server_operation (op);
                                                }
 
-                                       table_exec_server_operation (op);
+                                       if (!only_data)
+                                               {
+                                                       table_exec_server_operation (op);
+                                               }
 
                                        if (verbose)
                                                {
@@ -1078,8 +1119,6 @@ main (int argc, char *argv[])
                        update_metastore ();
                }
 
-       g_printf ("* Reading tables of reference database (%s).\n",
-                 ref_db_cnc);
        tables = NULL;
        if (explicit)
                {
@@ -1087,6 +1126,8 @@ main (int argc, char *argv[])
                }
        else
                {
+                       g_printf ("* Reading tables of reference database (%s).\n",
+                                 ref_db_cnc);
                        read_tables ();
                }
 
@@ -1096,8 +1137,11 @@ main (int argc, char *argv[])
                        copy_data ();
                }
 
-       g_printf ("* Reading views.\n");
-       read_views ();
+       if (!only_data)
+               {
+                       g_printf ("* Reading views.\n");
+                       read_views ();
+               }
 
        if (output_file != NULL)
                {