[glom] libglom: Added utils::build_sql_select_count_rows().



commit c262aaddb886b6878421bde51d4dad39c251ad00
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Mar 18 13:33:43 2011 +0100

    libglom: Added utils::build_sql_select_count_rows().
    
    * glom/base_db.[h|cc]: Make count_rows_returned_by() take a const SqlBuilder.
    * glom/libglom/utils.[h|cc]: Move some of count_rows_returned_by() into a
    build_sql_select_count_rows() method, for the convenience of UIs such as
    OnlineGlom.
    Bug #645110 (Ben Konrath)

 ChangeLog             |   10 ++++++++++
 glom/base_db.cc       |   36 +++++++++++++-----------------------
 glom/base_db.h        |    2 +-
 glom/libglom/utils.cc |   31 ++++++++++++++++++++++++++++---
 glom/libglom/utils.h  |   10 ++++++++++
 5 files changed, 62 insertions(+), 27 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index afee355..58e56b4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2011-03-18  Murray Cumming  <murrayc murrayc com>
+
+	libglom: Added utils::build_sql_select_count_rows().
+
+	* glom/base_db.[h|cc]: Make count_rows_returned_by() take a const SqlBuilder.
+	* glom/libglom/utils.[h|cc]: Move some of count_rows_returned_by() into a
+	build_sql_select_count_rows() method, for the convenience of UIs such as
+	OnlineGlom.
+	Bug #645110 (Ben Konrath)
+
 2011-03-14  Murray Cumming  <murrayc murrayc com>
 
 	Do not crash if PyDateTime_IMPORT fails.
diff --git a/glom/base_db.cc b/glom/base_db.cc
index fde20f9..71fbad2 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -1799,7 +1799,7 @@ bool Base_DB::get_primary_key_is_in_foundset(const FoundSet& found_set, const Gn
     return false;
 }
 
-int Base_DB::count_rows_returned_by(const Glib::RefPtr<Gnome::Gda::SqlBuilder>& sql_query)
+int Base_DB::count_rows_returned_by(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& sql_query)
 {
   if(!sql_query)
   {
@@ -1808,31 +1808,21 @@ 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, "glomarbitraryalias");
-
-  const Gnome::Gda::SqlBuilder::Id id_function = builder->add_function("COUNT", builder->add_id("*"));
-  builder->add_field_value_id(id_function);
-
+    Utils::build_sql_select_count_rows(sql_query);
 
   int result = 0;
 
-  //TODO: Is this inefficient?
-    Glib::RefPtr<Gnome::Gda::DataModel> datamodel = DbUtils::query_execute_select(builder);
-    if(datamodel && datamodel->get_n_rows() && datamodel->get_n_columns())
-    {
-      Gnome::Gda::Value value = datamodel->get_value_at(0, 0);
-      //This showed me that this contains a gint64: std::cerr << "DEBUG: value type=" << G_VALUE_TYPE_NAME(value.gobj()) << std::endl;
-      //For sqlite, this is an integer
-      if(value.get_value_type() == G_TYPE_INT64)
-        result = (int)value.get_int64();
-      else
-        result = value.get_int();
-    }
+  Glib::RefPtr<Gnome::Gda::DataModel> datamodel = DbUtils::query_execute_select(builder);
+  if(datamodel && datamodel->get_n_rows() && datamodel->get_n_columns())
+  {
+    const Gnome::Gda::Value value = datamodel->get_value_at(0, 0);
+    //This showed me that this contains a gint64: std::cerr << "DEBUG: value type=" << G_VALUE_TYPE_NAME(value.gobj()) << std::endl;
+    //For sqlite, this is an integer
+    if(value.get_value_type() == G_TYPE_INT64)
+      result = (int)value.get_int64();
+    else
+      result = value.get_int();
+  }
 
   //std::cout << "debug: " << G_STRFUNC << ": Returning " << result << std::endl;
   return result;
diff --git a/glom/base_db.h b/glom/base_db.h
index 79e3873..d2e8aca 100644
--- a/glom/base_db.h
+++ b/glom/base_db.h
@@ -70,7 +70,7 @@ public:
   virtual void set_document(Document* pDocument); //View override
   virtual void load_from_document(); //View override
 
-  static int count_rows_returned_by(const Glib::RefPtr<Gnome::Gda::SqlBuilder>& sql_query);
+  static int count_rows_returned_by(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& sql_query);
 
   sharedptr<Field> change_column(const Glib::ustring& table_name, const sharedptr<const Field>& field_old, const sharedptr<const Field>& field, Gtk::Window* parent_window) const;
 
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index baa1b7a..6d33559 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -631,7 +631,7 @@ Glib::ustring Utils::string_escape_underscores(const Glib::ustring& text)
 Glib::ustring Utils::locale_simplify(const Glib::ustring& locale_id)
 {
   Glib::ustring result = locale_id;
-  
+
   //At least Ubuntu Natty provides a long string such as this: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=en_US.UTF-8;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_AG.utf8;LC_PAPER=en_US.UTF-8;LC_NAME=en_US.UTF-8;LC_ADDRESS=en_US.UTF-8;LC_TELEPHONE=en_US.UTF-8;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=en_US.UTF-8
   //In Ubuntu Maverick, and earlier, it was apparently a simple string such as en_US.UTF-8.
 
@@ -651,7 +651,7 @@ Glib::ustring Utils::locale_simplify(const Glib::ustring& locale_id)
       result = result.substr(posCategory);
     }
   }
-  
+
   //Get everything before the .:
   const Glib::ustring::size_type posDot = result.find('.');
   if(posDot != Glib::ustring::npos)
@@ -665,7 +665,7 @@ Glib::ustring Utils::locale_simplify(const Glib::ustring& locale_id)
   {
     result = result.substr(0, posAt);
   }
-  
+
   //Get everything after the =, if any:
   const Glib::ustring::size_type posEquals = result.find('=');
   if(posEquals != Glib::ustring::npos)
@@ -1094,6 +1094,31 @@ Gnome::Gda::SqlExpr Utils::get_find_where_clause_quick(const Document* document,
   }
 }
 
+
+Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_select_count_rows(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& sql_query)
+{
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> result;
+
+  if(!sql_query)
+  {
+    std::cerr << "Base_DB::count_rows_returned_by(): sql_query was null." << std::endl;
+    return result;
+  }
+
+  result = 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 = result->add_sub_select( sql_query->get_sql_statement() );
+  result->select_add_target_id(target_id, "glomarbitraryalias");
+
+  const Gnome::Gda::SqlBuilder::Id id_function = result->add_function("COUNT", result->add_id("*"));
+  result->add_field_value_id(id_function);
+
+  return result;
+}
+
+
 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 3083ec1..498721b 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -115,6 +115,16 @@ 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);
 
+/** Build a SQL query to discover how many rows a SQL query would return if it was run.
+ *
+ * This uses a COUNT * on a the @a sql_query as a sub-statement.
+ * Be careful not to include ORDER BY clauses in the supplied SQL query, because that would make it unnecessarily slow.
+ *
+ * @sql_query A SQL query.
+ * @result The number of rows.
+ */
+Glib::RefPtr<Gnome::Gda::SqlBuilder> build_sql_select_count_rows(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& sql_query);
+
 type_list_values_with_second get_choice_values(const Document* document, const sharedptr<const LayoutItem_Field>& field, const Gnome::Gda::Value& foreign_key_value);
 
 /// Get the full query string suitable for use with std::cout.



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