From 95df60166b3b2d1d0aecd07ffca95cf2fc748797 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 30 Jun 2010 18:57:02 +0200 Subject: [PATCH] Bugfixes and first test. --- src/dbtransformer.c | 146 +++++++++++++++++++++++++++++++---------- src/libdbtransformer.h | 2 +- tests/test.c | 9 +++ tests/test.db | Bin 0 -> 3072 bytes tests/test.xml | 19 ++++++ 5 files changed, 142 insertions(+), 34 deletions(-) create mode 100644 tests/test.db create mode 100644 tests/test.xml diff --git a/src/dbtransformer.c b/src/dbtransformer.c index c04b0ce..d6c8a22 100644 --- a/src/dbtransformer.c +++ b/src/dbtransformer.c @@ -20,6 +20,7 @@ #include #endif +#include #include #include "libdbtransformer.h" @@ -42,6 +43,8 @@ typedef struct _DbtPrivate DbtPrivate; struct _DbtPrivate { GdaConnection *gda_conn; + + xmlDocPtr xml_doc; xmlNodePtr xml_root; }; @@ -64,6 +67,7 @@ dbt_init (Dbt *dbt) DbtPrivate *priv = DBT_GET_PRIVATE (dbt); priv->gda_conn = NULL; + priv->xml_doc = NULL; priv->xml_root = NULL; } @@ -95,6 +99,8 @@ dbt_get_connection_parser (Dbt *dbt) /* attach the parser object to the connection */ g_object_set_data_full (G_OBJECT (priv->gda_conn), "parser", parser, g_object_unref); + + return TRUE; } void @@ -181,6 +187,12 @@ dbt_set_xml (Dbt *dbt, xmlNodePtr xml_root) { priv->xml_root = NULL; } + else + { + /* creation of an xmlDoc */ + priv->xml_doc = xmlNewDoc ("1.0"); + xmlDocSetRootElement (priv->xml_doc, priv->xml_root); + } } void @@ -195,7 +207,7 @@ dbt_set_xml_from_filename (Dbt *dbt, const gchar *filename) g_return_if_fail (IS_DBT (dbt)); g_return_if_fail (filename != NULL); - filen = g_strstrip (g_strdup (filen)); + filen = g_strstrip (g_strdup (filename)); g_return_if_fail (g_strcmp0 (filen, "") != 0); @@ -209,7 +221,10 @@ dbt_set_xml_from_filename (Dbt *dbt, const gchar *filename) return; } - priv->xml_root = xmlDocGetRootElement (xdoc); + priv = DBT_GET_PRIVATE (dbt); + + priv->xml_doc = xdoc; + priv->xml_root = xmlDocGetRootElement (priv->xml_doc); if (priv->xml_root == NULL) { g_warning ("No root element in xml file."); @@ -229,6 +244,7 @@ dbt_check_db (Dbt *dbt) GdaSqlParser *parser; GdaStatement *stmt; + GdaDataModel *dm; GError *error; gchar *sql; @@ -252,39 +268,54 @@ dbt_check_db (Dbt *dbt) stmt = gda_sql_parser_parse_string (parser, sql, NULL, &error); if (stmt == NULL) { - /* table doesn't exist */ - GdaServerProvider *provider; - GdaServerOperation *op; - gint i; - - /* create a new GdaServerOperation object */ - provider = gda_connection_get_provider (priv->gda_conn); - op = gda_server_provider_create_operation (provider, priv->gda_conn, GDA_SERVER_OPERATION_CREATE_TABLE, NULL, &error); - if (!op) + g_warning ("Unable to create GdaStatement from sql: %s\n%s", + sql, error && error->message ? error->message : "No detail"); + } + else + { + dm = gda_connection_statement_execute_select (priv->gda_conn, stmt, NULL, &error); + g_object_unref (stmt); + if (!dm) { - g_print ("CREATE TABLE operation is not supported by the provider: %s\n", - error && error->message ? error->message : "No detail"); - return ret; - } - - /* Set parameter's values */ - /* table name */ - gda_server_operation_set_value_at (op, "dbt_transformations", &error, "/TABLE_DEF_P/TABLE_NAME"); + /* table doesn't exist */ + GdaServerProvider *provider; + GdaServerOperation *op; + gint i; + + /* create a new GdaServerOperation object */ + provider = gda_connection_get_provider (priv->gda_conn); + op = gda_server_provider_create_operation (provider, priv->gda_conn, GDA_SERVER_OPERATION_CREATE_TABLE, NULL, &error); + if (!op) + { + g_print ("CREATE TABLE operation is not supported by the provider: %s\n", + error && error->message ? error->message : "No detail"); + return ret; + } + + /* Set parameter's values */ + /* table name */ + gda_server_operation_set_value_at (op, "dbt_transformations", &error, "/TABLE_DEF_P/TABLE_NAME"); - /* "id' field */ - i = 0; - gda_server_operation_set_value_at (op, "id", &error, "/FIELDS_A/@COLUMN_NAME/%d", i); - gda_server_operation_set_value_at (op, "integer", &error, "/FIELDS_A/@COLUMN_TYPE/%d", i); - gda_server_operation_set_value_at (op, "TRUE", &error, "/FIELDS_A/@COLUMN_PKEY/%d", i); - - /* Actually execute the operation */ - if (!gda_server_provider_perform_operation (provider, priv->gda_conn, op, &error)) - { - g_print ("Error executing the operation: %s\n", - error && error->message ? error->message : "No detail"); - return ret; + /* "id" field */ + i = 0; + gda_server_operation_set_value_at (op, "id", &error, "/FIELDS_A/@COLUMN_NAME/%d", i); + gda_server_operation_set_value_at (op, "integer", &error, "/FIELDS_A/@COLUMN_TYPE/%d", i); + gda_server_operation_set_value_at (op, "TRUE", &error, "/FIELDS_A/@COLUMN_PKEY/%d", i); + + /* "operation" field */ + i++; + gda_server_operation_set_value_at (op, "operation", &error, "/FIELDS_A/@COLUMN_NAME/%d", i); + gda_server_operation_set_value_at (op, "text", &error, "/FIELDS_A/@COLUMN_TYPE/%d", i); + + /* execute the operation */ + if (!gda_server_provider_perform_operation (provider, priv->gda_conn, op, &error)) + { + g_print ("Error executing the operation: %s\n", + error && error->message ? error->message : "No detail"); + return ret; + } + g_object_unref (op); } - g_object_unref (op); } ret = TRUE; @@ -293,9 +324,15 @@ dbt_check_db (Dbt *dbt) } void -dbt_exec (Dbt *dbt) +dbt_transform (Dbt *dbt) { DbtPrivate *priv; + xmlXPathContextPtr xpcontext; + xmlXPathObjectPtr xpresult; + xmlNodeSetPtr xnodeset; + xmlNodePtr xnode; + + guint i; g_return_if_fail (IS_DBT (dbt)); @@ -308,6 +345,49 @@ dbt_exec (Dbt *dbt) { return; } + + xpcontext = xmlXPathNewContext (priv->xml_doc); + xpcontext->node = priv->xml_root; + + xpresult = xmlXPathEvalExpression ((const xmlChar *)"child::dbtransformation", xpcontext); + if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval)) + { + GdaSqlParser *parser = g_object_get_data (G_OBJECT (priv->gda_conn), "parser"); + if (!parser) + { + g_warning ("Invalid sql parser."); + return; + } + + xnodeset = xpresult->nodesetval; + for (i = 0; i < xnodeset->nodeNr; i++) + { + xnode = xnodeset->nodeTab[i]; + if (xmlStrcmp (xnode->name, (const xmlChar *)"sql") == 0) + { + 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 ("Unable to create GdaStatement from sql: %s\n%s", + sql, 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", error && error->message ? error->message : "no detail"); + } + g_object_unref (stmt); + } + } + } + } } /* PRIVATE */ diff --git a/src/libdbtransformer.h b/src/libdbtransformer.h index 4bf4f8c..a0993aa 100644 --- a/src/libdbtransformer.h +++ b/src/libdbtransformer.h @@ -59,7 +59,7 @@ void dbt_set_db_connection_from_string (Dbt *dbt, const gchar *cnc_string); void dbt_set_xml (Dbt *dbt, xmlNodePtr xml_root); void dbt_set_xml_from_filename (Dbt *dbt, const gchar *filename); -void dbt_exec (Dbt *dbt); +void dbt_transform (Dbt *dbt); G_END_DECLS diff --git a/tests/test.c b/tests/test.c index 43c0f24..858288e 100644 --- a/tests/test.c +++ b/tests/test.c @@ -18,10 +18,19 @@ #include "libdbtransformer.h" +Dbt *dbt; + int main (int argc, char **argv) { g_type_init (); + dbt = dbt_new (); + + dbt_set_db_connection_from_string (dbt, "SQLite://DB_DIR=tests;DB_NAME=test.db"); + dbt_set_xml_from_filename (dbt, "tests/test.xml"); + + dbt_transform (dbt); + return 0; } diff --git a/tests/test.db b/tests/test.db new file mode 100644 index 0000000000000000000000000000000000000000..3d0c0b13d79ef271d325309b707a447038e76eeb GIT binary patch literal 3072 zcmWFz^vNtqRY=P(%1ta$FlJz3U}R))P*7lCU|<4bWB}qbA_*X~k%Tdsyg(r)W+w*b z13+RFjE2Cl3V|9Xc5!24#zy&)#H5_ml%$gQlA^@CVn~k4%+D*vBE;+*P^5ylYov}senDzcVhPY(g_6{Y5=}csHgQ>5 z#z3&;B^jw{nMK7VFq#R)8ki`u*_xod3C#4PU^E0qLtq4kz-ay-fssDy< + + + + + CREATE TABLE clients (id integer, name varchar(50), CONSTRAINT PRIMARY KEY clients_pkey (id)) + + + CREATE TABLE orders (id integer, id_client integer, "date" date, CONSTRAINT PRIMARY KEY orders_pkey (id)) + + + + + + ALTER TABLE order ADD COLUMN (total double) + + + + -- 2.49.0