[2.0] DDC-312 - Refactored View support of all platforms a little - Oracle now also supports the retrieval of the sql used to generate a view so that a common datastructure View is a viable option.
This commit is contained in:
parent
3de3bbb969
commit
22edbcec33
@ -1484,7 +1484,13 @@ abstract class AbstractPlatform
|
||||
throw DBALException::notSupported(__METHOD__);
|
||||
}
|
||||
|
||||
public function getListViewsSql()
|
||||
/**
|
||||
* Get the SQL to list all views of a database or user.
|
||||
*
|
||||
* @param string $database
|
||||
* @return string
|
||||
*/
|
||||
public function getListViewsSql($database)
|
||||
{
|
||||
throw DBALException::notSupported(__METHOD__);
|
||||
}
|
||||
|
@ -192,13 +192,9 @@ class MySqlPlatform extends AbstractPlatform
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function getListViewsSql($database = null)
|
||||
public function getListViewsSql($database)
|
||||
{
|
||||
$sql = 'SELECT * FROM information_schema.VIEWS';
|
||||
if($database !== null) {
|
||||
$sql .= " WHERE TABLE_SCHEMA = '".$database."'";
|
||||
}
|
||||
return $sql;
|
||||
return "SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$database."'";
|
||||
}
|
||||
|
||||
public function getListTableForeignKeysSql($table, $database = null)
|
||||
|
@ -332,9 +332,9 @@ class OraclePlatform extends AbstractPlatform
|
||||
return 'SELECT * FROM all_users';
|
||||
}
|
||||
|
||||
public function getListViewsSql()
|
||||
public function getListViewsSql($database)
|
||||
{
|
||||
return 'SELECT view_name FROM sys.user_views';
|
||||
return 'SELECT view_name, text FROM sys.user_views';
|
||||
}
|
||||
|
||||
public function getCreateViewSql($name, $sql)
|
||||
|
@ -317,7 +317,7 @@ class PostgreSqlPlatform extends AbstractPlatform
|
||||
AND c.relname !~ '^pg_'";
|
||||
}
|
||||
|
||||
public function getListViewsSql()
|
||||
public function getListViewsSql($database)
|
||||
{
|
||||
return 'SELECT viewname, definition FROM pg_views';
|
||||
}
|
||||
|
@ -404,12 +404,7 @@ class SqlitePlatform extends AbstractPlatform
|
||||
. "WHERE type = 'table' ORDER BY name";
|
||||
}
|
||||
|
||||
public function getListTableViews()
|
||||
{
|
||||
return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
|
||||
}
|
||||
|
||||
public function getListViewsSql()
|
||||
public function getListViewsSql($database)
|
||||
{
|
||||
return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
|
||||
}
|
||||
|
@ -279,16 +279,12 @@ abstract class AbstractSchemaManager
|
||||
/**
|
||||
* List the views this connection has
|
||||
*
|
||||
* @example array(
|
||||
* array('name' => 'ViewA', 'sql' => 'SELECT * FROM foo'),
|
||||
* array('name' => 'ViewB', 'sql' => 'SELECT * FROM bar'),
|
||||
* )
|
||||
* @return array $views
|
||||
* @return View[]
|
||||
*/
|
||||
public function listViews()
|
||||
{
|
||||
$sql = $this->_platform->getListViewsSql();
|
||||
|
||||
$database = $this->_conn->getDatabase();
|
||||
$sql = $this->_platform->getListViewsSql($database);
|
||||
$views = $this->_conn->fetchAll($sql);
|
||||
|
||||
return $this->_getPortableViewsList($views);
|
||||
@ -465,12 +461,11 @@ abstract class AbstractSchemaManager
|
||||
/**
|
||||
* Create a new view
|
||||
*
|
||||
* @param string $name The name of the view
|
||||
* @param string $sql The sql to set to the view
|
||||
* @param View $view
|
||||
*/
|
||||
public function createView($name, $sql)
|
||||
public function createView(View $view)
|
||||
{
|
||||
$this->_execSql($this->_platform->getCreateViewSql($name, $sql));
|
||||
$this->_execSql($this->_platform->getCreateViewSql($view->getName(), $view->getSql()));
|
||||
}
|
||||
|
||||
/* dropAndCreate*() Methods */
|
||||
@ -550,13 +545,12 @@ abstract class AbstractSchemaManager
|
||||
/**
|
||||
* Drop and create a new view
|
||||
*
|
||||
* @param string $name The name of the view
|
||||
* @param string $sql The sql to set to the view
|
||||
* @param View $view
|
||||
*/
|
||||
public function dropAndCreateView($name, $sql)
|
||||
public function dropAndCreateView(View $view)
|
||||
{
|
||||
$this->tryMethod('dropView', $name);
|
||||
$this->createView($name, $sql);
|
||||
$this->tryMethod('dropView', $view->getName());
|
||||
$this->createView($view);
|
||||
}
|
||||
|
||||
/* alterTable() Methods */
|
||||
@ -763,8 +757,9 @@ abstract class AbstractSchemaManager
|
||||
{
|
||||
$list = array();
|
||||
foreach ($views as $key => $value) {
|
||||
if ($value = $this->_getPortableViewDefinition($value)) {
|
||||
$list[] = $value;
|
||||
if ($view = $this->_getPortableViewDefinition($value)) {
|
||||
$viewName = strtolower($view->getName());
|
||||
$list[$viewName] = $view;
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
@ -772,7 +767,7 @@ abstract class AbstractSchemaManager
|
||||
|
||||
protected function _getPortableViewDefinition($view)
|
||||
{
|
||||
return $view;
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function _getPortableTableForeignKeysList($tableForeignKeys)
|
||||
|
@ -36,10 +36,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
|
||||
{
|
||||
protected function _getPortableViewDefinition($view)
|
||||
{
|
||||
return array(
|
||||
'name' => $view['TABLE_NAME'],
|
||||
'sql' => $view['VIEW_DEFINITION']
|
||||
);
|
||||
return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
|
||||
}
|
||||
|
||||
protected function _getPortableTableDefinition($table)
|
||||
|
@ -37,10 +37,7 @@ class OracleSchemaManager extends AbstractSchemaManager
|
||||
{
|
||||
$view = \array_change_key_case($view, CASE_LOWER);
|
||||
|
||||
return array(
|
||||
'name' => $view['view_name'],
|
||||
'sql' => '',
|
||||
);
|
||||
return new View($view['view_name'], $view['text']);
|
||||
}
|
||||
|
||||
protected function _getPortableUserDefinition($user)
|
||||
|
@ -96,10 +96,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
|
||||
|
||||
protected function _getPortableViewDefinition($view)
|
||||
{
|
||||
return array(
|
||||
'name' => $view['viewname'],
|
||||
'sql' => $view['definition']
|
||||
);
|
||||
return new View($view['viewname'], $view['definition']);
|
||||
}
|
||||
|
||||
protected function _getPortableUserDefinition($user)
|
||||
|
@ -257,4 +257,9 @@ class SqliteSchemaManager extends AbstractSchemaManager
|
||||
|
||||
return new Column($tableColumn['name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
|
||||
}
|
||||
|
||||
protected function _getPortableViewDefinition($view)
|
||||
{
|
||||
return new View($view['name'], $view['sql']);
|
||||
}
|
||||
}
|
53
lib/Doctrine/DBAL/Schema/View.php
Normal file
53
lib/Doctrine/DBAL/Schema/View.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\DBAL\Schema;
|
||||
|
||||
/**
|
||||
* Representation of a Database View
|
||||
*
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @link www.doctrine-project.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
*/
|
||||
class View extends AbstractAsset
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $_sql;
|
||||
|
||||
public function __construct($name, $sql)
|
||||
{
|
||||
$this->_setName($name);
|
||||
$this->_sql = $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSql()
|
||||
{
|
||||
return $this->_sql;
|
||||
}
|
||||
}
|
@ -22,9 +22,4 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
|
||||
}
|
||||
$this->assertEquals(true, $found);
|
||||
}
|
||||
|
||||
protected function getCreateExampleViewSql()
|
||||
{
|
||||
return 'SELECT * from mysql.user';
|
||||
}
|
||||
}
|
@ -22,9 +22,4 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
|
||||
}
|
||||
$this->assertEquals(true, $found);
|
||||
}
|
||||
|
||||
protected function getCreateExampleViewSql()
|
||||
{
|
||||
return 'SELECT usename, passwd FROM pg_user';
|
||||
}
|
||||
}
|
@ -9,6 +9,28 @@ require_once __DIR__ . '/../../../TestInit.php';
|
||||
|
||||
class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase
|
||||
{
|
||||
/**
|
||||
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager
|
||||
*/
|
||||
protected $_sm;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$class = get_class($this);
|
||||
$e = explode('\\', $class);
|
||||
$testClass = end($e);
|
||||
$dbms = strtolower(str_replace('SchemaManagerTest', null, $testClass));
|
||||
|
||||
if ($this->_conn->getDatabasePlatform()->getName() !== $dbms)
|
||||
{
|
||||
$this->markTestSkipped('The ' . $testClass .' requires the use of ' . $dbms);
|
||||
}
|
||||
|
||||
$this->_sm = $this->_conn->getSchemaManager();
|
||||
}
|
||||
|
||||
public function testListSequences()
|
||||
{
|
||||
if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
|
||||
@ -255,28 +277,6 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
|
||||
$this->markTestSkipped('No Create Example View SQL was defined for this SchemaManager');
|
||||
}
|
||||
|
||||
public function testListViews()
|
||||
{
|
||||
$this->_sm->dropAndCreateView('test_create_view', $this->getCreateExampleViewSql());
|
||||
$views = $this->_sm->listViews();
|
||||
$this->assertTrue(count($views) >= 1, "There should be at least the fixture view created in the database, but none were found.");
|
||||
|
||||
$found = false;
|
||||
foreach($views AS $view) {
|
||||
if(!isset($view['name']) || !isset($view['sql'])) {
|
||||
$this->fail(
|
||||
"listViews() has to return entries with both name ".
|
||||
"and sql keys, but only ".implode(", ", array_keys($view))." are present."
|
||||
);
|
||||
}
|
||||
|
||||
if($view['name'] == 'test_create_view') {
|
||||
$found = true;
|
||||
}
|
||||
}
|
||||
$this->assertTrue($found, "'test_create_view' View was not found in listViews().");
|
||||
}
|
||||
|
||||
public function testCreateSchema()
|
||||
{
|
||||
$this->createTestTable('test_table');
|
||||
@ -353,26 +353,18 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
|
||||
$this->assertEquals(array('id'), array_map('strtolower', $foreignKey->getForeignColumns()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager
|
||||
*/
|
||||
protected $_sm;
|
||||
|
||||
protected function setUp()
|
||||
public function testCreateAndListViews()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->createTestTable('view_test_table');
|
||||
|
||||
$class = get_class($this);
|
||||
$e = explode('\\', $class);
|
||||
$testClass = end($e);
|
||||
$dbms = strtolower(str_replace('SchemaManagerTest', null, $testClass));
|
||||
$name = "doctrine_test_view";
|
||||
$sql = "SELECT * FROM view_test_table";
|
||||
|
||||
if ($this->_conn->getDatabasePlatform()->getName() !== $dbms)
|
||||
{
|
||||
$this->markTestSkipped('The ' . $testClass .' requires the use of ' . $dbms);
|
||||
}
|
||||
$view = new \Doctrine\DBAL\Schema\View($name, $sql);
|
||||
|
||||
$this->_sm = $this->_conn->getSchemaManager();
|
||||
$this->_sm->dropAndCreateView($view);
|
||||
|
||||
$views = $this->_sm->listViews();
|
||||
}
|
||||
|
||||
protected function createTestTable($name = 'test_table', $data = array())
|
||||
|
@ -44,12 +44,6 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
|
||||
$this->_sm->listUsers();
|
||||
}
|
||||
|
||||
protected function getCreateExampleViewSql()
|
||||
{
|
||||
$this->createTestTable('test_views');
|
||||
return 'SELECT * from test_views';
|
||||
}
|
||||
|
||||
public function testCreateAndDropDatabase()
|
||||
{
|
||||
$path = dirname(__FILE__).'/test_create_and_drop_sqlite_database.sqlite';
|
||||
|
Loading…
Reference in New Issue
Block a user