[glom] Base_DB: Move get_record_field_values() to libglom.



commit 29c67ddad2d62132edf9bb68305e97dfc1c58bcd
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Nov 28 10:00:29 2013 +0100

    Base_DB: Move get_record_field_values() to libglom.
    
    This lets us use it in tests.

 glom/base_db.cc          |   76 ++------------------------------------------
 glom/base_db.h           |    2 +-
 glom/libglom/db_utils.cc |   80 ++++++++++++++++++++++++++++++++++++++++++++++
 glom/libglom/db_utils.h  |    7 +++-
 4 files changed, 90 insertions(+), 75 deletions(-)
---
diff --git a/glom/base_db.cc b/glom/base_db.cc
index 5241d5a..aea05fe 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -948,80 +948,10 @@ void Base_DB::calculate_field(const LayoutFieldInRecord& field_in_record)
 
 }
 
-Base_DB::type_map_fields Base_DB::get_record_field_values_for_calculation(const Glib::ustring& table_name, 
const sharedptr<const Field> primary_key, const Gnome::Gda::Value& primary_key_value)
+Base_DB::type_map_fields Base_DB::get_record_field_values_for_calculation(const Glib::ustring& table_name, 
const sharedptr<const Field>& primary_key, const Gnome::Gda::Value& primary_key_value)
 {
-  type_map_fields field_values;
-
-  Document* document = get_document();
-  if(document)
-  {
-    //TODO: Cache the list of all fields, as well as caching (m_Fields) the list of all visible fields:
-    const Document::type_vec_fields fields = document->get_table_fields(table_name);
-
-    //TODO: This seems silly. We should just have a build_sql_select() that can take this container:
-    type_vecLayoutFields fieldsToGet;
-    for(Document::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
-    {
-      sharedptr<LayoutItem_Field> layout_item = sharedptr<LayoutItem_Field>::create();
-      layout_item->set_full_field_details(*iter);
-
-      fieldsToGet.push_back(layout_item);
-    }
-
-    if(!Conversions::value_is_empty(primary_key_value))
-    {
-      //sharedptr<const Field> fieldPrimaryKey = get_field_primary_key();
-
-      Glib::RefPtr<Gnome::Gda::SqlBuilder> query = Utils::build_sql_select_with_key(table_name, fieldsToGet, 
primary_key, primary_key_value);
-
-      Glib::RefPtr<const Gnome::Gda::DataModel> data_model;
-      try
-      {
-        data_model = DbUtils::query_execute_select(query);
-      }
-      catch(const Glib::Exception& ex)
-      {
-        std::cerr << G_STRFUNC << ": Exception while executing SQL: " << query << std::endl;
-        handle_error(ex);
-        return field_values;
-      }
-      if(data_model && data_model->get_n_rows())
-      {
-        int col_index = 0;
-        for(Document::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
-        {
-          //There should be only 1 row. Well, there could be more but we will ignore them.
-          sharedptr<const Field> field = *iter;
-          Gnome::Gda::Value value = data_model->get_value_at(col_index, 0);
-          //Never give a NULL-type value to the python calculation for types that don't use them:
-          //to prevent errors:
-          if(value.is_null())
-            value = Conversions::get_empty_value(field->get_glom_type());
-
-          field_values[field->get_name()] = value;
-          ++col_index;
-        }
-      }
-      else
-      {
-        //Maybe the record does not exist yet
-        //(Maybe we need the field values so we can calculate default values for some fields when creating 
the record.)
-        //So we create appropriate empty values below.
-      }
-    }
-
-    if(field_values.empty()) //Maybe there was no primary key, or maybe the record is not yet in the 
database.
-    {
-      //Create appropriate empty values:
-      for(Document::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
-      {
-        sharedptr<const Field> field = *iter;
-        field_values[field->get_name()] = Conversions::get_empty_value(field->get_glom_type());
-      }
-    }
-  }
-
-  return field_values;
+  const Document* document = get_document();
+  return DbUtils::get_record_field_values(document, table_name, primary_key, primary_key_value);
 }
 
 void Base_DB::set_entered_field_data(const sharedptr<const LayoutItem_Field>& /* field */, const 
Gnome::Gda::Value& /* value */)
diff --git a/glom/base_db.h b/glom/base_db.h
index 177c8c0..1251fb0 100644
--- a/glom/base_db.h
+++ b/glom/base_db.h
@@ -237,7 +237,7 @@ protected:
 
   typedef std::map<Glib::ustring, Gnome::Gda::Value> type_map_fields;
   //TODO: Performance: This is massively inefficient:
-  type_map_fields get_record_field_values_for_calculation(const Glib::ustring& table_name, const 
sharedptr<const Field> primary_key, const Gnome::Gda::Value& primary_key_value);
+  type_map_fields get_record_field_values_for_calculation(const Glib::ustring& table_name, const 
sharedptr<const Field>& primary_key, const Gnome::Gda::Value& primary_key_value);
 
 
   void do_lookups(const LayoutFieldInRecord& field_in_record, const Gtk::TreeModel::iterator& row, const 
Gnome::Gda::Value& field_value);
diff --git a/glom/libglom/db_utils.cc b/glom/libglom/db_utils.cc
index a51a8d0..f654ddb 100644
--- a/glom/libglom/db_utils.cc
+++ b/glom/libglom/db_utils.cc
@@ -2287,6 +2287,86 @@ Gnome::Gda::Value get_lookup_value(const Document* document, const Glib::ustring
   return result;
 }
 
+type_map_fields get_record_field_values(const Document* document, const Glib::ustring& table_name, const 
sharedptr<const Field>& primary_key, const Gnome::Gda::Value& primary_key_value)
+{
+  type_map_fields field_values;
+
+  if(!document)
+  {
+    std::cerr << G_STRFUNC << ": document is NULL." << std::endl;
+    return field_values;
+  }
+
+  //TODO: Cache the list of all fields, as well as caching (m_Fields) the list of all visible fields:
+  const Document::type_vec_fields fields = document->get_table_fields(table_name);
+
+  //TODO: This seems silly. We should just have a build_sql_select() that can take this container:
+  typedef std::vector< sharedptr<LayoutItem_Field> > type_vecLayoutFields;
+  type_vecLayoutFields fieldsToGet;
+  for(Document::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
+  {
+    const sharedptr<LayoutItem_Field> layout_item = sharedptr<LayoutItem_Field>::create();
+    layout_item->set_full_field_details(*iter);
+
+    fieldsToGet.push_back(layout_item);
+  }
+
+  if(!Conversions::value_is_empty(primary_key_value))
+  {
+    //sharedptr<const Field> fieldPrimaryKey = get_field_primary_key();
+
+    Glib::RefPtr<Gnome::Gda::SqlBuilder> query = Utils::build_sql_select_with_key(table_name, fieldsToGet, 
primary_key, primary_key_value);
+
+    Glib::RefPtr<const Gnome::Gda::DataModel> data_model;
+    try
+    {
+      data_model = DbUtils::query_execute_select(query);
+    }
+    catch(const Glib::Exception& ex)
+    {
+      std::cerr << G_STRFUNC << ": Exception while executing SQL: " << query << std::endl;
+      handle_error(ex);
+      return field_values;
+    }
+
+    if(data_model && data_model->get_n_rows())
+    {
+      int col_index = 0;
+      for(Document::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
+      {
+        //There should be only 1 row. Well, there could be more but we will ignore them.
+        sharedptr<const Field> field = *iter;
+        Gnome::Gda::Value value = data_model->get_value_at(col_index, 0);
+        //Never give a NULL-type value to the python calculation for types that don't use them:
+        //to prevent errors:
+        if(value.is_null())
+          value = Conversions::get_empty_value(field->get_glom_type());
+
+        field_values[field->get_name()] = value;
+        ++col_index;
+      }
+    }
+    else
+    {
+      //Maybe the record does not exist yet
+      //(Maybe we need the field values so we can calculate default values for some fields when creating the 
record.)
+      //So we create appropriate empty values below.
+    }
+  }
+
+  if(field_values.empty()) //Maybe there was no primary key, or maybe the record is not yet in the database.
+  {
+    //Create appropriate empty values:
+    for(Document::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
+    {
+      sharedptr<const Field> field = *iter;
+      field_values[field->get_name()] = Conversions::get_empty_value(field->get_glom_type());
+    }
+  }
+
+  return field_values;
+}
+
 } //namespace DbUtils
 
 } //namespace Glom
diff --git a/glom/libglom/db_utils.h b/glom/libglom/db_utils.h
index f4646e0..5da6fe1 100644
--- a/glom/libglom/db_utils.h
+++ b/glom/libglom/db_utils.h
@@ -214,7 +214,12 @@ bool remove_user_from_group(const Glib::ustring& user, const Glib::ustring& grou
 
 /** Get the value of the @a source_field from the @a relationship, using the @a key_value.
  */
-Gnome::Gda::Value get_lookup_value(const Document* document, const Glib::ustring& table_name, const 
sharedptr<const Relationship>& relationship, const sharedptr<const Field>& source_field, const 
Gnome::Gda::Value & key_value);
+Gnome::Gda::Value get_lookup_value(const Document* document, const Glib::ustring& table_name, const 
sharedptr<const Relationship>& relationship, const sharedptr<const Field>& source_field, const 
Gnome::Gda::Value& key_value);
+
+typedef std::map<Glib::ustring, Gnome::Gda::Value> type_map_fields;
+
+//TODO: Performance: This is massively inefficient:
+type_map_fields get_record_field_values(const Document* document, const Glib::ustring& table_name, const 
sharedptr<const Field>& primary_key, const Gnome::Gda::Value& primary_key_value);
   
 /** Allow a fake connection, so sqlbuilder_get_full_query() can work.
  */


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