[glom/modification: 23/33] Tables now have creation and modification fields, appropriately set.



commit 2931e18ea2611e702b92583b0e8717525756daa5
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu May 7 20:51:13 2009 +0200

    Tables now have creation and modification fields, appropriately set.
    
    	* glom/base_db.[h|cc]: Added static m_extra_modification_field_values,
    	to store the field information for the extra modification fields, filled in
    	init_extra_modification_fields.
    	set_field_value_in_database(): Also set the extra modification field
    	values.
    	* glom/base_db_table_data.cc: record_new(): Set values for the extra
    	creation fields, and show them if they are on the layout.
---
 ChangeLog                                   |   12 +++++++
 glom/base_db.cc                             |   42 +++++++++++++++++---------
 glom/base_db.h                              |    4 +-
 glom/base_db_table_data.cc                  |   28 +++++++++++++++--
 glom/utility_widgets/db_adddel/db_adddel.cc |    2 +-
 5 files changed, 66 insertions(+), 22 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 897a26d..6345249 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -162,6 +162,18 @@
 
 2009-05-07  Murray Cumming  <murrayc murrayc com>
 
+	Tables now have creation and modification fields, appropriately set.
+
+	* glom/base_db.[h|cc]: Added static m_extra_modification_field_values, 
+	to store the field information for the extra modification fields, filled in 
+	init_extra_modification_fields.
+	set_field_value_in_database(): Also set the extra modification field 
+	values.
+	* glom/base_db_table_data.cc: record_new(): Set values for the extra 
+	creation fields, and show them if they are on the layout.
+
+2009-05-07  Murray Cumming  <murrayc murrayc-x61>
+
 	Allow non-network-shared connections when using IPv6.
 
 	* glom/libglom/connectionpool_backends/postgres_self.cc: 
diff --git a/glom/base_db.cc b/glom/base_db.cc
index 9bdb5ad..932436b 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -720,7 +720,7 @@ Base_DB::type_vec_fields Base_DB::get_fields_for_table_from_database(const Glib:
 Base_DB::type_vec_fields Base_DB::get_fields_for_table(const Glib::ustring& table_name, bool including_system_fields) const
 {
   //Get field definitions from the database:
-  type_vec_fields fieldsDatabase = get_fields_for_table_from_database(table_name, including_system_fields);
+  const type_vec_fields fieldsDatabase = get_fields_for_table_from_database(table_name, including_system_fields);
 
   const Document* pDoc = dynamic_cast<const Document*>(get_document());
   if(!pDoc)
@@ -764,9 +764,9 @@ Base_DB::type_vec_fields Base_DB::get_fields_for_table(const Glib::ustring& tabl
     }
 
     //Add any fields that are in the database, but not in the document:
-    for(type_vec_fields::iterator iter = fieldsDatabase.begin(); iter != fieldsDatabase.end(); ++iter)
+    for(type_vec_fields::const_iterator iter = fieldsDatabase.begin(); iter != fieldsDatabase.end(); ++iter)
     {
-      Glib::ustring field_name = (*iter)->get_name();
+      const Glib::ustring field_name = (*iter)->get_name();
 
        //Look in the result so far:
        type_vec_fields::const_iterator iterFind = std::find_if(result.begin(), result.end(), predicate_FieldHasName<Field>(field_name));
@@ -2492,17 +2492,17 @@ bool Base_DB::set_field_value_in_database(const LayoutFieldInRecord& field_in_re
   return set_field_value_in_database(field_in_record, Gtk::TreeModel::iterator(), field_value, use_current_calculations, parent_window);
 }
 
-void Base_DB::init_extra_fields()
+void Base_DB::init_extra_modification_fields()
 {
-  if(!m_extra_field_values.empty())
+  if(!m_extra_modification_field_values.empty())
     return;
   
   //Fill the field information:
-  m_extra_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_DATE] = 
+  m_extra_modification_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_DATE] = 
     FieldTypeValue(G_TYPE_DATE, Gnome::Gda::Value());
-  m_extra_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_TIME] = 
+  m_extra_modification_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_TIME] = 
     FieldTypeValue(GDA_TYPE_TIME, Gnome::Gda::Value());
