[glom/sqlbuilder2] More use of SqlBuilder.



commit ec31388f2d55f9f86984e5c667a3562bfe085ce8
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Apr 27 17:48:22 2010 +0200

    More use of SqlBuilder.
    
    * glom/base_db.[h|cc]: Move sqlbuilder_get_full_query() to Utils so that
        other code can use it.
    * glom/libglom/utils.[h|cc]: get_choice_values(): Use SqlBuilder.

 ChangeLog             |    8 +++
 glom/base_db.cc       |  139 ++++++++-----------------------------------------
 glom/base_db.h        |   33 +++++-------
 glom/libglom/utils.cc |  132 +++++++++++++++++++++++++++++++++++++++++------
 glom/libglom/utils.h  |   15 +++++
 5 files changed, 175 insertions(+), 152 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 9edf5b8..01a01a0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,14 @@
 
     More use of SqlBuilder.
 
+	* glom/base_db.[h|cc]: Move sqlbuilder_get_full_query() to Utils so that
+    other code can use it.
+	* glom/libglom/utils.[h|cc]: get_choice_values(): Use SqlBuilder.
+
+2010-04-27  Murray Cumming  <murrayc murrayc com>
+
+    More use of SqlBuilder.
+
 	* glom/report_builder.cc: report_build_groupby(): Use the new
     SqlBuilder::select_group_by() method to replace another SQL string.
 
