From 801f60807a01631cef7855949b09172cbca1887d Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 2 Oct 2010 17:42:57 +0200 Subject: [PATCH] Added create_index and drop_index (but create_index doesn't work). --- docs/dbtransformer.dtd | 26 ++++-- src/dbtransformer.c | 188 +++++++++++++++++++++++++++++++++++++++++ tests/test.db | Bin 7168 -> 8192 bytes tests/test.xml | 24 +++++- 4 files changed, 227 insertions(+), 11 deletions(-) diff --git a/docs/dbtransformer.dtd b/docs/dbtransformer.dtd index 8add38a..3f5feca 100644 --- a/docs/dbtransformer.dtd +++ b/docs/dbtransformer.dtd @@ -1,33 +1,43 @@ - + - + rename_view*, + create_index*, + drop_index*)> - + + - + - + - + + + + + + + - + diff --git a/src/dbtransformer.c b/src/dbtransformer.c index 2133345..7173235 100644 --- a/src/dbtransformer.c +++ b/src/dbtransformer.c @@ -1071,6 +1071,186 @@ dbt_parse_rename_view (Dbt *dbt, xmlNodePtr xnode) return ret; } +static gboolean +dbt_parse_create_index (Dbt *dbt, xmlNodePtr xnode) +{ + DbtPrivate *priv; + + gboolean ret; + + xmlNodePtr cur; + + gchar *index_name; + gchar *table_name; + GSList *column_names; + + GError *error; + + ret = TRUE; + + priv = DBT_GET_PRIVATE (dbt); + + index_name = NULL; + table_name = NULL; + column_names = NULL; + + cur = xnode->children; + while (cur != NULL && ret) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"index_name") == 0) + { + index_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + } + else 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_names = g_slist_append (column_names, g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur)))); + } + + cur = cur->next; + } + + 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; + + error = NULL; + op = gda_server_provider_create_operation (priv->gda_provider, + priv->gda_conn, + GDA_SERVER_OPERATION_CREATE_INDEX, + NULL, + &error); + + error = NULL; + gda_server_operation_set_value_at (op, index_name, &error, "/INDEX_DESC_P/INDEX_NAME"); + if (error != NULL) + { + g_warning ("%s", error->message); + return FALSE; + } + + error = NULL; + gda_server_operation_set_value_at (op, table_name, &error, "/INDEX_DESC_P/INDEX_ON_TABLE"); + if (error != NULL) + { + g_warning ("%s", error->message); + return FALSE; + } + + 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); + } + + i = 0; + while (column_names != NULL) + { + error = NULL; + gda_server_operation_set_value_at (op, (gchar *)column_names->data, &error, "/INDEX_FIELDS_S/%d/INDEX_FIELD", i); + if (error != NULL) + { + g_warning ("%s", error->message); + } + + i++; + column_names = g_slist_next (column_names); + } + + 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); + } + + 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 index, the name of the table and one or more column's name."); + ret = FALSE; + } + + return ret; +} + +static gboolean +dbt_parse_drop_index (Dbt *dbt, xmlNodePtr xnode) +{ + DbtPrivate *priv; + + gboolean ret; + + xmlNodePtr cur; + + gchar *index_name; + + GError *error; + + ret = TRUE; + + priv = DBT_GET_PRIVATE (dbt); + + index_name = NULL; + + cur = xnode->children; + while (cur != NULL && ret) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"index_name") == 0) + { + index_name = g_strstrip (g_strdup ((gchar *)xmlNodeGetContent (cur))); + } + + cur = cur->next; + } + + if (index_name != NULL && g_strcmp0 (index_name, "") != 0) + { + GdaServerOperation *op; + + error = NULL; + op = gda_server_provider_create_operation (priv->gda_provider, + priv->gda_conn, + GDA_SERVER_OPERATION_DROP_INDEX, + NULL, + &error); + + error = NULL; + 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)) + { + 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 index."); + ret = FALSE; + } + + return ret; +} + /** * dbt_transform: * @dbt: @@ -1174,6 +1354,14 @@ dbt_transform (Dbt *dbt) { tosave = dbt_parse_rename_view (dbt, xnode); } + else if (xmlStrcmp (xnode->name, (const xmlChar *)"create_index") == 0) + { + tosave = dbt_parse_create_index (dbt, xnode); + } + else if (xmlStrcmp (xnode->name, (const xmlChar *)"drop_index") == 0) + { + tosave = dbt_parse_drop_index (dbt, xnode); + } else { g_warning ("Invalid tag: %s.", xnode->name); diff --git a/tests/test.db b/tests/test.db index 9a42333f759fa51e4a707e3e860b332abd51d08b..5027352d21c0d07a8bd3ff3588a961908e53bdbd 100644 GIT binary patch delta 550 zcmZp$XmFSyEjXEhfq?^v*`Rp&L>*(v$qc%WqJF@6C-rj?TgS?)~^ zV>Fd!@?*%9lw@qwEJ;ktNzE@xNi8adknEG=nH&X6@=FqP6jJg_fwG!FbNnX1WmIHj znas~*F7XAVkr%5*`(U7A1}{}<#`w&jEU zGO=+ITNN7%Lu2J+K~Av*GbKANPEMPYqWl7gD?nUM8?b}o^AdAY?T~G?(MOU1>C;Ei z2;?S}6eZ>rr{x#rCYEI8=hMaj*!2BtVKl S%0USLr05BfBA{ZPKgYdXEf2%QE>M63l0f#^z;i+$S+DsEh>&L$WEG zzlMoje6t(#E*1f>Cz*c(J;uP!{F{**=%L@66*$i`awm{W^FEA!Q + + orders_primary + orders + id + id_client + + + + + + clients_pkey + + + + - CREATE TABLE clients (id integer, name varchar(50), CONSTRAINT clients_pkey PRIMARY KEY (id)) + CREATE TABLE clients (id integer, name varchar(50)) + + + CREATE INDEX clients_pkey ON clients (id) - CREATE TABLE orders (id integer, id_client integer, "date" date, CONSTRAINT orders_pkey PRIMARY KEY (id)) + CREATE TABLE orders (id integer, id_client integer, "date" date) -- 2.49.0