]> saetta.ns0.it Git - libzakdbt/commitdiff
Added transformation drop_column.
authorAndrea Zagli <azagli@libero.it>
Sun, 21 Jan 2018 10:48:42 +0000 (11:48 +0100)
committerAndrea Zagli <azagli@libero.it>
Sun, 21 Jan 2018 10:48:42 +0000 (11:48 +0100)
docs/zakdbt.dtd
src/dbt.c
tests/test.xml

index deaa668c459096385e7857d1df8499474115ff83..2e328e1f3d15e69754b938b2b98476bcbfc84b73 100644 (file)
@@ -51,3 +51,5 @@
 <!ELEMENT column_auto_increment (#PCDATA)>
 <!ELEMENT column_unique (#PCDATA)>
 <!ELEMENT column_default (#PCDATA)>
+
+<!ELEMENT drop_column (table_name, column_name)>
index dec304bbb2eaeb5d784b3b3fdae8c6f5ad42e054..8dfed8403f36523c5cf9339ae1ea65945a7dd615 100644 (file)
--- a/src/dbt.c
+++ b/src/dbt.c
@@ -1336,59 +1336,151 @@ zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode)
                                                                   NULL,
                                                                   &error);
 
-                       error = NULL;
-                       gda_server_operation_set_value_at (op, table_name, &error, "/COLUMN_DEF_P/TABLE_NAME");
-
-                       error = NULL;
-                       gda_server_operation_set_value_at (op, column_name, &error, "/COLUMN_DEF_P/COLUMN_NAME");
-
-                       error = NULL;
-                       gda_server_operation_set_value_at (op, column_type, &error, "/COLUMN_DEF_P/COLUMN_TYPE");
-
-                       if (column_size != NULL && g_strcmp0 (column_size, "") != 0)
+                       if (op == NULL
+                           || error != NULL)
                                {
-                                       error = NULL;
-                                       gda_server_operation_set_value_at (op, column_size, &error, "/COLUMN_DEF_P/COLUMN_SIZE");
+                                       g_warning ("Unable to create GdaServerOperation add_column: %s.",
+                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                                       ret = FALSE;
                                }
-
-                       if (column_not_null != NULL && g_strcmp0 (column_not_null, "") != 0)
+                       else
                                {
                                        error = NULL;
-                                       gda_server_operation_set_value_at (op, column_not_null, &error, "/COLUMN_DEF_P/COLUMN_NNULL");
-                               }
+                                       gda_server_operation_set_value_at (op, table_name, &error, "/COLUMN_DEF_P/TABLE_NAME");
 
-                       if (column_auto_increment != NULL && g_strcmp0 (column_auto_increment, "") != 0)
-                               {
                                        error = NULL;
-                                       gda_server_operation_set_value_at (op, column_auto_increment, &error, "/COLUMN_DEF_P/COLUMN_AUTOINC");
-                               }
+                                       gda_server_operation_set_value_at (op, column_name, &error, "/COLUMN_DEF_P/COLUMN_NAME");
+
+                                       error = NULL;
+                                       gda_server_operation_set_value_at (op, column_type, &error, "/COLUMN_DEF_P/COLUMN_TYPE");
+
+                                       if (column_size != NULL && g_strcmp0 (column_size, "") != 0)
+                                               {
+                                                       error = NULL;
+                                                       gda_server_operation_set_value_at (op, column_size, &error, "/COLUMN_DEF_P/COLUMN_SIZE");
+                                               }
+
+                                       if (column_not_null != NULL && g_strcmp0 (column_not_null, "") != 0)
+                                               {
+                                                       error = NULL;
+                                                       gda_server_operation_set_value_at (op, column_not_null, &error, "/COLUMN_DEF_P/COLUMN_NNULL");
+                                               }
+
+                                       if (column_auto_increment != NULL && g_strcmp0 (column_auto_increment, "") != 0)
+                                               {
+                                                       error = NULL;
+                                                       gda_server_operation_set_value_at (op, column_auto_increment, &error, "/COLUMN_DEF_P/COLUMN_AUTOINC");
+                                               }
+
+                                       if (column_unique != NULL && g_strcmp0 (column_unique, "") != 0)
+                                               {
+                                                       error = NULL;
+                                                       gda_server_operation_set_value_at (op, column_unique, &error, "/COLUMN_DEF_P/COLUMN_UNIQUE");
+                                               }
+
+                                       if (column_default != NULL && g_strcmp0 (column_default, "") != 0)
+                                               {
+                                                       error = NULL;
+                                                       gda_server_operation_set_value_at (op, column_default, &error, "/COLUMN_DEF_P/COLUMN_DEFAULT");
+                                               }
 
-                       if (column_unique != NULL && g_strcmp0 (column_unique, "") != 0)
-                               {
                                        error = NULL;
-                                       gda_server_operation_set_value_at (op, column_unique, &error, "/COLUMN_DEF_P/COLUMN_UNIQUE");
+                                       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 at least:\n"
+                                  " - the name of the table and the column\n"
+                                  " - the column's type");
+                       ret = FALSE;
+               }
+
+       return ret;
+}
+
+static gboolean
+zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode)
+{
+       ZakDbtDbtPrivate *priv;
+
+       gboolean ret;
+
+       xmlNodePtr cur;
+
+       gchar *table_name;
+       gchar *column_name;
+
+       GError *error;
+
+       ret = TRUE;
+
+       priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt);
+
+       table_name = NULL;
+       column_name = NULL;
 
