From 343eff74da54cb78b1fa4b05b2ed7314baaf7d7a Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 24 Nov 2012 19:20:20 +0100 Subject: [PATCH] Read views. --- .anjuta_sym_db.db | Bin 34816 -> 34816 bytes src/main.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++ tests/db.db | Bin 3072 -> 3072 bytes tests/refdb.db | Bin 3072 -> 3072 bytes 4 files changed, 147 insertions(+) diff --git a/.anjuta_sym_db.db b/.anjuta_sym_db.db index a527b479be25c444eed00eb8001f939da53cb077..c8b7601ae068d8c59443796cbba6a82f658872bf 100644 GIT binary patch delta 456 zcmZvXJ4ho@7=`cupV6Cv=;&36_`)HA2wNCd)L7a_Z3MN6l4K>Q$+F{P1<{DFHZqmQ zKKLkf?_y)8f;P&+ij9?a+gXT$2s%~~+~zdrobPZlS9iXA}8>OTl~cdGfwnRGgn)-Gdm7+k7ZN^+=P%`ppGQFCft%1 k07uhO%5ncth1J}hQ#lzDebNQJ(XHq=@>DmY!r0qpe|gYZWB>pF 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 77295cccbf00de1e7b147391e34398fd8aeb3862..2dd433b277d62bed76ba1fcd5b9e5aecfa55e722 100644 GIT binary patch literal 3072 zcmeHG%TB^T6rE`au+fbnjgrVUE}(`cErN!f6)K`&v{VdXVG0bP3A9N&XjtM``4N7F z8-K&8mRF1 z)gE9K0kr`h8ImrABj~%%2<8;id@3fSQuyVRnjN-cMm;aMfYwmYYuaKTWUL-kZw#%X z>2Rx8>#E*@Yps<9Z|vIUh(J@-OP8viDaay}wF~vWWm delta 79 zcmZpWXpop7Ey%>cz`zW|AR2_9P1G?43hF*m;|1~zpCr(fW03GWP;Q#;t diff --git a/tests/refdb.db b/tests/refdb.db index 7b3d334f8d818d7d4b6caa60a80a2005c4f010a1..449a1933ba8e867db1382475b25888107a048160 100644 GIT binary patch delta 150 zcmZpWXpop7Ey%;bz`zW|Fu*xc$5@z$LHCgwFHne?NtS^*kg1AEc4Ogc#@J|PHc?GY zj`Fh1R4|TDE-fy}&rK~V28kee&Oxq@A+8Exp042vs4@zP#R|o#IjPAd3R((jMftf3 Ma3eNXF|~0508{NNH~;_u delta 55 zcmZpWXpop7Ey&5hz`zW|Fu*oZ#~3K6`$&x!D8$56#lRfMRJB=