From: Andrea Zagli Date: Sun, 1 Apr 2018 10:20:16 +0000 (+0200) Subject: Added option to return sql statements for missing tables/fields. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=HEAD;p=gdadbdiff Added option to return sql statements for missing tables/fields. --- diff --git a/src/main.c b/src/main.c index 38c2fd3..08bda30 100644 --- a/src/main.c +++ b/src/main.c @@ -26,6 +26,7 @@ static gchar *ref_db_cnc = NULL; static gchar *db_cnc = NULL; +static gboolean sql = FALSE; static GdaConnection *gda_conn_ref_db; static GdaConnection *gda_conn_db; @@ -48,8 +49,9 @@ typedef struct static GOptionEntry entries[] = { - { "ref-db-cnc", 0, 0, G_OPTION_ARG_STRING, &ref_db_cnc, "Reference database connection string", "CNC_STRING" }, - { "db-cnc", 0, 0, G_OPTION_ARG_STRING, &db_cnc, "Database connection string", "CNC_STRING" }, + { "ref-db-cnc", 'r', 0, G_OPTION_ARG_STRING, &ref_db_cnc, "Reference database connection string", "CNC_STRING" }, + { "db-cnc", 'd', 0, G_OPTION_ARG_STRING, &db_cnc, "Database connection string", "CNC_STRING" }, + { "sql", 's', 0, G_OPTION_ARG_NONE, &sql, "Show sql statement for table/field changes", NULL }, { NULL } }; @@ -129,7 +131,10 @@ read_fields (GdaConnection *gdacon, Table *table) field->gtype_type = gda_value_stringify (gda_data_model_get_value_at (dm_fields, 2, row, NULL)); field->size = g_value_get_int (gda_data_model_get_value_at (dm_fields, 3, row, NULL)); field->not_not_null = g_value_get_boolean (gda_data_model_get_value_at (dm_fields, 5, row, NULL)); - field->default_value = gda_value_stringify (gda_data_model_get_value_at (dm_fields, 6, row, NULL)); + if (!gda_value_is_null (gda_data_model_get_value_at (dm_fields, 6, row, NULL))) + { + field->default_value = gda_value_stringify (gda_data_model_get_value_at (dm_fields, 6, row, NULL)); + } g_message ("\t\tField: %s (%s %s %d)", field->name, field->data_type, field->gtype_type, field->size); g_hash_table_insert (table->ht_fields, field->name, field); @@ -180,6 +185,120 @@ read_tables (GdaConnection *gdacon, GHashTable *ht) } } +static void +create_field (GdaConnection *gdacon, Table *table, Field *field) +{ + GError *error; + GdaServerProvider *pr; + GdaServerOperation *op; + + pr = gda_connection_get_provider (gdacon); + + error = NULL; + op = gda_server_provider_create_operation (pr, gdacon, + GDA_SERVER_OPERATION_ADD_COLUMN, + NULL, + &error); + if (op == NULL + || error != NULL) + { + g_warning ("Unable to create GdaServerOperation add_column: %s", + error != NULL && error->message != NULL ? error->message : "no detail"); + } + else + { + error = NULL; + gda_server_operation_set_value_at (op, table->name, &error, "/COLUMN_DEF_P/TABLE_NAME"); + + error = NULL; + gda_server_operation_set_value_at (op, field->name, &error, "/COLUMN_DEF_P/COLUMN_NAME"); + + error = NULL; + gda_server_operation_set_value_at (op, field->data_type, &error, "/COLUMN_DEF_P/COLUMN_TYPE"); + + error = NULL; + gda_server_operation_set_value_at (op, g_strdup_printf ("%d", field->size), &error, "/COLUMN_DEF_P/COLUMN_SIZE"); + + if (field->default_value != NULL) + { + error = NULL; + gda_server_operation_set_value_at (op, field->default_value, &error, "/COLUMN_DEF_P/COLUMN_DEFAULT"); + } + + error = NULL; + g_message ("SQL: %s", + gda_server_provider_render_operation (pr, gdacon, op, &error)); + } + if (op != NULL) + { + g_object_unref (op); + } +} + +static void +create_table (GdaConnection *gdacon, Table *table) +{ + GError *error; + GdaServerProvider *pr; + GdaServerOperation *op; + + guint i; + GHashTableIter ht_iter; + gpointer key; + gpointer value; + + pr = gda_connection_get_provider (gdacon); + + error = NULL; + op = gda_server_provider_create_operation (pr, gdacon, + GDA_SERVER_OPERATION_CREATE_TABLE, + NULL, + &error); + if (op == NULL + || error != NULL) + { + g_warning ("Unable to create GdaServerOperation create_table: %s", + error != NULL && error->message != NULL ? error->message : "no detail"); + } + else + { + error = NULL; + gda_server_operation_set_value_at (op, table->name, &error, "/TABLE_DEF_P/TABLE_NAME"); + + i = 0; + g_hash_table_iter_init (&ht_iter, table->ht_fields); + while (g_hash_table_iter_next (&ht_iter, &key, &value)) + { + Field *field = (Field *)value; + + error = NULL; + gda_server_operation_set_value_at (op, field->name, &error, g_strdup_printf ("/FIELDS_A/@COLUMN_NAME/%d", i)); + + error = NULL; + gda_server_operation_set_value_at (op, field->data_type, &error, g_strdup_printf ("/FIELDS_A/@COLUMN_TYPE/%d", i)); + + error = NULL; + gda_server_operation_set_value_at (op, g_strdup_printf ("%d", field->size), &error, g_strdup_printf ("/FIELDS_A/@COLUMN_SIZE/%d", i)); + + if (field->default_value != NULL) + { + error = NULL; + gda_server_operation_set_value_at (op, field->default_value, &error, g_strdup_printf ("/FIELDS_A/@COLUMN_DEFAULT/%d", i)); + } + + i++; + } + + error = NULL; + g_message ("SQL: %s", + gda_server_provider_render_operation (pr, gdacon, op, &error)); + } + if (op != NULL) + { + g_object_unref (op); + } +} + static gboolean comparing_fields (Table *ref_table, Table *table) { @@ -198,6 +317,11 @@ comparing_fields (Table *ref_table, Table *table) { g_message ("Field «%s» missing on table «%s».", field->name, table->name); ret = FALSE; + + if (sql) + { + create_field (gda_conn_db, table, field); + } } } @@ -226,6 +350,11 @@ comparing (GHashTable *ht_ref_db_tables, GHashTable *ht_db_tables) { g_message ("Table «%s» missing.", ref_table->name); ret = FALSE; + + if (sql) + { + create_table (gda_conn_db, ref_table); + } } else {