]> saetta.ns0.it Git - libzakdbt/commitdiff
Added function ZakDbtUtils::db_to_xml (WIP). master
authorAndrea Zagli <azagli@libero.it>
Wed, 24 Jan 2018 16:51:32 +0000 (17:51 +0100)
committerAndrea Zagli <azagli@libero.it>
Wed, 24 Jan 2018 16:51:32 +0000 (17:51 +0100)
configure.ac
libzakdbt.pc.in
src/Makefile.am
src/libzakdbt.h
src/utils.c [new file with mode: 0644]
src/utils.h [new file with mode: 0644]
zdbt/zdbt.c

index d6047e02a8b307c5069e45fdfc0bc3b663183259..3f7bc88ba3c6f32fbbacb212490456be8e6eb9a9 100644 (file)
@@ -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)
index 89ba036592117f57006dd65f0875ffec3fa5079b..afefe66eb663235c75077f9efc05d8ca909edc08 100644 (file)
@@ -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}
index f880c963b55bea2c52217b3430b988f76dd1efb3..b835c7d83fa766a163297dc52023824113c9b754 100644 (file)
@@ -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)
 
index c9b1ef399442fee792a936c49877e579fe934868..41b5d1797c5badd2c4396f7d4f35277d77401d28 100644 (file)
@@ -24,6 +24,9 @@
 #include <libgda/libgda.h>
 
 
+#include "utils.h"
+
+
 G_BEGIN_DECLS
 
 
diff --git a/src/utils.c b/src/utils.c
new file mode 100644 (file)
index 0000000..4f72b69
--- /dev/null
@@ -0,0 +1,178 @@
+/*
+ * Copyright (C) 2018 Andrea Zagli <azagli@libero.it>
+ *
+ * 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 <config.h>
+#endif
+
+#include <string.h>
+
+#include <glib/gprintf.h>
+
+#include <libgdaex/libgdaex.h>
+
+#include <libxml/xpath.h>
+#include <libxml/xinclude.h>
+
+#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 (file)
index 0000000..fd93537
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2018 Andrea Zagli <azagli@libero.it>
+ *
+ * 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 <glib.h>
+#include <glib-object.h>
+#include <libgdaex/libgdaex.h>
+
+
+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__ */
index 7fc6cf32700ec35d3129a86f19402d87c891048d..5f42f5ce5ab4fad86a0b102fb8caa665b0e64af8 100644 (file)
 
 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;
 }