From 5ccf47e5afb25dfba1ccda68e62f7f57dd863f4d Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Fri, 21 Dec 2012 12:51:28 +0100 Subject: [PATCH] Start trying to get table's schema from a select. --- .cproject | 56 +++++++ .project | 199 ++++++++++++++++++++++ src/main.c | 472 ++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 595 insertions(+), 132 deletions(-) create mode 100644 .cproject create mode 100644 .project diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..dcf1892 --- /dev/null +++ b/.cproject @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..4fd5fa6 --- /dev/null +++ b/.project @@ -0,0 +1,199 @@ + + + gdadbcopy + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + + + 1356081445076 + + 30 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-autom4te.cache + + + + 1356081445080 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-Makefile + + + + 1356081445083 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-Makefile.in + + + + 1356081445088 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-config.h + + + + 1356081445091 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-aclocal.m4 + + + + 1356081445094 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-config.guess + + + + 1356081445097 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-config.h.in + + + + 1356081445102 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-config.log + + + + 1356081445106 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-config.status + + + + 1356081445109 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-config.sub + + + + 1356081445114 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-configure + + + + 1356081445117 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-depcomp + + + + 1356081445120 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-gdadbcopy.anjuta + + + + 1356081445124 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-install-sh + + + + 1356081445130 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-missing + + + + 1356081445137 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-mkinstalldirs + + + + 1356081445143 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-stamp-h1 + + + + 1356081445148 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-*.exe + + + + 1356081445171 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-*.o + + + + diff --git a/src/main.c b/src/main.c index ea90fb3..ccd1bc9 100644 --- a/src/main.c +++ b/src/main.c @@ -21,6 +21,7 @@ #endif #include +#include static gchar *ref_db_cnc = NULL; static gchar *db_cnc = NULL; @@ -83,6 +84,16 @@ update_metastore () } } +static gboolean +provider_support_operation (GdaConnection *cnc, GdaServerOperationType type) +{ + + return gda_server_provider_supports_operation (gda_connection_get_provider (cnc), + cnc, + type, + NULL); +} + static void copy_data () { @@ -214,6 +225,94 @@ copy_data () } } +static gboolean +field_server_operation (Table *table, + GdaServerOperation *op, + GdaMetaTableColumn *column, + guint idx_column) +{ + GError *error; + + gchar *column_name; + + /* sql builder */ + column_name = g_strdup_printf ("p_%s", column->column_name); + 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, + column_name, + column->gtype, + column->nullok)); + g_free (column_name); + + /* server operation */ + error = NULL; + if (!gda_server_operation_set_value_at (op, + column->column_name, + &error, + "/FIELDS_A/@COLUMN_NAME/%d", idx_column)) + { + g_warning ("Error on setting GdaServerOperation's value: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + return FALSE; + } + + error = NULL; + if (!gda_server_operation_set_value_at (op, + column->column_type, + &error, + "/FIELDS_A/@COLUMN_TYPE/%d", idx_column)) + { + g_warning ("Error on setting GdaServerOperation's value: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + return FALSE; + } + + if (column->pkey) + { + error = NULL; + if (!gda_server_operation_set_value_at (op, + "TRUE", + &error, + "/FIELDS_A/@COLUMN_PKEY/%d", idx_column)) + { + g_warning ("Error on setting GdaServerOperation's value: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + return FALSE; + } + } + + if (!column->nullok) + { + error = NULL; + if (!gda_server_operation_set_value_at (op, + "TRUE", + &error, + "/FIELDS_A/@COLUMN_NNUL/%d", idx_column)) + { + g_warning ("Error on setting GdaServerOperation's value: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + return FALSE; + } + } + + if (column->default_value != NULL) + { + error = NULL; + if (!gda_server_operation_set_value_at (op, + column->default_value, + &error, + "/FIELDS_A/@COLUMN_DEFAULT/%d", idx_column)) + { + g_warning ("Error on setting GdaServerOperation's value: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + return FALSE; + } + } + + return TRUE; +} + static void read_fields (Table *table, GdaServerOperation *op) { @@ -231,7 +330,6 @@ read_fields (Table *table, GdaServerOperation *op) GdaMetaTableColumn *column; guint i; - gchar *column_name; mcontext.column_names = g_new (gchar *, 1); mcontext.column_names[0] = "table_name"; @@ -299,83 +397,251 @@ read_fields (Table *table, GdaServerOperation *op) column->default_value); } - /* sql builder */ - column_name = g_strdup_printf ("p_%s", column->column_name); - 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, - column_name, - column->gtype, - column->nullok)); - g_free (column_name); - - /* server operation */ - error = NULL; - if (!gda_server_operation_set_value_at (op, - column->column_name, - &error, - "/FIELDS_A/@COLUMN_NAME/%d", i)) - { - g_warning ("Error on setting GdaServerOperation's value: %s.", - error != NULL && error->message != NULL ? error->message : "no details"); - continue; - } + field_server_operation (table, op, column, i); + } + } + g_object_unref (mstruct); +} + +static void +drop_table (Table *table) +{ + GError *error; + + GdaServerOperation *op; + + if (!provider_support_operation (gda_conn_db, GDA_SERVER_OPERATION_DROP_TABLE)) + { + g_warning ("Provider doesn't support GDA_SERVER_OPERATION_DROP_TABLE."); + return; + } + error = NULL; + op = gda_connection_create_operation (gda_conn_db, GDA_SERVER_OPERATION_DROP_TABLE, NULL, &error); + if (op == NULL + || error != NULL) + { + g_warning ("Error on creating GdaServerOperation: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + } + else + { + error = NULL; + if (!gda_server_operation_set_value_at (op, table->name, &error, + "/TABLE_DESC_P/TABLE_NAME")) + { + g_warning ("Error on setting GdaServerOperation's value: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + } + else + { error = NULL; - if (!gda_server_operation_set_value_at (op, - column->column_type, - &error, - "/FIELDS_A/@COLUMN_TYPE/%d", i)) + if (!gda_connection_perform_operation (gda_conn_db, op, &error)) { - g_warning ("Error on setting GdaServerOperation's value: %s.", + g_warning ("Error on performing GdaServerOperation: %s.", error != NULL && error->message != NULL ? error->message : "no details"); - continue; - } + } + } + } + + if (op != NULL) + { + g_object_unref (op); + } +} + +static GdaServerOperation +*table_start_server_operation (Table *table) +{ + GError *error; + + 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."); + return NULL; + } + + /* 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"); + return NULL; + } + + 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"); + return NULL; + } + + return op; +} + +static void +table_exec_server_operation (GdaServerOperation *op) +{ + GError *error; + + g_return_if_fail (GDA_IS_SERVER_OPERATION (op)); + + 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"); + } +} + +static void +output_server_operation (GdaServerOperation *op) +{ + GError *error; + + g_return_if_fail (GDA_IS_SERVER_OPERATION (op)); + + error = NULL; + xmlAddChild (xroot, + gda_server_operation_save_data_to_xml (op, + &error)); + if (error != NULL) + { + g_warning ("Error on saving GdaServerOperation to xml: %s.", + error->message != NULL ? error->message : "no details"); + } +} + +static void +read_tables_explicit () +{ + /* if there's some tables speicified on command line */ + if (ht_tables_to_copy != NULL) + { + /* try to read the schema with an sql */ + GdaSqlParser *parser; + + GHashTableIter iter; + gpointer key; + gpointer value; + + gchar *sql; + GdaStatement *stmt; + GError *error; + GdaDataModel *dm; + + guint cols; + guint col; + + Table *table; + GdaServerOperation *op; + GdaColumn *gda_column; + GdaMetaTableColumn *mt_column; + + g_printf ("* Reading tables on reference database declared on command line.\n"); + + parser = gda_connection_create_parser (gda_conn_ref_db); + if (!parser) + { + /* @cnc doe snot provide its own parser => use default one */ + parser = gda_sql_parser_new (); + } - if (column->pkey) + g_hash_table_iter_init (&iter, ht_tables_to_copy); + while (g_hash_table_iter_next (&iter, &key, &value)) + { + sql = g_strdup_printf ("SELECT * FROM %s", (gchar *)key); + stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL); + g_free (sql); + dm = gda_connection_statement_execute_select (gda_conn_ref_db, stmt, NULL, &error); + g_object_unref (stmt); + if (dm != NULL) { - error = NULL; - if (!gda_server_operation_set_value_at (op, - "TRUE", - &error, - "/FIELDS_A/@COLUMN_PKEY/%d", i)) + cols = gda_data_model_get_n_columns (dm); + if (cols > 0) { - g_warning ("Error on setting GdaServerOperation's value: %s.", - error != NULL && error->message != NULL ? error->message : "no details"); - continue; + table = g_new0 (Table, 1); + table->name = g_strdup ((gchar *)key); + table->sqlbuilder = NULL; + table->fields = NULL; + + g_printf ("\tTable: %s\n", + table->name); + + if (drop_tables) + { + drop_table (table); + } + + op = table_start_server_operation (table); + if (op == NULL) + { + continue; + } } - } - if (!column->nullok) - { - error = NULL; - if (!gda_server_operation_set_value_at (op, - "TRUE", - &error, - "/FIELDS_A/@COLUMN_NNUL/%d", i)) + for (col = 0; col < cols; col++) { - g_warning ("Error on setting GdaServerOperation's value: %s.", - error != NULL && error->message != NULL ? error->message : "no details"); - continue; + gda_column = gda_data_model_describe_column (dm, col); + + mt_column = g_new0 (GdaMetaTableColumn, 1); + mt_column->column_name = g_strdup (gda_column_get_name (gda_column)); + mt_column->column_type = g_strdup (gda_column_get_dbms_type (gda_column)); + mt_column->gtype = gda_column_get_g_type (gda_column); + /*mt_column->pkey = gda_column_get_*/ + mt_column->nullok = gda_column_get_allow_null (gda_column); + mt_column->default_value = gda_value_stringify (gda_column_get_default_value (gda_column)); + table->fields = g_slist_append (table->fields, mt_column); + + if (verbose) + { + g_printf ("\t\tField: %s - %s - %d - %d - %d - %s\n", + mt_column->column_name, + mt_column->column_type, + mt_column->gtype, + mt_column->pkey, + mt_column->nullok, + mt_column->default_value); + } + + field_server_operation (table, op, mt_column, col); } - } - if (column->default_value != NULL) - { - error = NULL; - if (!gda_server_operation_set_value_at (op, - column->default_value, - &error, - "/FIELDS_A/@COLUMN_DEFAULT/%d", i)) + if (cols > 0) { - g_warning ("Error on setting GdaServerOperation's value: %s.", - error != NULL && error->message != NULL ? error->message : "no details"); - continue; + if (output_file != NULL) + { + output_server_operation (op); + } + + table_exec_server_operation (op); + + if (verbose) + { + error = NULL; + stmt = gda_sql_builder_get_statement (table->sqlbuilder, &error); + error = NULL; + g_printf ("\t\tSql: %s\n", + gda_statement_to_sql (stmt, NULL, &error)); + g_object_unref (stmt); + } + + g_object_unref (op); } } } } - g_object_unref (mstruct); } static void @@ -401,6 +667,12 @@ read_tables () if (dm_tables != NULL && error == NULL) { rows = gda_data_model_get_n_rows (dm_tables); + if (rows == 0) + { + g_printf ("* No table on reference datababe.\n"); + read_tables_explicit (); + } + for (row = 0; row < rows; row++) { table = g_new0 (Table, 1); @@ -424,61 +696,12 @@ read_tables () if (drop_tables) { - error = NULL; - op = gda_connection_create_operation (gda_conn_db, GDA_SERVER_OPERATION_DROP_TABLE, NULL, &error); - if (op == NULL - || error != NULL) - { - g_warning ("Error on creating GdaServerOperation: %s.", - error != NULL && error->message != NULL ? error->message : "no details"); - } - else - { - error = NULL; - if (!gda_server_operation_set_value_at (op, table->name, &error, - "/TABLE_DESC_P/TABLE_NAME")) - { - g_warning ("Error on setting GdaServerOperation's value: %s.", - error != NULL && error->message != NULL ? error->message : "no details"); - } - else - { - 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"); - } - } - } - - if (op != NULL) - { - g_object_unref (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); - - /* 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; + drop_table (table); } - error = NULL; - if (!gda_server_operation_set_value_at (op, table->name, &error, - "/TABLE_DEF_P/TABLE_NAME")) + op = table_start_server_operation (table); + if (op == NULL) { - g_warning ("Error on setting GdaServerOperation's value: %s.", - error != NULL && error->message != NULL ? error->message : "no details"); continue; } @@ -486,25 +709,10 @@ read_tables () if (output_file != NULL) { - error = NULL; - xmlAddChild (xroot, - gda_server_operation_save_data_to_xml (op, - &error)); - if (error != NULL) - { - g_warning ("Error on saving GdaServerOperation to xml: %s.", - error->message != NULL ? error->message : "no details"); - continue; - } + output_server_operation (op); } - 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; - } + table_exec_server_operation (op); if (verbose) { @@ -755,7 +963,7 @@ main (int argc, char *argv[]) { g_warning ("Could not open connection to reference database: %s.\n", error != NULL && error->message != NULL ? error->message : "no detail"); - return; + return 0; } error = NULL; @@ -766,7 +974,7 @@ main (int argc, char *argv[]) { g_warning ("Could not open connection to database: %s.\n", error != NULL && error->message != NULL ? error->message : "no detail"); - return; + return 0; } if (tables_to_copy != NULL) -- 2.49.0