[glom] libglom: Add escape_sql_id() and use it for table add/drop/rename.



commit 1950fa892b04fca1e8ffe7c364df9db3477affb0
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Nov 8 11:23:41 2011 +0100

    libglom: Add escape_sql_id() and use it for table add/drop/rename.
    
    * glom/libglom/db_utils.[h|cc]: Add escape_sql_id(), using
    gda_connection_quote_sql_identifier().
    create_table(), rename_table(), drop_table(): Use it instead of
    manually adding quotes with no escaping.
    * tests/test_selfhosting_new_then_alter_table.cc: Mention that
    the commented-out test still fails because of libgda bug #663608 .

 ChangeLog                                      |   11 +++++++++
 glom/libglom/db_utils.cc                       |   29 ++++++++++++++++++++---
 glom/libglom/db_utils.h                        |    3 ++
 tests/test_selfhosting_new_then_alter_table.cc |    3 +-
 4 files changed, 41 insertions(+), 5 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 4695e70..0fe1f65 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2011-11-08  Murray Cumming  <murrayc murrayc com>
 
+	libglom: Add escape_sql_id() and use it for table add/drop/rename.
+
+	* glom/libglom/db_utils.[h|cc]: Add escape_sql_id(), using 
+	gda_connection_quote_sql_identifier().
+	create_table(), rename_table(), drop_table(): Use it instead of 
+	manually adding quotes with no escaping.
+	* tests/test_selfhosting_new_then_alter_table.cc: Mention that 
+	the commented-out test still fails because of libgda bug #663608 .
+
+2011-11-08  Murray Cumming  <murrayc murrayc com>
+
 	libglom: Add DbUtils::rename_table() and drop_table() and test them.
 
 	* glom/navigation/box_tables.c: Move query-building code to 
diff --git a/glom/libglom/db_utils.cc b/glom/libglom/db_utils.cc
index bfce765..26cba52 100644
--- a/glom/libglom/db_utils.cc
+++ b/glom/libglom/db_utils.cc
@@ -1144,7 +1144,7 @@ bool create_table(const sharedptr<const TableInfo>& table_info, const Document::
     info->set_g_type( Field::get_gda_type_for_glom_type(field->get_glom_type()) );
     field->set_field_info(info); //TODO_Performance
 
-    Glib::ustring sql_field_description = "\"" + field->get_name() + "\" " + field->get_sql_type();
+    Glib::ustring sql_field_description = escape_sql_id(field->get_name()) + " " + field->get_sql_type();
 
     if(field->get_primary_key())
       sql_field_description += " NOT NULL  PRIMARY KEY";
@@ -1166,7 +1166,7 @@ bool create_table(const sharedptr<const TableInfo>& table_info, const Document::
   {
     //TODO: Escape the table name?
     //TODO: Use GDA_SERVER_OPERATION_CREATE_TABLE instead?
-    table_creation_succeeded = query_execute_string( "CREATE TABLE \"" + table_info->get_name() + "\" (" + sql_fields + ");" );
+    table_creation_succeeded = query_execute_string( "CREATE TABLE " + escape_sql_id(table_info->get_name()) + " (" + sql_fields + ");" );
     if(!table_creation_succeeded)
       std::cerr << G_STRFUNC << ": CREATE TABLE failed." << std::endl;
   }
@@ -1860,15 +1860,36 @@ int count_rows_returned_by(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& sql
 bool rename_table(const Glib::ustring& table_name, const Glib::ustring& new_table_name)
 {
   //TODO: Escape the table names:
-  return query_execute_string( "ALTER TABLE \"" + table_name + "\" RENAME TO \"" + new_table_name + "\"");
+  return query_execute_string( "ALTER TABLE " + escape_sql_id(table_name) + " RENAME TO " + escape_sql_id(new_table_name));
 }
 
 bool drop_table(const Glib::ustring& table_name)
 {
   //TODO: Escape the table names:
-  return DbUtils::query_execute_string( "DROP TABLE \"" + table_name + "\"");
+  return DbUtils::query_execute_string( "DROP TABLE " + escape_sql_id(table_name));
 }
 
+Glib::ustring escape_sql_id(const Glib::ustring& id)
+{
+  if(id.empty())
+  {
+    std::cerr << G_STRFUNC << ": id is empty." << std::endl;
+    return id;
+  }
+
+  Glib::RefPtr<Gnome::Gda::Connection> gda_connection = get_connection();
+  if(!gda_connection)
+  {
+    std::cerr << G_STRFUNC << ": No gda_connection." << std::endl;
+    return id;
+  }
+
+  //Always put it in quotes even if 
+
+  return gda_connection->quote_sql_identifier(id);
+}
+
+
 } //namespace DbUtils
 
 } //namespace Glom
diff --git a/glom/libglom/db_utils.h b/glom/libglom/db_utils.h
index cc27bbb..da5e0bf 100644
--- a/glom/libglom/db_utils.h
+++ b/glom/libglom/db_utils.h
@@ -147,6 +147,9 @@ bool rename_table(const Glib::ustring& table_name, const Glib::ustring& new_tabl
  */
 bool drop_table(const Glib::ustring& table_name);
 
+/** Escape, and quote, SQL identifiers such as table names.
+ */
+Glib::ustring escape_sql_id(const Glib::ustring& id);
 
 } //namespace DbUtils
 
diff --git a/tests/test_selfhosting_new_then_alter_table.cc b/tests/test_selfhosting_new_then_alter_table.cc
index 31b4779..c2ce5cb 100644
--- a/tests/test_selfhosting_new_then_alter_table.cc
+++ b/tests/test_selfhosting_new_then_alter_table.cc
@@ -77,7 +77,8 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   if(!result)
     return false;
 
-  /** TODO: Make this work:
+  /* TODO: Uncomment this when this libgda bug is fixed:
+   * https://bugzilla.gnome.org/show_bug.cgi?id=663608
   result = do_test(hosting_mode, table_name + "with\"quote", new_table_name + "with\"quote");
   if(!result)
     return false;



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