[Pkg-owncloud-commits] [owncloud] 185/199: - 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
David Prévot
taffit at moszumanska.debian.org
Sun Jun 1 18:53:25 UTC 2014
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository owncloud.
commit 999f6216dcfea2d5344c2ceda744461e18d32241
Author: Thomas Müller <thomas.mueller at tmit.eu>
Date: Mon May 12 13:54:20 2014 +0200
- 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 df9d1ae..422f783 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 1e90c8b..4208dbd 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 70d1f98..93fabc1 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 cfa2d6f..c07e32a 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() {
@@ -74,51 +72,8 @@ class Test_DBSchema extends PHPUnit_Framework_TestCase {
/**
* @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.');
}
}
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git
More information about the Pkg-owncloud-commits
mailing list