[glom] Replaced Utils::build_sql_select_count_row() with DbUtils::count_rows_returned_by().



commit ab8d63e8710c0438bbeb29ac3590ba1f3dbe6761
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Oct 20 13:02:15 2011 +0200

    Replaced Utils::build_sql_select_count_row() with DbUtils::count_rows_returned_by().
    
    	* glom/libglom/utils.cc:
    	* glom/base_db.[h|cc]: Moved count_rows_returned_by() to
     	* glom/libglom/db_utils.[h|cc]: and also moved
      Utils::build_sql_select_count_row() making it static (not API).
    	* glom/frame_glom.cc:
    	* glom/mode_data/datawidget/treemodel_db.cc: Adapted.

 ChangeLog                                 |   13 +++++-
 glom/base_db.cc                           |   30 -------------
 glom/base_db.h                            |    2 -
 glom/frame_glom.cc                        |    2 +-
 glom/libglom/db_utils.cc                  |   67 +++++++++++++++++++++++++++++
 glom/libglom/db_utils.h                   |   10 ++++
 glom/libglom/utils.cc                     |   25 -----------
 glom/libglom/utils.h                      |   10 ----
 glom/mode_data/datawidget/treemodel_db.cc |    2 +-
 9 files changed, 91 insertions(+), 70 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index b8baed6..6c01189 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,15 @@
-2011-10-19  Murray Cumming  <murrayc murrayc com>>
+2011-10-20  Murray Cumming  <murrayc murrayc com>
+
+	Replaced Utils::build_sql_select_count_row() with DbUtils::count_rows_returned_by().
+
+	* glom/libglom/utils.cc:
+	* glom/base_db.[h|cc]: Moved count_rows_returned_by() to 
+ 	* glom/libglom/db_utils.[h|cc]: and also moved 
+  Utils::build_sql_select_count_row() making it static (not API).
+	* glom/frame_glom.cc:
+	* glom/mode_data/datawidget/treemodel_db.cc: Adapted.
+
+2011-10-19  Murray Cumming  <murrayc murrayc com>
 
 	SQL Injection Test: Try to avoid quoting by giving a wrong field type.
 
diff --git a/glom/base_db.cc b/glom/base_db.cc
index 1c44828..53e11b4 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -1685,36 +1685,6 @@ 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<const Gnome::Gda::SqlBuilder>& sql_query)
-{
-  if(!sql_query)
-  {
-    std::cerr << G_STRFUNC << ": sql_query was null." << std::endl;
-    return 0;
-  }
-
-  Glib::RefPtr<Gnome::Gda::SqlBuilder> builder =
-    Utils::build_sql_select_count_rows(sql_query);
-
-  int result = 0;
-
-  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;
-}
-
-
 void Base_DB::set_found_set_where_clause_for_portal(FoundSet& found_set, const sharedptr<LayoutItem_Portal>& portal, const Gnome::Gda::Value& foreign_key_value)
 {
   found_set.m_table_name = Glib::ustring();
diff --git a/glom/base_db.h b/glom/base_db.h
index 796e338..10fa135 100644
--- a/glom/base_db.h
+++ b/glom/base_db.h
@@ -70,8 +70,6 @@ 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<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;
 
   typedef std::vector< sharedptr<Field> > type_vec_fields;
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index 1293924..9951da7 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -403,7 +403,7 @@ void Frame_Glom::show_table_allow_empty(const Glib::ustring& table_name, const G
           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());
-          const int count = Base_DB::count_rows_returned_by(sql_query_without_sort);
+          const int count = DbUtils::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 */) );
         }
diff --git a/glom/libglom/db_utils.cc b/glom/libglom/db_utils.cc
index 7cc7068..8c02f65 100644
--- a/glom/libglom/db_utils.cc
+++ b/glom/libglom/db_utils.cc
@@ -1798,6 +1798,73 @@ Glib::ustring get_unused_database_name(const Glib::ustring& base_name)
   return Glib::ustring();    
 }
 
+/** 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.
+ */
+static Glib::RefPtr<Gnome::Gda::SqlBuilder> 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 << G_STRFUNC << ": 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;
+}
+
+int count_rows_returned_by(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& sql_query)
+{
+  if(!sql_query)
+  {
+    std::cerr << G_STRFUNC << ": sql_query was null." << std::endl;
+    return 0;
+  }
+
+  const Glib::RefPtr<const Gnome::Gda::SqlBuilder> builder =
+    build_sql_select_count_rows(sql_query);
+
+  int result = 0;
+
+  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) //With the PostgreSQL backend.
+      result = (int)value.get_int64();
+    else if(value.get_value_type() == G_TYPE_INT) //With the PostgreSQL backend.
+    {
+      result = value.get_int(); //With the SQLite backend.
+    }
+    else
+    {
+      std::cerr << G_STRFUNC << ": The COUNT query returned an unexpected value type: " << g_type_name(value.get_value_type()) << std::endl;
+      result = -1;
+    }
+  }
+
+  //std::cout << "debug: " << G_STRFUNC << ": Returning " << result << std::endl;
+  return result;
+}
+
 } //namespace DbUtils
 
 } //namespace Glom
diff --git a/glom/libglom/db_utils.h b/glom/libglom/db_utils.h
index 8b5950a..26a8bbb 100644
--- a/glom/libglom/db_utils.h
+++ b/glom/libglom/db_utils.h
@@ -129,6 +129,16 @@ bool layout_field_should_have_navigation(const Glib::ustring& table_name, const
  */
 Glib::ustring get_unused_database_name(const Glib::ustring& base_name);
 
+/** 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. Or -1 if something went wrong.
+ */
+int count_rows_returned_by(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& sql_query);
+
 } //namespace DbUtils
 
 } //namespace Glom
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index cad1f49..6d10dc6 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -1118,31 +1118,6 @@ 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 8f37630..6f46c2e 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -119,16 +119,6 @@ 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.
diff --git a/glom/mode_data/datawidget/treemodel_db.cc b/glom/mode_data/datawidget/treemodel_db.cc
index 2e94c75..901f697 100644
--- a/glom/mode_data/datawidget/treemodel_db.cc
+++ b/glom/mode_data/datawidget/treemodel_db.cc
@@ -364,7 +364,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());
-      const int count = Base_DB::count_rows_returned_by(sql_query_without_sort);
+      const int count = DbUtils::count_rows_returned_by(sql_query_without_sort);
       if(count < 0)
       {
         std::cerr << G_STRFUNC << ": count is < 0" << std::endl;



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