#include "libdbtransformer.h"
-gchar *str_xslt_doc =
+static gchar *str_xslt_doc =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<xsl:stylesheet version=\"1.0\"\n"
"xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n"
GValue *value,
GParamSpec *pspec);
+static gboolean dbt_check_db (Dbt *dbt, gboolean create_tables);
+
#define DBT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_DBT, DbtPrivate))
typedef struct _DbtPrivate DbtPrivate;
dbt_sort_xml (dbt);
}
+/**
+ * dbt_db_is_updated:
+ * @dbt:
+ *
+ */
+gboolean
+dbt_db_is_updated (Dbt *dbt)
+{
+ gboolean ret;
+
+ DbtPrivate *priv;
+
+ xmlXPathContextPtr xpcontext;
+ xmlXPathObjectPtr xpresult;
+ xmlNodeSetPtr xnodeset;
+
+ gchar *str_id;
+ guint id;
+
+ gchar *sql;
+ GdaStatement *stmt;
+ GdaDataModel *dm;
+ GError *error;
+
+ g_return_val_if_fail (IS_DBT (dbt), FALSE);
+
+ priv = DBT_GET_PRIVATE (dbt);
+
+ ret = FALSE;
+
+ if (!dbt_check_db (dbt, FALSE))
+ {
+ return ret;
+ }
+
+ xpcontext = xmlXPathNewContext (priv->xml_doc);
+ xpcontext->node = priv->xml_root;
+
+ xpresult = xmlXPathEvalExpression ((const xmlChar *)"child::dbtransformation[last()]", xpcontext);
+ if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval)
+ && xpresult->nodesetval->nodeNr == 1)
+ {
+ xnodeset = xpresult->nodesetval;
+ str_id = xmlGetProp (xnodeset->nodeTab[0], (const gchar *)"id");
+ if (str_id == NULL)
+ {
+ return ret;
+ }
+ id = strtol (str_id, NULL, 10);
+ if (id <= 0)
+ {
+ return ret;
+ }
+
+ if (!GDA_IS_SQL_PARSER (priv->gda_parser))
+ {
+ g_warning ("Invalid sql parser.");
+ return ret;
+ }
+
+ sql = g_strdup_printf ("SELECT id FROM dbt_transformations"
+ " WHERE id = %d",
+ id);
+ error = NULL;
+ stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error);
+ if (stmt == NULL)
+ {
+ g_warning ("Unable to create GdaStatement from sql: %s\n%s.",
+ sql, error && error->message ? error->message : "No detail");
+ return ret;
+ }
+ else
+ {
+ dm = gda_connection_statement_execute_select (priv->gda_conn, stmt, NULL, &error);
+ g_object_unref (stmt);
+ if (!dm || gda_data_model_get_n_rows (dm) == 0)
+ {
+ return ret;
+ }
+ }
+ }
+ else
+ {
+ g_warning ("No last transformation found.");
+ return ret;
+ }
+
+ ret = TRUE;
+
+ return ret;
+}
+
static gboolean
-dbt_check_db (Dbt *dbt)
+dbt_check_db (Dbt *dbt, gboolean create_tables)
{
DbtPrivate *priv;
stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error);
if (stmt == NULL)
{
- g_warning ("Unable to create GdaStatement from sql: %s\n%s",
+ g_warning ("Unable to create GdaStatement from sql: %s\n%s.",
sql, error && error->message ? error->message : "No detail");
}
else
g_object_unref (stmt);
if (!dm)
{
+ if (!create_tables)
+ {
+ return ret;
+ }
+
/* table doesn't exist */
GdaServerOperation *op;
gint i;
if (!op)
{
g_warning ("CREATE TABLE operation is not supported by the provider: %s.",
- error != NULL && error->message != NULL ? error->message : "no detail");
+ error != NULL && error->message != NULL ? error->message : "no detail");
return ret;
}
if (!gda_server_provider_perform_operation (priv->gda_provider, priv->gda_conn, op, &error))
{
g_warning ("Error executing the operation: %s.",
- error != NULL && error->message != NULL ? error->message : "No detail");
+ error != NULL && error->message != NULL ? error->message : "no detail");
return ret;
}
g_object_unref (op);
g_return_if_fail (priv->gda_conn != NULL);
g_return_if_fail (priv->xml_root != NULL);
- if (!dbt_check_db (dbt))
+ if (!dbt_check_db (dbt, TRUE))
{
return;
}
xpcontext->node = priv->xml_root;
xpresult = xmlXPathEvalExpression ((const xmlChar *)"child::dbtransformation", xpcontext);
- if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval) || xpresult->nodesetval->nodeNr < 1)
+ if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval)
+ || xpresult->nodesetval->nodeNr < 1)
{
xnodeset = xpresult->nodesetval;
{
tosave = TRUE;
- str_id= xmlGetProp (xnodeset->nodeTab[i], (const gchar *)"id");
+ str_id = xmlGetProp (xnodeset->nodeTab[i], (const gchar *)"id");
if (str_id == NULL)
{
g_warning ("Operation id cannot be minor equal to zero.");
continue;
}
id = strtol (str_id, NULL, 10);
+ if (id <= 0)
+ {
+ g_warning ("Operation id cannot be minor equal to zero.");
+ continue;
+ }
if (dbt_check_to_execute (dbt, id))
{