From: Andrea Zagli Date: Sun, 21 Jan 2018 11:39:39 +0000 (+0100) Subject: Added transformation create_table. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=3c0e37e6001cf18129f4f4b950beebc40a1f2fd6;p=libzakdbt Added transformation create_table. --- diff --git a/docs/zakdbt.dtd b/docs/zakdbt.dtd index 2e328e1..4b36c59 100644 --- a/docs/zakdbt.dtd +++ b/docs/zakdbt.dtd @@ -21,6 +21,8 @@ + + diff --git a/src/dbt.c b/src/dbt.c index 8dfed84..a01c47a 100644 --- a/src/dbt.c +++ b/src/dbt.c @@ -64,6 +64,10 @@ static void zak_dbt_dbt_get_property (GObject *object, static gboolean zak_dbt_dbt_check_db (ZakDbtDbt *zak_dbt_dbt, gboolean create_tables); +static gboolean zak_dbt_dbt_parse_create_index (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode); +static gboolean zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode); +static gboolean zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode); + #define ZAK_DBT_DBT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_DBT_TYPE_DBT, ZakDbtDbtPrivate)) typedef struct _ZakDbtDbtPrivate ZakDbtDbtPrivate; @@ -795,6 +799,123 @@ zak_dbt_dbt_parse_gda_op (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) return ret; } +static gboolean +zak_dbt_dbt_parse_create_table (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +{ + ZakDbtDbtPrivate *priv; + + gboolean ret; + + xmlNodePtr cur; + gchar *table_name; + + xmlNode *node; + + GError *error; + + ret = TRUE; + + priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_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) + { + GdaServerOperation *op; + + error = NULL; + op = gda_server_provider_create_operation (priv->gda_provider, + priv->gda_conn, + GDA_SERVER_OPERATION_CREATE_TABLE, + NULL, + &error); + if (op == NULL + || error != NULL) + { + g_warning ("Unable to create GdaServerOperation create_table: %s.", + error != NULL && error->message != NULL ? error->message : "no detail"); + ret = FALSE; + } + else + { + error = NULL; + gda_server_operation_set_value_at (op, table_name, &error, "/TABLE_DEF_P/TABLE_NAME"); + + error = NULL; + gda_server_operation_set_value_at (op, "zdbt_to_be_delete", &error, "/FIELDS_A/@COLUMN_NAME/0"); + + error = NULL; + gda_server_operation_set_value_at (op, "integer", &error, "/FIELDS_A/@COLUMN_TYPE/0"); + + 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); + + if (ret) + { + cur = xnode->children; + while (cur != NULL && ret) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"add_column") == 0) + { + node = xmlNewNode (NULL, (const xmlChar *)"table_name"); + xmlNodeSetContent (node, (xmlChar *)table_name); + xmlAddChild (cur, node); + + zak_dbt_dbt_parse_add_column (zak_dbt_dbt, cur); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"create_index") == 0) + { + node = xmlNewNode (NULL, (const xmlChar *)"table_name"); + xmlNodeSetContent (node, (xmlChar *)table_name); + xmlAddChild (cur, node); + + zak_dbt_dbt_parse_create_index (zak_dbt_dbt, cur); + } + + cur = cur->next; + } + + /* drop of temp column zdbt_to_be_delete */ + cur = xmlNewNode (NULL, (const xmlChar *)"drop_colum"); + + node = xmlNewNode (NULL, (const xmlChar *)"table_name"); + xmlNodeSetContent (node, (const xmlChar *)table_name); + xmlAddChild (cur, node); + + node = xmlNewNode (NULL, (const xmlChar *)"column_name"); + xmlNodeSetContent (node, (const xmlChar *)"zdbt_to_be_delete"); + xmlAddChild (cur, node); + + zak_dbt_dbt_parse_drop_column (zak_dbt_dbt, cur); + } + } + } + else + { + g_warning ("You must provide the name of the table to create."); + ret = FALSE; + } + + return ret; +} + static gboolean zak_dbt_dbt_parse_drop_table (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) { @@ -1340,7 +1461,7 @@ zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) || error != NULL) { g_warning ("Unable to create GdaServerOperation add_column: %s.", - error != NULL && error->message != NULL ? error->message : "no details"); + error != NULL && error->message != NULL ? error->message : "no detail"); ret = FALSE; } else @@ -1456,7 +1577,7 @@ zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) || error != NULL) { g_warning ("Unable to create GdaServerOperation drop_column: %s.", - error != NULL && error->message != NULL ? error->message : "no details"); + error != NULL && error->message != NULL ? error->message : "no detail"); ret = FALSE; } else @@ -1471,7 +1592,7 @@ zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) 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"); + error != NULL && error->message != NULL ? error->message : "no detail"); ret = FALSE; } g_object_unref (op); @@ -1574,6 +1695,10 @@ zak_dbt_dbt_transform (ZakDbtDbt *zak_dbt_dbt) { tosave = zak_dbt_dbt_parse_gda_op (zak_dbt_dbt, xnode); } + else if (xmlStrcmp (xnode->name, (const xmlChar *)"create_table") == 0) + { + tosave = zak_dbt_dbt_parse_create_table (zak_dbt_dbt, xnode); + } else if (xmlStrcmp (xnode->name, (const xmlChar *)"drop_table") == 0) { tosave = zak_dbt_dbt_parse_drop_table (zak_dbt_dbt, xnode); diff --git a/tests/test.xml b/tests/test.xml index fb90101..399e67b 100644 --- a/tests/test.xml +++ b/tests/test.xml @@ -99,4 +99,29 @@ + + + newtablename_create_table + + wrong_table_name + id_create_table + integer + TRUE + 0 + + + new_col_create_table + varchar + 255 + TRUE + '' + + + wrong_table_name + newtablename_create_table_idx + id + + + +