]> saetta.ns0.it Git - libzakdbt/commitdiff
Added transformation add_column.
authorAndrea Zagli <azagli@libero.it>
Sun, 21 Jan 2018 10:09:45 +0000 (11:09 +0100)
committerAndrea Zagli <azagli@libero.it>
Sun, 21 Jan 2018 10:09:45 +0000 (11:09 +0100)
docs/zakdbt.dtd
src/dbt.c
tests/test.xml

index 3f5feca6110e506a9ae88c98d2a43d3169b66fd6..deaa668c459096385e7857d1df8499474115ff83 100644 (file)
@@ -8,7 +8,8 @@
                           drop_view*,
                           rename_view*,
                           create_index*,
-                          drop_index*)>
+                          drop_index*,
+                          add_column*)>
 
 <!ATTLIST dbtransformation
        id CDATA #REQUIRED
 <!ELEMENT column_name (#PCDATA)>
 
 <!ELEMENT drop_index (index_name)>
+
+<!ELEMENT add_column (table_name, column_name, column_type, column_size, column_not_null, column_auto_increment, column_unique, column_default)>
+
+<!ELEMENT column_type (#PCDATA)>
+<!ELEMENT column_size (#PCDATA)>
+<!ELEMENT column_not_null (#PCDATA)>
+<!ELEMENT column_auto_increment (#PCDATA)>
+<!ELEMENT column_unique (#PCDATA)>
+<!ELEMENT column_default (#PCDATA)>
index 2d1e5dbc5695abd503e37cec1d7a51059cae889f..dec304bbb2eaeb5d784b3b3fdae8c6f5ad42e054 100644 (file)
--- 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);
index d748b271e51717f3653be99f56dfd26b76325e4c..a5c78eb2075b6b8f579dd815338e8c65179634c4 100644 (file)
                </gda_op>
        </dbtransformation>
 
+       <dbtransformation id="8">
+               <add_column>
+                       <table_name>newtablename</table_name>
+                       <column_name>new_col</column_name>
+                       <column_type>varchar</column_type>
+                       <column_size>255</column_size>
+                       <column_not_null>TRUE</column_not_null>
+                       <column_default>''</column_default>
+               </add_column>
+       </dbtransformation>
+
 </dbtransformer>