]> saetta.ns0.it Git - libzakdbt/commitdiff
Depends on libgda 4.1.8.
authorAndrea Zagli <azagli@libero.it>
Fri, 30 Jul 2010 17:17:20 +0000 (19:17 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 30 Jul 2010 17:17:20 +0000 (19:17 +0200)
Some adjustments.
Added and managed drop_table tag.
Some gtk-doc.
Managed includes on xml file.

configure.ac
docs/dbtransformer.dtd
src/dbtransformer.c
tests/test.db
tests/test.xml

index 5507bcb59b6cd231f8548bd0183680c191cbf965..28660691c41813c293ebe64d07ddaa2df91ed954 100644 (file)
@@ -29,7 +29,7 @@ AC_PROG_RANLIB
 GTK_DOC_CHECK
 
 # Checks for libraries.
-PKG_CHECK_MODULES(DBT, [libgda-4.0 >= 4.0.0])
+PKG_CHECK_MODULES(DBT, [libgda-4.0 >= 4.1.8])
 
 AC_SUBST(DBT_CFLAGS)
 AC_SUBST(DBT_LIBS)
index eef1a95004c03efd569776f1a56c0cc9e55994be..b040d0802694537bb14afe045f0dad52e3392b06 100644 (file)
@@ -1,6 +1,6 @@
 <!ELEMENT dbtransformer (transformation+)>
 
-<!ELEMENT transformation (comment?, gda_op*, sql*)>
+<!ELEMENT transformation (comment?, gda_op*, sql*, drop_table*)>
 
 <!ATTLIST transformation
        id CDATA #REQUIRED
@@ -9,3 +9,7 @@
 <!ELEMENT comment (#PCDATA)>
 
 <!ELEMENT sql (#PCDATA)>
+
+<!ELEMENT drop_table (table_name)>
+
+<!ELEMENT table_name (#PCDATA) #REQUIRED>
index 94f758cdc680438ac43af1ece208e7dfaa3dd015..ad40381b1fc31065be749baf5a1db46371e84e3a 100644 (file)
@@ -21,6 +21,7 @@
 #endif
 
 #include <libxml/xpath.h>
+#include <libxml/xinclude.h>
 #include <sql-parser/gda-sql-parser.h>
 
 #include "libdbtransformer.h"
@@ -102,6 +103,12 @@ dbt_get_connection_parser (Dbt *dbt)
        return TRUE;
 }
 
+/**
+ * dbt_set_db_connection:
+ * @dbt:
+ * @gda_conn:
+ *
+ */
 void
 dbt_set_db_connection (Dbt *dbt, GdaConnection *gda_conn)
 {
@@ -117,10 +124,19 @@ dbt_set_db_connection (Dbt *dbt, GdaConnection *gda_conn)
        if (!dbt_get_connection_parser (dbt))
                {
                        priv->gda_conn= NULL;
+
+                       g_warning ("Unable to obtain the GdaSqlParser from the connection.");
+
                        return;
                }
 }
 
+/**
+ * dbt_set_db_connection_from_string:
+ * @dbt:
+ * @cnc_string:
+ *
+ */
 void
 dbt_set_db_connection_from_string (Dbt *dbt, const gchar *cnc_string)
 {
@@ -145,8 +161,8 @@ dbt_set_db_connection_from_string (Dbt *dbt, const gchar *cnc_string)
                                               &error);
        if (!priv->gda_conn)
                {
-                       g_warning ("Could not open connection: %s\n",
-                                  error && error->message ? error->message : "No detail");
+                       g_warning ("Could not open connection: %s.\n",
+                                  error != NULL && error->message != NULL ? error->message : "no detail");
                        return;
                }
 
@@ -171,6 +187,12 @@ dbt_check_xml_root (xmlNodePtr xml_root)
                }
 }
 
+/**
+ * dbt_set_xml:
+ * @dbt:
+ * @xml_root:
+ *
+ */
 void
 dbt_set_xml (Dbt *dbt, xmlNodePtr xml_root)
 {
@@ -191,9 +213,20 @@ dbt_set_xml (Dbt *dbt, xmlNodePtr xml_root)
                        /* creation of an xmlDoc */
                        priv->xml_doc = xmlNewDoc ("1.0");
                        xmlDocSetRootElement (priv->xml_doc, priv->xml_root);
+
+                       /* xmlinclude */
+                       if (xmlXIncludeProcess (priv->xml_doc) < 0)
+                               {
+                                       g_warning ("Unable to include xml external files.");
+                               }
                }
 }
