[libgda: 5/17] DB: New API and minor corrections



commit 2ff089f8178da2a7c782fd3be648d45fe008bc19
Author: Pavlo Solntsev <p sun fun gmail com>
Date:   Mon Mar 18 22:18:20 2019 -0500

    DB: New API and minor corrections
    
     New API was added: gda_db_table_rename and gda_db_table_add_column

 libgda/gda-db-table.c | 195 +++++++++++++++++++++++++++++++++++++++++++-------
 libgda/gda-db-table.h |  13 +++-
 2 files changed, 180 insertions(+), 28 deletions(-)
---
diff --git a/libgda/gda-db-table.c b/libgda/gda-db-table.c
index 0fd1d7b2a..018a4346c 100644
--- a/libgda/gda-db-table.c
+++ b/libgda/gda-db-table.c
@@ -667,7 +667,7 @@ gda_db_table_update (GdaDbTable *self,
       else
         newcolumncount++; /* We need to count new column. See below */
 
-      if(!gda_db_column_prepare_add (it->data, op, error))
+      if(!gda_db_column_prepare_add (GDA_DB_COLUMN(it->data), op, error))
         {
           g_slist_free (res);
           goto on_error;
@@ -708,18 +708,10 @@ gda_db_table_create (GdaDbTable *self,
                      gboolean ifnotexists,
                      GError **error)
 {
-  g_return_val_if_fail (self, FALSE);
-  g_return_val_if_fail (cnc, FALSE);
+  g_return_val_if_fail (GDA_IS_DB_TABLE (self), FALSE);
+  g_return_val_if_fail (GDA_IS_CONNECTION (cnc) && gda_connection_is_opened (cnc), FALSE);
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
- if (!gda_connection_is_opened (cnc))
-    {
-      g_set_error (error,
-                   GDA_DB_CATALOG_ERROR,
-                   GDA_DB_CATALOG_CONNECTION_CLOSED,
-                   _("Connection is not opened"));
-      return FALSE;
-    }
   gda_lockable_lock (GDA_LOCKABLE(cnc));
 
   GdaServerProvider *provider = NULL;
@@ -733,13 +725,13 @@ gda_db_table_create (GdaDbTable *self,
                                              GDA_SERVER_OPERATION_CREATE_TABLE,
                                              NULL,
                                              error);
-  if (op) 
+  if (op)
     {
       g_object_set_data_full (G_OBJECT (op), "connection", g_object_ref (cnc), g_object_unref);
 
-      if (gda_db_table_prepare_create (self, op, ifnotexists, error)) 
+      if (gda_db_table_prepare_create (self, op, ifnotexists, error))
         {
-#ifdef GDA_DEBUG_NO
+#ifdef GDA_DEBUG
           gchar* str = gda_server_operation_render (op, error);
           g_message ("Operation: %s", str);
           g_free (str);
@@ -795,22 +787,175 @@ gda_db_table_append_fkey (GdaDbTable *self,
 }
 
 /**
- * gda_db_table_append_constraint:
- * @self: a #GdaDbTable instance
- * @constr: a constraint string to append
+ * gda_db_table_rename:
+ * @old_name: The originam table to rename.
+ * @new_name: The new table to rename to
+ * @cnc: Connection to use
+ * @error: An error holder
  *
- * Append constraint expresion @constr to the table. This expresion will be used to create the table
- * in the format:
+ * This method performs rename operation on the table
+ */
+gboolean
+gda_db_table_rename (GdaDbTable *old_name,
+                     GdaDbTable *new_name,
+                     GdaConnection *cnc,
+                     GError **error)
+{
+  G_DEBUG_HERE();
+  GdaServerOperation *op = NULL;
+  GdaServerProvider *provider = NULL;
+
+  g_return_val_if_fail(GDA_IS_DB_TABLE (old_name), FALSE);
+  g_return_val_if_fail(GDA_IS_DB_TABLE (old_name), FALSE);
+  g_return_val_if_fail(GDA_IS_CONNECTION (cnc), FALSE);
+  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+  if (!gda_connection_is_opened (cnc))
+    {
+      g_warning ("Connection is not opened");
+      return FALSE;
+    }
+
+  gda_lockable_lock (GDA_LOCKABLE (cnc));
+  provider = gda_connection_get_provider (cnc);
+
+  op = gda_server_provider_create_operation (provider, cnc, GDA_SERVER_OPERATION_RENAME_TABLE,
+                                             NULL, error);
+
+  if (!op)
+    goto on_error;
+
+  if (!gda_server_operation_set_value_at (op, gda_db_base_get_full_name (GDA_DB_BASE (old_name)),
+                                          error, "/TABLE_DESC_P/TABLE_NAME"))
+    goto on_error;
+
+  if (!gda_server_operation_set_value_at (op, gda_db_base_get_full_name (GDA_DB_BASE (new_name)),
+                                          error, "/TABLE_DESC_P/TABLE_NEW_NAME"))
+    goto on_error;
+
+  if (!gda_server_provider_perform_operation (provider, cnc, op, error))
+    goto on_error;
+
+  g_object_unref (op);
+
+  gda_lockable_unlock (GDA_LOCKABLE (cnc));
+
+  return TRUE;
+
+on_error:
+  if (op)
+    g_object_unref (op);
+
+  gda_lockable_unlock (GDA_LOCKABLE (cnc));
+
+  return FALSE;
+}
+
+/**
+ * gda_db_table_add_column:
+ * @self: an instance of #GdaDbTable where table should be added
+ * @col: a column to add
+ * @cnc: an opened connection to use
+ * @error: An error container
+ *
+ * This is a convenient method to add a column @col to the table @self.
  *
- * CREATE_TABLE &lt;table name&gt; (&lt;column description&gt;, CONSTRAINT @constr);
+ * Returns: %TRUE if no error occures and %FALSE otherwise
  *
  * Since: 6.0
  */
-void
-gda_db_table_append_constraint (GdaDbTable *self,
-                                const gchar *constr)
+gboolean
+gda_db_table_add_column (GdaDbTable *self,
+                         GdaDbColumn *col,
+                         GdaConnection *cnc,
+                         GError **error)
 {
-  GdaDbTablePrivate *priv = gda_db_table_get_instance_private (self);
+  G_DEBUG_HERE();
+  GdaServerOperation *op = NULL;
+  GdaServerProvider *provider = NULL;
+  gchar *buffer_str = NULL;
+
+  g_return_val_if_fail(GDA_IS_DB_TABLE (self), FALSE);
+  g_return_val_if_fail(GDA_IS_DB_COLUMN (col), FALSE);
+  g_return_val_if_fail(GDA_IS_CONNECTION (cnc), FALSE);
+  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+  if (!gda_connection_is_opened (cnc))
+    {
+      g_warning ("Connection is not opened");
+      return FALSE;
+    }
+
+  gda_lockable_lock (GDA_LOCKABLE (cnc));
+
+  provider = gda_connection_get_provider (cnc);
+
+  op = gda_server_provider_create_operation (provider, cnc, GDA_SERVER_OPERATION_ADD_COLUMN,
+                                             NULL, error);
+
+  if (!op)
+    {
+      g_warning("ServerOperation is NULL\n");
+      goto on_error;
+    }
+
+  if (!gda_server_operation_set_value_at (op, gda_db_base_get_full_name (GDA_DB_BASE (self)),
+                                          error, "/COLUMN_DEF_P/TABLE_NAME"))
+    goto on_error;
+
+  if (!gda_server_operation_set_value_at (op, gda_db_column_get_name (col),
+                                          error, "/COLUMN_DEF_P/COLUMN_NAME"))
+    goto on_error;
+
+  if (!gda_server_operation_set_value_at (op, gda_db_column_get_ctype(col),
+                                          error, "/COLUMN_DEF_P/COLUMN_TYPE"))
+    goto on_error;
+
+  guint size = gda_db_column_get_size (col);
 
-  priv->mp_constraint = g_slist_append (priv->mp_constraint, g_strdup (constr));
+  buffer_str = g_strdup_printf("%d", size);
+
+  if (!gda_server_operation_set_value_at (op, buffer_str,
+                                          error, "/COLUMN_DEF_P/COLUMN_SIZE"))
+    goto on_error;
+
+  g_free (buffer_str);
+  buffer_str = NULL;
+
+  guint scale = gda_db_column_get_scale (col);
+
+  buffer_str = g_strdup_printf("%d", scale);
+
+  if (!gda_server_operation_set_value_at (op, buffer_str,
+                                          error, "/COLUMN_DEF_P/COLUMN_SCALE"))
+    goto on_error;
+
+  g_free (buffer_str);
+  buffer_str = NULL;
+
+  if (!gda_server_operation_set_value_at (op, GDA_BOOL_TO_STR (gda_db_column_get_nnul (col)),
+                                          error, "/COLUMN_DEF_P/COLUMN_NNUL"))
+    goto on_error;
+
+  if (!gda_server_provider_perform_operation (provider, cnc, op, error))
+    goto on_error;
+
+  g_object_unref (op);
+
+  gda_lockable_unlock (GDA_LOCKABLE (cnc));
+
+  return TRUE;
+
+on_error:
+  if (op)
+    g_object_unref (op);
+
+  if (buffer_str)
+    g_free (buffer_str);
+
+  gda_lockable_unlock (GDA_LOCKABLE (cnc));
+
+  return FALSE;
 }
+
+G_END_DECLS
diff --git a/libgda/gda-db-table.h b/libgda/gda-db-table.h
index 7821ffdfb..f95bb964a 100644
--- a/libgda/gda-db-table.h
+++ b/libgda/gda-db-table.h
@@ -21,7 +21,7 @@
 #define __GDA_DB_TABLE_H__
 
 #include "gda-db-base.h"
-#include "gda-db-column.h" 
+#include "gda-db-column.h"
 #include "gda-db-fkey.h"
 #include <libxml/parser.h>
 #include <libxml/xmlwriter.h>
@@ -81,8 +81,15 @@ gboolean        gda_db_table_create          (GdaDbTable *self,
 void            gda_db_table_append_fkey (GdaDbTable *self,
                                           GdaDbFkey *fkey);
 
-void            gda_db_table_append_constraint (GdaDbTable *self,
-                                                const gchar *constr);
+gboolean        gda_db_table_rename      (GdaDbTable *old_name,
+                                          GdaDbTable *new_name,
+                                          GdaConnection *cnc,
+                                          GError **error);
+
+gboolean        gda_db_table_add_column (GdaDbTable *self,
+                                         GdaDbColumn *col,
+                                         GdaConnection *cnc,
+                                         GError **error);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]