From 403ca8632f6d99b72583585e51a455e0039ceccd Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 4 Aug 2019 09:30:26 +0200 Subject: [PATCH] Added tool to export tables to csv. --- .gitignore | 5 +- Makefile.am | 2 +- configure.ac | 1 + tools/Makefile.am | 13 +++++ tools/gdaex_exp_csv.c | 119 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 tools/Makefile.am create mode 100644 tools/gdaex_exp_csv.c diff --git a/.gitignore b/.gitignore index 830881c..4bf3eba 100644 --- a/.gitignore +++ b/.gitignore @@ -56,4 +56,7 @@ tests/sqlbuilder tests/metastore *~ *.gir -*.typelib \ No newline at end of file +*.typelib +tools/*.exe +tools/gdaex_exp_csv +*.csv \ No newline at end of file diff --git a/Makefile.am b/Makefile.am index 462f209..03bbee7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ DISTCHECK_CONFIGURE_FLAGS = --enable-gtk-doc --enable-introspection -SUBDIRS = data po src gdaex2gettext docs tests +SUBDIRS = data po src gdaex2gettext docs tests tools ACLOCAL_AMFLAGS = -I m4 diff --git a/configure.ac b/configure.ac index cbaf3a2..3bed8c3 100644 --- a/configure.ac +++ b/configure.ac @@ -95,6 +95,7 @@ AC_CONFIG_FILES([ docs/reference/version.xml gdaex2gettext/Makefile tests/Makefile + tools/Makefile po/Makefile.in ]) AC_OUTPUT diff --git a/tools/Makefile.am b/tools/Makefile.am new file mode 100644 index 0000000..5732459 --- /dev/null +++ b/tools/Makefile.am @@ -0,0 +1,13 @@ +LIBS = $(GDAEX_LIBS) \ + -export-dynamic + +AM_CPPFLAGS = $(GDAEX_CFLAGS) \ + -I$(top_srcdir)/src \ + -DTESTSDIR="\"@abs_builddir@\"" + +noinst_PROGRAMS = \ + gdaex_exp_csv + +LDADD = $(top_builddir)/src/libgdaex.la + +EXTRA_DIST = diff --git a/tools/gdaex_exp_csv.c b/tools/gdaex_exp_csv.c new file mode 100644 index 0000000..132bebb --- /dev/null +++ b/tools/gdaex_exp_csv.c @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2019 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. + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include + +static gchar *cnc_string = NULL; +static gchar **tables = NULL; + +static GOptionEntry entries[] = +{ + { "cnc_string", 'c', 0, G_OPTION_ARG_STRING, &cnc_string, "Connection string", NULL}, + { "table", 't', 0, G_OPTION_ARG_STRING_ARRAY, &tables, "Table (can be used more times)", NULL }, + { NULL } +}; + + +int +main (int argc, char **argv) +{ + GdaEx *gdaex; + + GError *error; + + GOptionContext *context; + + guint l; + guint i; + + GdaSet *gdaset; + + gda_init (); + + error = NULL; + context = g_option_context_new ("- export table(s) to csv"); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + g_error ("Error command line parsing: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + return 0; + } + + if (cnc_string == NULL) + { + g_error ("No connection string supplied."); + return 0; + } + + gdaex = gdaex_new_from_string (cnc_string); + if (gdaex == NULL) + { + g_error ("Unable to connect to database: %s", cnc_string); + } + + l = g_strv_length (tables); + if (l < 1) + { + g_error ("No table(s) to export supplied."); + return 0; + } + + gdaset = gda_set_new_inline (5, + "NAMES_ON_FIRST_LINE", G_TYPE_BOOLEAN, TRUE, + "SEPARATOR", G_TYPE_STRING, ";", + "QUOTE", G_TYPE_STRING, "", + "OVERWRITE", G_TYPE_BOOLEAN, TRUE, + "NULL_AS_EMPTY", G_TYPE_BOOLEAN, TRUE); + + for (i = 0; i < l; i++) + { + gchar *sql; + GdaDataModel *dm; + + sql = g_strdup_printf ("SELECT * FROM %s", + tables[i]); + dm = gdaex_query (gdaex, sql); + g_free (sql); + if (!gdaex_data_model_is_empty (dm)) + { + error = NULL; + gda_data_model_export_to_file (dm, + GDA_DATA_MODEL_IO_TEXT_SEPARATED, + g_strdup_printf ("%s.csv", tables[i]), + NULL, + 0, + NULL, + 0, + gdaset, + &error); + } + + if (dm != NULL) + { + g_object_unref (dm); + } + } + + return 0; +} -- 2.49.0