diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2014-05-12 13:54:20 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2014-05-30 23:34:42 +0200 |
commit | 999f6216dcfea2d5344c2ceda744461e18d32241 (patch) | |
tree | f99dbdfcb889f8c3c870b6a4bf1e656f0874806b /lib/private/db.php | |
parent | da6aae28ad0cb1bad3d0693f126a5436af64240d (diff) | |
download | nextcloud-server-999f6216dcfea2d5344c2ceda744461e18d32241.tar.gz nextcloud-server-999f6216dcfea2d5344c2ceda744461e18d32241.zip |
- 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
Diffstat (limited to 'lib/private/db.php')
-rw-r--r-- | lib/private/db.php | 74 |
1 files changed, 59 insertions, 15 deletions
diff --git a/lib/private/db.php b/lib/private/db.php index df9d1aeca3a..422f783c745 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(); } /** @@ -343,15 +349,6 @@ class OC_DB { } /** - * 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 * @return bool @@ -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; + } } |