[glom] libglom: Utils: Avoid SQL strings for extra_join for doubly-related records.



commit c8bc14d5df8ae02ea1085b5c06ff45575ef68964
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue May 25 14:53:24 2010 +0200

    libglom: Utils: Avoid SQL strings for extra_join for doubly-related records.
    
    	* glom/libglom/data_structure/foundset.[h|cc]: Remove the extra_group_by
    	member variable.
    	* glom/base_db.cc: set_found_set_where_clause_for_portal(): Do not create
    	the SQL GROUP BY string here.
    	* glom/libglom/utils.[h|cc]: build_sql_select_with_where_clause(): Remove
    	the extra_group_by parameter, instead grouping by all fields if there is
    	an extra_join_by, because there is a 1-to-1 correlation.
    	Also take extra_join as a Relationship instead of a SQL JOIN string, and
    	reuse the existing code to define a join from a relationship.
    	* glom/frame_glom.cc:
    	* glom/libglom/data_structure/foundset.cc:
    	* glom/libglom/data_structure/foundset.h:
    	* glom/libglom/utils.cc:
    	* glom/libglom/utils.h:
    	* glom/mode_data/box_data_calendar_related.cc:
    	* glom/print_layout/canvas_print_layout.cc:
    	* glom/report_builder.cc:
    	* glom/utility_widgets/db_adddel/glom_db_treemodel.cc: Adapt.

 ChangeLog                                          |   23 ++++
 glom/base_db.cc                                    |   26 +----
 glom/frame_glom.cc                                 |    8 +-
 glom/libglom/data_structure/foundset.cc            |    3 -
 glom/libglom/data_structure/foundset.h             |    3 +-
 glom/libglom/utils.cc                              |  137 +++++++++++---------
 glom/libglom/utils.h                               |    9 +-
 glom/mode_data/box_data_calendar_related.cc        |    2 +-
 glom/print_layout/canvas_print_layout.cc           |    5 +-
 glom/report_builder.cc                             |    3 +-
 .../utility_widgets/db_adddel/glom_db_treemodel.cc |    4 +-
 11 files changed, 115 insertions(+), 108 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index f00df4b..98bd74a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,28 @@
 2010-05-25  Murray Cumming  <murrayc murrayc-x61>
 
+	libglom: Utils: Avoid SQL strings for extra_join for doubly-related records.
+
+	* glom/libglom/data_structure/foundset.[h|cc]: Remove the extra_group_by 
+	member variable.
+	* glom/base_db.cc: set_found_set_where_clause_for_portal(): Do not create 
+	the SQL GROUP BY string here.
+	* glom/libglom/utils.[h|cc]: build_sql_select_with_where_clause(): Remove 
+	the extra_group_by parameter, instead grouping by all fields if there is 
+	an extra_join_by, because there is a 1-to-1 correlation.
+	Also take extra_join as a Relationship instead of a SQL JOIN string, and 
+	reuse the existing code to define a join from a relationship.
+	* glom/frame_glom.cc:
+	* glom/libglom/data_structure/foundset.cc:
+	* glom/libglom/data_structure/foundset.h:
+	* glom/libglom/utils.cc:
+	* glom/libglom/utils.h:
+	* glom/mode_data/box_data_calendar_related.cc:
+	* glom/print_layout/canvas_print_layout.cc:
+	* glom/report_builder.cc:
+	* glom/utility_widgets/db_adddel/glom_db_treemodel.cc: Adapt.
+
+2010-05-25  Murray Cumming  <murrayc murrayc-x61>
+
 	Document: get_field_used_in_relationship_to_one(): const correction.
 
 	* glom/libglom/document/document.[h|cc]: 
