[glom] libglom: Add DbUtils::rename_table() and drop_table() and test them.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glom] libglom: Add DbUtils::rename_table() and drop_table() and test them.
- Date: Tue, 8 Nov 2011 09:04:26 +0000 (UTC)
commit f3979a58754cf97129c1f46dd0b54d340037a867
Author: Murray Cumming <murrayc murrayc com>
Date: Tue Nov 8 10:04:15 2011 +0100
libglom: Add DbUtils::rename_table() and drop_table() and test them.
* glom/navigation/box_tables.c: Move query-building code to
* glom/libglom/db_utils.[h|cc]: rename_table() and drop_table().
* Makefile_tests.am
* tests/test_selfhosting_new_then_alter_table.cc: Add a test of these
functions, and of table creation. This shows that we need to escape
SQL identifiers to allow characters such as " in table names.
ChangeLog | 11 +++
Makefile_tests.am | 6 ++
glom/libglom/db_utils.cc | 12 +++
glom/libglom/db_utils.h | 9 ++
glom/navigation/box_tables.cc | 4 +-
tests/test_selfhosting_new_then_alter_table.cc | 110 ++++++++++++++++++++++++
6 files changed, 150 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 729720f..4695e70 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+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
+ * glom/libglom/db_utils.[h|cc]: rename_table() and drop_table().
+ * Makefile_tests.am
+ * tests/test_selfhosting_new_then_alter_table.cc: Add a test of these
+ functions, and of table creation. This shows that we need to escape
+ SQL identifiers to allow characters such as " in table names.
+
2011-11-07 Murray Cumming <murrayc murrayc com>
Require the latest libgdamm.
diff --git a/Makefile_tests.am b/Makefile_tests.am
index f7db526..ecea00b 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -31,6 +31,7 @@ check_PROGRAMS = \
tests/test_selfhosting_new_then_image \
tests/test_selfhosting_new_then_backup_restore \
tests/test_selfhosting_new_then_get_privs \
+ tests/test_selfhosting_new_then_alter_table \
tests/test_selfhosting_sqlinjection \
tests/import/test_parsing \
tests/import/test_signals
@@ -50,6 +51,7 @@ TESTS = tests/test_document_load \
tests/test_selfhosting_new_then_backup_restore \
tests/test_selfhosting_new_then_image \
tests/test_selfhosting_new_then_get_privs \
+ tests/test_selfhosting_new_then_alter_table \
tests/test_selfhosting_sqlinjection \
tests/import/test_parsing \
tests/import/test_signals
@@ -160,6 +162,10 @@ tests_test_selfhosting_new_then_get_privs_SOURCES = tests/test_selfhosting_new_t
tests_test_selfhosting_new_then_get_privs_LDADD = $(tests_ldadd)
tests_test_selfhosting_new_then_get_privs_CPPFLAGS = $(tests_cppflags)
+tests_test_selfhosting_new_then_alter_table_SOURCES = tests/test_selfhosting_new_then_alter_table.cc $(sources_test_selfhosting_utils)
+tests_test_selfhosting_new_then_alter_table_LDADD = $(tests_ldadd)
+tests_test_selfhosting_new_then_alter_table_CPPFLAGS = $(tests_cppflags)
+
tests_test_selfhosting_sqlinjection_SOURCES = tests/test_selfhosting_sqlinjection.cc $(sources_test_selfhosting_utils)
tests_test_selfhosting_sqlinjection_LDADD = $(tests_ldadd)
tests_test_selfhosting_sqlinjection_CPPFLAGS = $(tests_cppflags)
diff --git a/glom/libglom/db_utils.cc b/glom/libglom/db_utils.cc
index 2c85367..bfce765 100644
--- a/glom/libglom/db_utils.cc
+++ b/glom/libglom/db_utils.cc
@@ -1857,6 +1857,18 @@ int count_rows_returned_by(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& sql
return result;
}
+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 + "\"");
+}
+
+bool drop_table(const Glib::ustring& table_name)
+{
+ //TODO: Escape the table names:
+ return DbUtils::query_execute_string( "DROP TABLE \"" + table_name + "\"");
+}
+
} //namespace DbUtils
} //namespace Glom
diff --git a/glom/libglom/db_utils.h b/glom/libglom/db_utils.h
index 26a8bbb..cc27bbb 100644
--- a/glom/libglom/db_utils.h
+++ b/glom/libglom/db_utils.h
@@ -139,6 +139,15 @@ Glib::ustring get_unused_database_name(const Glib::ustring& base_name);
*/
int count_rows_returned_by(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& sql_query);
+/** Rename a table in the database.
+ */
+bool rename_table(const Glib::ustring& table_name, const Glib::ustring& new_table_name);
+
+/* Remove a table from the database.
+ */
+bool drop_table(const Glib::ustring& table_name);
+
+
} //namespace DbUtils
} //namespace Glom
diff --git a/glom/navigation/box_tables.cc b/glom/navigation/box_tables.cc
index 095905b..54ec84c 100644
--- a/glom/navigation/box_tables.cc
+++ b/glom/navigation/box_tables.cc
@@ -314,7 +314,7 @@ void Box_Tables::on_adddel_Delete(const Gtk::TreeModel::iterator& rowStart, cons
//Delete the table:
if(iButtonClicked == Gtk::RESPONSE_OK)
{
- const bool test = DbUtils::query_execute_string( "DROP TABLE \"" + table_name + "\"");
+ const bool test = DbUtils::drop_table(table_name);
if(!test)
std::cerr << G_STRFUNC << ": DROP TABLE failed." << std::endl;
else
@@ -407,7 +407,7 @@ void Box_Tables::on_adddel_changed(const Gtk::TreeModel::iterator& row, guint co
//Rename the table:
if(iButtonClicked == Gtk::RESPONSE_OK)
{
- const bool test = DbUtils::query_execute_string( "ALTER TABLE \"" + table_name + "\" RENAME TO \"" + table_name_new + "\"");
+ const bool test = DbUtils::rename_table(table_name, table_name_new);
if(test)
{
//Change the AddDel item's key:
diff --git a/tests/test_selfhosting_new_then_alter_table.cc b/tests/test_selfhosting_new_then_alter_table.cc
new file mode 100644
index 0000000..31b4779
--- /dev/null
+++ b/tests/test_selfhosting_new_then_alter_table.cc
@@ -0,0 +1,110 @@
+/* Glom
+ *
+ * Copyright (C) 2010 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+71 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "tests/test_selfhosting_utils.h"
+#include <libglom/init.h>
+#include <libglom/utils.h>
+#include <libglom/db_utils.h>
+#include <glibmm/fileutils.h>
+#include <glibmm/miscutils.h>
+#include <libgda/gda-blob-op.h>
+#include <glib.h> //For g_assert()
+#include <iostream>
+#include <cstdlib> //For EXIT_SUCCESS and EXIT_FAILURE
+#include <cstring> //For memcmp().
+
+static bool do_test(Glom::Document::HostingMode hosting_mode, const Glib::ustring& first_table_name, const Glib::ustring& renamed_table_name)
+{
+ Glom::Document document;
+ const bool recreated =
+ test_create_and_selfhost_from_example("example_smallbusiness.glom", document, hosting_mode);
+ if(!recreated)
+ {
+ std::cerr << "Recreation failed." << std::endl;
+ return false;
+ }
+
+ if(!Glom::DbUtils::create_table_with_default_fields(&document, first_table_name))
+ {
+ std::cerr << "Failure: create_table_with_default_fields() failed." << std::endl;
+ return false;
+ }
+
+ if(!Glom::DbUtils::rename_table(first_table_name, renamed_table_name))
+ {
+ std::cerr << "Failure: rename_table() failed." << std::endl;
+ return false;
+ }
+
+ if(!Glom::DbUtils::drop_table(renamed_table_name))
+ {
+ std::cerr << "Failure: drop_table() failed." << std::endl;
+ return false;
+ }
+
+ test_selfhosting_cleanup();
+
+ return true;
+}
+
+static bool test(Glom::Document::HostingMode hosting_mode)
+{
+ const Glib::ustring table_name = "sometable";
+ const Glib::ustring new_table_name = "renamedtable";
+
+ bool result = do_test(hosting_mode, table_name, new_table_name);
+ if(!result)
+ return false;
+
+ result = do_test(hosting_mode, table_name + "-plusahyphen", new_table_name + "-plusahyphen");
+ if(!result)
+ return false;
+
+ /** TODO: Make this work:
+ result = do_test(hosting_mode, table_name + "with\"quote", new_table_name + "with\"quote");
+ if(!result)
+ return false;
+ */
+
+ return result;
+}
+
+int main()
+{
+ Glom::libglom_init();
+
+ if(!test(Glom::Document::HOSTING_MODE_POSTGRES_SELF))
+ {
+ std::cerr << "Failed with PostgreSQL" << std::endl;
+ test_selfhosting_cleanup();
+ return EXIT_FAILURE;
+ }
+
+ if(!test(Glom::Document::HOSTING_MODE_SQLITE))
+ {
+ std::cerr << "Failed with SQLite" << std::endl;
+ test_selfhosting_cleanup();
+ return EXIT_FAILURE;
+ }
+
+ Glom::libglom_deinit();
+
+ return EXIT_SUCCESS;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]