Commit 1950fa89 authored by Murray Cumming's avatar Murray Cumming

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 .
parent f3979a58
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> 2011-11-08 Murray Cumming <murrayc@murrayc.com>
libglom: Add DbUtils::rename_table() and drop_table() and test them. libglom: Add DbUtils::rename_table() and drop_table() and test them.
......
...@@ -1144,7 +1144,7 @@ bool create_table(const sharedptr<const TableInfo>& table_info, const Document:: ...@@ -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()) ); info->set_g_type( Field::get_gda_type_for_glom_type(field->get_glom_type()) );
field->set_field_info(info); //TODO_Performance 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()) if(field->get_primary_key())
sql_field_description += " NOT NULL PRIMARY KEY"; sql_field_description += " NOT NULL PRIMARY KEY";
...@@ -1166,7 +1166,7 @@ bool create_table(const sharedptr<const TableInfo>& table_info, const Document:: ...@@ -1166,7 +1166,7 @@ bool create_table(const sharedptr<const TableInfo>& table_info, const Document::
{ {
//TODO: Escape the table name? //TODO: Escape the table name?
//TODO: Use GDA_SERVER_OPERATION_CREATE_TABLE instead? //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) if(!table_creation_succeeded)
std::cerr << G_STRFUNC << ": CREATE TABLE failed." << std::endl; 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 ...@@ -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) bool rename_table(const Glib::ustring& table_name, const Glib::ustring& new_table_name)
{ {
//TODO: Escape the table names: //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) bool drop_table(const Glib::ustring& table_name)
{ {
//TODO: Escape the table names: //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 DbUtils
} //namespace Glom } //namespace Glom
...@@ -147,6 +147,9 @@ bool rename_table(const Glib::ustring& table_name, const Glib::ustring& new_tabl ...@@ -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); 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 } //namespace DbUtils
......
...@@ -77,7 +77,8 @@ static bool test(Glom::Document::HostingMode hosting_mode) ...@@ -77,7 +77,8 @@ static bool test(Glom::Document::HostingMode hosting_mode)
if(!result) if(!result)
return false; 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"); result = do_test(hosting_mode, table_name + "with\"quote", new_table_name + "with\"quote");
if(!result) if(!result)
return false; return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment