--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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__ */
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 }
};
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;
}