-  m_extra_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_USER] = 
+  m_extra_modification_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_USER] = 
     FieldTypeValue(G_TYPE_STRING, Gnome::Gda::Value());
 }
 
@@ -2537,7 +2537,7 @@ bool Base_DB::set_field_value_in_database(const LayoutFieldInRecord& layoutfield
 
   const Glib::ustring table_name = field_in_record.m_table_name;
   Glib::ustring strQuery = "UPDATE \"" + field_in_record.m_table_name + "\"";
-  strQuery += " SET \"" + field_in_record.m_field->get_name() + "\" = " + field_in_record.m_field->get_gda_holder_string();
+  strQuery += " SET \"" + field_name + "\" = " + field_in_record.m_field->get_gda_holder_string();
 
 
   //Set these extra fields too, each time we change a value:
@@ -2546,17 +2546,23 @@ bool Base_DB::set_field_value_in_database(const LayoutFieldInRecord& layoutfield
   //  And use the Postgres cleverness to do it instead? Maybe like this:
   //  http://blog.revsys.com/2006/08/automatically_u.html
   ConnectionPool* connection_pool = ConnectionPool::get_instance();
-  init_extra_fields();
-  m_extra_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_DATE].second = 
+  init_extra_modification_fields();
+  m_extra_modification_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_DATE].second = 
     Utils::get_current_date_utc_as_value();
-  m_extra_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_TIME].second = 
+  m_extra_modification_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_TIME].second = 
     Utils::get_current_time_utc_as_value();
-  m_extra_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_USER].second = 
+  m_extra_modification_field_values[GLOM_STANDARD_DEFAULT_FIELD_MODIFICATION_USER].second = 
     Gnome::Gda::Value(connection_pool->get_user());
   
