From 592b1d37e6611a7cdda05afc4479c54c9c1378de Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 21 Jan 2018 11:09:45 +0100 Subject: [PATCH] Added transformation add_column. --- docs/zakdbt.dtd | 12 +++- src/dbt.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test.xml | 11 ++++ 3 files changed, 170 insertions(+), 1 deletion(-) diff --git a/docs/zakdbt.dtd b/docs/zakdbt.dtd index 3f5feca..deaa668 100644 --- a/docs/zakdbt.dtd +++ b/docs/zakdbt.dtd @@ -8,7 +8,8 @@ drop_view*, rename_view*, create_index*, - drop_index*)> + drop_index*, + add_column*)> + + + + + + + + + diff --git a/src/dbt.c b/src/dbt.c index 2d1e5db..dec304b 100644 --- a/src/dbt.c +++ b/src/dbt.c @@ -1251,6 +1251,150 @@ zak_dbt_dbt_parse_drop_index (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) return ret; } +static gboolean +zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +{ + ZakDbtDbtPrivate *priv; + + gboolean ret; + + xmlNodePtr cur; + + gchar *table_name; + gchar *column_name; + gchar *column_type; + gchar *column_size; + gchar *column_not_null; + gchar *column_auto_increment; + gchar *column_unique; + gchar *column_default; + + GError *error; + + ret = TRUE; + + priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); + + table_name = NULL; + column_name = NULL; + column_type = NULL; + column_size = NULL; + column_not_null = NULL; + column_auto_increment = NULL; + column_unique = NULL; + column_default = 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 *)"column_name") == 0) + { + column_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"column_type") == 0) + { + column_type = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"column_size") == 0) + { + column_size = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"column_not_null") == 0) + { + column_not_null = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"column_auto_increment") == 0) + { + column_auto_increment = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"column_unique") == 0) + { + column_unique = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"column_default") == 0) + { + column_default = 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 + && column_type != NULL && g_strcmp0 (column_type, "") != 0) + { + GdaServerOperation *op; + + error = NULL; + op = gda_server_provider_create_operation (priv->gda_provider, + priv->gda_conn, + GDA_SERVER_OPERATION_ADD_COLUMN, + 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) + { + 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"); + } + + 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 at least:\n" + " - the name of the table and the column\n" + " - the column's type"); + ret = FALSE; + } + + return ret; +} + /** * zak_dbt_dbt_transform: * @zak_dbt_dbt: @@ -1362,6 +1506,10 @@ zak_dbt_dbt_transform (ZakDbtDbt *zak_dbt_dbt) { tosave = zak_dbt_dbt_parse_drop_index (zak_dbt_dbt, xnode); } + else if (xmlStrcmp (xnode->name, (const xmlChar *)"add_column") == 0) + { + tosave = zak_dbt_dbt_parse_add_column (zak_dbt_dbt, xnode); + } else { g_warning ("Invalid tag: %s.", xnode->name); diff --git a/tests/test.xml b/tests/test.xml index d748b27..a5c78eb 100644 --- a/tests/test.xml +++ b/tests/test.xml @@ -81,4 +81,15 @@ + + + newtablename + new_col + varchar + 255 + TRUE + '' + + + -- 2.49.0