-
+/**
+ * dbt_set_xml_from_filename:
+ * @dbt:
+ * @filename:
+ *
+ */
 void
 dbt_set_xml_from_filename (Dbt *dbt, const gchar *filename)
 {
@@ -211,7 +244,6 @@ dbt_set_xml_from_filename (Dbt *dbt, const gchar *filename)
        g_return_if_fail (g_strcmp0 (filen, "") != 0);  
 
        /* TODO validate against DTD */
-       /* TODO allow include */
 
        xdoc = xmlParseFile (filen);
        if (xdoc == NULL)
@@ -223,6 +255,13 @@ dbt_set_xml_from_filename (Dbt *dbt, const gchar *filename)
        priv = DBT_GET_PRIVATE (dbt);
 
        priv->xml_doc = xdoc;
+
+       /* xmlinclude */
+       if (xmlXIncludeProcess (priv->xml_doc) < 0)
+               {
+                       g_warning ("Unable to include xml external files.");
+               }
+
        priv->xml_root = xmlDocGetRootElement (priv->xml_doc);
        if (priv->xml_root == NULL)
                {
@@ -284,8 +323,8 @@ dbt_check_db (Dbt *dbt)
                                        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");
+                                                       g_warning ("CREATE TABLE operation is not supported by the provider: %s.",
+                                                                        error != NULL && error->message != NULL ? error->message : "no detail");
                                                        return ret;
                                                }
 
@@ -314,8 +353,8 @@ dbt_check_db (Dbt *dbt)
                                        /* 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");
+                                                       g_warning ("Error executing the operation: %s.",
+                                                                        error != NULL && error->message != NULL ? error->message : "No detail");
                                                        return ret;
                                                }
                                        g_object_unref (op);                    
@@ -354,8 +393,8 @@ dbt_check_to_execute (Dbt *dbt, guint id)
        stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error);
        if (!stmt)
                {
-                       g_warning ("Unable to check operation existance: %s\n",
-                                  error && error->message ? error->message : "No detail");
+                       g_warning ("Unable to check operation existance: %s.",
+                                  error != NULL && error->message != NULL ? error->message : "no detail");
                }
        else
                {
@@ -405,16 +444,17 @@ dbt_save_transformation (Dbt *dbt, guint id, const gchar *operation)
        stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error);
        if (!stmt)
                {
-                       g_warning ("Unable to save operation: %s\n",
-                                  error && error->message ? error->message : "No detail");
+                       g_warning ("Unable to save operation: %s.",
+                                  error != NULL && error->message != NULL ? 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");
+                                       g_warning ("NON SELECT error: %s.\n%s",
+                                                  error != NULL && error->message != NULL ? error->message : "no detail",
+                                                  sql);
                                }
                        else
                                {
@@ -453,8 +493,9 @@ dbt_parse_sql (Dbt *dbt, xmlNodePtr xnode)
        stmt = gda_sql_parser_parse_string (priv->gda_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");
+                       g_warning ("Unable to create GdaStatement from sql: %s.\n%s",
+                                          error != NULL && error->message != NULL ? error->message : "no detail",
+                                          sql);
                        ret = FALSE;
                }
        else
@@ -464,8 +505,9 @@ dbt_parse_sql (Dbt *dbt, xmlNodePtr xnode)
                        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_warning ("NON SELECT error: %s.\n%s",
+                                                  error != NULL && error->message != NULL ? error->message : "no detail",
+                                                  sql);
                                        ret = FALSE;
                                }
                        g_object_unref (stmt);
@@ -504,7 +546,7 @@ dbt_parse_gda_op (Dbt *dbt, xmlNodePtr xnode)
 
                        if (xmlStrcmp (cur->name, (const xmlChar *)"serv_op_data") != 0)
                                {
-                                       g_warning ("Invalid tag \"%s\".\n", cur->name);
+                                       g_warning ("Invalid tag \"%s\".", cur->name);
                                        ret = FALSE;
                                        cur = cur->next;
                                        continue;
@@ -512,33 +554,11 @@ dbt_parse_gda_op (Dbt *dbt, xmlNodePtr xnode)
 
                        str_operation_type = (gchar *)xmlGetProp(cur, (const xmlChar *)"type");
 
-                       /* TODO
-                        * must find the GdaServerOperationType of the string
-                        * i asked on gda mailing list
-                        * for now i implement it
-                        */
-                       operation_type = GDA_SERVER_OPERATION_LAST;
-                       if (g_strcmp0 (str_operation_type, "CREATE_DB") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_DB;
-                       if (g_strcmp0 (str_operation_type, "DROP_DB") == 0) operation_type = GDA_SERVER_OPERATION_DROP_DB;
-                       if (g_strcmp0 (str_operation_type, "CREATE_TABLE") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_TABLE;
-                       if (g_strcmp0 (str_operation_type, "DROP_TABLE") == 0) operation_type = GDA_SERVER_OPERATION_DROP_TABLE;
-                       if (g_strcmp0 (str_operation_type, "CREATE_INDEX") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_INDEX;
-                       if (g_strcmp0 (str_operation_type, "DROP_INDEX") == 0) operation_type = GDA_SERVER_OPERATION_DROP_INDEX;
-                       if (g_strcmp0 (str_operation_type, "RENAME_TABLE") == 0) operation_type = GDA_SERVER_OPERATION_RENAME_TABLE;
-                       if (g_strcmp0 (str_operation_type, "COMMENT_TABLE") == 0) operation_type = GDA_SERVER_OPERATION_COMMENT_TABLE;
-                       if (g_strcmp0 (str_operation_type, "ADD_COLUMN") == 0) operation_type = GDA_SERVER_OPERATION_ADD_COLUMN;
-                       if (g_strcmp0 (str_operation_type, "DROP_COLUMN") == 0) operation_type = GDA_SERVER_OPERATION_DROP_COLUMN;
-                       if (g_strcmp0 (str_operation_type, "COMMENT_COLUMN") == 0) operation_type = GDA_SERVER_OPERATION_COMMENT_COLUMN;
-                       if (g_strcmp0 (str_operation_type, "CREATE_VIEW") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_VIEW;
-                       if (g_strcmp0 (str_operation_type, "DROP_VIEW") == 0) operation_type = GDA_SERVER_OPERATION_DROP_VIEW;
-                       if (g_strcmp0 (str_operation_type, "CREATE_USER") == 0) operation_type = GDA_SERVER_OPERATION_CREATE_USER;
-                       if (g_strcmp0 (str_operation_type, "DROP_USER") == 0) operation_type = GDA_SERVER_OPERATION_DROP_USER;
-                       if (g_strcmp0 (str_operation_type, "ALTER_USER") == 0) operation_type = GDA_SERVER_OPERATION_ALTER_USER;
-
-                       if (operation_type == GDA_SERVER_OPERATION_LAST)
+                       operation_type = gda_server_operation_string_to_op_type (str_operation_type);
+                       if (operation_type == G_MAXINT)
                                {
-                                       g_warning ("Operation type \"%s\" not supported.\n%s\n", str_operation_type,
-                                                          error && error->message ? error->message : "No detail");
+                                       g_warning ("Operation type \"%s\" not supported: %s.", str_operation_type,
+                                                          error != NULL && error->message != NULL ? error->message : "no detail");
                                        return FALSE;
                                }
 
@@ -549,8 +569,8 @@ dbt_parse_gda_op (Dbt *dbt, xmlNodePtr xnode)
                                        op = gda_server_provider_create_operation (provider, priv->gda_conn, operation_type, NULL, &error);
                                        if (!op)
                                                {
-                                                       g_warning ("Unable to create operation type \"%s\".\n%s\n", str_operation_type,
-                                                                          error && error->message ? error->message : "No detail");
+                                                       g_warning ("Unable to create operation type \"%s\": %s.", str_operation_type,
+                                                                          error != NULL && error->message != NULL ? error->message : "no detail");
                                                        ret = FALSE;
                                                }
                                        else
@@ -558,16 +578,16 @@ dbt_parse_gda_op (Dbt *dbt, xmlNodePtr xnode)
                                                        error = NULL;
                                                        if (!gda_server_operation_load_data_from_xml (op, cur, &error))
                                                                {
-                                                                       g_warning ("Unable to load GdaServerOperation from the file.\n%s\n",
-                                                                                          error && error->message ? error->message : "No detail");
+                                                                       g_warning ("Unable to load GdaServerOperation from the file: %s.\n",
+                                                                                          error != NULL && error->message != NULL ? error->message : "no detail");
                                                                        ret = FALSE;
                                                                }
                                                        else
                                                                {
                                                                        if (!gda_server_provider_perform_operation (provider, priv->gda_conn, op, &error))
                                                                                {
-                                                                                       g_warning ("Error on executing GdaServerOperation from the file.\n%s\n",
-                                                                                                          error && error->message ? error->message : "No detail");
+                                                                                       g_warning ("Error on executing GdaServerOperation from the file: %s.\n",
+                                                                                                          error != NULL && error->message != NULL ? error->message : "no detail");
                                                                                        ret = FALSE;
                                                                                }
                                                                }
@@ -576,7 +596,7 @@ dbt_parse_gda_op (Dbt *dbt, xmlNodePtr xnode)
                                }
                        else
                                {
-                                       g_warning ("Operation type \"%s\" not supported by the provider.\n", str_operation_type);
+                                       g_warning ("Operation type \"%s\" not supported by the provider.", str_operation_type);
                                        ret = FALSE;
                                }
 
@@ -586,6 +606,79 @@ dbt_parse_gda_op (Dbt *dbt, xmlNodePtr xnode)
        return ret;
 }
 
+static gboolean
+dbt_parse_drop_table (Dbt *dbt, xmlNodePtr xnode)
+{
+       DbtPrivate *priv;
+
+       gboolean ret;
+
+       xmlNodePtr cur;
+       gchar *table_name;
+
+       GError *error;
+
+       ret = TRUE;
+
+       priv = DBT_GET_PRIVATE (dbt);
+
+       table_name = NULL;
+       cur = xnode->children;
+       while (cur != NULL && ret)
+               {
+                       if (xmlStrcmp (cur->name, (const xmlChar *)"table_name") == 0)
+                               {
+                                       table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur)));
+                               }
+
+                       cur = cur->next;
+               }
+
+       if (table_name != NULL && g_strcmp0 (table_name, "") != 0)
+               {
+                       gchar *sql;
+                       GdaStatement *stmt;
+
+                       sql = g_strdup_printf ("DROP TABLE %s", table_name);
+
+                       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",
+                                                          error != NULL && error->message != NULL ? error->message : "no detail",
+                                                          sql);
+                                       ret = FALSE;
+                               }
+                       else
+                               {
+                                       guint nrows;
+
+                                       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",
+                                                                          error != NULL && error->message != NULL ? error->message : "no detail",
+                                                                          sql);
+                                                       ret = FALSE;
+                                               }
+                                       g_object_unref (stmt);
+                               }
+               }
+       else
+               {
+                       g_warning ("You must provide the name of the table.");
+                       ret = FALSE;
+               }
+
+       return ret;
+}
+
+/**
+ * dbt_transform:
+ * @dbt:
+ *
+ */
 void
 dbt_transform (Dbt *dbt)
 {
@@ -636,8 +729,8 @@ dbt_transform (Dbt *dbt)
                                                                        error = NULL;
                                                                        if (!gda_connection_begin_transaction (priv->gda_conn, NULL, GDA_TRANSACTION_ISOLATION_UNKNOWN, &error))
                                                                                {
-                                                                                       g_warning ("Unable to start the transaction: %s\n",
-                                                                                                  error && error->message ? error->message : "No detail");
+                                                                                       g_warning ("Unable to start the transaction: %s.\n",
+                                                                                                  error != NULL && error->message != NULL ? error->message : "no detail");
                                                                                        continue;
                                                                                }
                                                                }
