Add tableExists to public db api
This commit is contained in:
parent
8af3991d0c
commit
778d8dbafd
|
@ -325,42 +325,7 @@ class OC_DB {
|
||||||
* @throws \OC\DatabaseException
|
* @throws \OC\DatabaseException
|
||||||
*/
|
*/
|
||||||
public static function tableExists($table) {
|
public static function tableExists($table) {
|
||||||
|
$connection = \OC::$server->getDatabaseConnection();
|
||||||
$table = OC_Config::getValue('dbtableprefix', 'oc_' ) . trim($table);
|
return $connection->tableExists($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 \OC\DatabaseException("Unknown database type: $dbType");
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result->fetchOne() === $table;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,6 +177,18 @@ class Connection extends \Doctrine\DBAL\Connection implements IDBConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a table exists
|
||||||
|
*
|
||||||
|
* @param string $table table name without the prefix
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function tableExists($table){
|
||||||
|
$table = $this->tablePrefix . trim($table);
|
||||||
|
$schema = $this->getSchemaManager();
|
||||||
|
return $schema->tablesExist(array($table));
|
||||||
|
}
|
||||||
|
|
||||||
// internal use
|
// internal use
|
||||||
/**
|
/**
|
||||||
* @param string $statement
|
* @param string $statement
|
||||||
|
|
|
@ -162,7 +162,15 @@ interface IDBConnection {
|
||||||
/**
|
/**
|
||||||
* Drop a table from the database if it exists
|
* Drop a table from the database if it exists
|
||||||
*
|
*
|
||||||
* @param string $table
|
* @param string $table table name without the prefix
|
||||||
*/
|
*/
|
||||||
public function dropTable($table);
|
public function dropTable($table);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a table exists
|
||||||
|
*
|
||||||
|
* @param string $table table name without the prefix
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function tableExists($table);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue