From: Andrea Zagli Date: Mon, 22 Jan 2018 16:30:24 +0000 (+0100) Subject: Refactoring of internal parse functions that now return sql string. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=4907086c7738bc5c12a5abc9a7aad8a8dcb5bf34;p=libzakdbt Refactoring of internal parse functions that now return sql string. --- diff --git a/src/dbt.c b/src/dbt.c index a01c47a..f53b235 100644 --- a/src/dbt.c +++ b/src/dbt.c @@ -64,9 +64,9 @@ 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); +static gchar *zak_dbt_dbt_parse_create_index (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode); +static gchar *zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode); +static gchar *zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode); #define ZAK_DBT_DBT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_DBT_TYPE_DBT, ZakDbtDbtPrivate)) @@ -644,8 +644,7 @@ zak_dbt_dbt_save_transformation (ZakDbtDbt *zak_dbt_dbt, guint id, const gchar * } else { - guint nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error); - if (nrows == -1) + if (gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error) == -1) { g_warning ("NON SELECT error: %s.\n%s", error != NULL && error->message != NULL ? error->message : "no detail", @@ -661,76 +660,32 @@ zak_dbt_dbt_save_transformation (ZakDbtDbt *zak_dbt_dbt, guint id, const gchar * return ret; } -static gboolean -zak_dbt_dbt_parse_sql (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_sql (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { - ZakDbtDbtPrivate *priv; - - gchar *sql; - GError *error; - GdaStatement *stmt; - - gboolean ret; - - ret = TRUE; - - priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); - - if (!GDA_IS_SQL_PARSER (priv->gda_parser)) - { - g_warning ("Invalid sql parser."); - return FALSE; - } - - sql = (gchar *)xmlNodeGetContent (xnode); - - error = NULL; - stmt = gda_sql_parser_parse_string (priv->gda_parser, g_strstrip (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); - } - - return ret; + return g_strstrip ((gchar *)xmlNodeGetContent (xnode)); } -static gboolean -zak_dbt_dbt_parse_gda_op (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_gda_op (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; - xmlNodePtr cur; + xmlNode *cur; gchar *str_operation_type; GdaServerOperationType operation_type; GError *error; GdaServerOperation *op; - ret = TRUE; + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlNodeIsText (cur)) { @@ -741,7 +696,7 @@ zak_dbt_dbt_parse_gda_op (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) if (xmlStrcmp (cur->name, (const xmlChar *)"serv_op_data") != 0) { g_warning ("Invalid tag \"%s\".", cur->name); - ret = FALSE; + ret = NULL; cur = cur->next; continue; } @@ -752,8 +707,8 @@ zak_dbt_dbt_parse_gda_op (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) if (operation_type == G_MAXINT) { g_warning ("Operation type \"%s\" not supported: %s.", str_operation_type, - error != NULL && error->message != NULL ? error->message : "no detail"); - return FALSE; + error != NULL && error->message != NULL ? error->message : "no detail"); + return NULL; } if (gda_server_provider_supports_operation (priv->gda_provider, priv->gda_conn, operation_type, NULL)) @@ -764,7 +719,7 @@ zak_dbt_dbt_parse_gda_op (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) { g_warning ("Unable to create operation type \"%s\": %s.", str_operation_type, error != NULL && error->message != NULL ? error->message : "no detail"); - ret = FALSE; + ret = NULL; } else { @@ -773,15 +728,18 @@ zak_dbt_dbt_parse_gda_op (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) { g_warning ("Unable to load GdaServerOperation from the file: %s.\n", error != NULL && error->message != NULL ? error->message : "no detail"); - ret = FALSE; + ret = NULL; } else { - if (!gda_server_provider_perform_operation (priv->gda_provider, priv->gda_conn, op, &error)) + error = NULL; + ret = gda_server_provider_render_operation (priv->gda_provider, priv->gda_conn, op, &error); + if (ret == NULL + || error != NULL) { - g_warning ("Error on executing GdaServerOperation from the file: %s.\n", + g_warning ("Error on rendering GdaServerOperation from the file: %s.\n", error != NULL && error->message != NULL ? error->message : "no detail"); - ret = FALSE; + ret = NULL; } } g_object_unref (op); @@ -790,7 +748,7 @@ zak_dbt_dbt_parse_gda_op (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) else { g_warning ("Operation type \"%s\" not supported by the provider.", str_operation_type); - ret = FALSE; + ret = NULL; } cur = cur->next; @@ -799,12 +757,13 @@ 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) +static gchar +*zak_dbt_dbt_parse_create_table (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; + GString *sql; xmlNodePtr cur; gchar *table_name; @@ -812,28 +771,30 @@ zak_dbt_dbt_parse_create_table (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) xmlNode *node; GError *error; + GdaServerOperation *op; - ret = TRUE; + gchar *drop; + + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); table_name = NULL; cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlStrcmp (cur->name, (const xmlChar *)"table_name") == 0) { - table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + table_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } cur = cur->next; } - if (table_name != NULL && g_strcmp0 (table_name, "") != 0) + 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, @@ -845,7 +806,7 @@ zak_dbt_dbt_parse_create_table (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) { g_warning ("Unable to create GdaServerOperation create_table: %s.", error != NULL && error->message != NULL ? error->message : "no detail"); - ret = FALSE; + ret = NULL; } else { @@ -859,16 +820,18 @@ zak_dbt_dbt_parse_create_table (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) 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)) + sql = g_string_new (gda_server_provider_render_operation (priv->gda_provider, priv->gda_conn, op, &error)); + if (sql == NULL + || error != NULL) { g_warning ("Error executing the operation: %s.", error != NULL && error->message != NULL ? error->message : "no detail"); - ret = FALSE; + ret = NULL; } - g_object_unref (op); - - if (ret) + else { + g_object_unref (op); + cur = xnode->children; while (cur != NULL && ret) { @@ -903,116 +866,131 @@ zak_dbt_dbt_parse_create_table (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) xmlNodeSetContent (node, (const xmlChar *)"zdbt_to_be_delete"); xmlAddChild (cur, node); - zak_dbt_dbt_parse_drop_column (zak_dbt_dbt, cur); + drop = zak_dbt_dbt_parse_drop_column (zak_dbt_dbt, cur); + + g_string_append_printf (sql, ";\n%s", drop != NULL ? drop : ""); + + g_free (drop); + + ret = g_strdup (sql->str); } + g_string_free (sql, TRUE); } } else { g_warning ("You must provide the name of the table to create."); - ret = FALSE; + ret = NULL; } + g_free (table_name); + return ret; } -static gboolean -zak_dbt_dbt_parse_drop_table (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_drop_table (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; - xmlNodePtr cur; + xmlNode *cur; gchar *table_name; GError *error; - ret = TRUE; + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); table_name = NULL; cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlStrcmp (cur->name, (const xmlChar *)"table_name") == 0) { - table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + table_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } cur = cur->next; } - if (table_name != NULL && g_strcmp0 (table_name, "") != 0) + if (table_name != NULL + && g_strcmp0 (table_name, "") != 0) { - gchar *sql; - GdaStatement *stmt; - - sql = g_strdup_printf ("DROP TABLE %s", table_name); + GdaServerOperation *op; error = NULL; - stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error); - if (stmt == NULL) + op = gda_server_provider_create_operation (priv->gda_provider, + priv->gda_conn, + GDA_SERVER_OPERATION_DROP_TABLE, + NULL, + &error); + if (op == NULL + || error != NULL) { - g_warning ("Unable to create GdaStatement from sql: %s.\n%s", - error != NULL && error->message != NULL ? error->message : "no detail", - sql); - ret = FALSE; + g_warning ("Unable to create GdaServerOperation create_table: %s.", + error != NULL && error->message != NULL ? error->message : "no detail"); + ret = NULL; } else { - guint nrows; + error = NULL; + gda_server_operation_set_value_at (op, table_name, &error, "/TABLE_DESC_P/TABLE_NAME"); - nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error); - if (nrows == -1) + error = NULL; + ret = gda_server_provider_render_operation (priv->gda_provider, priv->gda_conn, op, &error); + if (ret == NULL + || error != NULL) { - g_warning ("NON SELECT error: %s.\n%s", - error != NULL && error->message != NULL ? error->message : "no detail", - sql); - ret = FALSE; + g_warning ("Error executing the operation: %s.", + error != NULL && error->message != NULL ? error->message : "no detail"); + ret = NULL; } - g_object_unref (stmt); } } else { g_warning ("You must provide the name of the table to drop."); - ret = FALSE; + ret = NULL; } + g_free (table_name); + return ret; } -static gboolean -zak_dbt_dbt_parse_rename_table (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_rename_table (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; - xmlNodePtr cur; + xmlNode *cur; gchar *table_name; gchar *new_table_name; GError *error; + GdaServerOperation *op; - ret = TRUE; + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); table_name = NULL; new_table_name = NULL; cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlStrcmp (cur->name, (const xmlChar *)"table_name") == 0) { - table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + table_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"new_table_name") == 0) { - new_table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + new_table_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } cur = cur->next; @@ -1021,131 +999,140 @@ zak_dbt_dbt_parse_rename_table (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) 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); + error = NULL; gda_server_operation_set_value_at (op, table_name, &error, "/TABLE_DESC_P/TABLE_NAME"); + + error = NULL; 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)) + ret = gda_server_provider_render_operation (priv->gda_provider, priv->gda_conn, op, &error); + if (ret == NULL + || error != NULL) { - g_warning ("Error executing the operation: %s.", - error != NULL && error->message != NULL ? error->message : "No detail"); - ret = FALSE; + g_warning ("Error executing the operation: %s", + error != NULL && error->message != NULL ? error->message : "no detail."); + ret = NULL; } g_object_unref (op); } else { - g_warning ("You must provide the name of the table to drop."); - ret = FALSE; + g_warning ("You must provide the name of the table to rename and the new name."); + ret = NULL; } + g_free (table_name); + g_free (new_table_name); + return ret; } -static gboolean -zak_dbt_dbt_parse_drop_view (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_drop_view (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; - xmlNodePtr cur; + xmlNode *cur; gchar *view_name; GError *error; - ret = TRUE; + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); view_name = NULL; cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlStrcmp (cur->name, (const xmlChar *)"view_name") == 0) { - view_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + view_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } cur = cur->next; } - if (view_name != NULL && g_strcmp0 (view_name, "") != 0) + if (view_name != NULL + && g_strcmp0 (view_name, "") != 0) { - gchar *sql; - GdaStatement *stmt; - - sql = g_strdup_printf ("DROP VIEW %s", view_name); + GdaServerOperation *op; error = NULL; - stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error); - if (stmt == NULL) + op = gda_server_provider_create_operation (priv->gda_provider, + priv->gda_conn, + GDA_SERVER_OPERATION_DROP_VIEW, + NULL, + &error); + if (op == NULL + || error != NULL) { - g_warning ("Unable to create GdaStatement from sql: %s.\n%s", - error != NULL && error->message != NULL ? error->message : "no detail", - sql); - ret = FALSE; + g_warning ("Unable to create GdaServerOperation create_table: %s.", + error != NULL && error->message != NULL ? error->message : "no detail"); + ret = NULL; } else { - guint nrows; + error = NULL; + gda_server_operation_set_value_at (op, view_name, &error, "/VIEW_DESC_P/VIEW_NAME"); - nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error); - if (nrows == -1) + error = NULL; + ret = gda_server_provider_render_operation (priv->gda_provider, priv->gda_conn, op, &error); + if (ret == NULL + || error != NULL) { - g_warning ("NON SELECT error: %s.\n%s", - error != NULL && error->message != NULL ? error->message : "no detail", - sql); - ret = FALSE; + g_warning ("Error executing the operation: %s.", + error != NULL && error->message != NULL ? error->message : "no detail"); + ret = NULL; } - g_object_unref (stmt); } } else { g_warning ("You must provide the name of the view to drop."); - ret = FALSE; + ret = NULL; } return ret; } -static gboolean -zak_dbt_dbt_parse_rename_view (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_rename_view (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; - xmlNodePtr cur; + xmlNode *cur; gchar *view_name; gchar *new_view_name; GError *error; - ret = TRUE; + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); view_name = NULL; cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlStrcmp (cur->name, (const xmlChar *)"view_name") == 0) { - view_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + view_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"new_view_name") == 0) { - new_view_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + new_view_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } cur = cur->next; @@ -1166,19 +1153,16 @@ zak_dbt_dbt_parse_rename_view (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) g_warning ("Unable to create GdaStatement from sql: %s.\n%s", error != NULL && error->message != NULL ? error->message : "no detail", sql); - ret = FALSE; + ret = NULL; } else { - guint nrows; - - nrows = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error); - if (nrows == -1) + if (gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error) == -1) { g_warning ("NON SELECT error: %s.\n%s", error != NULL && error->message != NULL ? error->message : "no detail", sql); - ret = FALSE; + ret = NULL; } g_object_unref (stmt); } @@ -1186,20 +1170,20 @@ zak_dbt_dbt_parse_rename_view (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) else { g_warning ("You must provide the name of the view to drop."); - ret = FALSE; + ret = NULL; } return ret; } -static gboolean -zak_dbt_dbt_parse_create_index (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_create_index (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; - xmlNodePtr cur; + xmlNode *cur; gchar *index_name; gchar *table_name; @@ -1207,7 +1191,7 @@ zak_dbt_dbt_parse_create_index (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) GError *error; - ret = TRUE; + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); @@ -1216,19 +1200,19 @@ zak_dbt_dbt_parse_create_index (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) column_names = NULL; cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlStrcmp (cur->name, (const xmlChar *)"index_name") == 0) { - index_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + index_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"table_name") == 0) { - table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + table_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"column_name") == 0) { - column_names = g_slist_append (column_names, g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur)))); + column_names = g_slist_append (column_names, g_strstrip ((gchar *)xmlNodeGetContent (cur))); } cur = cur->next; @@ -1237,7 +1221,7 @@ zak_dbt_dbt_parse_create_index (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) if (index_name != NULL && g_strcmp0 (index_name, "") != 0 && table_name != NULL && g_strcmp0 (table_name, "") != 0 && column_names != NULL && g_slist_length (column_names) > 0) - {g_debug("%s %s",index_name,table_name); + { GdaServerOperation *op; guint i; @@ -1265,11 +1249,11 @@ zak_dbt_dbt_parse_create_index (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) } error = NULL; -g_debug("%s",gda_server_provider_render_operation(priv->gda_provider, priv->gda_conn, op, &error)); - if (error != NULL) - { - g_warning ("%s", error->message); - } + g_debug("%s",gda_server_provider_render_operation(priv->gda_provider, priv->gda_conn, op, &error)); + if (error != NULL) + { + g_warning ("%s", error->message); + } i = 0; while (column_names != NULL) @@ -1286,55 +1270,57 @@ g_debug("%s",gda_server_provider_render_operation(priv->gda_provider, priv->gda_ } error = NULL; -g_debug("%s",gda_server_provider_render_operation(priv->gda_provider, priv->gda_conn, op, &error)); - if (error != NULL) - { - g_warning ("%s", error->message); - } + g_debug("%s",gda_server_provider_render_operation(priv->gda_provider, priv->gda_conn, op, &error)); + if (error != NULL) + { + g_warning ("%s", error->message); + } error = NULL; - if (!gda_server_provider_perform_operation (priv->gda_provider, priv->gda_conn, op, &error)) + ret = gda_server_provider_render_operation (priv->gda_provider, priv->gda_conn, op, &error); + if (ret == NULL + || error != NULL) { g_warning ("Error executing the operation: %s.", error != NULL && error->message != NULL ? error->message : "no detail"); - ret = FALSE; + ret = NULL; } g_object_unref (op); } else { g_warning ("You must provide the name of the index, the name of the table and one or more column's name."); - ret = FALSE; + ret = NULL; } return ret; } -static gboolean -zak_dbt_dbt_parse_drop_index (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_drop_index (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; - xmlNodePtr cur; + xmlNode *cur; gchar *index_name; GError *error; - ret = TRUE; + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); index_name = NULL; cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlStrcmp (cur->name, (const xmlChar *)"index_name") == 0) { - index_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + index_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } cur = cur->next; @@ -1355,31 +1341,33 @@ zak_dbt_dbt_parse_drop_index (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) gda_server_operation_set_value_at (op, index_name, &error, "/INDEX_DESC_P/INDEX_NAME"); error = NULL; - if (!gda_server_provider_perform_operation (priv->gda_provider, priv->gda_conn, op, &error)) + ret = gda_server_provider_render_operation (priv->gda_provider, priv->gda_conn, op, &error); + if (ret == NULL + || error != NULL) { - g_warning ("Error executing the operation: %s.", - error != NULL && error->message != NULL ? error->message : "No detail"); - ret = FALSE; + g_warning ("Error executing the operation: %s", + error != NULL && error->message != NULL ? error->message : "no detail."); + ret = NULL; } g_object_unref (op); } else { g_warning ("You must provide the name of the index."); - ret = FALSE; + ret = NULL; } return ret; } -static gboolean -zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; - xmlNodePtr cur; + xmlNode *cur; gchar *table_name; gchar *column_name; @@ -1392,7 +1380,7 @@ zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) GError *error; - ret = TRUE; + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); @@ -1406,39 +1394,39 @@ zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) column_default = NULL; cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlStrcmp (cur->name, (const xmlChar *)"table_name") == 0) { - table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + table_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"column_name") == 0) { - column_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + column_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"column_type") == 0) { - column_type = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + column_type = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"column_size") == 0) { - column_size = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + column_size = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"column_not_null") == 0) { - column_not_null = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + column_not_null = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"column_auto_increment") == 0) { - column_auto_increment = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + column_auto_increment = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"column_unique") == 0) { - column_unique = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + column_unique = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"column_default") == 0) { - column_default = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + column_default = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } cur = cur->next; @@ -1462,7 +1450,7 @@ zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) { g_warning ("Unable to create GdaServerOperation add_column: %s.", error != NULL && error->message != NULL ? error->message : "no detail"); - ret = FALSE; + ret = NULL; } else { @@ -1506,11 +1494,13 @@ zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) } error = NULL; - if (!gda_server_provider_perform_operation (priv->gda_provider, priv->gda_conn, op, &error)) + ret = gda_server_provider_render_operation (priv->gda_provider, priv->gda_conn, op, &error); + if (ret == NULL + || error != NULL) { g_warning ("Error executing the operation: %s.", error != NULL && error->message != NULL ? error->message : "No detail"); - ret = FALSE; + ret = NULL; } g_object_unref (op); } @@ -1520,27 +1510,27 @@ zak_dbt_dbt_parse_add_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) g_warning ("You must provide at least:\n" " - the name of the table and the column\n" " - the column's type"); - ret = FALSE; + ret = NULL; } return ret; } -static gboolean -zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) +static gchar +*zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNode *xnode) { ZakDbtDbtPrivate *priv; - gboolean ret; + gchar *ret; - xmlNodePtr cur; + xmlNode *cur; gchar *table_name; gchar *column_name; GError *error; - ret = TRUE; + ret = NULL; priv = ZAK_DBT_DBT_GET_PRIVATE (zak_dbt_dbt); @@ -1548,15 +1538,15 @@ zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) column_name = NULL; cur = xnode->children; - while (cur != NULL && ret) + while (cur != NULL) { if (xmlStrcmp (cur->name, (const xmlChar *)"table_name") == 0) { - table_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + table_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } else if (xmlStrcmp (cur->name, (const xmlChar *)"column_name") == 0) { - column_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + column_name = g_strstrip ((gchar *)xmlNodeGetContent (cur)); } cur = cur->next; @@ -1578,7 +1568,7 @@ zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) { g_warning ("Unable to create GdaServerOperation drop_column: %s.", error != NULL && error->message != NULL ? error->message : "no detail"); - ret = FALSE; + ret = NULL; } else { @@ -1589,11 +1579,13 @@ zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) 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)) + ret = gda_server_provider_render_operation (priv->gda_provider, priv->gda_conn, op, &error); + if (ret == NULL + || error != NULL) { g_warning ("Error executing the operation: %s.", error != NULL && error->message != NULL ? error->message : "no detail"); - ret = FALSE; + ret = NULL; } g_object_unref (op); } @@ -1602,7 +1594,7 @@ zak_dbt_dbt_parse_drop_column (ZakDbtDbt *zak_dbt_dbt, xmlNodePtr xnode) { g_warning ("You must provide at least:\n" " - the name of the table and the column"); - ret = FALSE; + ret = NULL; } return ret; @@ -1626,7 +1618,13 @@ zak_dbt_dbt_transform (ZakDbtDbt *zak_dbt_dbt) gchar *str_id; guint id; - gboolean tosave; + gchar *sql; + + gchar *operation; + xmlBufferPtr xbuf; + GdaBatch *batch; + + const GSList *list; guint i; @@ -1642,6 +1640,12 @@ zak_dbt_dbt_transform (ZakDbtDbt *zak_dbt_dbt) return; } + if (!GDA_IS_SQL_PARSER (priv->gda_parser)) + { + g_warning ("Invalid sql parser."); + return; + } + xpcontext = xmlXPathNewContext (priv->xml_doc); xpcontext->node = priv->xml_root; @@ -1653,8 +1657,6 @@ zak_dbt_dbt_transform (ZakDbtDbt *zak_dbt_dbt) for (i = 0; i < xnodeset->nodeNr; i++) { - tosave = TRUE; - str_id = xmlGetProp (xnodeset->nodeTab[i], (const gchar *)"id"); if (str_id == NULL) { @@ -1682,89 +1684,137 @@ zak_dbt_dbt_transform (ZakDbtDbt *zak_dbt_dbt) } } + sql = NULL; + xnode = xnodeset->nodeTab[i]->children; while (xnode != NULL) { if (!xmlNodeIsText (xnode)) { + sql = NULL; + if (xmlStrcmp (xnode->name, (const xmlChar *)"sql") == 0) { - tosave = zak_dbt_dbt_parse_sql (zak_dbt_dbt, xnode); + sql = zak_dbt_dbt_parse_sql (zak_dbt_dbt, xnode); } else if (xmlStrcmp (xnode->name, (const xmlChar *)"gda_op") == 0) { - tosave = zak_dbt_dbt_parse_gda_op (zak_dbt_dbt, xnode); + sql = 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); + sql = 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); + sql = zak_dbt_dbt_parse_drop_table (zak_dbt_dbt, xnode); } else if (xmlStrcmp (xnode->name, (const xmlChar *)"rename_table") == 0) { - tosave = zak_dbt_dbt_parse_rename_table (zak_dbt_dbt, xnode); + sql = zak_dbt_dbt_parse_rename_table (zak_dbt_dbt, xnode); } else if (xmlStrcmp (xnode->name, (const xmlChar *)"drop_view") == 0) { - tosave = zak_dbt_dbt_parse_drop_view (zak_dbt_dbt, xnode); + sql = zak_dbt_dbt_parse_drop_view (zak_dbt_dbt, xnode); } else if (xmlStrcmp (xnode->name, (const xmlChar *)"rename_view") == 0) { - tosave = zak_dbt_dbt_parse_rename_view (zak_dbt_dbt, xnode); + sql = zak_dbt_dbt_parse_rename_view (zak_dbt_dbt, xnode); } else if (xmlStrcmp (xnode->name, (const xmlChar *)"create_index") == 0) { - tosave = zak_dbt_dbt_parse_create_index (zak_dbt_dbt, xnode); + sql = zak_dbt_dbt_parse_create_index (zak_dbt_dbt, xnode); } else if (xmlStrcmp (xnode->name, (const xmlChar *)"drop_index") == 0) { - tosave = zak_dbt_dbt_parse_drop_index (zak_dbt_dbt, xnode); + sql = 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); + sql = 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); + sql = zak_dbt_dbt_parse_drop_column (zak_dbt_dbt, xnode); } else { g_warning ("Invalid tag: %s.", xnode->name); - tosave = FALSE; + sql = NULL; + } + + if (sql != NULL) + { + /* execute the transformation */ + error = NULL; + batch = gda_sql_parser_parse_string_as_batch (priv->gda_parser, sql, NULL, &error); + if (batch == NULL) + { + g_warning ("Unable to create batch of GdaStatement's from sql: %s.\n%s", + error != NULL && error->message != NULL ? error->message : "no detail", + sql); + g_free (sql); + sql = NULL; + break; + } + else + { + for (list = gda_batch_get_statements (batch); list; list = list->next) + { + if (gda_connection_statement_execute_non_select (priv->gda_conn, GDA_STATEMENT (list->data), NULL, NULL, &error) == -1) + { + g_warning ("NON SELECT error: %s.\n%s", + error != NULL && error->message != NULL ? error->message : "no detail", + sql); + g_free (sql); + sql = NULL; + break; + } + } + } } } xnode = xnode->next; } - if (tosave) + if (sql != NULL) { - gchar *operation; - xmlBufferPtr xbuf; - + /* saving transformation in db */ xbuf = xmlBufferCreate (); xmlNodeDump (xbuf, priv->xml_doc, xnodeset->nodeTab[i], 0, 0); operation = g_strstrip (g_strdup (xbuf->content)); - tosave = zak_dbt_dbt_save_transformation (zak_dbt_dbt, id, g_strstrip (operation)); - } - - /* ending transaction */ - if (gda_connection_supports_feature (priv->gda_conn, GDA_CONNECTION_FEATURE_TRANSACTIONS)) - { - error = NULL; - if (tosave) + if (!zak_dbt_dbt_save_transformation (zak_dbt_dbt, id, g_strstrip (operation))) { - gda_connection_commit_transaction (priv->gda_conn, NULL, &error); + /* ending transaction */ + if (gda_connection_supports_feature (priv->gda_conn, GDA_CONNECTION_FEATURE_TRANSACTIONS)) + { + error = NULL; + gda_connection_rollback_transaction (priv->gda_conn, NULL, &error); + } } else { + /* ending transaction */ + if (gda_connection_supports_feature (priv->gda_conn, GDA_CONNECTION_FEATURE_TRANSACTIONS)) + { + error = NULL; + gda_connection_commit_transaction (priv->gda_conn, NULL, &error); + } + } + } + else + { + /* ending transaction */ + if (gda_connection_supports_feature (priv->gda_conn, GDA_CONNECTION_FEATURE_TRANSACTIONS)) + { + error = NULL; gda_connection_rollback_transaction (priv->gda_conn, NULL, &error); } } + + g_free (sql); + sql = NULL; } } }