From: Andrea Zagli Date: Sat, 24 Nov 2012 18:20:20 +0000 (+0100) Subject: Read views. X-Git-Tag: 0.0.1~5 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=343eff74da54cb78b1fa4b05b2ed7314baaf7d7a;p=gdadbcopy Read views. --- diff --git a/.anjuta_sym_db.db b/.anjuta_sym_db.db index a527b47..c8b7601 100644 Binary files a/.anjuta_sym_db.db and b/.anjuta_sym_db.db differ diff --git a/src/main.c b/src/main.c index cc612f5..9ec049e 100644 --- a/src/main.c +++ b/src/main.c @@ -452,6 +452,150 @@ read_tables () } } +static void +read_views () +{ + GError *error; + + GdaDataModel *dm_views; + + guint rows; + guint row; + + gchar *view_name; + gchar *view_def; + + GdaServerOperation *op; + + GValue *gval; + GdaMetaStore *store; + GdaMetaStruct *mstruct; + GdaMetaDbObject *dbo; + + error = NULL; + dm_views = gda_connection_get_meta_store_data (gda_conn_ref_db, + GDA_CONNECTION_META_VIEWS, + &error, + 0); + if (dm_views != NULL && error == NULL) + { + rows = gda_data_model_get_n_rows (dm_views); + for (row = 0; row < rows; row++) + { + view_name = g_strdup (gda_value_stringify (gda_data_model_get_value_at (dm_views, 0, row, NULL))); + + g_printf ("\tView: %s\n", + view_name); + + if (drop_tables) + { + gchar *sql; + + sql = g_strdup_printf ("DROP VIEW %s", + view_name); + gda_connection_execute_non_select_command (gda_conn_db, + sql, + NULL); + + g_free (sql); + } + + error = NULL; + + gval = gda_value_new (G_TYPE_STRING); + g_value_set_string (gval, view_name); + + store = gda_connection_get_meta_store (gda_conn_ref_db); + mstruct = gda_meta_struct_new (store, GDA_META_STRUCT_FEATURE_NONE); + dbo = gda_meta_struct_complement (mstruct, GDA_META_DB_VIEW, + NULL, NULL, + gval, &error); + + gda_value_free (gval); + if (dbo != NULL) + { + gchar *tmp; + gchar *tmp_low; + + GdaMetaView *meta_view = GDA_META_VIEW (dbo); + + tmp_low = g_ascii_strup (meta_view->view_def, -1); + tmp = g_strstr_len (tmp_low, -1, "SELECT"); + if (tmp != NULL) + { + view_def = g_strdup (meta_view->view_def + + (tmp - tmp_low)); + g_free (tmp_low); + } + else + { + g_free (tmp_low); + g_warning ("Unable to get view's definition."); + continue; + } + } + else + { + g_warning ("Unable to get view's definition: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + continue; + } + + if (verbose) + { + g_printf ("\t\tSql: %s\n", + view_def); + } + + /* starting server operation */ + error = NULL; + op = gda_connection_create_operation (gda_conn_db, GDA_SERVER_OPERATION_CREATE_VIEW, 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, view_name, &error, + "/VIEW_DEF_P/VIEW_NAME")) + { + g_warning ("Error on setting GdaServerOperation's value: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + continue; + } + + error = NULL; + if (!gda_server_operation_set_value_at (op, view_def, &error, + "/VIEW_DEF_P/VIEW_DEF")) + { + 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_free (view_def); + g_free (view_name); + + g_object_unref (op); + } + } + if (dm_views != NULL) + { + g_object_unref (dm_views); + } +} + int main (int argc, char *argv[]) { @@ -514,6 +658,9 @@ main (int argc, char *argv[]) g_printf ("* Copying data.\n"); copy_data (); + g_printf ("* Reading views.\n"); + read_views (); + g_object_unref (gda_conn_ref_db); g_object_unref (gda_conn_db); diff --git a/tests/db.db b/tests/db.db index 77295cc..2dd433b 100644 Binary files a/tests/db.db and b/tests/db.db differ diff --git a/tests/refdb.db b/tests/refdb.db index 7b3d334..449a193 100644 Binary files a/tests/refdb.db and b/tests/refdb.db differ