-  for(type_extra_field_values::const_iterator iter = m_extra_field_values.begin(); iter != m_extra_field_values.end(); ++iter)
+  for(type_extra_field_values::const_iterator iter = m_extra_modification_field_values.begin(); iter != m_extra_modification_field_values.end(); ++iter)
   {
+    //In the unlikely (impossible?) event that the main field being changed 
+    //is one of the extra modification fields, don't try to specify it again:
+    if(iter->first == field_name)
+      continue;
+
     const FieldTypeValue& item = iter->second;
+
     if(get_field_exists_in_database(table_name, iter->first))
     {
       strQuery += ", \"" + Glib::ustring(iter->first) /* name */ + "\" = " + 
@@ -2609,8 +2615,14 @@ bool Base_DB::set_field_value_in_database(const LayoutFieldInRecord& layoutfield
 
   //For the extra fields, 
   //show the new database values in the UI, if the fields are on the layout:
-  for(type_extra_field_values::const_iterator iter = m_extra_field_values.begin(); iter != m_extra_field_values.end(); ++iter)
+  for(type_extra_field_values::const_iterator iter = m_extra_modification_field_values.begin(); iter != m_extra_modification_field_values.end(); ++iter)
   {
+    //In the unlikely (impossible?) event that the main field being changed 
+    //was one of the extra modification fields, don't try to change its 
+    //displayed value. We ignore it above anyway.
+    if(iter->first == field_name)
+      continue;
+
     const FieldTypeValue& item = iter->second;
     
     sharedptr<LayoutItem_Field> layout_item = glom_sharedptr_clone(layoutfield_in_record.m_field);
diff --git a/glom/base_db.h b/glom/base_db.h
index 5d87cb0..b6f3696 100644
--- a/glom/base_db.h
+++ b/glom/base_db.h
@@ -410,7 +410,7 @@ protected:
 
 private:
   /// Fill m_extra_field_values with the extra field definitions. 
-  void init_extra_fields();
+  void init_extra_modification_fields();
 
 protected:
 
@@ -419,7 +419,7 @@ protected:
   //Extra creation/modification fields to set:
   typedef std::pair<GType, Gnome::Gda::Value> FieldTypeValue;
   typedef std::map<const gchar*, FieldTypeValue> type_extra_field_values;
-  static type_extra_field_values m_extra_field_values;
+  static type_extra_field_values m_extra_modification_field_values;
 
 };
 
diff --git a/glom/base_db_table_data.cc b/glom/base_db_table_data.cc
index eb44a69..f1dde6f 100644
--- a/glom/base_db_table_data.cc
+++ b/glom/base_db_table_data.cc
@@ -18,10 +18,11 @@
  * Boston, MA 02111-1307, USA.
  */
 
-#include "base_db_table_data.h"
+#include <glom/base_db_table_data.h>
 #include <libglom/data_structure/glomconversions.h>
+#include <libglom/standard_table_prefs_fields.h>
 #include <glom/application.h>
-#include "python_embed/glom_python.h"
+#include <glom/python_embed/glom_python.h>
 #include <glom/utils_ui.h>
 #include <sstream>
 #include <glibmm/i18n.h>
@@ -94,12 +95,13 @@ bool Base_DB_Table_Data::record_new(bool use_entered_data, const Gnome::Gda::Val
   }
 
   Document* document = get_document();
+  ConnectionPool* connection_pool = ConnectionPool::get_instance();
 
   //Calculate any necessary field values and enter them:
   for(type_vecLayoutFields::const_iterator iter = fieldsToAdd.begin(); iter != fieldsToAdd.end(); ++iter)
   {
     sharedptr<LayoutItem_Field> layout_item = *iter;
-
+    
     //If the user did not enter something in this field:
     Gnome::Gda::Value value = get_entered_field_data(layout_item);
 
@@ -182,6 +184,24 @@ bool Base_DB_Table_Data::record_new(bool use_entered_data, const Gnome::Gda::Val
               value = get_entered_field_data(layout_item);
           }
 
+          //Handle the special creation fields:
+          //TODO_performance: Avoid these string comparisons for each field: 
+          if(field_name == GLOM_STANDARD_DEFAULT_FIELD_CREATION_DATE)
+          {
+            value =  Utils::get_current_date_utc_as_value();
+            set_entered_field_data(layout_item, value);
+          }
+          else if(field_name == GLOM_STANDARD_DEFAULT_FIELD_CREATION_TIME)
+          {
+            value = Utils::get_current_time_utc_as_value();
+            set_entered_field_data(layout_item, value);
+          }
+          else if(field_name == GLOM_STANDARD_DEFAULT_FIELD_CREATION_USER)
+          {
+            value = Gnome::Gda::Value(connection_pool->get_user());
+            set_entered_field_data(layout_item, value);
+          }
+
           /* //TODO: This would be too many small queries when adding one record.
           //Check whether the value meets uniqueness constraints:
           if(field->get_primary_key() || field->get_unique_key())
@@ -202,7 +222,7 @@ bool Base_DB_Table_Data::record_new(bool use_entered_data, const Gnome::Gda::Val
   
             strNames += "\"" + field_name + "\"";
             strValues += field->get_gda_holder_string();
-            Glib::RefPtr<Gnome::Gda::Holder> holder = field->get_holder(value);
+            const Glib::RefPtr<Gnome::Gda::Holder> holder = field->get_holder(value);
             params->add_holder(holder);
   
             map_added[field_name] = true;
diff --git a/glom/utility_widgets/db_adddel/db_adddel.cc b/glom/utility_widgets/db_adddel/db_adddel.cc
index 7bd6635..5c10f39 100644
--- a/glom/utility_widgets/db_adddel/db_adddel.cc
+++ b/glom/utility_widgets/db_adddel/db_adddel.cc
@@ -2311,7 +2311,7 @@ void DbAddDel::user_changed(const Gtk::TreeModel::iterator& row, guint col)
     else
     {
       //A field value was entered, but the record has not been added yet, because not enough information exists yet.
-       g_warning("Box_Data_List::on_adddel_user_changed(): debug: record not yet added.");
+      g_warning("Box_Data_List::on_adddel_user_changed(): debug: record not yet added.");
     }
   }
 }



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