]> saetta.ns0.it Git - gdadbcopy/commitdiff
Read views.
authorAndrea Zagli <azagli@libero.it>
Sat, 24 Nov 2012 18:20:20 +0000 (19:20 +0100)
committerAndrea Zagli <azagli@libero.it>
Sat, 24 Nov 2012 18:20:20 +0000 (19:20 +0100)
.anjuta_sym_db.db
src/main.c
tests/db.db
tests/refdb.db

index a527b479be25c444eed00eb8001f939da53cb077..c8b7601ae068d8c59443796cbba6a82f658872bf 100644 (file)
Binary files a/.anjuta_sym_db.db and b/.anjuta_sym_db.db differ
index cc612f563b6802ae32a4f1c21d8eba283de24582..9ec049e8973ffdebfda5a6b50fb2ac3fe3da4773 100644 (file)
@@ -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);
 
index 77295cccbf00de1e7b147391e34398fd8aeb3862..2dd433b277d62bed76ba1fcd5b9e5aecfa55e722 100644 (file)
Binary files a/tests/db.db and b/tests/db.db differ
index 7b3d334f8d818d7d4b6caa60a80a2005c4f010a1..449a1933ba8e867db1382475b25888107a048160 100644 (file)
Binary files a/tests/refdb.db and b/tests/refdb.db differ