diff --git a/glom/base_db.cc b/glom/base_db.cc
index dacbf64..9f98932 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -212,7 +212,7 @@ Glib::RefPtr<Gnome::Gda::DataModel> Base_DB::query_execute_select(const Glib::us
   const Application* app = Application::get_application();
   if(stmt && app && app->get_show_sql_debug())
   {
-    const std::string full_query = sqlbuilder_get_full_query(gda_connection, strQuery, params);
+    const std::string full_query = Utils::sqlbuilder_get_full_query(gda_connection, strQuery, params);
     std::cout << "Debug: Base_DB::query_execute_select():  " << full_query << std::endl;
   }
 
@@ -241,7 +241,7 @@ Glib::RefPtr<Gnome::Gda::DataModel> Base_DB::query_execute_select(const Glib::us
 
   if(!result)
   {
-    const std::string full_query = sqlbuilder_get_full_query(gda_connection, strQuery, params);
+    const std::string full_query = Utils::sqlbuilder_get_full_query(gda_connection, strQuery, params);
     std::cerr << "Glom  Base_DB::query_execute_select(): Error while executing SQL" << std::endl <<
       "  " <<  full_query << std::endl;
     handle_error();
@@ -281,10 +281,10 @@ Glib::RefPtr<Gnome::Gda::DataModel> Base_DB::query_execute_select(const Glib::Re
   const Application* app = Application::get_application();
   if(app && app->get_show_sql_debug())
   {
-    const std::string full_query = sqlbuilder_get_full_query(builder, params);
+    const std::string full_query = Utils::sqlbuilder_get_full_query(builder, params);
     std::cout << "Debug: Base_DB::query_execute_select():  " << full_query << std::endl;
   }
-  
+
 #ifdef GLIBMM_EXCEPTIONS_ENABLED
   try
   {
@@ -311,8 +311,8 @@ Glib::RefPtr<Gnome::Gda::DataModel> Base_DB::query_execute_select(const Glib::Re
 
   if(!result)
   {
-    const std::string full_query = sqlbuilder_get_full_query(builder, params);
-    std::cerr << "Glom  Base_DB::query_execute_select(): Error while executing SQL: " 
+    const std::string full_query = Utils::sqlbuilder_get_full_query(builder, params);
+    std::cerr << "Glom  Base_DB::query_execute_select(): Error while executing SQL: "
       << std::endl << "  " << full_query << std::endl << std::endl;
     handle_error();
   }
@@ -369,7 +369,7 @@ bool Base_DB::query_execute(const Glib::ustring& strQuery,
   const Application* app = Application::get_application();
   if(stmt && app && app->get_show_sql_debug())
   {
-    const std::string full_query = sqlbuilder_get_full_query(gda_connection, strQuery, params);
+    const std::string full_query = Utils::sqlbuilder_get_full_query(gda_connection, strQuery, params);
     std::cerr << "Debug: Base_DB::query_execute(): " << full_query << std::endl;
   }
 
@@ -383,7 +383,7 @@ bool Base_DB::query_execute(const Glib::ustring& strQuery,
   catch(const Glib::Error& error)
   {
     std::cerr << "BaseDB::query_execute: ConnectionError: " << error.what() << std::endl;
-    const std::string full_query = sqlbuilder_get_full_query(gda_connection, strQuery, params);
+    const std::string full_query = Utils::sqlbuilder_get_full_query(gda_connection, strQuery, params);
     std::cerr << "  full_query: " << full_query << std::endl;
     return false;
   }
@@ -393,7 +393,7 @@ bool Base_DB::query_execute(const Glib::ustring& strQuery,
   if(exec_error.get())
   {
     std::cerr << "BaseDB::query_execute: ConnectionError: " << exec_error->what() << std::endl;
-    const std::string full_query = sqlbuilder_get_full_query(gda_connection, stmt, params);
+    const std::string full_query = Utils::sqlbuilder_get_full_query(gda_connection, stmt, params);
     std::cerr << "  full_query: " << full_query << std::endl;
     return false;
   }
@@ -423,12 +423,12 @@ bool Base_DB::query_execute(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& bu
   }
 
   Glib::RefPtr<Gnome::Gda::Connection> gda_connection = sharedconnection->get_gda_connection();
-  
+
   //Debug output:
   const Application* app = Application::get_application();
   if(app && app->get_show_sql_debug())
   {
-    const std::string full_query = sqlbuilder_get_full_query(builder, params);
+    const std::string full_query = Utils::sqlbuilder_get_full_query(builder, params);
     std::cerr << "Debug: Base_DB::query_execute(): " << full_query << std::endl;
   }
 
@@ -442,7 +442,7 @@ bool Base_DB::query_execute(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& bu
   catch(const Glib::Error& error)
   {
     std::cerr << "BaseDB::query_execute: ConnectionError: " << error.what() << std::endl;
-    const std::string full_query = sqlbuilder_get_full_query(builder, params);
+    const std::string full_query = Utils::sqlbuilder_get_full_query(builder, params);
     std::cerr << "  full_query: " << full_query << std::endl;
     return false;
   }
@@ -452,7 +452,7 @@ bool Base_DB::query_execute(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& bu
   if(exec_error.get())
   {
     std::cerr << "BaseDB::query_execute: ConnectionError: " << exec_error->what() << std::endl;
-    const std::string full_query = sqlbuilder_get_full_query(builder, params);
+    const std::string full_query = Utils::sqlbuilder_get_full_query(builder, params);
     std::cerr << "  full_query: " << full_query << std::endl;
     return false;
   }
@@ -1155,7 +1155,7 @@ SystemPrefs Base_DB::get_database_preferences() const
   while(attempts < 2)
   {
     bool succeeded = true;
-    std::string full_query = sqlbuilder_get_full_query(builder);
+    std::string full_query = Utils::sqlbuilder_get_full_query(builder);
     Glib::RefPtr<Gnome::Gda::DataModel> datamodel = query_execute_select(builder);
     if(datamodel && (datamodel->get_n_rows() != 0))
     {
@@ -1440,7 +1440,7 @@ void Base_DB::set_database_preferences(const SystemPrefs& prefs)
   }
   builder->set_where(builder->add_cond(Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
                                        builder->add_id(GLOM_STANDARD_TABLE_PREFS_FIELD_ID),
-                                       builder->add_expr(1)));                                 
+                                       builder->add_expr(1)));
   const bool test = query_execute(builder);
   if(!test)
     std::cerr << "Base_DB::set_database_preferences(): UPDATE failed." << std::endl;
@@ -1893,7 +1893,7 @@ bool Base_DB::insert_example_data(const Glib::ustring& table_name) const
         builder->add_field_value_as_value(field->get_name(), value);
       }
     }
-    
+
     insert_succeeded = query_execute(builder);
     if(!insert_succeeded)
       break;
@@ -2028,7 +2028,7 @@ sharedptr<LayoutItem_Text> Base_DB::offer_textobject(const sharedptr<LayoutItem_
 
   Dialog_TextObject* dialog = 0;
   Utils::get_glade_widget_derived_with_warning(dialog);
-  
+
   if(transient_for)
     dialog->set_transient_for(*transient_for);
 
@@ -2052,7 +2052,7 @@ sharedptr<LayoutItem_Image> Base_DB::offer_imageobject(const sharedptr<LayoutIte
 
   Dialog_ImageObject* dialog = 0;
   Utils::get_glade_widget_derived_with_warning(dialog);
- 
+
   if(transient_for)
     dialog->set_transient_for(*transient_for);
 
@@ -2076,7 +2076,7 @@ sharedptr<LayoutItem_Notebook> Base_DB::offer_notebook(const sharedptr<LayoutIte
 
   Dialog_Notebook* dialog = 0;
   Utils::get_glade_widget_derived_with_warning(dialog);
-  
+
   if(transient_for)
     dialog->set_transient_for(*transient_for);
 
@@ -3146,7 +3146,7 @@ Gnome::Gda::Value Base_DB::get_lookup_value(const Glib::ustring& /* table_name *
       builder->add_cond(Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
         builder->add_id(to_key_field->get_name()), //TODO: It would be nice to specify the table here too.
         builder->add_expr(value_to_key_field)));
-        
+
     Glib::RefPtr<Gnome::Gda::DataModel> data_model = query_execute_select(builder);
     if(data_model && data_model->get_n_rows())
     {
@@ -3400,11 +3400,12 @@ int Base_DB::count_rows_returned_by(const Glib::ustring& sql_query)
 {
   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:
   const Glib::ustring query_count = "SELECT COUNT (*) FROM (" + sql_query + ") AS glomarbitraryalias";
-  
+
   /* TODO: Use SqlBuilder when we discover how to use a sub-query, or when this function can take a Sqlbuilder.
   Glib::RefPtr<Gnome::Gda::SqlBuilder> builder =
     Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_SELECT);
@@ -3740,101 +3741,5 @@ Glib::ustring Base_DB::get_active_layout_platform(Document* document)
 }
 
 
-std::string Base_DB::sqlbuilder_get_full_query(
-    const Glib::RefPtr<Gnome::Gda::Connection>& connection, 
-    const Glib::ustring& query,
-    const Glib::RefPtr<const Gnome::Gda::Set>& params)
-{
-  Glib::ustring result = "glom_query_not_parsed";
-  
-  try
-  {
-    Glib::RefPtr<Gnome::Gda::SqlParser> parser = connection->create_parser();
-    if(parser)
-    {
-      Glib::RefPtr<Gnome::Gda::Statement> stmt = parser->parse_string(query);
-      if(stmt)
-        result = stmt->to_sql(params);
-    }
-  }
-  catch(const Glib::Exception& ex)
-  {
-    std::cerr << "sqlbuilder_get_full_query(): exception while parsing query: " << ex.what() << std::endl;
-  }
-  catch(const std::exception& ex)
-  {
-    std::cerr << "sqlbuilder_get_full_query(): exception while parsing query: " << ex.what() << std::endl;
-  }
-  
-  //Convert to something that std::cout should be able to handle.
-  const Glib::ScopedPtr<char> buf(g_convert_with_fallback(
-    result.raw().data(), result.raw().size(), 
-    "ISO-8859-1", "UTF-8", 
-    (char*)"?", 
-    0, 0, 0));
-  return std::string(buf.get());
-}
-
-std::string Base_DB::sqlbuilder_get_full_query(
-  const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder,
-  const Glib::RefPtr<const Gnome::Gda::Set>& params)
-{
-  Glib::ustring result = "glom_query_not_parsed";
-  
-  try
-  {
-    Glib::RefPtr<Gnome::Gda::Statement> stmt = builder->get_statement();
-    if(stmt)
-      result = stmt->to_sql(params);
-  }
-  catch(const Glib::Exception& ex)
-  {
-    std::cerr << "sqlbuilder_get_full_query(): exception while getting query: " << ex.what() << std::endl;
-  }
-  catch(const std::exception& ex)
-  {
-    std::cerr << "sqlbuilder_get_full_query(): exception while getting query: " << ex.what() << std::endl;
-  }
-  
-  //Convert to something that std::cout should be able to handle.
-  const Glib::ScopedPtr<char> buf(g_convert_with_fallback(
-    result.raw().data(), result.raw().size(), 
-    "ISO-8859-1", "UTF-8", 
-    (char*)"?", 
-    0, 0, 0));
-  return std::string(buf.get());
-}
-
-std::string Base_DB::sqlbuilder_get_full_query(
-  const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder)
-{
-  Glib::ustring result = "glom_query_not_parsed";
-  
-  try
-  {
-    Glib::RefPtr<Gnome::Gda::Statement> stmt = builder->get_statement();
-    if(stmt)
-      result = stmt->to_sql();
-  }
-  catch(const Glib::Exception& ex)
-  {
-    std::cerr << "sqlbuilder_get_full_query(): exception while getting query: " << ex.what() << std::endl;
-  }
-  catch(const std::exception& ex)
-  {
-    std::cerr << "sqlbuilder_get_full_query(): exception while getting query: " << ex.what() << std::endl;
-  }
-  
-  //Convert to something that std::cout should be able to handle.
-  const Glib::ScopedPtr<char> buf(g_convert_with_fallback(
-    result.raw().data(), result.raw().size(), 
-    "ISO-8859-1", "UTF-8", 
-    (char*)"?", 
-    0, 0, 0));
-  return std::string(buf.get());
-}
-  
-  
-
 
 } //namespace Glom
diff --git a/glom/base_db.h b/glom/base_db.h
index bfa0b08..f46eb01 100644
--- a/glom/base_db.h
+++ b/glom/base_db.h
@@ -85,10 +85,10 @@ public:
    * This method handles any Gda exceptions caused by executing the command.
    */
   static Glib::RefPtr<Gnome::Gda::DataModel> query_execute_select(
-    const Glib::ustring& strQuery, 
+    const Glib::ustring& strQuery,
     const Glib::RefPtr<Gnome::Gda::Set>& params = Glib::RefPtr<Gnome::Gda::Set>(0));
   static Glib::RefPtr<Gnome::Gda::DataModel> query_execute_select(
-     const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder, 
+     const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder,
      const Glib::RefPtr<const Gnome::Gda::Set>& params = Glib::RefPtr<const Gnome::Gda::Set>(0));
 
 
@@ -99,9 +99,9 @@ public:
                             const Glib::RefPtr<Gnome::Gda::Set>& params = Glib::RefPtr<Gnome::Gda::Set>(0));
   static bool query_execute(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder,
                             const Glib::RefPtr<const Gnome::Gda::Set>& params = Glib::RefPtr<const Gnome::Gda::Set>(0));
-  
+
   static int count_rows_returned_by(const Glib::ustring& sql_query);
-  
+
 #ifndef GLOM_ENABLE_CLIENT_ONLY
   bool add_standard_groups();
   bool add_standard_tables() const;
@@ -142,17 +142,17 @@ protected:
   /** Allow the user to select a field from the list of fields for the table.
    */
   sharedptr<LayoutItem_Field> offer_field_list_select_one_field(const Glib::ustring& table_name, Gtk::Window* transient_for = 0);
-  
-  /** Allow the user to select a field from the list of fields for the table, 
+
+  /** Allow the user to select a field from the list of fields for the table,
    * with @a start_field selected by default.
    */
   sharedptr<LayoutItem_Field> offer_field_list_select_one_field(const sharedptr<const LayoutItem_Field>& start_field, const Glib::ustring& table_name, Gtk::Window* transient_for = 0);
-  
-  
+
+
   /** Allow the user to select fields from the list of fields for the table.
    */
   type_list_field_items offer_field_list(const Glib::ustring& table_name, Gtk::Window* transient_for = 0);
-  
+
 
   sharedptr<LayoutItem_Field> offer_field_formatting(const sharedptr<const LayoutItem_Field>& start_field, const Glib::ustring& table_name, Gtk::Window* transient_for = 0);
 
@@ -397,13 +397,13 @@ protected:
    */
   void set_found_set_where_clause_for_portal(FoundSet& found_set, const sharedptr<LayoutItem_Portal>& portal, const Gnome::Gda::Value& foreign_key_value);
 
-  /** Update GDA's information about the table structure, such as the 
+  /** Update GDA's information about the table structure, such as the
    * field list and their types.
    * Call this whenever changing the table structure, for instance with an ALTER query.
    * This may take a few seconds to return.
    */
   void update_gda_metastore_for_table(const Glib::ustring& table_name) const;
-  
+
   static Glib::RefPtr<Gnome::Gda::Connection> get_connection();
 
   static bool get_field_primary_key_index_for_fields(const type_vec_fields& fields, guint& field_column);
@@ -437,19 +437,14 @@ protected:
   static void handle_error(const Glib::Exception& ex);
   static void handle_error(const std::exception& ex); //TODO_port: This is probably useless now.
   static bool handle_error();
-  
+
 private:
-  /// Get the full query string suitable for use with std::cout.
-  static std::string sqlbuilder_get_full_query(
-    const Glib::RefPtr<Gnome::Gda::Connection>& connection, 
-    const Glib::ustring& query,
-    const Glib::RefPtr<const Gnome::Gda::Set>& params);
-    
+
   /// Get the full query string suitable for use with std::cout.
   static std::string sqlbuilder_get_full_query(
     const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder,
     const Glib::RefPtr<const Gnome::Gda::Set>& params);
-    
+
   /// Get the full query string suitable for use with std::cout.
   static std::string sqlbuilder_get_full_query(
     const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder);
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index ffa9bc9..49cb268 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -236,7 +236,7 @@ static void add_to_relationships_list(type_list_relationships& list_relationship
     list_relationships.push_back(uses_rel);
   }
 
- 
+
 }
 
 
@@ -307,7 +307,7 @@ Glib::ustring Utils::build_sql_select_fields_to_get(const Glib::ustring& table_n
     return sql_part_fields;
   }
 
-  //LEFT OUTER JOIN will get the field values from the other tables, 
+  //LEFT OUTER JOIN will get the field values from the other tables,
   //and give us our fields for this table even if there is no corresponding value in the other table.
   for(type_list_relationships::const_iterator iter = list_relationships.begin(); iter != list_relationships.end(); ++iter)
   {
@@ -340,7 +340,7 @@ Glib::ustring Utils::build_sql_select_with_where_clause(const Glib::ustring& tab
     table_name, fieldsToGet, sort_clause, sql_part_from, sql_part_leftouterjoin);
 
   //Build the whole SQL statement:
-  Glib::ustring result = 
+  Glib::ustring result =
     "SELECT " + sql_part_fields +
     " FROM \"" + table_name + '\"';
 
@@ -444,14 +444,17 @@ Utils::type_list_values_with_second Utils::get_choice_values(const sharedptr<con
   }
 
   const bool with_second = !choice_second.empty();
-  const Glib::ustring sql_second = "\"" + to_table + "\".\"" + choice_second + '\"';
 
   //Get possible values from database, sorted by the first column.
-  Glib::ustring sql_query = "SELECT \"" + to_table + "\".\"" + choice_field + '\"';
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> builder =
+      Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_SELECT);
+  builder->select_add_field(choice_field, to_table);
+  builder->select_add_target(to_table);
+
   if(with_second)
-    sql_query += ", " + sql_second;
+    builder->select_add_field(choice_second, to_table);
 
-  sql_query += " FROM \"" + choice_relationship->get_to_table() + "\" ORDER BY \"" + to_table + "\".\"" + choice_field + '\"';
+  builder->select_order_by( builder->add_id(choice_field) );
 
   //std::cout << "debug: get_choice_values(): query: " << sql_query << std::endl;
   //Connect to database:
@@ -467,6 +470,8 @@ Utils::type_list_values_with_second Utils::get_choice_values(const sharedptr<con
   if(!connection)
     return list_values;
 
+  const std::string sql_query =
+    sqlbuilder_get_full_query(builder);
   //std::cout << "get_choice_values: Executing SQL: " << sql_query << std::endl;
 #ifdef GLIBMM_EXCEPTIONS_ENABLED
   Glib::RefPtr<Gnome::Gda::DataModel> datamodel = connection->get_gda_connection()->statement_execute_select(sql_query);
@@ -495,7 +500,7 @@ Utils::type_list_values_with_second Utils::get_choice_values(const sharedptr<con
         itempair.second = datamodel->get_value_at(1, row, error);
 #endif
 
-      list_values.push_back(itempair);      
+      list_values.push_back(itempair);
     }
   }
   else
@@ -528,7 +533,7 @@ Glib::ustring Utils::string_escape_underscores(const Glib::ustring& text)
   return result;
 }
 
-/** Get just the first part of a locale, such as de_DE, 
+/** Get just the first part of a locale, such as de_DE,
  * ignoring, for instance, .UTF-8 or @euro at the end.
  */
 Glib::ustring Utils::locale_simplify(const Glib::ustring& locale_id)
@@ -654,13 +659,13 @@ Utils::type_vec_strings Utils::string_separate(const Glib::ustring& str, const G
   const Glib::ustring::size_type size_separator = separator.size();
 
   //A stack of quotes, so that we can handle nested quotes, whether they are " or ':
-  typedef std::stack<Glib::ustring> type_queue_quotes; 
+  typedef std::stack<Glib::ustring> type_queue_quotes;
   type_queue_quotes m_current_quotes;
 
   Glib::ustring::size_type unprocessed_start = 0;
   Glib::ustring::size_type item_start = 0;
   while(unprocessed_start < size)
-  { 
+  {
     //std::cout << "while unprocessed: un_processed_start=" << unprocessed_start << std::endl;
     Glib::ustring::size_type posComma = str.find(separator, unprocessed_start);
 
@@ -673,18 +678,18 @@ Utils::type_vec_strings Utils::string_separate(const Glib::ustring& str, const G
       if(ignore_quoted_separator)
       {
         //std::cout << "  debug: attempting to ignore quoted separators: " << separator << std::endl;
-       
+
         Glib::ustring::size_type posLastQuote = unprocessed_start;
 
         //std::cout << "    debug: posLastQuote=" << posLastQuote << std::endl;
         //std::cout << "    debug: posComma=" << posComma << std::endl;
- 
-  
+
+
         bool bContinue = true;
         while(bContinue && (posLastQuote < posComma))
         {
           //std::cout << "  continue" << std::endl;
-          Glib::ustring closing_quote; 
+          Glib::ustring closing_quote;
           if(!m_current_quotes.empty())
             closing_quote = m_current_quotes.top();
 
@@ -759,7 +764,7 @@ Utils::type_vec_strings Utils::string_separate(const Glib::ustring& str, const G
         // Do not add this item to the result, because it was quoted.
         continue;
       }
-     
+
       unprocessed_start = posComma + size_separator; //The while loops stops when this is empty.
     }
     else //if no separator found:
@@ -849,4 +854,99 @@ bool Utils::file_exists(const Glib::ustring& uri)
 }
 
 
+std::string Utils::sqlbuilder_get_full_query(
+    const Glib::RefPtr<Gnome::Gda::Connection>& connection,
+    const Glib::ustring& query,
+    const Glib::RefPtr<const Gnome::Gda::Set>& params)
+{
+  Glib::ustring result = "glom_query_not_parsed";
+
+  try
+  {
+    Glib::RefPtr<Gnome::Gda::SqlParser> parser = connection->create_parser();
+    if(parser)
+    {
+      Glib::RefPtr<Gnome::Gda::Statement> stmt = parser->parse_string(query);
+      if(stmt)
+        result = stmt->to_sql(params);
+    }
+  }
+  catch(const Glib::Exception& ex)
+  {
+    std::cerr << "sqlbuilder_get_full_query(): exception while parsing query: " << ex.what() << std::endl;
+  }
+  catch(const std::exception& ex)
+  {
+    std::cerr << "sqlbuilder_get_full_query(): exception while parsing query: " << ex.what() << std::endl;
+  }
+
+  //Convert to something that std::cout should be able to handle.
+  const Glib::ScopedPtr<char> buf(g_convert_with_fallback(
+    result.raw().data(), result.raw().size(),
+    "ISO-8859-1", "UTF-8",
+    (char*)"?",
+    0, 0, 0));
+  return std::string(buf.get());
+}
+
+
+std::string Utils::sqlbuilder_get_full_query(
+  const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder,
+  const Glib::RefPtr<const Gnome::Gda::Set>& params)
+{
+  Glib::ustring result = "glom_query_not_parsed";
+
+  try
+  {
+    Glib::RefPtr<Gnome::Gda::Statement> stmt = builder->get_statement();
+    if(stmt)
+      result = stmt->to_sql(params);
+  }
+  catch(const Glib::Exception& ex)
+  {
+    std::cerr << "sqlbuilder_get_full_query(): exception while getting query: " << ex.what() << std::endl;
+  }
+  catch(const std::exception& ex)
+  {
+    std::cerr << "sqlbuilder_get_full_query(): exception while getting query: " << ex.what() << std::endl;
+  }
+
+  //Convert to something that std::cout should be able to handle.
+  const Glib::ScopedPtr<char> buf(g_convert_with_fallback(
+    result.raw().data(), result.raw().size(),
+    "ISO-8859-1", "UTF-8",
+    (char*)"?",
+    0, 0, 0));
+  return std::string(buf.get());
+}
+
+std::string Utils::sqlbuilder_get_full_query(
+  const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder)
+{
+  Glib::ustring result = "glom_query_not_parsed";
+
+  try
+  {
+    Glib::RefPtr<Gnome::Gda::Statement> stmt = builder->get_statement();
+    if(stmt)
+      result = stmt->to_sql();
+  }
+  catch(const Glib::Exception& ex)
+  {
+    std::cerr << "sqlbuilder_get_full_query(): exception while getting query: " << ex.what() << std::endl;
+  }
+  catch(const std::exception& ex)
+  {
+    std::cerr << "sqlbuilder_get_full_query(): exception while getting query: " << ex.what() << std::endl;
+  }
+
+  //Convert to something that std::cout should be able to handle.
+  const Glib::ScopedPtr<char> buf(g_convert_with_fallback(
+    result.raw().data(), result.raw().size(),
+    "ISO-8859-1", "UTF-8",
+    (char*)"?",
+    0, 0, 0));
+  return std::string(buf.get());
+}
+
 } //namespace Glom
diff --git a/glom/libglom/utils.h b/glom/libglom/utils.h
index e95bb60..af7fd6b 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -99,6 +99,21 @@ Glib::ustring build_sql_select_with_key(
 typedef std::list< std::pair<Gnome::Gda::Value, Gnome::Gda::Value> > type_list_values_with_second;
 type_list_values_with_second get_choice_values(const sharedptr<const LayoutItem_Field>& field);
 
+/// Get the full query string suitable for use with std::cout.
+std::string sqlbuilder_get_full_query(
+  const Glib::RefPtr<Gnome::Gda::Connection>& connection,
+  const Glib::ustring& query,
+  const Glib::RefPtr<const Gnome::Gda::Set>& params);
+
+/// Get the full query string suitable for use with std::cout.
+std::string sqlbuilder_get_full_query(
+  const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder,
+  const Glib::RefPtr<const Gnome::Gda::Set>& params);
+
+/// Get the full query string suitable for use with std::cout.
+std::string sqlbuilder_get_full_query(
+  const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder);
+
 /** Guess an appropriate identifier name based on a human-readable title
  */
 Glib::ustring create_name_from_title(const Glib::ustring& title);



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