@@ -655,9 +748,13 @@ dbt_transform (Dbt *dbt)
                                                                                                {
                                                                                                        tosave = dbt_parse_gda_op (dbt, xnode);
                                                                                                }
+                                                                                       else if (xmlStrcmp (xnode->name, (const xmlChar *)"drop_table") == 0)
+                                                                                               {
+                                                                                                       tosave = dbt_parse_drop_table (dbt, xnode);
+                                                                                               }
                                                                                        else
                                                                                                {
-                                                                                                       g_warning ("Invalid tag: %s", xnode->name);
+                                                                                                       g_warning ("Invalid tag: %s.", xnode->name);
                                                                                                        tosave = FALSE;
                                                                                                }
                                                                                }
index a9fddd1624212b486cf7307dff87f6dfbdd69a05..71d519cb119412557cc5f6f1fa8abe81c21b6d4c 100644 (file)
Binary files a/tests/test.db and b/tests/test.db differ
index cd4b0f168be0e355cba115839fc9a6571f366452..a406bd6bea500cd877110dc4c32d5fe34a7333ec 100644 (file)
@@ -2,6 +2,18 @@
 <dbtransformer>
 
        <dbtransformation id="1">
+               <drop_table>
+                       <table_name>clients</table_name>
+               </drop_table>
+               <drop_table>
+                       <table_name>orders</table_name>
+               </drop_table>
+               <drop_table>
+                       <table_name>newtable</table_name>
+               </drop_table>
+       </dbtransformation>
+
+       <!--<dbtransformation id="1">
                <sql>
                        CREATE TABLE clients (id integer, name varchar(50), CONSTRAINT clients_pkey PRIMARY KEY (id))
                </sql>
@@ -14,7 +26,7 @@
                <sql>
                        ALTER TABLE orders ADD COLUMN total double
                </sql>
-       </dbtransformation>
+       </dbtransformation>-->
 
        <dbtransformation id="3">
                <gda_op>