From 999f6216dcfea2d5344c2ceda744461e18d32241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Mon, 12 May 2014 13:54:20 +0200 Subject: [PATCH] - fix dropTable() and introduce tableExists() - kill replaceDB() - this function is unused and it's implementation obviously wrong - add method annotation OC_DB_StatementWrapper::fetchAll - remove duplicate code in Test_DBSchema and reuse OC_DB::tableExists - remove unused variables --- lib/private/db.php | 74 ++++++++++++++++++++++------ lib/private/db/mdb2schemamanager.php | 34 ------------- lib/private/db/statementwrapper.php | 1 + tests/lib/dbschema.php | 54 ++------------------ 4 files changed, 65 insertions(+), 98 deletions(-) diff --git a/lib/private/db.php b/lib/private/db.php index df9d1aeca3..422f783c74 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -46,9 +46,6 @@ class OC_DB { */ static private $connection; //the preferred connection to use, only Doctrine - static private $prefix=null; - static private $type=null; - /** * connects to the database * @return boolean|null true if connection can be established or false on error @@ -325,12 +322,21 @@ class OC_DB { } /** - * drop a table + * drop a table - the database prefix will be prepended * @param string $tableName the table to drop */ public static function dropTable($tableName) { - $schemaManager = self::getMDB2SchemaManager(); - $schemaManager->dropTable($tableName); + + $tableName = OC_Config::getValue('dbtableprefix', 'oc_' ) . trim($tableName); + + self::$connection->beginTransaction(); + + $platform = self::$connection->getDatabasePlatform(); + $sql = $platform->getDropTableSQL($platform->quoteIdentifier($tableName)); + + self::$connection->query($sql); + + self::$connection->commit(); } /** @@ -342,15 +348,6 @@ class OC_DB { $schemaManager->removeDBStructure($file); } - /** - * replaces the ownCloud tables with a new set - * @param string $file path to the MDB2 xml db export file - */ - public static function replaceDB( $file ) { - $schemaManager = self::getMDB2SchemaManager(); - $schemaManager->replaceDB($file); - } - /** * check if a result is an error, works with Doctrine * @param mixed $result @@ -405,4 +402,51 @@ class OC_DB { self::$connection->disableQueryStatementCaching(); } } + + /** + * Checks if a table exists in the database - the database prefix will be prepended + * + * @param string $table + * @return bool + * @throws 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 DatabaseException("Unknown database type: $dbType"); + } + + return $result->fetchOne() === $table; + } } diff --git a/lib/private/db/mdb2schemamanager.php b/lib/private/db/mdb2schemamanager.php index 1e90c8bda5..4208dbd18f 100644 --- a/lib/private/db/mdb2schemamanager.php +++ b/lib/private/db/mdb2schemamanager.php @@ -94,19 +94,6 @@ class MDB2SchemaManager { return $this->executeSchemaChange($schemaDiff); } - /** - * drop a table - * @param string $tableName the table to drop - */ - public function dropTable($tableName) { - $sm = $this->conn->getSchemaManager(); - $fromSchema = $sm->createSchema(); - $toSchema = clone $fromSchema; - $toSchema->dropTable($tableName); - $sql = $fromSchema->getMigrateToSql($toSchema, $this->conn->getDatabasePlatform()); - $this->conn->executeQuery($sql); - } - /** * remove all tables defined in a database structure xml file * @param string $file the xml file describing the tables @@ -124,27 +111,6 @@ class MDB2SchemaManager { $this->executeSchemaChange($schemaDiff); } - /** - * replaces the ownCloud tables with a new set - * @param string $file path to the MDB2 xml db export file - */ - public function replaceDB( $file ) { - $apps = \OC_App::getAllApps(); - $this->conn->beginTransaction(); - // Delete the old tables - $this->removeDBStructure( \OC::$SERVERROOT . '/db_structure.xml' ); - - foreach($apps as $app) { - $path = \OC_App::getAppPath($app).'/appinfo/database.xml'; - if(file_exists($path)) { - $this->removeDBStructure( $path ); - } - } - - // Create new tables - $this->conn->commit(); - } - /** * @param \Doctrine\DBAL\Schema\Schema $schema * @return bool diff --git a/lib/private/db/statementwrapper.php b/lib/private/db/statementwrapper.php index 70d1f985a4..93fabc147c 100644 --- a/lib/private/db/statementwrapper.php +++ b/lib/private/db/statementwrapper.php @@ -13,6 +13,7 @@ * @method string errorCode(); * @method array errorInfo(); * @method integer rowCount(); + * @method array fetchAll(integer $fetchMode = null); */ class OC_DB_StatementWrapper { /** diff --git a/tests/lib/dbschema.php b/tests/lib/dbschema.php index cfa2d6fd9a..c07e32a404 100644 --- a/tests/lib/dbschema.php +++ b/tests/lib/dbschema.php @@ -24,10 +24,8 @@ class Test_DBSchema extends PHPUnit_Framework_TestCase { $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content ); file_put_contents( $this->schema_file2, $content ); - $prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); - - $this->table1 = $prefix.$r.'cntcts_addrsbks'; - $this->table2 = $prefix.$r.'cntcts_cards'; + $this->table1 = $r.'cntcts_addrsbks'; + $this->table2 = $r.'cntcts_cards'; } public function tearDown() { @@ -71,54 +69,11 @@ class Test_DBSchema extends PHPUnit_Framework_TestCase { $this->assertTableNotExist($this->table2); } - /** - * @param string $table - */ - public function tableExist($table) { - - switch (OC_Config::getValue( 'dbtype', 'sqlite' )) { - 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; - } - - $name = $result->fetchOne(); - if ($name === $table) { - return true; - } else { - return false; - } - } - /** * @param string $table */ public function assertTableExist($table) { - $this->assertTrue($this->tableExist($table), 'Table ' . $table . ' does not exist'); + $this->assertTrue(OC_DB::tableExists($table), 'Table ' . $table . ' does not exist'); } /** @@ -128,8 +83,9 @@ class Test_DBSchema extends PHPUnit_Framework_TestCase { $type=OC_Config::getValue( "dbtype", "sqlite" ); if( $type == 'sqlite' || $type == 'sqlite3' ) { // sqlite removes the tables after closing the DB + $this->assertTrue(true); } else { - $this->assertFalse($this->tableExist($table), 'Table ' . $table . ' exists.'); + $this->assertFalse(OC_DB::tableExists($table), 'Table ' . $table . ' exists.'); } } }