]> saetta.ns0.it Git - libzakdbt/commitdiff
Bugfix.
authorAndrea Zagli <azagli@libero.it>
Fri, 30 Jul 2010 18:06:52 +0000 (20:06 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 30 Jul 2010 18:06:52 +0000 (20:06 +0200)
Added and managed rename_table and rename_view.

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

index 28f1e998c154e5ab710171109489c5570f0e8c46..8add38a599c1f49254e9a9c8c152b413e76f1864 100644 (file)
@@ -4,7 +4,9 @@
                           gda_op*,
                           sql*,
                           drop_table*,
-                          drop_view*)>
+                          rename_table*,
+                          drop_view*,
+                          rename_view*)>
 
 <!ATTLIST transformation
        id CDATA #REQUIRED
 
 <!ELEMENT drop_table (table_name)>
 
+<!ELEMENT rename_table (table_name, new_table_name)>
+
 <!ELEMENT table_name (#PCDATA) #REQUIRED>
 
+<!ELEMENT new_table_name (#PCDATA) #REQUIRED>
+
 <!ELEMENT drop_view (view_name)>
 
+<!ELEMENT rename_view (view_name, new_view_name)>
+
 <!ELEMENT view_name (#PCDATA) #REQUIRED>
+
+<!ELEMENT new_view_name (#PCDATA) #REQUIRED>
index 8f25b00c49d103e5b833cd0a459d74d8be5abbb0..0e549721b71ecfa025e7396bacf737abd3def5cb 100644 (file)
@@ -45,6 +45,7 @@ struct _DbtPrivate
        {
                GdaConnection *gda_conn;
                GdaSqlParser *gda_parser;
+               GdaServerProvider *gda_provider;
 
                xmlDocPtr xml_doc;
                xmlNodePtr xml_root;
@@ -171,6 +172,8 @@ dbt_set_db_connection_from_string (Dbt *dbt, const gchar *cnc_string)
                        priv->gda_conn= NULL;
                        return;
                }
+
+       priv->gda_provider = gda_connection_get_provider (priv->gda_conn);
 }
 
 static gboolean
@@ -314,13 +317,11 @@ dbt_check_db (Dbt *dbt)
                        if (!dm)
                                {
                                        /* 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);
+                                       op = gda_server_provider_create_operation (priv->gda_provider, priv->gda_conn, GDA_SERVER_OPERATION_CREATE_TABLE, NULL, &error);
                                        if (!op)
                                                {
                                                        g_warning ("CREATE TABLE operation is not supported by the provider: %s.",
@@ -351,7 +352,7 @@ dbt_check_db (Dbt *dbt)
                                        */
 
                                        /* execute the operation */
-                                       if (!gda_server_provider_perform_operation (provider, priv->gda_conn, op, &error))
+                                       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");
@@ -377,6 +378,8 @@ dbt_check_to_execute (Dbt *dbt, guint id)
        GError *error;
        GdaStatement *stmt;
 
+       g_return_val_if_fail (id > 0, FALSE);
+
        priv = DBT_GET_PRIVATE (dbt);
 
        ret = FALSE;
@@ -527,7 +530,6 @@ dbt_parse_gda_op (Dbt *dbt, xmlNodePtr xnode)
 
        gchar *str_operation_type;
        GdaServerOperationType operation_type;
-       GdaServerProvider *provider;
        GError *error;
        GdaServerOperation *op;
 
@@ -562,11 +564,10 @@ dbt_parse_gda_op (Dbt *dbt, xmlNodePtr xnode)
                                        return FALSE;
                                }
 
-                       provider = gda_connection_get_provider (priv->gda_conn);
-                       if (gda_server_provider_supports_operation (provider, priv->gda_conn, operation_type, NULL))
+                       if (gda_server_provider_supports_operation (priv->gda_provider, priv->gda_conn, operation_type, NULL))
                                {
                                        error = NULL;
-                                       op = gda_server_provider_create_operation (provider, priv->gda_conn, operation_type, NULL, &error);
+                                       op = gda_server_provider_create_operation (priv->gda_provider, priv->gda_conn, operation_type, NULL, &error);
                                        if (!op)
                                                {
                                                        g_warning ("Unable to create operation type \"%s\": %s.", str_operation_type,
@@ -584,7 +585,7 @@ dbt_parse_gda_op (Dbt *dbt, xmlNodePtr xnode)
                                                                }
                                                        else
                                                                {
-                                                                       if (!gda_server_provider_perform_operation (provider, priv->gda_conn, op, &error))
+                                                                       if (!gda_server_provider_perform_operation (priv->gda_provider, priv->gda_conn, op, &error))
                                                                                {
                                                                                        g_warning ("Error on executing GdaServerOperation from the file: %s.\n",
                                                                                                           error != NULL && error->message != NULL ? error->message : "no detail");
@@ -674,6 +675,72 @@ dbt_parse_drop_table (Dbt *dbt, xmlNodePtr xnode)
        return ret;
 }
 
+static gboolean
+dbt_parse_rename_table (Dbt *dbt, xmlNodePtr xnode)
+{
+       DbtPrivate *priv;
+
+       gboolean ret;
+
+       xmlNodePtr cur;
+       gchar *table_name;
+       gchar *new_table_name;
+
+       GError *error;
+
+       ret = TRUE;
+
+       priv = DBT_GET_PRIVATE (dbt);
+
+       table_name = NULL;
+       new_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)));
+                               }
+                       else if (xmlStrcmp (cur->name, (const xmlChar *)"new_table_name") == 0)
+                               {
+                                       new_table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur)));
+                               }
+
+                       cur = cur->next;
+               }
+
+       if (table_name != NULL && g_strcmp0 (table_name, "") != 0
+           && new_table_name != NULL && g_strcmp0 (new_table_name, "") != 0)
+               {
+                       GdaServerOperation *op;
+
+                       error = NULL;
+                       op = gda_server_provider_create_operation (priv->gda_provider,
+                                                                  priv->gda_conn,
+                                                                  GDA_SERVER_OPERATION_RENAME_TABLE,
+                                                                  NULL,
+                                                                  &error);
+                       gda_server_operation_set_value_at (op, table_name, &error, "/TABLE_DESC_P/TABLE_NAME");
+                       gda_server_operation_set_value_at (op, new_table_name, &error, "/TABLE_DESC_P/TABLE_NEW_NAME");
+
+                       error = NULL;
+                       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");
+                                       ret = FALSE;
+                               }
+                       g_object_unref (op);                    
+               }
+       else
+               {
+                       g_warning ("You must provide the name of the table to drop.");
+                       ret = FALSE;
+               }
+
+       return ret;
+}
+
 static gboolean
 dbt_parse_drop_view (Dbt *dbt, xmlNodePtr xnode)
 {
@@ -742,6 +809,80 @@ dbt_parse_drop_view (Dbt *dbt, xmlNodePtr xnode)
        return ret;
 }
 
