[glom] libglom: Added build_sql_update_with_where_clause().



commit e0e6e9b2b1ef2c385c439988e9de9def16b7c9bc
Author: Murray Cumming <murrayc murrayc com>
Date:   Sat Oct 29 22:22:36 2011 +0200

    libglom: Added build_sql_update_with_where_clause().
    
    	* glom/libglom/utils.[h|cc]: Added build_sql_update_with_where_clause().
    	* glom/base_db.cc: set_field_value_in_database(): use it here instead
    	of manually creating a SqlBuilder.

 ChangeLog             |   17 +++++++++++++++++
 glom/base_db.cc       |   13 ++++++-------
 glom/libglom/utils.cc |   42 ++++++++++++++++++++++++++++++++++++++++++
 glom/libglom/utils.h  |    7 +++++++
 4 files changed, 72 insertions(+), 7 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index de8dd18..c41144f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2011-10-29  Murray Cumming  <murrayc murrayc com>
+
+	libglom: Added build_sql_update_with_where_clause().
+
+	* glom/libglom/utils.[h|cc]: Added build_sql_update_with_where_clause().
+	* glom/base_db.cc: set_field_value_in_database(): use it here instead 
+	of manually creating a SqlBuilder.
+
+2011-10-28  Murray Cumming  <murrayc murrayc com>
+
+	Add a test for Image fields.
+
+	* Makefile_tests.am:
+	* tests/test_selfhosting_new_then_image.cc: Add a test to read an image field
+	value, though this currently fails.
+	I will later add a write-then-read test.
+
 2011-10-28  Murray Cumming  <murrayc murrayc com>
 
 	Add a test for Image fields.
diff --git a/glom/base_db.cc b/glom/base_db.cc
index 53e11b4..b63f50d 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -1148,13 +1148,12 @@ bool Base_DB::set_field_value_in_database(const LayoutFieldInRecord& layoutfield
   const Glib::ustring field_name = field_in_record.m_field->get_name();
   if(!field_name.empty()) //This should not happen.
   {
-    Glib::RefPtr<Gnome::Gda::SqlBuilder> builder = Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_UPDATE);
-    builder->set_table(field_in_record.m_table_name);
-    builder->add_field_value_as_value(field_in_record.m_field->get_name(), field_value);
-    builder->set_where(
-      builder->add_cond(Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
-        builder->add_field_id(field_in_record.m_key->get_name(), field_in_record.m_table_name),
-        builder->add_expr_as_value(field_in_record.m_key_value)));
+    const Gnome::Gda::SqlExpr where_clause = 
+      Utils::build_simple_where_expression(field_in_record.m_table_name,
+        field_in_record.m_key, field_in_record.m_key_value);
+    const Glib::RefPtr<const Gnome::Gda::SqlBuilder> builder = 
+      Utils::build_sql_update_with_where_clause(field_in_record.m_table_name,
+        field_in_record.m_field, field_value, where_clause);
 
     try //TODO: The exceptions are probably already handled by query_execute(
     {
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index 6b3be7f..d5adbbd 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -1148,6 +1148,48 @@ Gnome::Gda::SqlExpr Utils::get_find_where_clause_quick(const Document* document,
   }
 }
 
+Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_update_with_where_clause(
+  const Glib::ustring& table_name,
+  const sharedptr<const Field>& field, const Gnome::Gda::Value& value,
+  const Gnome::Gda::SqlExpr& where_clause)
+{
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> builder;
+
+  if(!field || field->get_name().empty())
+  {
+    std::cerr << "field was null or its name was empty." << std::endl;
+    return builder;
+  }
+
+  if(table_name.empty())
+  {
+    std::cerr << "table_name was empty." << std::endl;
+    return builder;
+  }
+
+  //Build the whole SQL statement:
+  try
+  {
+    builder = Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_UPDATE);
+    builder->set_table(table_name);
+
+    builder->add_field_value_as_value(field->get_name(), value);
+
+    //Add the WHERE clause:
+    if(!where_clause.empty())
+    {
+      const int id = builder->import_expression(where_clause);
+      builder->set_where(id);
+    }
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Exception: " << ex.what() << std::endl;
+  }
+
+  return builder;
+}
+
 bool Utils::delete_directory(const Glib::RefPtr<Gio::File>& directory)
 {
   if(!(directory->query_exists()))
diff --git a/glom/libglom/utils.h b/glom/libglom/utils.h
index b3f6c39..063e0e8 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -126,6 +126,13 @@ Glib::RefPtr<Gnome::Gda::SqlBuilder> build_sql_select_count_rows(const Glib::Ref
 
 Gnome::Gda::SqlExpr get_find_where_clause_quick(const Document* document, const Glib::ustring& table_name, const Gnome::Gda::Value& quick_search);
 
+/** Generate a SQL statement to UPDATE field values,
+ */
+Glib::RefPtr<Gnome::Gda::SqlBuilder> build_sql_update_with_where_clause(
+  const Glib::ustring& table_name,
+  const sharedptr<const Field>& field, const Gnome::Gda::Value& value,
+  const Gnome::Gda::SqlExpr& where_clause);
+
 typedef std::list<Gnome::Gda::Value> type_list_values;
 typedef std::list< std::pair<Gnome::Gda::Value, type_list_values> > type_list_values_with_second; //TODO: Rename this now that we have more than just 1 extra field.
 type_list_values_with_second get_choice_values_all(const Document* document, const sharedptr<const LayoutItem_Field>& field);



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