From: Andrea Zagli Date: Wed, 30 Jun 2010 17:52:37 +0000 (+0200) Subject: Bugfixes and implemented operation "sql". X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=fc5eef986df1f2037d6fd78a39914804931563ac;p=libzakdbt Bugfixes and implemented operation "sql". --- diff --git a/src/dbtransformer.c b/src/dbtransformer.c index d6c8a22..19231a6 100644 --- a/src/dbtransformer.c +++ b/src/dbtransformer.c @@ -323,6 +323,56 @@ dbt_check_db (Dbt *dbt) return ret; } +static gboolean +dbt_save_transformation (Dbt *dbt, guint id, const gchar *operation) +{ + DbtPrivate *priv; + + gboolean ret; + + gchar *sql; + GError *error; + GdaStatement *stmt; + + priv = DBT_GET_PRIVATE (dbt); + + ret = FALSE; + + GdaSqlParser *parser = g_object_get_data (G_OBJECT (priv->gda_conn), "parser"); + if (!parser) + { + g_warning ("Invalid sql parser."); + return ret; + } + + sql = g_strdup_printf ("INSERT INTO dbt_transformations" + " (id, operation)" + " VALUES (%d, '%s')", + id, operation); + stmt = gda_sql_parser_parse_string (parser, sql, NULL, &error); + if (!stmt) + { + g_warning ("Unable to save operation: %s\n", + error && error->message ? error->message : "No detail"); + } + else + { + guint nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error); + if (nrows == -1) + { + g_warning ("NON SELECT error: %s\n%s\n", sql, + error && error->message ? error->message : "No detail"); + } + else + { + ret = TRUE; + } + g_object_unref (stmt); + } + + return ret; +} + void dbt_transform (Dbt *dbt) { @@ -332,6 +382,9 @@ dbt_transform (Dbt *dbt) xmlNodeSetPtr xnodeset; xmlNodePtr xnode; + guint id; + gboolean tosave; + guint i; g_return_if_fail (IS_DBT (dbt)); @@ -360,34 +413,68 @@ dbt_transform (Dbt *dbt) } xnodeset = xpresult->nodesetval; + + if (xnodeset->nodeNr < 1) + { + g_warning ("No transformations found."); + } + for (i = 0; i < xnodeset->nodeNr; i++) { - xnode = xnodeset->nodeTab[i]; - if (xmlStrcmp (xnode->name, (const xmlChar *)"sql") == 0) - { - gchar *sql = (gchar *)xmlNodeGetContent (xnode); + tosave = TRUE; - GdaStatement *stmt; - GError *error = NULL; + id = strtol (xmlGetProp (xnodeset->nodeTab[i], (const gchar *)"id"), NULL, 10); - stmt = gda_sql_parser_parse_string (parser, g_strstrip (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"); - } - else + xnode = xnodeset->nodeTab[i]->children; + while (xnode != NULL) + { + if (!xmlNodeIsText (xnode) + && xmlStrcmp (xnode->name, (const xmlChar *)"sql") == 0) { - guint nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error); - if (nrows == -1) + gchar *sql = (gchar *)xmlNodeGetContent (xnode); + + GdaStatement *stmt; + GError *error = NULL; + + stmt = gda_sql_parser_parse_string (parser, g_strstrip (sql), NULL, &error); + if (stmt == NULL) { - g_warning ("NON SELECT error: %s\n", error && error->message ? error->message : "no detail"); + g_warning ("Unable to create GdaStatement from sql: %s\n%s", + sql, error && error->message ? error->message : "No detail"); } - g_object_unref (stmt); + else + { + guint nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error); + if (nrows == -1) + { + g_warning ("NON SELECT error: %s\n%s\n", sql, + error && error->message ? error->message : "No detail"); + } + g_object_unref (stmt); + } + } + else if (!xmlNodeIsText (xnode)) + { + g_warning ("Invalid tag: %s", xnode->name); } + + xnode = xnode->next; + } + + if (tosave) + { + /*gchar *operation; + xmlBufferPtr xbuf = xmlBufferCreate (); + xmlNodeDump (xbuf, priv->xml_doc, xnodeset->nodeTab[i], 2, 1); + operation = xml + dbt_save_transformation (dbt, id, g_strstrip (operation));*/ } } } + else + { + g_warning ("No transformation found."); + } } /* PRIVATE */ diff --git a/tests/test.db b/tests/test.db index 3d0c0b1..761e02c 100644 Binary files a/tests/test.db and b/tests/test.db differ diff --git a/tests/test.xml b/tests/test.xml index b28cd9a..7411538 100644 --- a/tests/test.xml +++ b/tests/test.xml @@ -3,16 +3,16 @@ - CREATE TABLE clients (id integer, name varchar(50), CONSTRAINT PRIMARY KEY clients_pkey (id)) + CREATE TABLE clients (id integer, name varchar(50), CONSTRAINT clients_pkey PRIMARY KEY (id)) - CREATE TABLE orders (id integer, id_client integer, "date" date, CONSTRAINT PRIMARY KEY orders_pkey (id)) + CREATE TABLE orders (id integer, id_client integer, "date" date, CONSTRAINT orders_pkey PRIMARY KEY (id)) - ALTER TABLE order ADD COLUMN (total double) + ALTER TABLE orders ADD COLUMN total double