From c7b0026d38f74ba6e0e80c79390d6d35e473072c Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Fri, 30 Jul 2010 20:06:52 +0200 Subject: [PATCH] Bugfix. Added and managed rename_table and rename_view. --- docs/dbtransformer.dtd | 12 ++- src/dbtransformer.c | 176 ++++++++++++++++++++++++++++++++++++++--- tests/test.db | Bin 7168 -> 7168 bytes tests/test.xml | 7 ++ 4 files changed, 184 insertions(+), 11 deletions(-) diff --git a/docs/dbtransformer.dtd b/docs/dbtransformer.dtd index 28f1e99..8add38a 100644 --- a/docs/dbtransformer.dtd +++ b/docs/dbtransformer.dtd @@ -4,7 +4,9 @@ gda_op*, sql*, drop_table*, - drop_view*)> + rename_table*, + drop_view*, + rename_view*)> + + + + + + + + diff --git a/src/dbtransformer.c b/src/dbtransformer.c index 8f25b00..0e54972 100644 --- a/src/dbtransformer.c +++ b/src/dbtransformer.c @@ -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); diff --git a/tests/test.db b/tests/test.db index 71d519cb119412557cc5f6f1fa8abe81c21b6d4c..a61c14bdd724eae9870c1869037efd63015b5f5e 100644 GIT binary patch delta 289 zcmZp$Xt0KKc2GUz^%;05xSnO-n3A7FN3dcpXT@#SVgmQu#a zuNnV3MliF9tEw`VmLw+Sq~@iTL+Hd@6gG=et3rsQlaH%{60$O-$udlmo1K^s zv2ZanE(Kb{6wJ63X#8|WtwwDY7KWy1o0O!IqQt!7wEUvn#FEVXJcY~@TO|`EJ1$O6 zo1)Y_pk0&KbL)q4+5pXs2iqSH60}3M#zr4a5~Nulq8p+IXq=5cx>a_YgSplU0swH8 BSTz6u delta 113 zcmZp$Xt0KKc2Fz7y#;05xSnZ7YFA7FN3`o{Q@@#SVgmQu#a zuNnUeIfl3@gg83+xGE&)WTtvCvx!fB%P7gnGMSx8Z?hBgAr=lMreL6MreL5Rcc#t3 HTx$gZFj^UI diff --git a/tests/test.xml b/tests/test.xml index a406bd6..2abf1cd 100644 --- a/tests/test.xml +++ b/tests/test.xml @@ -56,4 +56,11 @@ + + + newtable + newtablename + + + -- 2.49.0