- 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
This commit is contained in:
Thomas Müller 2014-05-12 13:54:20 +02:00
parent da6aae28ad
commit 999f6216dc
4 changed files with 65 additions and 98 deletions

View File

@ -46,9 +46,6 @@ class OC_DB {
*/ */
static private $connection; //the preferred connection to use, only Doctrine static private $connection; //the preferred connection to use, only Doctrine
static private $prefix=null;
static private $type=null;
/** /**
* connects to the database * connects to the database
* @return boolean|null true if connection can be established or false on error * @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 * @param string $tableName the table to drop
*/ */
public static function dropTable($tableName) { 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); $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 * check if a result is an error, works with Doctrine
* @param mixed $result * @param mixed $result
@ -405,4 +402,51 @@ class OC_DB {
self::$connection->disableQueryStatementCaching(); 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;
}
} }

View File

@ -94,19 +94,6 @@ class MDB2SchemaManager {
return $this->executeSchemaChange($schemaDiff); 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 * remove all tables defined in a database structure xml file
* @param string $file the xml file describing the tables * @param string $file the xml file describing the tables
@ -124,27 +111,6 @@ class MDB2SchemaManager {
$this->executeSchemaChange($schemaDiff); $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 * @param \Doctrine\DBAL\Schema\Schema $schema
* @return bool * @return bool

View File

@ -13,6 +13,7 @@
* @method string errorCode(); * @method string errorCode();
* @method array errorInfo(); * @method array errorInfo();
* @method integer rowCount(); * @method integer rowCount();
* @method array fetchAll(integer $fetchMode = null);
*/ */
class OC_DB_StatementWrapper { class OC_DB_StatementWrapper {
/** /**

View File

@ -24,10 +24,8 @@ class Test_DBSchema extends PHPUnit_Framework_TestCase {
$content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content ); $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
file_put_contents( $this->schema_file2, $content ); file_put_contents( $this->schema_file2, $content );
$prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); $this->table1 = $r.'cntcts_addrsbks';
$this->table2 = $r.'cntcts_cards';
$this->table1 = $prefix.$r.'cntcts_addrsbks';
$this->table2 = $prefix.$r.'cntcts_cards';
} }
public function tearDown() { public function tearDown() {
@ -71,54 +69,11 @@ class Test_DBSchema extends PHPUnit_Framework_TestCase {
$this->assertTableNotExist($this->table2); $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 * @param string $table
*/ */
public function assertTableExist($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" ); $type=OC_Config::getValue( "dbtype", "sqlite" );
if( $type == 'sqlite' || $type == 'sqlite3' ) { if( $type == 'sqlite' || $type == 'sqlite3' ) {
// sqlite removes the tables after closing the DB // sqlite removes the tables after closing the DB
$this->assertTrue(true);
} else { } else {
$this->assertFalse($this->tableExist($table), 'Table ' . $table . ' exists.'); $this->assertFalse(OC_DB::tableExists($table), 'Table ' . $table . ' exists.');
} }
} }
} }