From 0e56f2d2ba7621afe0abf6103b9efa5bac18210d Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 3 Jan 2018 15:51:15 +0100 Subject: [PATCH] Added functions GdaEx::metastore_update, ::metastore_get_tables and ::metastore_get_table_fields. --- .gitignore | 1 + src/gdaex.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++ src/gdaex.h | 6 ++- tests/Makefile.am | 1 + tests/metastore.c | 71 +++++++++++++++++++++++++++++++++ 5 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 tests/metastore.c diff --git a/.gitignore b/.gitignore index cd1690d..830881c 100644 --- a/.gitignore +++ b/.gitignore @@ -53,6 +53,7 @@ tests/*.exe tests/select tests/getsql tests/sqlbuilder +tests/metastore *~ *.gir *.typelib \ No newline at end of file diff --git a/src/gdaex.c b/src/gdaex.c index fc6938e..5cd2419 100644 --- a/src/gdaex.c +++ b/src/gdaex.c @@ -3771,6 +3771,105 @@ const gchar return ret; } +gboolean +gdaex_meta_store_udpate (GdaEx *gdaex, GdaMetaContext *mcontext) +{ + gboolean ret; + + GError *error; + + GdaExPrivate *priv = GDAEX_GET_PRIVATE (gdaex); + + error = NULL; + ret = gda_connection_update_meta_store (priv->gda_conn, mcontext, &error); + if (!ret) + { + g_warning (_("Unable to update meta store: %s"), + error != NULL && error->message != NULL ? error->message : _("no detail.")); + } + + return ret; +} + +GdaDataModel +*gdaex_meta_store_get_tables (GdaEx *gdaex, gboolean update_meta_store) +{ + GdaDataModel *dm; + + GError *error; + + GdaMetaContext mcontext = {"_tables", 0, NULL, NULL}; + + GdaExPrivate *priv = GDAEX_GET_PRIVATE (gdaex); + + dm = NULL; + + if (update_meta_store) + { + gdaex_meta_store_udpate (gdaex, &mcontext); + } + + error = NULL; + dm = gda_connection_get_meta_store_data (priv->gda_conn, + GDA_CONNECTION_META_TABLES, + &error, + 0); + + if (dm == NULL + || error != NULL) + { + g_warning (_("Unable to get tables list: %s"), + error != NULL && error->message != NULL ? error->message : _("no detail.")); + } + + return dm; +} + +GdaDataModel +*gdaex_meta_store_get_table_fields (GdaEx *gdaex, const gchar *table_name, gboolean update_meta_store) +{ + GdaDataModel *dm; + + GError *error; + GValue *gval; + + GdaMetaContext mcontext = {"_columns", 1, NULL, NULL}; + + GdaExPrivate *priv = GDAEX_GET_PRIVATE (gdaex); + + dm = NULL; + + if (update_meta_store) + { + mcontext.column_names = g_new (gchar *, 1); + mcontext.column_names[0] = "table_name"; + mcontext.column_values = g_new (GValue *, 1); + mcontext.column_values[0] = gda_value_new (G_TYPE_STRING); + g_value_take_string (mcontext.column_values[0], + gda_sql_identifier_quote (table_name, priv->gda_conn, NULL, FALSE, FALSE)); + + gdaex_meta_store_udpate (gdaex, &mcontext); + } + + gval = gda_value_new (G_TYPE_STRING); + g_value_take_string (gval, (gchar *)table_name); + + error = NULL; + dm = gda_connection_get_meta_store_data (priv->gda_conn, + GDA_CONNECTION_META_FIELDS, + &error, + 1, + "name", gval); + + if (dm == NULL + || error != NULL) + { + g_warning (_("Unable to get table fields list: %s"), + error != NULL && error->message != NULL ? error->message : _("no detail.")); + } + + return dm; +} /* PRIVATE */ static void diff --git a/src/gdaex.h b/src/gdaex.h index 555c9b5..f0c5f7c 100644 --- a/src/gdaex.h +++ b/src/gdaex.h @@ -1,7 +1,7 @@ /* * gdaex.h * - * Copyright (C) 2005-2017 Andrea Zagli + * Copyright (C) 2005-2018 Andrea Zagli * * This file is part of libgdaex. * @@ -311,6 +311,10 @@ const gchar *gdaex_get_blob (GdaEx *gdaex, const gchar *filename_field_name, const gchar *blob_field_name); +gboolean gdaex_meta_store_udpate (GdaEx *gdaex, GdaMetaContext *mcontext); +GdaDataModel *gdaex_meta_store_get_tables (GdaEx *gdaex, gboolean update_meta_store); +GdaDataModel *gdaex_meta_store_get_table_fields (GdaEx *gdaex, const gchar *table_name, gboolean update_meta_store); + G_END_DECLS diff --git a/tests/Makefile.am b/tests/Makefile.am index 57543bd..6fe70f6 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -7,6 +7,7 @@ AM_CPPFLAGS = $(GDAEX_CFLAGS) \ noinst_PROGRAMS = fill_liststore \ getsql \ + metastore \ query_editor \ select \ sqlbuilder \ diff --git a/tests/metastore.c b/tests/metastore.c new file mode 100644 index 0000000..657d7bf --- /dev/null +++ b/tests/metastore.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2018 Andrea Zagli + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include + +int +main (int argc, char **argv) +{ + GdaEx *gdaex; + + GdaDataModel *dm; + GdaDataModel *dmf; + + guint rows; + guint row; + + guint rowsf; + guint rowf; + + gtk_init (&argc, &argv); + + gdaex = gdaex_new_from_string (argv[1]); + if (gdaex == NULL) + { + g_error ("Unable to connect to the db: %s.", argv[1]); + } + + dm = gdaex_meta_store_get_tables (gdaex, TRUE); + if (dm != NULL) + { + rows = gda_data_model_get_n_rows (dm); + for (row = 0; row < rows; row++) + { + g_printf ("Table: %s\n" + "--------------------------------------------------------------\n", + gdaex_data_model_get_value_stringify_at (dm, row, 0)); + + dmf = gdaex_meta_store_get_table_fields (gdaex, gdaex_data_model_get_value_stringify_at (dm, row, 0), TRUE); + if (dmf != NULL) + { + rowsf = gda_data_model_get_n_rows (dmf); + for (rowf = 0; rowf < rowsf; rowf++) + { + g_printf ("\t- %s\n", + gdaex_data_model_get_value_stringify_at (dmf, rowf, 0)); + } + g_object_unref (dmf); + } + + g_printf ("\n"); + } + g_object_unref (dm); + } + + return 0; +} -- 2.49.0