[glom/glom-1-18] libglom: Added utils::build_sql_select_count_rows().



commit 8b9af4ac472454c2a5f81a67e3abfc243b6fbeac
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       |   45 +++++++++++++++------------------------------
 glom/base_db.h        |    2 +-
 glom/libglom/utils.cc |   31 ++++++++++++++++++++++++++++---
 glom/libglom/utils.h  |   10 ++++++++++
 5 files changed, 64 insertions(+), 34 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index c0b369d..3dec573 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 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-18  Murray Cumming  <murrayc murrayc com>
+
 	ComboEntry: Really have an entry.
 
 	* glom/mode_data/datawidget/comboentry.cc: Constructor: Pass true to the
diff --git a/glom/base_db.cc b/glom/base_db.cc
index 2528fba..cd3231e 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -1886,45 +1886,30 @@ 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)
-  {
-    std::cerr << "Base_DB::count_rows_returned_by(): sql_query was null." << std::endl;
-    return 0;
-  }
-
   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())
-    {
+  Glib::RefPtr<Gnome::Gda::DataModel> datamodel = DbUtils::query_execute_select(builder);
+  if(datamodel && datamodel->get_n_rows() && datamodel->get_n_columns())
+  {
 #ifdef GLIBMM_EXCEPTIONS_ENABLED
-      Gnome::Gda::Value value = datamodel->get_value_at(0, 0);
+    Gnome::Gda::Value value = datamodel->get_value_at(0, 0);
 #else
-      std::auto_ptr<Glib::Error> value_error;
-      Gnome::Gda::Value value = datamodel->get_value_at(0, 0, value_error);
+    std::auto_ptr<Glib::Error> value_error;
+    Gnome::Gda::Value value = datamodel->get_value_at(0, 0, value_error);
 #endif
-      //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();
-    }
+    //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: count_rows_returned_by(): Returning " << result << std::endl;
   return result;
diff --git a/glom/base_db.h b/glom/base_db.h
index baadd32..38e54e3 100644
--- a/glom/base_db.h
+++ b/glom/base_db.h
@@ -74,7 +74,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 9b6728b..9879c73 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -616,7 +616,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.
 
@@ -636,7 +636,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)
@@ -650,7 +650,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)
@@ -1079,6 +1079,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 13339c6..39c7156 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -111,6 +111,16 @@ Glib::RefPtr<Gnome::Gda::SqlBuilder> build_sql_select_with_key(
 
 Gnome::Gda::SqlExpr get_find_where_clause_quick(const Document* document, const Glib::ustring& table_name, const Gnome::Gda::Value& quick_search);
 
+/** 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);
+
 
 typedef std::list< std::pair<Gnome::Gda::Value, Gnome::Gda::Value> > type_list_values_with_second;
 type_list_values_with_second get_choice_values_all(const Document* document, const sharedptr<const LayoutItem_Field>& field, sharedptr<const LayoutItem_Field>& layout_choice_first, sharedptr<const LayoutItem_Field>& layout_choice_second);



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