diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2014-05-31 00:47:03 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2014-05-31 00:47:03 +0200 |
commit | 86b06c72bbdd8fb9c230b9a7b6574b32ec67b598 (patch) | |
tree | b9e1e53ca9d8e3f17151dbc2189e9be762883d5e /lib | |
parent | 5dd3e34f57bebf14766b39142d7cdc62e928f36b (diff) | |
parent | 999f6216dcfea2d5344c2ceda744461e18d32241 (diff) | |
download | nextcloud-server-86b06c72bbdd8fb9c230b9a7b6574b32ec67b598.tar.gz nextcloud-server-86b06c72bbdd8fb9c230b9a7b6574b32ec67b598.zip |
Merge pull request #8800 from owncloud/db-changes-as-required-by-8483
Database code cleanup
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/db.php | 74 | ||||
-rw-r--r-- | lib/private/db/mdb2schemamanager.php | 34 | ||||
-rw-r--r-- | lib/private/db/statementwrapper.php | 1 |
3 files changed, 60 insertions, 49 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; + } } diff --git a/lib/private/db/mdb2schemamanager.php b/lib/private/db/mdb2schemamanager.php index 1e90c8bda5c..4208dbd18f4 100644 --- a/lib/private/db/mdb2schemamanager.php +++ b/lib/private/db/mdb2schemamanager.php @@ -95,19 +95,6 @@ class MDB2SchemaManager { } /** - * 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 */ @@ -125,27 +112,6 @@ class MDB2SchemaManager { } /** - * 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 70d1f985a41..93fabc147ca 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 { /** |