-                       if (column_default != NULL && g_strcmp0 (column_default, "") != 0)
+       cur = xnode->children;
+       while (cur != NULL && ret)
+               {
+                       if (xmlStrcmp (cur->name, (const xmlChar *)"table_name") == 0)
                                {
-                                       error = NULL;
-                                       gda_server_operation_set_value_at (op, column_default, &error, "/COLUMN_DEF_P/COLUMN_DEFAULT");
+                                       table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur)));
+                               }
+                       else if (xmlStrcmp (cur->name, (const xmlChar *)"column_name") == 0)
+                               {
+                                       column_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur)));
                                }
 
+                       cur = cur->next;
+               }
+
+       if (table_name != NULL && g_strcmp0 (table_name, "") != 0
+           && column_name != NULL && g_strcmp0 (column_name, "") != 0)
+               {
+                       GdaServerOperation *op;
+
                        error = NULL;
-                       if (!gda_server_provider_perform_operation (priv->gda_provider, priv->gda_conn, op, &error))
+                       op = gda_server_provider_create_operation (priv->gda_provider,
+                                                                  priv->gda_conn,
+                                                                  GDA_SERVER_OPERATION_DROP_COLUMN,
+                                                                  NULL,
+                                                                  &error);
+                       if (op == NULL
+                           || error != NULL)
                                {
-                                       g_warning ("Error executing the operation: %s.",
-                                                  error != NULL && error->message != NULL ? error->message : "No detail");
+                                       g_warning ("Unable to create GdaServerOperation drop_column: %s.",
+                                                  error != NULL && error->message != NULL ? error->message : "no details");
                                        ret = FALSE;
                                }
-                       g_object_unref (op);
+                       else
+                               {
+                                       error = NULL;
+                                       gda_server_operation_set_value_at (op, table_name, &error, "/COLUMN_DESC_P/TABLE_NAME");
+
+                                       error = NULL;
+                                       gda_server_operation_set_value_at (op, column_name, &error, "/COLUMN_DESC_P/COLUMN_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 details");
+                                                       ret = FALSE;
+                                               }
+                                       g_object_unref (op);
+                               }
                }
        else
                {
                        g_warning ("You must provide at least:\n"
-                                  " - the name of the table and the column\n"
-                                  " - the column's type");
+                                  " - the name of the table and the column");
                        ret = FALSE;
                }
 
@@ -1510,6 +1602,10 @@ zak_dbt_dbt_transform (ZakDbtDbt *zak_dbt_dbt)
                                                                                                {
                                                                                                        tosave = zak_dbt_dbt_parse_add_column (zak_dbt_dbt, xnode);
                                                                                                }
+                                                                                       else if (xmlStrcmp (xnode->name, (const xmlChar *)"drop_column") == 0)
+                                                                                               {
+                                                                                                       tosave = zak_dbt_dbt_parse_drop_column (zak_dbt_dbt, xnode);
+                                                                                               }
                                                                                        else
                                                                                                {
                                                                                                        g_warning ("Invalid tag: %s.", xnode->name);
index a5c78eb2075b6b8f579dd815338e8c65179634c4..fb90101ca842acf6017e2fa59a816e72d2ee0ba1 100644 (file)
                </add_column>
        </dbtransformation>
 
+       <dbtransformation id="9">
+         <drop_column>
+               <table_name>newtablename</table_name>
+               <column_name>new_col</column_name>
+         </drop_column>
+       </dbtransformation>
+
 </dbtransformer>