Commit f3979a58 authored by Murray Cumming's avatar Murray Cumming

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.
parent 0fbfb145
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.
......
......@@ -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)
......
......@@ -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
......@@ -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
......
......@@ -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:
......
/* 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;
}
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