diff --git a/glom/base_db.cc b/glom/base_db.cc
index fefa9c1..42c7702 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -1343,7 +1343,7 @@ Gnome::Gda::Value Base_DB::get_field_value_in_database(const sharedptr<Field>& f
   Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query = Utils::build_sql_select_with_where_clause(found_set.m_table_name,
     list_fields,
     found_set.m_where_clause,
-    Glib::ustring(), type_sort_clause(), Glib::ustring(),
+    sharedptr<const Relationship>() /* extra_join */, type_sort_clause(),
     1 /* limit */);
 
   Glib::RefPtr<const Gnome::Gda::DataModel> data_model = DbUtils::query_execute_select(sql_query);
@@ -1934,8 +1934,7 @@ void Base_DB::set_found_set_where_clause_for_portal(FoundSet& found_set, const s
 {
   found_set.m_table_name = Glib::ustring();
   found_set.m_where_clause = Gnome::Gda::SqlExpr();
-  found_set.m_extra_join = Glib::ustring();
-  found_set.m_extra_group_by = Glib::ustring();
+  found_set.m_extra_join = sharedptr<const Relationship>();
 
   if( !portal
       || Conversions::value_is_empty(foreign_key_value) )
@@ -1961,25 +1960,7 @@ void Base_DB::set_found_set_where_clause_for_portal(FoundSet& found_set, const s
     //Add the extra JOIN:
     sharedptr<UsesRelationship> uses_rel_temp = sharedptr<UsesRelationship>::create();
     uses_rel_temp->set_relationship(relationship);
-    //found_set.m_extra_join = uses_rel_temp->get_sql_join_alias_definition();
-    found_set.m_extra_join = "LEFT OUTER JOIN \"" + relationship->get_to_table() + "\" AS \"" + uses_rel_temp->get_sql_join_alias_name() + "\" ON (\"" + uses_rel_temp->get_sql_join_alias_name() + "\".\"" + relationship_related->get_from_field() + "\" = \"" + relationship_related->get_to_table() + "\".\"" + relationship_related->get_to_field() + "\")";
-
-
-    //Add an extra GROUP BY to ensure that we get no repeated records from the doubly-related table:
-    LayoutGroup::type_list_items portal_items = portal->get_items();
-    Utils::type_vecConstLayoutFields fields;
-    for(LayoutGroup::type_list_items::iterator iter = portal_items.begin(); iter != portal_items.end(); ++iter)
-    {
-      sharedptr<LayoutItem_Field> item_field = sharedptr<LayoutItem_Field>::cast_dynamic(*iter);
-      if(item_field)
-        fields.push_back(item_field);
-    }
-
-    //TODO:?
-    //const Glib::ustring sql_part_fields = Utils::build_sql_select_fields_to_get(
-    //  found_set.m_table_name, fields, found_set.m_sort_clause);
-    //found_set.m_extra_group_by = "GROUP BY " + sql_part_fields;
-
+    found_set.m_extra_join = relationship;
 
     //Adjust the WHERE clause appropriately for the extra JOIN:
     where_clause_to_table_name = uses_rel_temp->get_sql_join_alias_name();
@@ -1991,7 +1972,6 @@ void Base_DB::set_found_set_where_clause_for_portal(FoundSet& found_set, const s
     //std::cout << "extra_join where_clause_to_key_field=" << where_clause_to_key_field->get_name() << std::endl;
   }
 
-  // TODO: Where is this used? Should we use parameters for this query instead of sql()?
   if(where_clause_to_key_field)
   {
     found_set.m_where_clause =
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index 48d21fd..b0592ed 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -471,7 +471,7 @@ void Frame_Glom::show_table_allow_empty(const Glib::ustring& table_name, const G
           layout_item_temp->set_full_field_details(field_primary_key);
           type_vecLayoutFields layout_fields;
           layout_fields.push_back(layout_item_temp);
-          Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query_without_sort = Utils::build_sql_select_with_where_clause(found_set.m_table_name, layout_fields, found_set.m_where_clause, found_set.m_extra_join, type_sort_clause(), found_set.m_extra_group_by);
+          Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query_without_sort = Utils::build_sql_select_with_where_clause(found_set.m_table_name, layout_fields, found_set.m_where_clause, found_set.m_extra_join, type_sort_clause());
           const int count = Base_DB::count_rows_returned_by(sql_query_without_sort);
           if(count < 10000) //Arbitrary large number.
             found_set.m_sort_clause.push_back( type_pair_sort_field(layout_item_sort, true /* ascending */) );
@@ -687,7 +687,7 @@ void Frame_Glom::export_data_to_vector(Document::type_example_rows& the_vector,
     return;
   }
 
-  Glib::RefPtr<Gnome::Gda::SqlBuilder> query = Utils::build_sql_select_with_where_clause(found_set.m_table_name, fieldsSequence, found_set.m_where_clause, found_set.m_extra_join, found_set.m_sort_clause, found_set.m_extra_group_by);
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> query = Utils::build_sql_select_with_where_clause(found_set.m_table_name, fieldsSequence, found_set.m_where_clause, found_set.m_extra_join, found_set.m_sort_clause);
 
   //TODO: Lock the database (prevent changes) during export.
   Glib::RefPtr<Gnome::Gda::DataModel> result = DbUtils::query_execute_select(query);
@@ -737,7 +737,7 @@ void Frame_Glom::export_data_to_string(Glib::ustring& the_string, const FoundSet
     return;
   }
 
-  Glib::RefPtr<Gnome::Gda::SqlBuilder> query = Utils::build_sql_select_with_where_clause(found_set.m_table_name, fieldsSequence, found_set.m_where_clause, found_set.m_extra_join, found_set.m_sort_clause, found_set.m_extra_group_by);
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> query = Utils::build_sql_select_with_where_clause(found_set.m_table_name, fieldsSequence, found_set.m_where_clause, found_set.m_extra_join, found_set.m_sort_clause);
 
   //TODO: Lock the database (prevent changes) during export.
   Glib::RefPtr<const Gnome::Gda::DataModel> result = DbUtils::query_execute_select(query);
@@ -789,7 +789,7 @@ void Frame_Glom::export_data_to_stream(std::ostream& the_stream, const FoundSet&
     return;
   }
 
-  Glib::RefPtr<Gnome::Gda::SqlBuilder> query = Utils::build_sql_select_with_where_clause(found_set.m_table_name, fieldsSequence, found_set.m_where_clause, found_set.m_extra_join, found_set.m_sort_clause, found_set.m_extra_group_by);
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> query = Utils::build_sql_select_with_where_clause(found_set.m_table_name, fieldsSequence, found_set.m_where_clause, found_set.m_extra_join, found_set.m_sort_clause);
 
   //TODO: Lock the database (prevent changes) during export.
   Glib::RefPtr<const Gnome::Gda::DataModel> result = DbUtils::query_execute_select(query);
diff --git a/glom/libglom/data_structure/foundset.cc b/glom/libglom/data_structure/foundset.cc
index 7b4ac89..985a845 100644
--- a/glom/libglom/data_structure/foundset.cc
+++ b/glom/libglom/data_structure/foundset.cc
@@ -31,7 +31,6 @@ FoundSet::FoundSet(const FoundSet& src)
 :  m_table_name(src.m_table_name),
    m_extra_join(src.m_extra_join),
    m_where_clause(src.m_where_clause),
-   m_extra_group_by(src.m_extra_group_by),
    m_sort_clause(src.m_sort_clause)
 {
 }
@@ -41,7 +40,6 @@ FoundSet& FoundSet::operator=(const FoundSet& src)
   m_table_name = src.m_table_name;
   m_extra_join = src.m_extra_join;
   m_where_clause = src.m_where_clause;
-  m_extra_group_by = src.m_extra_group_by;
   m_sort_clause = src.m_sort_clause;
 
   return *this;
@@ -52,7 +50,6 @@ bool FoundSet::operator==(const FoundSet& src) const
   return (m_table_name == src.m_table_name)
       && (m_extra_join == src.m_extra_join)
       /* TODO: && (m_where_clause == src.m_where_clause) */
-      && (m_extra_group_by == src.m_extra_group_by)
       && (m_sort_clause == src.m_sort_clause);
 }
 
diff --git a/glom/libglom/data_structure/foundset.h b/glom/libglom/data_structure/foundset.h
index af26856..5eeacaa 100644
--- a/glom/libglom/data_structure/foundset.h
+++ b/glom/libglom/data_structure/foundset.h
@@ -48,9 +48,8 @@ public:
   bool has_no_criteria() const;
 
   Glib::ustring m_table_name;
-  Glib::ustring m_extra_join; // Only used for doubly-related related records (portals), in which case the WHERE clause is also slightly different.
+  sharedptr<const Relationship> m_extra_join; // Only used for doubly-related related records (portals), in which case the WHERE clause is also slightly different.
   Gnome::Gda::SqlExpr m_where_clause;
-  Glib::ustring m_extra_group_by;  // Only used for doubly-related related records (portals), in which case the WHERE clause is also slightly different.
 
   ///field, ascending
   typedef std::pair< sharedptr<const LayoutItem_Field>, bool> type_pair_sort_field;
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index bf659c9..4584633 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -195,7 +195,7 @@ Glib::ustring Utils::string_replace(const Glib::ustring& src, const Glib::ustrin
 }
 
 
-Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_select_with_where_clause(const Glib::ustring& table_name, const type_vecLayoutFields& fieldsToGet, const Gnome::Gda::SqlExpr& where_clause, const Glib::ustring& extra_join, const type_sort_clause& sort_clause, const Glib::ustring& extra_group_by, guint limit)
+Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_select_with_where_clause(const Glib::ustring& table_name, const type_vecLayoutFields& fieldsToGet, const Gnome::Gda::SqlExpr& where_clause, const sharedptr<const Relationship>& extra_join, const type_sort_clause& sort_clause, guint limit)
 {
   //TODO_Performance:
   type_vecConstLayoutFields constFieldsToGet;
@@ -204,7 +204,7 @@ Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_select_with_where_clause(c
     constFieldsToGet.push_back(*iter);
   }
 
-  return build_sql_select_with_where_clause(table_name, constFieldsToGet, where_clause, extra_join, sort_clause, extra_group_by, limit);
+  return build_sql_select_with_where_clause(table_name, constFieldsToGet, where_clause, extra_join, sort_clause, limit);
 }
 
 
@@ -239,7 +239,59 @@ static void add_to_relationships_list(type_list_relationships& list_relationship
 
 }
 
-void Utils::build_sql_select_add_fields_to_get(const Glib::RefPtr<Gnome::Gda::SqlBuilder>& builder, const Glib::ustring& table_name, const type_vecConstLayoutFields& fieldsToGet, const type_sort_clause& sort_clause)
+static void builder_add_join(const Glib::RefPtr<Gnome::Gda::SqlBuilder>& builder, const sharedptr<const UsesRelationship>& uses_relationship)
+{
+  sharedptr<const Relationship> relationship = uses_relationship->get_relationship();
+  if(!relationship->get_has_fields()) //TODO: Handle related_record has_fields.
+  {
+    if(relationship->get_has_to_table())
+    {
+      //It is a relationship that only specifies the table, without specifying linking fields:
+      builder->select_add_target(relationship->get_to_table());
+    }
+
+    return;
+  }
+    
+  // Define the alias name as returned by get_sql_join_alias_name():
+
+  // Specify an alias, to avoid ambiguity when using 2 relationships to the same table.
+  const Glib::ustring alias_name = uses_relationship->get_sql_join_alias_name();
+
+  // Add the JOIN:
+  if(!uses_relationship->get_has_related_relationship_name())
+  {
+    const guint to_target_id = builder->select_add_target(relationship->get_to_table(), alias_name);
+
+    builder->select_join_targets(
+      builder->select_add_target(relationship->get_from_table()),
+      to_target_id,
+      Gnome::Gda::SQL_SELECT_JOIN_LEFT,
+      builder->add_cond(
+        Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
+        builder->add_id("\"" + relationship->get_from_table() + "\".\"" + relationship->get_from_field() + "\""),
+        builder->add_id("\"" + alias_name + "\".\"" + relationship->get_to_field() + "\"") ) );
+  }
+  else
+  {
+     UsesRelationship parent_relationship;
+     parent_relationship.set_relationship(relationship);
+     sharedptr<const Relationship> related_relationship = uses_relationship->get_related_relationship();
+
+     const guint to_target_id = builder->select_add_target(related_relationship->get_to_table(), alias_name);
+
+     builder->select_join_targets(
+       builder->select_add_target(relationship->get_from_table()), //TODO: Must we use the ID from select_add_target_id()?
+       to_target_id,
+       Gnome::Gda::SQL_SELECT_JOIN_LEFT,
+       builder->add_cond(
+         Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
+         builder->add_id("\"" + parent_relationship.get_sql_join_alias_name() + "\".\"" + related_relationship->get_from_field() + "\""),
+         builder->add_id("\"" + alias_name + "\".\"" + related_relationship->get_to_field() + "\"") ) );
+  }
+}
+
+void Utils::build_sql_select_add_fields_to_get(const Glib::RefPtr<Gnome::Gda::SqlBuilder>& builder, const Glib::ustring& table_name, const type_vecConstLayoutFields& fieldsToGet, const type_sort_clause& sort_clause, bool extra_join)
 {
   //Get all relationships used in the query:
   typedef std::list< sharedptr<const UsesRelationship> > type_list_relationships;
@@ -263,51 +315,7 @@ void Utils::build_sql_select_add_fields_to_get(const Glib::RefPtr<Gnome::Gda::Sq
   for(type_list_relationships::const_iterator iter = list_relationships.begin(); iter != list_relationships.end(); ++iter)
   {
     sharedptr<const UsesRelationship> uses_relationship = *iter;
-    sharedptr<const Relationship> relationship = uses_relationship->get_relationship();
-    if(relationship->get_has_fields()) //TODO: Handle related_record has_fields.
-    {
-      // Define the alias name as returned by get_sql_join_alias_name():
-
-      // Specify an alias, to avoid ambiguity when using 2 relationships to the same table.
-		  const Glib::ustring alias_name = uses_relationship->get_sql_join_alias_name();
-
-		  // Add the JOIN:
-		  if(!uses_relationship->get_has_related_relationship_name())
-		  {
-		    const guint to_target_id = builder->select_add_target(relationship->get_to_table(), alias_name);
-
-		    builder->select_join_targets(
-		      builder->select_add_target(relationship->get_from_table()),
-		      to_target_id,
-		      Gnome::Gda::SQL_SELECT_JOIN_LEFT,
-		      builder->add_cond(
-		        Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
-		        builder->add_id("\"" + relationship->get_from_table() + "\".\"" + relationship->get_from_field() + "\""),
-		        builder->add_id("\"" + alias_name + "\".\"" + relationship->get_to_field() + "\"") ) );
-		  }
-		  else
-		  {
-		     UsesRelationship parent_relationship;
-		     parent_relationship.set_relationship(relationship);
-		     sharedptr<const Relationship> related_relationship = uses_relationship->get_related_relationship();
-
-		     const guint to_target_id = builder->select_add_target(related_relationship->get_to_table(), alias_name);
-
-		     builder->select_join_targets(
-		       builder->select_add_target(relationship->get_from_table()), //TODO: Must we use the ID from select_add_target_id()?
-		       to_target_id,
-		       Gnome::Gda::SQL_SELECT_JOIN_LEFT,
-		       builder->add_cond(
-		         Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
-		         builder->add_id("\"" + parent_relationship.get_sql_join_alias_name() + "\".\"" + related_relationship->get_from_field() + "\""),
-		         builder->add_id("\"" + alias_name + "\".\"" + related_relationship->get_to_field() + "\"") ) );
-		  }
-    }
-    else if(relationship->get_has_to_table())
-    {
-      //It is a relationship that only specifies the table, without specifying linking fields:
-      builder->select_add_target(relationship->get_to_table());
-    }
+    builder_add_join(builder, uses_relationship);
   }
   
   bool one_added = false;
@@ -334,7 +342,13 @@ void Utils::build_sql_select_add_fields_to_get(const Glib::RefPtr<Gnome::Gda::Sq
       builder->add_field_id(id_function);
     }
     else
-      builder->select_add_field(layout_item->get_name(), parent);
+    {
+      const guint id = builder->select_add_field(layout_item->get_name(), parent);
+
+      //Avoid duplicate records with doubly-related fields:
+      if(extra_join)
+        builder->select_group_by(id);
+    }
   
     
     one_added = true;
@@ -348,19 +362,23 @@ void Utils::build_sql_select_add_fields_to_get(const Glib::RefPtr<Gnome::Gda::Sq
 }
 
 
-Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_select_with_where_clause(const Glib::ustring& table_name, const type_vecConstLayoutFields& fieldsToGet, const Gnome::Gda::SqlExpr& where_clause, const Glib::ustring& extra_join, const type_sort_clause& sort_clause, const Glib::ustring& extra_group_by, guint limit)
+Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_select_with_where_clause(const Glib::ustring& table_name, const type_vecConstLayoutFields& fieldsToGet, const Gnome::Gda::SqlExpr& where_clause, const sharedptr<const Relationship>& extra_join, const type_sort_clause& sort_clause, guint limit)
 {
   //Build the whole SQL statement:
   Glib::RefPtr<Gnome::Gda::SqlBuilder> builder = Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_SELECT);
   builder->select_add_target(table_name);
 
   //Add the fields to SELECT, plus the tables that they are selected FROM.
-  Utils::build_sql_select_add_fields_to_get(builder, table_name, fieldsToGet, sort_clause);
-  //builder->select_add_field(primary_key->get_name(), table_name);
+  //We tell it whether extra_join is empty, so it can do an extra GROUP BY if necessary.
+  //TODO: Try to use DISTINCT instead, with a proper test case.
+  Utils::build_sql_select_add_fields_to_get(builder, table_name, fieldsToGet, sort_clause, extra_join /* bool */);
   
-  //TODO:
-  //if(!extra_join.empty())
- //   sql_part_leftouterjoin += (' ' + extra_join + ' ');
+  if(extra_join)
+  {
+    sharedptr<UsesRelationship> uses_relationship;
+    uses_relationship->set_relationship(extra_join);
+    builder_add_join(builder, uses_relationship);
+  }
 
   //Add the WHERE clause:
   if(!where_clause.empty())
@@ -369,13 +387,6 @@ Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_select_with_where_clause(c
     builder->set_where(id);
   }
 
-  //Extra GROUP_BY clause for doubly-related records. This must be before the ORDER BY sort clause:
-  //TODO:
-  //if(!extra_group_by.empty())
-  //{
-  //  result += (' ' + extra_group_by + ' ');
-  //}
-
   //Sort clause:
   if(!sort_clause.empty())
   {
@@ -446,7 +457,7 @@ Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_select_with_key(const Glib
   {
     const Gnome::Gda::SqlExpr where_clause = build_simple_where_expression(table_name, key_field, key_value);
     return Utils::build_sql_select_with_where_clause(table_name, fieldsToGet, where_clause,
-      Glib::ustring(), type_sort_clause(),  Glib::ustring(), limit);
+      sharedptr<const Relationship>(), type_sort_clause(), limit);
   }
 
   return Glib::RefPtr<Gnome::Gda::SqlBuilder>();
diff --git a/glom/libglom/utils.h b/glom/libglom/utils.h
index a69994f..870a85b 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -61,7 +61,8 @@ void build_sql_select_add_fields_to_get(
   const Glib::RefPtr<Gnome::Gda::SqlBuilder>& builder,
   const Glib::ustring& table_name,
   const type_vecConstLayoutFields& fieldsToGet,
-  const type_sort_clause& sort_clause);
+  const type_sort_clause& sort_clause,
+  bool extra_join);
 
 /** Generate a SQL statement to SELECT field values,
  * even if the fields are in related (or doubly related) records,
@@ -71,9 +72,8 @@ Glib::RefPtr<Gnome::Gda::SqlBuilder> build_sql_select_with_where_clause(
   const Glib::ustring& table_name,
   const type_vecLayoutFields& fieldsToGet,
   const Gnome::Gda::SqlExpr& where_clause = Gnome::Gda::SqlExpr(),
-  const Glib::ustring& extra_join = Glib::ustring(),
+  const sharedptr<const Relationship>& extra_join = sharedptr<const Relationship>(),
   const type_sort_clause& sort_clause = type_sort_clause(),
-  const Glib::ustring& extra_group_by = Glib::ustring(),
   guint limit = 0);
 
 /** Just a version of build_sql_select_with_where_clause() that takes a list of const fields.
@@ -82,9 +82,8 @@ Glib::RefPtr<Gnome::Gda::SqlBuilder> build_sql_select_with_where_clause(
   const Glib::ustring& table_name,
   const type_vecConstLayoutFields& fieldsToGet,
   const Gnome::Gda::SqlExpr& where_clause = Gnome::Gda::SqlExpr(),
-  const Glib::ustring& extra_join = Glib::ustring(),
+  const sharedptr<const Relationship>& extra_join = sharedptr<const Relationship>(),
   const type_sort_clause& sort_clause = type_sort_clause(),
-  const Glib::ustring& extra_group_by = Glib::ustring(),
   guint limit = 0);
 
 Glib::RefPtr<Gnome::Gda::SqlBuilder> build_sql_select_with_key(
diff --git a/glom/mode_data/box_data_calendar_related.cc b/glom/mode_data/box_data_calendar_related.cc
index 4316bd5..1942bfd 100644
--- a/glom/mode_data/box_data_calendar_related.cc
+++ b/glom/mode_data/box_data_calendar_related.cc
@@ -197,7 +197,7 @@ bool Box_Data_Calendar_Related::fill_from_database()
     //Do one SQL query for the whole month and store the cached values here:
     clear_cached_database_values();
 
-    Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query = Utils::build_sql_select_with_where_clause(m_found_set.m_table_name, m_FieldsShown, where_clause, m_found_set.m_extra_join, m_found_set.m_sort_clause, m_found_set.m_extra_group_by);
+    Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query = Utils::build_sql_select_with_where_clause(m_found_set.m_table_name, m_FieldsShown, where_clause, m_found_set.m_extra_join, m_found_set.m_sort_clause);
     //std::cout << "DEBUG: sql_query=" << sql_query << std::endl;
     Glib::RefPtr<const Gnome::Gda::DataModel> datamodel = DbUtils::query_execute_select(sql_query);
     if(!(datamodel))
diff --git a/glom/print_layout/canvas_print_layout.cc b/glom/print_layout/canvas_print_layout.cc
index 1fe9e08..d07aeaa 100644
--- a/glom/print_layout/canvas_print_layout.cc
+++ b/glom/print_layout/canvas_print_layout.cc
@@ -627,8 +627,7 @@ void Canvas_PrintLayout::fill_with_data(const Glib::RefPtr<Goocanvas::Group>& ca
 
   Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query = Utils::build_sql_select_with_where_clause(found_set.m_table_name,
     fieldsToGet,
-    found_set.m_where_clause, Glib::ustring() /* extra_join */, found_set.m_sort_clause, 
-    Glib::ustring(),
+    found_set.m_where_clause, sharedptr<const Relationship>() /* extra_join */, found_set.m_sort_clause,
     1);
   
   bool records_found = false;
@@ -732,7 +731,7 @@ void Canvas_PrintLayout::fill_with_data_portal(const Glib::RefPtr<CanvasLayoutIt
   found_set.m_table_name = portal->get_table_used(Glib::ustring() /* parent table_name, not used. */);
   set_found_set_where_clause_for_portal(found_set, portal, foreign_key_value);
 
-  Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query = Utils::build_sql_select_with_where_clause(found_set.m_table_name, fields_shown, found_set.m_where_clause, found_set.m_extra_join, found_set.m_sort_clause, found_set.m_extra_group_by);
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query = Utils::build_sql_select_with_where_clause(found_set.m_table_name, fields_shown, found_set.m_where_clause, found_set.m_extra_join, found_set.m_sort_clause);
   //std::cout << "DEBUG: sql_query=" << sql_query << std::endl;
   Glib::RefPtr<Gnome::Gda::DataModel> datamodel = DbUtils::query_execute_select(sql_query);
   if(!(datamodel))
diff --git a/glom/report_builder.cc b/glom/report_builder.cc
index 291bb90..84c43df 100644
--- a/glom/report_builder.cc
+++ b/glom/report_builder.cc
@@ -323,8 +323,7 @@ void ReportBuilder::report_build_records(const FoundSet& found_set, xmlpp::Eleme
 
     Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query = Utils::build_sql_select_with_where_clause(found_set.m_table_name,
       fieldsToGet,
-      found_set.m_where_clause, Glib::ustring() /* extra_join */, found_set.m_sort_clause,
-      Glib::ustring(),
+      found_set.m_where_clause, sharedptr<const Relationship>() /* extra_join */, found_set.m_sort_clause,
       limit);
 
     bool records_found = false;
diff --git a/glom/utility_widgets/db_adddel/glom_db_treemodel.cc b/glom/utility_widgets/db_adddel/glom_db_treemodel.cc
index 80d1647..77d29da 100644
--- a/glom/utility_widgets/db_adddel/glom_db_treemodel.cc
+++ b/glom/utility_widgets/db_adddel/glom_db_treemodel.cc
@@ -271,7 +271,7 @@ bool DbTreeModel::refresh_from_database(const FoundSet& found_set)
 
   if(m_connection && !m_found_set.m_table_name.empty() && m_get_records)
   {
-    Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query = Utils::build_sql_select_with_where_clause(m_found_set.m_table_name, m_column_fields, m_found_set.m_where_clause, m_found_set.m_extra_join, m_found_set.m_sort_clause, m_found_set.m_extra_group_by);
+    Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query = Utils::build_sql_select_with_where_clause(m_found_set.m_table_name, m_column_fields, m_found_set.m_where_clause, m_found_set.m_extra_join, m_found_set.m_sort_clause);
     //std::cout << "  Debug: DbTreeModel::refresh_from_database():  " << sql_query << std::endl;
 
     m_gda_datamodel = DbUtils::query_execute_select(sql_query, true /* use_cursor */);
@@ -301,7 +301,7 @@ bool DbTreeModel::refresh_from_database(const FoundSet& found_set)
 
       //This doesn't work with cursor-based models: const int count = m_gda_datamodel->get_n_rows();
       //because rows count is -1 until we have iterated to the last row.
-      Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query_without_sort = Utils::build_sql_select_with_where_clause(m_found_set.m_table_name, m_column_fields, m_found_set.m_where_clause, m_found_set.m_extra_join, type_sort_clause(), m_found_set.m_extra_group_by);
+      Glib::RefPtr<Gnome::Gda::SqlBuilder> sql_query_without_sort = Utils::build_sql_select_with_where_clause(m_found_set.m_table_name, m_column_fields, m_found_set.m_where_clause, m_found_set.m_extra_join, type_sort_clause());
       const int count = Base_DB::count_rows_returned_by(sql_query_without_sort);
       if(count < 0)
       {



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