+static gboolean
+dbt_parse_rename_view (Dbt *dbt, xmlNodePtr xnode)
+{
+       DbtPrivate *priv;
+
+       gboolean ret;
+
+       xmlNodePtr cur;
+       gchar *view_name;
+       gchar *new_view_name;
+
+       GError *error;
+
+       ret = TRUE;
+
+       priv = DBT_GET_PRIVATE (dbt);
+
+       view_name = NULL;
+       cur = xnode->children;
+       while (cur != NULL && ret)
+               {
+                       if (xmlStrcmp (cur->name, (const xmlChar *)"view_name") == 0)
+                               {
+                                       view_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur)));
+                               }
+                       else if (xmlStrcmp (cur->name, (const xmlChar *)"new_view_name") == 0)
+                               {
+                                       new_view_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur)));
+                               }
+
+                       cur = cur->next;
+               }
+
+       if (view_name != NULL && g_strcmp0 (view_name, "") != 0
+           && new_view_name != NULL && g_strcmp0 (new_view_name, "") != 0)
+               {
+                       gchar *sql;
+                       GdaStatement *stmt;
+
+                       sql = g_strdup_printf ("ALTER VIEW %s RENAME TO %s", view_name, new_view_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 view to drop.");
+                       ret = FALSE;
+               }
+
+       return ret;
+}
+
 /**
  * dbt_transform:
  * @dbt:
@@ -758,6 +899,7 @@ dbt_transform (Dbt *dbt)
 
        GError *error;
 
+       gchar *str_id;
        guint id;
        gboolean tosave;
 
@@ -787,7 +929,13 @@ dbt_transform (Dbt *dbt)
                                {
                                        tosave = TRUE;
 
-                                       id = strtol (xmlGetProp (xnodeset->nodeTab[i], (const gchar *)"id"), NULL, 10);
+                                       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 (dbt_check_to_execute (dbt, id))
                                                {
@@ -820,10 +968,18 @@ dbt_transform (Dbt *dbt)
                                                                                                {
                                                                                                        tosave = dbt_parse_drop_table (dbt, xnode);
                                                                                                }
+                                                                                       else if (xmlStrcmp (xnode->name, (const xmlChar *)"rename_table") == 0)
+                                                                                               {
+                                                                                                       tosave = dbt_parse_rename_table (dbt, xnode);
+                                                                                               }
                                                                                        else if (xmlStrcmp (xnode->name, (const xmlChar *)"drop_view") == 0)
                                                                                                {
                                                                                                        tosave = dbt_parse_drop_view (dbt, xnode);
                                                                                                }
+                                                                                       else if (xmlStrcmp (xnode->name, (const xmlChar *)"rename_view") == 0)
+                                                                                               {
+                                                                                                       tosave = dbt_parse_rename_view (dbt, xnode);
+                                                                                               }
                                                                                        else
                                                                                                {
                                                                                                        g_warning ("Invalid tag: %s.", xnode->name);
index 71d519cb119412557cc5f6f1fa8abe81c21b6d4c..a61c14bdd724eae9870c1869037efd63015b5f5e 100644 (file)
Binary files a/tests/test.db and b/tests/test.db differ
index a406bd6bea500cd877110dc4c32d5fe34a7333ec..2abf1cdf569600091a14a84d7e6baa4929c949d4 100644 (file)
                </gda_op>
        </dbtransformation>
 
+       <dbtransformation id="4">
+               <rename_table>
+                       <table_name>newtable</table_name>
+                       <new_table_name>newtablename</new_table_name>
+               </rename_table>
+       </dbtransformation>
+
 </dbtransformer>