]> source.dussan.org Git - nextcloud-server.git/commitdiff
- fix dropTable() and introduce tableExists()
authorThomas Müller <thomas.mueller@tmit.eu>
Mon, 12 May 2014 11:54:20 +0000 (13:54 +0200)
committerThomas Müller <thomas.mueller@tmit.eu>
Fri, 30 May 2014 21:34:42 +0000 (23:34 +0200)
- 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
lib/private/db/mdb2schemamanager.php
lib/private/db/statementwrapper.php
tests/lib/dbschema.php

index df9d1aeca3a85a3301a0b7a4133c781ef827e8d1..422f783c745633ac3941f5ff1fd79ba889b54ea2 100644 (file)
@@ -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;
+       }
 }
index 1e90c8bda5ce94c8f3ae14daf8438dfdb48eb77a..4208dbd18f4e1c0386aeffecb8f93b5028a3a471 100644 (file)
@@ -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
index 70d1f985a41ef15c93d3e0536bf8705b6a326eef..93fabc147caab6e538bb8ff4e812e14777fa4ca5 100644 (file)
@@ -13,6 +13,7 @@
  * @method string errorCode();
  * @method array errorInfo();
  * @method integer rowCount();
+ * @method array fetchAll(integer $fetchMode = null);
  */
 class OC_DB_StatementWrapper {
        /**
index cfa2d6fd9aab0b7247798a79a24ccfe639719a79..c07e32a404e3a40211eccc5bb82b9922fe41c435 100644 (file)
@@ -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.');
                }
        }
 }