From: Andrea Zagli Date: Sun, 21 Jan 2018 10:48:42 +0000 (+0100) Subject: Added transformation drop_column. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=df3618d09a7727dcc5941e906350a46072632aca;p=libzakdbt Added transformation drop_column. --- diff --git a/docs/zakdbt.dtd b/docs/zakdbt.dtd index deaa668..2e328e1 100644 --- a/docs/zakdbt.dtd +++ b/docs/zakdbt.dtd @@ -51,3 +51,5 @@ + + diff --git a/src/dbt.c b/src/dbt.c index dec304b..8dfed84 100644 --- 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); diff --git a/tests/test.xml b/tests/test.xml index a5c78eb..fb90101 100644 --- a/tests/test.xml +++ b/tests/test.xml @@ -92,4 +92,11 @@ + + + newtablename + new_col + + +