Commit 22edbcec authored by beberlei's avatar beberlei

[2.0] DDC-312 - Refactored View support of all platforms a little - Oracle...

[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.
parent 3de3bbb9
...@@ -1484,7 +1484,13 @@ abstract class AbstractPlatform ...@@ -1484,7 +1484,13 @@ abstract class AbstractPlatform
throw DBALException::notSupported(__METHOD__); 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__); throw DBALException::notSupported(__METHOD__);
} }
......
...@@ -192,13 +192,9 @@ class MySqlPlatform extends AbstractPlatform ...@@ -192,13 +192,9 @@ class MySqlPlatform extends AbstractPlatform
return $sql; return $sql;
} }
public function getListViewsSql($database = null) public function getListViewsSql($database)
{ {
$sql = 'SELECT * FROM information_schema.VIEWS'; return "SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$database."'";
if($database !== null) {
$sql .= " WHERE TABLE_SCHEMA = '".$database."'";
}
return $sql;
} }
public function getListTableForeignKeysSql($table, $database = null) public function getListTableForeignKeysSql($table, $database = null)
......
...@@ -332,9 +332,9 @@ class OraclePlatform extends AbstractPlatform ...@@ -332,9 +332,9 @@ class OraclePlatform extends AbstractPlatform
return 'SELECT * FROM all_users'; 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) public function getCreateViewSql($name, $sql)
......
...@@ -317,7 +317,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -317,7 +317,7 @@ class PostgreSqlPlatform extends AbstractPlatform
AND c.relname !~ '^pg_'"; AND c.relname !~ '^pg_'";
} }
public function getListViewsSql() public function getListViewsSql($database)
{ {
return 'SELECT viewname, definition FROM pg_views'; return 'SELECT viewname, definition FROM pg_views';
} }
......
...@@ -404,12 +404,7 @@ class SqlitePlatform extends AbstractPlatform ...@@ -404,12 +404,7 @@ class SqlitePlatform extends AbstractPlatform
. "WHERE type = 'table' ORDER BY name"; . "WHERE type = 'table' ORDER BY name";
} }
public function getListTableViews() public function getListViewsSql($database)
{
return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
}
public function getListViewsSql()
{ {
return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL"; return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
} }
......
...@@ -279,16 +279,12 @@ abstract class AbstractSchemaManager ...@@ -279,16 +279,12 @@ abstract class AbstractSchemaManager
/** /**
* List the views this connection has * List the views this connection has
* *
* @example array( * @return View[]
* array('name' => 'ViewA', 'sql' => 'SELECT * FROM foo'),
* array('name' => 'ViewB', 'sql' => 'SELECT * FROM bar'),
* )
* @return array $views
*/ */
public function listViews() public function listViews()
{ {
$sql = $this->_platform->getListViewsSql(); $database = $this->_conn->getDatabase();
$sql = $this->_platform->getListViewsSql($database);
$views = $this->_conn->fetchAll($sql); $views = $this->_conn->fetchAll($sql);
return $this->_getPortableViewsList($views); return $this->_getPortableViewsList($views);
...@@ -465,12 +461,11 @@ abstract class AbstractSchemaManager ...@@ -465,12 +461,11 @@ abstract class AbstractSchemaManager
/** /**
* Create a new view * Create a new view
* *
* @param string $name The name of the view * @param View $view
* @param string $sql The sql to set to the 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 */ /* dropAndCreate*() Methods */
...@@ -550,13 +545,12 @@ abstract class AbstractSchemaManager ...@@ -550,13 +545,12 @@ abstract class AbstractSchemaManager
/** /**
* Drop and create a new view * Drop and create a new view
* *
* @param string $name The name of the view * @param View $view
* @param string $sql The sql to set to the view
*/ */
public function dropAndCreateView($name, $sql) public function dropAndCreateView(View $view)
{ {
$this->tryMethod('dropView', $name); $this->tryMethod('dropView', $view->getName());
$this->createView($name, $sql); $this->createView($view);
} }
/* alterTable() Methods */ /* alterTable() Methods */
...@@ -763,8 +757,9 @@ abstract class AbstractSchemaManager ...@@ -763,8 +757,9 @@ abstract class AbstractSchemaManager
{ {
$list = array(); $list = array();
foreach ($views as $key => $value) { foreach ($views as $key => $value) {
if ($value = $this->_getPortableViewDefinition($value)) { if ($view = $this->_getPortableViewDefinition($value)) {
$list[] = $value; $viewName = strtolower($view->getName());
$list[$viewName] = $view;
} }
} }
return $list; return $list;
...@@ -772,7 +767,7 @@ abstract class AbstractSchemaManager ...@@ -772,7 +767,7 @@ abstract class AbstractSchemaManager
protected function _getPortableViewDefinition($view) protected function _getPortableViewDefinition($view)
{ {
return $view; return false;
} }
protected function _getPortableTableForeignKeysList($tableForeignKeys) protected function _getPortableTableForeignKeysList($tableForeignKeys)
......
...@@ -36,10 +36,7 @@ class MySqlSchemaManager extends AbstractSchemaManager ...@@ -36,10 +36,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
{ {
protected function _getPortableViewDefinition($view) protected function _getPortableViewDefinition($view)
{ {
return array( return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
'name' => $view['TABLE_NAME'],
'sql' => $view['VIEW_DEFINITION']
);
} }
protected function _getPortableTableDefinition($table) protected function _getPortableTableDefinition($table)
......
...@@ -37,10 +37,7 @@ class OracleSchemaManager extends AbstractSchemaManager ...@@ -37,10 +37,7 @@ class OracleSchemaManager extends AbstractSchemaManager
{ {
$view = \array_change_key_case($view, CASE_LOWER); $view = \array_change_key_case($view, CASE_LOWER);
return array( return new View($view['view_name'], $view['text']);
'name' => $view['view_name'],
'sql' => '',
);
} }
protected function _getPortableUserDefinition($user) protected function _getPortableUserDefinition($user)
......
...@@ -96,10 +96,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -96,10 +96,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
protected function _getPortableViewDefinition($view) protected function _getPortableViewDefinition($view)
{ {
return array( return new View($view['viewname'], $view['definition']);
'name' => $view['viewname'],
'sql' => $view['definition']
);
} }
protected function _getPortableUserDefinition($user) protected function _getPortableUserDefinition($user)
......
...@@ -40,7 +40,7 @@ class Schema extends AbstractAsset ...@@ -40,7 +40,7 @@ class Schema extends AbstractAsset
* @var array * @var array
*/ */
protected $_tables = array(); protected $_tables = array();
/** /**
* @var array * @var array
*/ */
......
...@@ -257,4 +257,9 @@ class SqliteSchemaManager extends AbstractSchemaManager ...@@ -257,4 +257,9 @@ class SqliteSchemaManager extends AbstractSchemaManager
return new Column($tableColumn['name'], \Doctrine\DBAL\Types\Type::getType($type), $options); return new Column($tableColumn['name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
} }
protected function _getPortableViewDefinition($view)
{
return new View($view['name'], $view['sql']);
}
} }
\ No newline at end of file
<?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;
}
}
\ No newline at end of file
...@@ -22,9 +22,4 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -22,9 +22,4 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
} }
$this->assertEquals(true, $found); $this->assertEquals(true, $found);
} }
protected function getCreateExampleViewSql()
{
return 'SELECT * from mysql.user';
}
} }
\ No newline at end of file
...@@ -22,9 +22,4 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -22,9 +22,4 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
} }
$this->assertEquals(true, $found); $this->assertEquals(true, $found);
} }
protected function getCreateExampleViewSql()
{
return 'SELECT usename, passwd FROM pg_user';
}
} }
\ No newline at end of file
...@@ -9,6 +9,28 @@ require_once __DIR__ . '/../../../TestInit.php'; ...@@ -9,6 +9,28 @@ require_once __DIR__ . '/../../../TestInit.php';
class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase 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() public function testListSequences()
{ {
if(!$this->_conn->getDatabasePlatform()->supportsSequences()) { if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
...@@ -255,28 +277,6 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest ...@@ -255,28 +277,6 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->markTestSkipped('No Create Example View SQL was defined for this SchemaManager'); $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() public function testCreateSchema()
{ {
$this->createTestTable('test_table'); $this->createTestTable('test_table');
...@@ -353,26 +353,18 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest ...@@ -353,26 +353,18 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->assertEquals(array('id'), array_map('strtolower', $foreignKey->getForeignColumns())); $this->assertEquals(array('id'), array_map('strtolower', $foreignKey->getForeignColumns()));
} }
/** public function testCreateAndListViews()
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager
*/
protected $_sm;
protected function setUp()
{ {
parent::setUp(); $this->createTestTable('view_test_table');
$class = get_class($this); $name = "doctrine_test_view";
$e = explode('\\', $class); $sql = "SELECT * FROM view_test_table";
$testClass = end($e);
$dbms = strtolower(str_replace('SchemaManagerTest', null, $testClass));
if ($this->_conn->getDatabasePlatform()->getName() !== $dbms) $view = new \Doctrine\DBAL\Schema\View($name, $sql);
{
$this->markTestSkipped('The ' . $testClass .' requires the use of ' . $dbms);
}
$this->_sm = $this->_conn->getSchemaManager(); $this->_sm->dropAndCreateView($view);
$views = $this->_sm->listViews();
} }
protected function createTestTable($name = 'test_table', $data = array()) protected function createTestTable($name = 'test_table', $data = array())
......
...@@ -44,12 +44,6 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -44,12 +44,6 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->_sm->listUsers(); $this->_sm->listUsers();
} }
protected function getCreateExampleViewSql()
{
$this->createTestTable('test_views');
return 'SELECT * from test_views';
}
public function testCreateAndDropDatabase() public function testCreateAndDropDatabase()
{ {
$path = dirname(__FILE__).'/test_create_and_drop_sqlite_database.sqlite'; $path = dirname(__FILE__).'/test_create_and_drop_sqlite_database.sqlite';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment