From 8af3991d0cf129316b177731686af547fe39a698 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 8 Dec 2014 14:35:57 +0100 Subject: Add dropTable to IDBConnection --- lib/private/db.php | 11 +---------- lib/private/db/connection.php | 13 +++++++++++++ lib/public/idbconnection.php | 7 +++++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/private/db.php b/lib/private/db.php index f8015133682..9387cc48457 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -263,16 +263,7 @@ class OC_DB { */ public static function dropTable($tableName) { $connection = \OC::$server->getDatabaseConnection(); - $tableName = OC_Config::getValue('dbtableprefix', 'oc_' ) . trim($tableName); - - $connection->beginTransaction(); - - $platform = $connection->getDatabasePlatform(); - $sql = $platform->getDropTableSQL($platform->quoteIdentifier($tableName)); - - $connection->executeQuery($sql); - - $connection->commit(); + $connection->dropTable($tableName); } /** diff --git a/lib/private/db/connection.php b/lib/private/db/connection.php index a6cdf858899..e2d90c8fc82 100644 --- a/lib/private/db/connection.php +++ b/lib/private/db/connection.php @@ -164,6 +164,19 @@ class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { return $msg; } + /** + * Drop a table from the database if it exists + * + * @param string $table table name without the prefix + */ + public function dropTable($table) { + $table = $this->tablePrefix . trim($table); + $schema = $this->getSchemaManager(); + if($schema->tablesExist(array($table))) { + $schema->dropTable($table); + } + } + // internal use /** * @param string $statement diff --git a/lib/public/idbconnection.php b/lib/public/idbconnection.php index ce17d293e86..bc563d20b42 100644 --- a/lib/public/idbconnection.php +++ b/lib/public/idbconnection.php @@ -158,4 +158,11 @@ interface IDBConnection { * @return \Doctrine\DBAL\Platforms\AbstractPlatform The database platform. */ public function getDatabasePlatform(); + + /** + * Drop a table from the database if it exists + * + * @param string $table + */ + public function dropTable($table); } -- cgit v1.2.3 From 778d8dbafd982a018c7425f660f59c7ecaa97d2b Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 8 Dec 2014 18:00:42 +0100 Subject: Add tableExists to public db api --- lib/private/db.php | 39 ++------------------------------------- lib/private/db/connection.php | 12 ++++++++++++ lib/public/idbconnection.php | 10 +++++++++- 3 files changed, 23 insertions(+), 38 deletions(-) diff --git a/lib/private/db.php b/lib/private/db.php index 9387cc48457..dc25092e276 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -325,42 +325,7 @@ class OC_DB { * @throws \OC\DatabaseException */ public static function tableExists($table) { - - $table = OC_Config::getValue('dbtableprefix', 'oc_' ) . trim($table); - - $dbType = OC_Config::getValue( 'dbtype', 'sqlite' ); - switch ($dbType) { - case 'sqlite': - case 'sqlite3': - $sql = "SELECT name FROM sqlite_master " - . "WHERE type = 'table' AND name = ? " - . "UNION ALL SELECT name FROM sqlite_temp_master " - . "WHERE type = 'table' AND name = ?"; - $result = \OC_DB::executeAudited($sql, array($table, $table)); - break; - case 'mysql': - $sql = 'SHOW TABLES LIKE ?'; - $result = \OC_DB::executeAudited($sql, array($table)); - break; - case 'pgsql': - $sql = 'SELECT tablename AS table_name, schemaname AS schema_name ' - . 'FROM pg_tables WHERE schemaname NOT LIKE \'pg_%\' ' - . 'AND schemaname != \'information_schema\' ' - . 'AND tablename = ?'; - $result = \OC_DB::executeAudited($sql, array($table)); - break; - case 'oci': - $sql = 'SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME = ?'; - $result = \OC_DB::executeAudited($sql, array($table)); - break; - case 'mssql': - $sql = 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ?'; - $result = \OC_DB::executeAudited($sql, array($table)); - break; - default: - throw new \OC\DatabaseException("Unknown database type: $dbType"); - } - - return $result->fetchOne() === $table; + $connection = \OC::$server->getDatabaseConnection(); + return $connection->tableExists($table); } } diff --git a/lib/private/db/connection.php b/lib/private/db/connection.php index e2d90c8fc82..9de7a719ff5 100644 --- a/lib/private/db/connection.php +++ b/lib/private/db/connection.php @@ -177,6 +177,18 @@ class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { } } + /** + * Check if a table exists + * + * @param string $table table name without the prefix + * @return bool + */ + public function tableExists($table){ + $table = $this->tablePrefix . trim($table); + $schema = $this->getSchemaManager(); + return $schema->tablesExist(array($table)); + } + // internal use /** * @param string $statement diff --git a/lib/public/idbconnection.php b/lib/public/idbconnection.php index bc563d20b42..32310fe755f 100644 --- a/lib/public/idbconnection.php +++ b/lib/public/idbconnection.php @@ -162,7 +162,15 @@ interface IDBConnection { /** * Drop a table from the database if it exists * - * @param string $table + * @param string $table table name without the prefix */ public function dropTable($table); + + /** + * Check if a table exists + * + * @param string $table table name without the prefix + * @return bool + */ + public function tableExists($table); } -- cgit v1.2.3 From b66c0a1631eeea2ac252880ed65e277120fa6655 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 8 Dec 2014 18:01:00 +0100 Subject: add unit tests for tableExists and dropTable --- tests/lib/db/connection.php | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/lib/db/connection.php diff --git a/tests/lib/db/connection.php b/tests/lib/db/connection.php new file mode 100644 index 00000000000..070c75db55d --- /dev/null +++ b/tests/lib/db/connection.php @@ -0,0 +1,69 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\DB; + +use Doctrine\DBAL\Platforms\SqlitePlatform; +use OC\DB\MDB2SchemaManager; + +class Connection extends \Test\TestCase { + /** + * @var \OCP\IDBConnection + */ + private $connection; + + public function setUp() { + parent::setUp(); + $this->connection = \OC::$server->getDatabaseConnection(); + } + + /** + * @param string $table + */ + public function assertTableExist($table) { + if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { + // sqlite removes the tables after closing the DB + $this->assertTrue(true); + } else { + $this->assertTrue($this->connection->tableExists($table), 'Table ' . $table . ' exists.'); + } + } + + /** + * @param string $table + */ + public function assertTableNotExist($table) { + if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { + // sqlite removes the tables after closing the DB + $this->assertTrue(true); + } else { + $this->assertFalse($this->connection->tableExists($table), 'Table ' . $table . ' doesnt exists.'); + } + } + + private function makeTestTable() { + $schemaManager = new MDB2SchemaManager($this->connection); + $schemaManager->createDbFromStructure(__DIR__ . '/testschema.xml'); + } + + public function testTableExists() { + $this->assertTableNotExist('table'); + $this->makeTestTable(); + $this->assertTableExist('table'); + } + + /** + * @depends testTableExists + */ + public function testDropTable() { + $this->assertTableExist('table'); + $this->connection->dropTable('table'); + $this->assertTableNotExist('table'); + } +} -- cgit v1.2.3 From 6984fa8a19f09fe8e3c9ac8c48225dd40b1cc94c Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 9 Dec 2014 17:26:39 +0100 Subject: Quote identifiers for oracle --- lib/private/db/oracleconnection.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/private/db/oracleconnection.php b/lib/private/db/oracleconnection.php index 4cec7bc4ae4..726ac1e4b6d 100644 --- a/lib/private/db/oracleconnection.php +++ b/lib/private/db/oracleconnection.php @@ -47,4 +47,31 @@ class OracleConnection extends Connection { $identifier = $this->quoteKeys($identifier); return parent::delete($tableName, $identifier); } + + /** + * Drop a table from the database if it exists + * + * @param string $table table name without the prefix + */ + public function dropTable($table) { + $table = $this->tablePrefix . trim($table); + $table = $this->quoteIdentifier($table); + $schema = $this->getSchemaManager(); + if($schema->tablesExist(array($table))) { + $schema->dropTable($table); + } + } + + /** + * Check if a table exists + * + * @param string $table table name without the prefix + * @return bool + */ + public function tableExists($table){ + $table = $this->tablePrefix . trim($table); + $table = $this->quoteIdentifier($table); + $schema = $this->getSchemaManager(); + return $schema->tablesExist(array($table)); + } } -- cgit v1.2.3