[glom/glom-1-18] Deleting tables: Remove the auto-increment too.



commit 4e8aa3129a2864a999f739aa0eac080107ee9564
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Oct 13 19:55:48 2011 +0200

    Deleting tables: Remove the auto-increment too.
    
    	* glom/libglom/db_utils.[h|cc]: Added remove_auto_increment().
    	* glom/navigation/box_tables.cc: on_adddel_Delete(): Remove the primary key's
    	autoincrement too.
    	Bug #661653
    
    Conflicts:
    
    	glom/libglom/db_utils.h
    	glom/navigation/box_tables.cc

 ChangeLog                     |    9 +++++++++
 glom/libglom/db_utils.cc      |   24 ++++++++++++++++++++++++
 glom/libglom/db_utils.h       |    4 +++-
 glom/navigation/box_tables.cc |   23 ++++++++++++++++++++---
 4 files changed, 56 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 2852d7a..898509c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2011-10-13  Murray Cumming  <murrayc murrayc com>
 
+	Deleting tables: Remove the auto-increment too.
+
+	* glom/libglom/db_utils.[h|cc]: Added remove_auto_increment().
+	* glom/navigation/box_tables.cc: on_adddel_Delete(): Remove the primary key's
+	autoincrement too.
+	Bug #661653 
+
+2011-10-13  Murray Cumming  <murrayc murrayc com>
+
 	DbUtils: Add some parameter checks to the autoincrement methods.
 
 	* glom/libglom/db_utils.cc: Otherwise we risk having an empty where clause,
diff --git a/glom/libglom/db_utils.cc b/glom/libglom/db_utils.cc
index a4064ef..e094c61 100644
--- a/glom/libglom/db_utils.cc
+++ b/glom/libglom/db_utils.cc
@@ -1558,6 +1558,30 @@ static void recalculate_next_auto_increment_value(const Glib::ustring& table_nam
     std::cerr << "recalculate_next_auto_increment_value(): SELECT MAX() failed." << std::endl;
 }
 
+void remove_auto_increment(const Glib::ustring& table_name, const Glib::ustring& field_name)
+{
+  if(table_name.empty())
+  {
+    std::cerr << G_STRFUNC << ": table_name is empty" << std::endl;
+    return;
+  }
+  
+  if(field_name.empty())
+  {
+    std::cerr << G_STRFUNC << ": field_name is empty" << std::endl;
+    return;
+  }
+  
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> builder =
+    Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_DELETE);
+  builder->set_table(GLOM_STANDARD_TABLE_AUTOINCREMENTS_TABLE_NAME);
+  builder_set_where_autoincrement(builder, table_name, field_name);
+
+  const bool test = query_execute(builder);
+  if(!test)
+    std::cerr << G_STRFUNC << ": UPDATE failed." << std::endl;
+}
+
 bool insert_example_data(Document* document, const Glib::ustring& table_name)
 {
   //TODO_Performance: Avoid copying:
diff --git a/glom/libglom/db_utils.h b/glom/libglom/db_utils.h
index 66e72b3..804cc45 100644
--- a/glom/libglom/db_utils.h
+++ b/glom/libglom/db_utils.h
@@ -103,7 +103,9 @@ Gnome::Gda::Value auto_increment_insert_first_if_necessary(const Glib::ustring&
   */
 Gnome::Gda::Value get_next_auto_increment_value(const Glib::ustring& table_name, const Glib::ustring& field_name);
 
-
+/** Use this, for instance, when deleting a table.
+ */
+void remove_auto_increment(const Glib::ustring& table_name, const Glib::ustring& field_name);
 
 //TODO: It would be nice to use sharedptr<const Relationship>& instead of sharedptr<Relationship>&,
 //but it does not seem possible to pass a sharedptr<const Relationship> for a sharedptr<const Relationship>&.
diff --git a/glom/navigation/box_tables.cc b/glom/navigation/box_tables.cc
index a79bccc..e53eb9c 100644
--- a/glom/navigation/box_tables.cc
+++ b/glom/navigation/box_tables.cc
@@ -288,7 +288,7 @@ void Box_Tables::on_adddel_Delete(const Gtk::TreeModel::iterator& rowStart, cons
   bool something_changed = false;
   for(Gtk::TreeModel::iterator iter = rowStart; iter != iterAfter; ++iter)
   {
-    Glib::ustring table_name = m_AddDel.get_value_key(iter);
+    const Glib::ustring table_name = m_AddDel.get_value_key(iter);
 
     if(!table_name.empty())
     {
@@ -310,7 +310,12 @@ void Box_Tables::on_adddel_Delete(const Gtk::TreeModel::iterator& rowStart, cons
           Gtk::MessageDialog dialog(Utils::bold_message(_("Delete Table")), true);
           dialog.set_secondary_text(strMsg);
           dialog.set_transient_for(*Application::get_application());
-          int iButtonClicked = dialog.run();
+          const int iButtonClicked = dialog.run();
+          
+          Glib::ustring primary_key_field_name; 
+          const sharedptr<const Field> primary_key_field = get_field_primary_key_for_table(table_name);
+          if(primary_key_field)
+            primary_key_field_name = primary_key_field->get_name();
 
           //Delete the table:
           if(iButtonClicked == Gtk::RESPONSE_OK)
@@ -320,7 +325,19 @@ void Box_Tables::on_adddel_Delete(const Gtk::TreeModel::iterator& rowStart, cons
               std::cerr << "Box_Tables::on_adddel_Delete(): DROP TABLE failed." << std::endl;
             else
             {
-              get_document()->remove_table(table_name); //Forget about it in the document too.
+              //Remove the auto-increment row.
+              //Otherwise it would not start at 0 if a table with the same name is added again later.
+              //TODO: Remove rows for other auto-increment fields too:
+              if(!primary_key_field_name.empty())
+                DbUtils::remove_auto_increment(table_name, primary_key_field_name);
+              else
+              {
+                std::cerr << G_STRFUNC << ": primary_key_field_name is empty" << std::endl;
+              }
+              
+              //Forget about it in the document too.
+              get_document()->remove_table(table_name);
+              
               something_changed = true;
             }
           }



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