From 53bc93a4ca8dfc8768c95425b16fc9c85378a19d Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 24 Jan 2018 17:51:32 +0100 Subject: [PATCH] Added function ZakDbtUtils::db_to_xml (WIP). --- configure.ac | 3 +- libzakdbt.pc.in | 4 +- src/Makefile.am | 6 +- src/libzakdbt.h | 3 + src/utils.c | 178 ++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 37 ++++++++++ zdbt/zdbt.c | 31 ++++++--- 7 files changed, 246 insertions(+), 16 deletions(-) create mode 100644 src/utils.c create mode 100644 src/utils.h diff --git a/configure.ac b/configure.ac index d6047e0..3f7bc88 100644 --- a/configure.ac +++ b/configure.ac @@ -35,7 +35,8 @@ GTK_DOC_CHECK # Checks for libraries. PKG_CHECK_MODULES(DBT, [libgda-5.0 - libxslt >= 1.0.0]) + libxslt >= 1.0.0 + libgdaex >= 0.7.0]) AC_SUBST(DBT_CFLAGS) AC_SUBST(DBT_LIBS) diff --git a/libzakdbt.pc.in b/libzakdbt.pc.in index 89ba036..afefe66 100644 --- a/libzakdbt.pc.in +++ b/libzakdbt.pc.in @@ -4,8 +4,8 @@ libdir=@libdir@ includedir=@includedir@ Name: @PACKAGE_NAME@ -Description: Class to manage db changes. +Description: Class to manage/track db schema changes. Version: @PACKAGE_VERSION@ -Requires: libgda-5.0 libxslt +Requires: libgda-5.0 libxslt libgdaex Libs: -L${libdir} -lzakdbt Cflags: -I${includedir} diff --git a/src/Makefile.am b/src/Makefile.am index f880c96..b835c7d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,11 +4,13 @@ AM_CPPFLAGS = $(DBT_CFLAGS) lib_LTLIBRARIES = libzakdbt.la -libzakdbt_la_SOURCES = dbt.c +libzakdbt_la_SOURCES = dbt.c \ + utils.c libzakdbt_la_LDFLAGS = -no-undefined -libzakdbt_include_HEADERS = libzakdbt.h +libzakdbt_include_HEADERS = libzakdbt.h \ + utils.h libzakdbt_includedir = $(includedir)/$(PACKAGE) diff --git a/src/libzakdbt.h b/src/libzakdbt.h index c9b1ef3..41b5d17 100644 --- a/src/libzakdbt.h +++ b/src/libzakdbt.h @@ -24,6 +24,9 @@ #include +#include "utils.h" + + G_BEGIN_DECLS diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..4f72b69 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2018 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#include + +#include + +#include +#include + +#include "libzakdbt.h" +#include "utils.h" + +static xmlNode +*zak_dbt_utils_db_to_xml_table_add_column (GdaEx *gdaex, GdaDataModel *fields, guint row) +{ + xmlNode *xnode_ret; + xmlNode *xnode; + + xnode_ret = xmlNewNode (NULL, (xmlChar *)"add_column"); + + xnode = xmlNewNode (NULL, (xmlChar *)"column_name"); + xmlNodeSetContent (xnode, (xmlChar *)gdaex_data_model_get_value_stringify_at (fields, row, 0)); + xmlAddChild (xnode_ret, xnode); + + xnode = xmlNewNode (NULL, (xmlChar *)"column_type"); + xmlNodeSetContent (xnode, (xmlChar *)gdaex_data_model_get_value_stringify_at (fields, row, 1)); + xmlAddChild (xnode_ret, xnode); + + xnode = xmlNewNode (NULL, (xmlChar *)"column_size"); + xmlNodeSetContent (xnode, (xmlChar *)g_strdup_printf ("%d", gdaex_data_model_get_value_integer_at (fields, row, 3))); + xmlAddChild (xnode_ret, xnode); + + return xnode_ret; +} + +static xmlNode +*zak_dbt_utils_db_to_xml_create_table (GdaEx *gdaex, const gchar *table_name) +{ + xmlNode *xnode_create; + xmlNode *xnode; + + GdaDataModel *fields; + + guint row; + guint rows; + + xnode_create = xmlNewNode (NULL, (xmlChar *)"create_table"); + + xnode = xmlNewNode (NULL, (xmlChar *)"table_name"); + xmlNodeSetContent (xnode, (xmlChar *)table_name); + xmlAddChild (xnode_create, xnode); + + fields = gdaex_meta_store_get_table_fields (gdaex, table_name, TRUE); + if (!gdaex_data_model_is_empty (fields)) + { + rows = gda_data_model_get_n_rows (fields); + for (row = 0; row < rows; row++) + { + xnode = zak_dbt_utils_db_to_xml_table_add_column (gdaex, + fields, + row); + xmlAddChild (xnode_create, xnode); + } + } + if (fields != NULL) + { + g_object_unref (fields); + } + + return xnode_create; +} + +void +zak_dbt_utils_db_to_xml (GdaEx *gdaex, const gchar *filename) +{ + xmlDoc *xdoc; + xmlNode *xnode_root; + xmlNode *xnode_dbt; + + GdaDataModel *tables; + + guint row; + guint rows; + + xmlNode *xnode_table; + + xmlChar *buf; + int buf_size; + + xdoc = xmlNewDoc ((xmlChar *)"1.0"); + xnode_root = xmlNewNode (NULL, (xmlChar *)"dbtransformer"); + xmlDocSetRootElement (xdoc, xnode_root); + + tables = gdaex_meta_store_get_tables (gdaex, TRUE); + if (!gdaex_data_model_is_empty (tables)) + { + rows = gda_data_model_get_n_rows (tables); + for (row = 0; row < rows; row++) + { + xnode_dbt = xmlNewNode (NULL, (xmlChar *)"dbtransformation"); + xmlNewProp (xnode_dbt, (xmlChar *)"id", (xmlChar *)g_strdup_printf ("%d", row)); + xmlAddChild (xnode_root, xnode_dbt); + + xnode_table = zak_dbt_utils_db_to_xml_create_table (gdaex, + gdaex_data_model_get_value_stringify_at (tables, row, 0)); + xmlAddChild (xnode_dbt, xnode_table); + } + } + if (tables != NULL) + { + g_object_unref (tables); + } + + buf = NULL; + xmlDocDumpFormatMemory (xdoc, + &buf, + &buf_size, + 2); + + if (filename == NULL) + { + g_printf ("%s", buf); + } + else + { + GFile *gfile; + GError *error; + + gfile = g_file_new_for_path (filename); + + error = NULL; + g_file_replace_contents (gfile, + (const char *)buf, + buf_size, + NULL, + FALSE, + G_FILE_CREATE_NONE, + NULL, + NULL, + &error); + + g_object_unref (gfile); + } +} + +void +zak_dbt_utils_db_to_xml_from_cnc_string (const gchar *cnc_string, const gchar *filename) +{ + GdaEx *gdaex; + + gdaex = gdaex_new_from_string (cnc_string); + + zak_dbt_utils_db_to_xml (gdaex, filename); + + gdaex_free (gdaex); +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..fd93537 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2018 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __LIBZAKDBT_UTILS_H__ +#define __LIBZAKDBT_UTILS_H__ + +#include +#include +#include + + +G_BEGIN_DECLS + + +void zak_dbt_utils_db_to_xml (GdaEx *gdaex, const gchar *filename); +void zak_dbt_utils_db_to_xml_from_cnc_string (const gchar *cnc_string, const gchar *filename); + + +G_END_DECLS + + +#endif /* __LIBZAKDBT_UTILS_H__ */ diff --git a/zdbt/zdbt.c b/zdbt/zdbt.c index 7fc6cf3..5f42f5c 100644 --- a/zdbt/zdbt.c +++ b/zdbt/zdbt.c @@ -20,11 +20,13 @@ static gchar *cnc_string; static gchar *xml_filename; +static gboolean db_to_xml = FALSE; static GOptionEntry entries[] = { { "cnc_string", 'c', 0, G_OPTION_ARG_STRING, &cnc_string, "Connection string to database", NULL }, { "xml_filename", 'x', 0, G_OPTION_ARG_FILENAME, &xml_filename, "ZakDbt xml file name", NULL }, + { "db_to_xml", 'd', 0, G_OPTION_ARG_NONE, &db_to_xml, "Ouput the db schema as ZakDbt xml file", NULL }, { NULL } }; @@ -60,22 +62,29 @@ main (int argc, char **argv) return 0; } - dbt = zak_dbt_dbt_new (); - - zak_dbt_dbt_set_db_connection_from_string (dbt, cnc_string); - zak_dbt_dbt_set_xml_from_filename (dbt, xml_filename); - - if (zak_dbt_dbt_db_is_updated (dbt)) + if (db_to_xml) { - g_message ("Database is updated."); - return 0; + zak_dbt_utils_db_to_xml_from_cnc_string (cnc_string, xml_filename); } else { - g_message ("Database is not updated."); - } + dbt = zak_dbt_dbt_new (); - zak_dbt_dbt_transform (dbt); + zak_dbt_dbt_set_db_connection_from_string (dbt, cnc_string); + zak_dbt_dbt_set_xml_from_filename (dbt, xml_filename); + + if (zak_dbt_dbt_db_is_updated (dbt)) + { + g_message ("Database is updated."); + return 0; + } + else + { + g_message ("Database is not updated."); + } + + zak_dbt_dbt_transform (dbt); + } return 0; } -- 2.49.0