[glom] ConnectionPool::change_columns(): Add/Remove autoincement rows here.



commit ba1dc3bd2c1e65708f47560a842dcfd412a2459e
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Mar 1 10:43:00 2012 +0100

    ConnectionPool::change_columns(): Add/Remove autoincement rows here.
    
    * glom/libglom/connectionpool.cc: change_columns(): Add or remove the
    auto-increment row from the database preferences table. This would
    be added automatically anyway but this makes sure that it is there
    for editing as soon as the field is added, and makes sure that it is
    removed.
    * tests/test_selfhosting_new_then_change_columns.cc: Try setting a
    field as auto-increment.

 ChangeLog                                         |   12 +++++++
 glom/libglom/connectionpool.cc                    |   25 ++++++++++++++
 tests/test_selfhosting_new_then_change_columns.cc |   36 ++++++++++++++++++++-
 3 files changed, 72 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 6c8aae2..2616b74 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2012-03-01  Murray Cumming  <murrayc murrayc com>
 
+	ConnectionPool::change_columns(): Add/Remove autoincement rows here.
+
+	* glom/libglom/connectionpool.cc: change_columns(): Add or remove the
+	auto-increment row from the database preferences table. This would 
+	be added automatically anyway but this makes sure that it is there
+	for editing as soon as the field is added, and makes sure that it is 
+	removed.
+	* tests/test_selfhosting_new_then_change_columns.cc: Try setting a 
+	field as auto-increment.
+
+2012-03-01  Murray Cumming  <murrayc murrayc com>
+
 	Tests: Update the expected number of locales.
 
 	* tests/test_document_load_translations.cc: There are now 7 locales in the
diff --git a/glom/libglom/connectionpool.cc b/glom/libglom/connectionpool.cc
index eaa2834..4c97445 100644
--- a/glom/libglom/connectionpool.cc
+++ b/glom/libglom/connectionpool.cc
@@ -23,6 +23,7 @@
 #include <libglom/connectionpool.h>
 #include <libglom/document/document.h>
 #include <libglom/utils.h>
+#include <libglom/db_utils.h>
 //#include <libgdamm/connectionevent.h>
 
 #include <libglom/connectionpool_backends/postgres_central.h>
@@ -762,7 +763,31 @@ bool ConnectionPool::change_columns(const Glib::ustring& table_name, const type_
 
   const bool result = m_backend->change_columns(m_refGdaConnection, table_name, old_fields, new_fields);
   m_refGdaConnection->update_meta_store_table(table_name, m_backend->get_public_schema_name());
+  if(!result)
+    return false;
 
+  //Add or remove any auto-increment rows:
+  //The new auto-increment row would actually be added automatically,
+  //but this makes it available even before a record has been added. 
+  type_vec_const_fields::const_iterator iter_old = old_fields.begin();
+  type_vec_const_fields::const_iterator iter_new = new_fields.begin();
+  while( (iter_old != old_fields.end()) && (iter_new != new_fields.end()) )
+  {
+    const sharedptr<const Field> field_old = *iter_old;
+    const sharedptr<const Field> field_new = *iter_new;
+    if(field_old && field_new
+      && field_old->get_auto_increment() != field_new->get_auto_increment())
+    {
+      if(field_new->get_auto_increment())
+        DbUtils::auto_increment_insert_first_if_necessary(table_name, field_new->get_name());
+      else
+        DbUtils::remove_auto_increment(table_name, field_new->get_name());
+    }
+
+    ++iter_old;
+    ++iter_new;
+  }
+ 
   return result;
 }
 
diff --git a/tests/test_selfhosting_new_then_change_columns.cc b/tests/test_selfhosting_new_then_change_columns.cc
index e333ed2..00699fe 100644
--- a/tests/test_selfhosting_new_then_change_columns.cc
+++ b/tests/test_selfhosting_new_then_change_columns.cc
@@ -22,6 +22,7 @@
 #include <libglom/init.h>
 #include <libglom/utils.h>
 #include <libglom/db_utils.h>
+#include <libglom/data_structure/glomconversions.h>
 #include <libglom/connectionpool.h>
 #include <glibmm/fileutils.h>
 #include <glibmm/miscutils.h>
@@ -44,7 +45,7 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   
   const Glib::ustring table_name = "contacts";
   const Glib::ustring field_name_original = "date_of_birth";
-  const Glom::sharedptr<const Glom::Field> field = document.get_field(table_name, field_name_original);
+  Glom::sharedptr<const Glom::Field> field = document.get_field(table_name, field_name_original);
   if(!field)
   {
     std::cerr << "Failure: Could not get field." << std::endl;
@@ -85,6 +86,7 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   }
 
   //Try another change:
+  field = Glom::glom_sharedptr_clone(field_new);
   field_new->set_glom_type(Glom::Field::TYPE_NUMERIC);
   try
   {
@@ -102,6 +104,7 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   }
 
   //Try another change:
+  field = Glom::glom_sharedptr_clone(field_new);
   field_new->set_name("somenewfieldname");
   try
   {
@@ -117,6 +120,37 @@ static bool test(Glom::Document::HostingMode hosting_mode)
     std::cerr << "Failure: change_column() threw an exception: " << ex.what() << std::endl;
     return false;
   }
+
+  //Try to make it auto-increment:
+  field = Glom::glom_sharedptr_clone(field_new);
+  field_new->set_auto_increment();
+  try
+  {
+    const bool test = connection_pool->change_column(table_name, field, field_new);
+    if(!test)
+    {
+      std::cerr << "Failure: change_column() failed." << std::endl;
+      return false;
+    }
+  }
+  catch(const Glib::Error& ex)
+  { 
+    std::cerr << "Failure: change_column() threw an exception: " << ex.what() << std::endl;
+    return false;
+  }
+
+  //Check that the auto-increment works:
+  //Actually checking for a 0 here is not very useful,
+  //but at least we are running some of the relevant code:
+  const Gnome::Gda::Value value_next = 
+    Glom::DbUtils::get_next_auto_increment_value(table_name, field_new->get_name());
+  const double value_next_as_double = Glom::Conversions::get_double_for_gda_value_numeric(value_next);
+  if(value_next_as_double != 0)
+  {
+    std::cerr << "Failure: The next auto-increment value is not 0 as expected. Instead it is: " << value_next_as_double << std::endl;
+    return false;
+  }
+
   
   //Add a field:
   try



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