[glom/sqlbuilder2] Correct use of SQL functions with SqlBuilder.



commit a7e84d467d0716209a0a1a440bfd7a8d7531ff62
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu May 20 12:44:12 2010 +0200

    Correct use of SQL functions with SqlBuilder.
    
    * glom/base_db.cc: count_rows_returned_by(): Use SqlBuilder::add_field_id()
        so the function is really used. And add a target alias. This now works.
    * glom/libglom/python_embed/py_glom_relatedrecord.cc: generic_aggregate()
    * glom/libglom/utils.cc: build_sql_select_add_fields_to_get():
    * glom/utility_widgets/db_adddel/glom_db_treemodel.cc: get_record_counts():
        Use add_field_id() when adding a function to SqlBuilder, so it is
        really used.

 ChangeLog                                          |   12 ++++++++++++
 glom/base_db.cc                                    |   14 ++++++--------
 glom/libglom/python_embed/py_glom_relatedrecord.cc |    5 ++++-
 glom/libglom/utils.cc                              |    3 ++-
 .../utility_widgets/db_adddel/glom_db_treemodel.cc |    5 ++++-
 5 files changed, 28 insertions(+), 11 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 1d46a1a..70e0ca7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2010-05-20  Murray Cumming  <murrayc murrayc com>
+
+    Correct use of SQL functions with SqlBuilder.
+
+	* glom/base_db.cc: count_rows_returned_by(): Use SqlBuilder::add_field_id()
+    so the function is really used. And add a target alias. This now works.
+	* glom/libglom/python_embed/py_glom_relatedrecord.cc: generic_aggregate()
+	* glom/libglom/utils.cc: build_sql_select_add_fields_to_get():
+	* glom/utility_widgets/db_adddel/glom_db_treemodel.cc: get_record_counts():
+    Use add_field_id() when adding a function to SqlBuilder, so it is
+    really used.
+
 2010-05-19  Murray Cumming  <murrayc murrayc com>
 
 	More use of SqlBuilder. Almost finished.
diff --git a/glom/base_db.cc b/glom/base_db.cc
index 79c2535..e983a4a 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -1887,7 +1887,6 @@ bool Base_DB::get_primary_key_is_in_foundset(const FoundSet& found_set, const Gn
 
 int Base_DB::count_rows_returned_by(const Glib::RefPtr<Gnome::Gda::SqlBuilder>& sql_query)
 {
-  std::cout << "Base_DB::count_rows_returned_by(): debug: input sql_query=" << Utils::sqlbuilder_get_full_query(sql_query) << std::endl;
   if(!sql_query)
   {
     std::cerr << "Base_DB::count_rows_returned_by(): sql_query was null." << std::endl;
@@ -1896,20 +1895,19 @@ int Base_DB::count_rows_returned_by(const Glib::RefPtr<Gnome::Gda::SqlBuilder>&
 
   Glib::RefPtr<Gnome::Gda::SqlBuilder> builder =
     Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_SELECT);
+
+  //Note that the alias is just because the SQL syntax requires it - we get an error if we don't use it.
+  //Be careful not to include ORDER BY clauses in this, because that would make it unnecessarily slow:
   const guint target_id = builder->add_sub_select( sql_query->get_sql_statement() );
-  builder->select_add_target_id(target_id);
+  builder->select_add_target_id(target_id, "glomarbitraryalias");
 
-  builder->add_function("COUNT", builder->add_id("*"));
+  const guint id_function = builder->add_function("COUNT", builder->add_id("*"));
+  builder->add_field_id(id_function);
 
 
   int result = 0;
 
-  //TODO: Use SqlBuilder for this?
   //TODO: Is this inefficient?
-  //Note that the alias is just because the SQL syntax requires it - we get an error if we don't use it.
-  //Be careful not to include ORDER BY clauses in this, because that would make it unnecessarily slow:
-  //TODO: Add alias too? const Glib::ustring query_count = "SELECT COUNT (*) FROM (" + sql_query + ") AS glomarbitraryalias";
-
     Glib::RefPtr<Gnome::Gda::DataModel> datamodel = DbUtils::query_execute_select(builder);
     if(datamodel && datamodel->get_n_rows() && datamodel->get_n_columns())
     {
diff --git a/glom/libglom/python_embed/py_glom_relatedrecord.cc b/glom/libglom/python_embed/py_glom_relatedrecord.cc
index 0fb059b..1aea8b2 100644
--- a/glom/libglom/python_embed/py_glom_relatedrecord.cc
+++ b/glom/libglom/python_embed/py_glom_relatedrecord.cc
@@ -195,7 +195,10 @@ boost::python::object PyGlomRelatedRecord::generic_aggregate(const std::string&
   //Get the aggregate value from the related records:
   Glib::RefPtr<Gnome::Gda::SqlBuilder> builder =
     Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_SELECT);
-  builder->add_function(aggregate, builder->add_id(field_name)); //TODO: It would be nice to specify the table here too.
+
+  const guint id_function = builder->add_function(aggregate, builder->add_id(field_name)); //TODO: It would be nice to specify the table here too.
+  builder->add_field_id(id_function);
+
   builder->select_add_target(related_table);
   builder->set_where(
     builder->add_cond(Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index e62ec55..1d28349 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -294,9 +294,10 @@ void Utils::build_sql_select_add_fields_to_get(const Glib::RefPtr<Gnome::Gda::Sq
     const LayoutItem_FieldSummary* fieldsummary = dynamic_cast<const LayoutItem_FieldSummary*>(layout_item.obj());
     if(fieldsummary)
     {
-      builder->add_function(
+      const guint id_function = builder->add_function(
         fieldsummary->get_summary_type_sql(),
         builder->add_id(layout_item->get_sql_name(table_name)) ); //TODO: It would be nice to specify the table here too.
+      builder->add_field_id(id_function);
     }
     else
       builder->select_add_field(layout_item->get_name(), parent);
diff --git a/glom/utility_widgets/db_adddel/glom_db_treemodel.cc b/glom/utility_widgets/db_adddel/glom_db_treemodel.cc
index b845ba5..80d1647 100644
--- a/glom/utility_widgets/db_adddel/glom_db_treemodel.cc
+++ b/glom/utility_widgets/db_adddel/glom_db_treemodel.cc
@@ -908,7 +908,10 @@ void DbTreeModel::get_record_counts(gulong& total, gulong& found) const
 
       Glib::RefPtr<Gnome::Gda::SqlBuilder> builder =
         Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_SELECT);
-      builder->add_function("count", builder->add_id("*")); //TODO: Is * allowed here?
+
+      const guint id_function = builder->add_function("count", builder->add_id("*")); //TODO: Is * allowed here?
+      builder->add_field_id(id_function);
+
       builder->select_add_target(m_found_set.m_table_name);
 
       Glib::RefPtr<Gnome::Gda::DataModel> datamodel = DbUtils::query_execute_select(builder);



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