Preliminary support for database views
This commit is contained in:
parent
daeef41b2a
commit
1e0d675759
@ -10,15 +10,15 @@ require_once("Access.php");
|
||||
*/
|
||||
class Doctrine_Query extends Doctrine_Access {
|
||||
/**
|
||||
* @var array $fetchmodes an array containing all fetchmodes
|
||||
* @var array $fetchmodes an array containing all fetchmodes
|
||||
*/
|
||||
private $fetchModes = array();
|
||||
/**
|
||||
* @var array $tables an array containing all the tables used in the query
|
||||
* @var array $tables an array containing all the tables used in the query
|
||||
*/
|
||||
private $tables = array();
|
||||
/**
|
||||
* @var array $collections an array containing all collections this parser has created/will create
|
||||
* @var array $collections an array containing all collections this parser has created/will create
|
||||
*/
|
||||
private $collections = array();
|
||||
|
||||
@ -26,19 +26,24 @@ class Doctrine_Query extends Doctrine_Access {
|
||||
|
||||
private $joins = array();
|
||||
/**
|
||||
* @var array $data fetched data
|
||||
* @var array $data fetched data
|
||||
*/
|
||||
private $data = array();
|
||||
/**
|
||||
* @var Doctrine_Session $session Doctrine_Session object
|
||||
* @var Doctrine_Session $session Doctrine_Session object
|
||||
*/
|
||||
private $session;
|
||||
/**
|
||||
* @var Doctrine_View $view Doctrine_View object
|
||||
*/
|
||||
private $view;
|
||||
|
||||
|
||||
private $inheritanceApplied = false;
|
||||
|
||||
private $aggregate = false;
|
||||
/**
|
||||
* @var array $connectors component connectors
|
||||
* @var array $connectors component connectors
|
||||
*/
|
||||
private $connectors = array();
|
||||
/**
|
||||
@ -50,7 +55,7 @@ class Doctrine_Query extends Doctrine_Access {
|
||||
*/
|
||||
private $tableIndexes = array();
|
||||
/**
|
||||
* @var array $dql DQL query string parts
|
||||
* @var array $dql DQL query string parts
|
||||
*/
|
||||
protected $dql = array(
|
||||
"columns" => array(),
|
||||
@ -85,6 +90,32 @@ class Doctrine_Query extends Doctrine_Access {
|
||||
public function __construct(Doctrine_Session $session) {
|
||||
$this->session = $session;
|
||||
}
|
||||
/**
|
||||
* @return Doctrine_Session
|
||||
*/
|
||||
public function getSession() {
|
||||
return $this->session;
|
||||
}
|
||||
/**
|
||||
* setView
|
||||
* sets a database view this query object uses
|
||||
* this method should only be called internally by doctrine
|
||||
*
|
||||
* @param Doctrine_View $view database view
|
||||
* @return void
|
||||
*/
|
||||
public function setView(Doctrine_View $view) {
|
||||
$this->view = $view;
|
||||
}
|
||||
/**
|
||||
* getView
|
||||
*
|
||||
* @return Doctrine_View
|
||||
*/
|
||||
public function getView() {
|
||||
return $this->view;
|
||||
}
|
||||
|
||||
/**
|
||||
* clear
|
||||
* resets all the variables
|
||||
@ -393,13 +424,18 @@ class Doctrine_Query extends Doctrine_Access {
|
||||
public function execute($params = array()) {
|
||||
$this->data = array();
|
||||
$this->collections = array();
|
||||
|
||||
if( ! $this->view)
|
||||
$query = $this->getQuery();
|
||||
else
|
||||
$query = $this->view->getSelectSql();
|
||||
|
||||
switch(count($this->tables)):
|
||||
case 0:
|
||||
throw new DQLException();
|
||||
break;
|
||||
case 1:
|
||||
$query = $this->getQuery();
|
||||
|
||||
|
||||
$keys = array_keys($this->tables);
|
||||
|
||||
|
103
Doctrine/View.php
Normal file
103
Doctrine/View.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* Doctrine_View
|
||||
*
|
||||
* this class represents a database view
|
||||
*/
|
||||
class Doctrine_View {
|
||||
/**
|
||||
* SQL DROP constant
|
||||
*/
|
||||
const DROP = 'DROP VIEW %s';
|
||||
/**
|
||||
* SQL CREATE constant
|
||||
*/
|
||||
const CREATE = 'CREATE VIEW %s AS %s';
|
||||
/**
|
||||
* SQL SELECT constant
|
||||
*/
|
||||
const SELECT = 'SELECT * FROM %s';
|
||||
|
||||
|
||||
/**
|
||||
* @var string $name
|
||||
*/
|
||||
protected $name;
|
||||
/**
|
||||
* @var Doctrine_Query $query
|
||||
*/
|
||||
protected $query;
|
||||
/**
|
||||
* @var PDO $dbh
|
||||
*/
|
||||
protected $dbh;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
*
|
||||
* @param Doctrine_Query $query
|
||||
*/
|
||||
public function __construct(Doctrine_Query $query) {
|
||||
$this->name = get_class($this);
|
||||
$this->query = $query;
|
||||
$this->query->setView($this);
|
||||
$this->dbh = $query->getSession()->getDBH();
|
||||
}
|
||||
/**
|
||||
* simple get method for getting
|
||||
* the associated query object
|
||||
*
|
||||
* @return Doctrine_Query
|
||||
*/
|
||||
public function getQuery() {
|
||||
return $this->query;
|
||||
}
|
||||
/**
|
||||
* returns the name of this view
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName() {
|
||||
return $this->name;
|
||||
}
|
||||
/**
|
||||
* returns the database handler
|
||||
*
|
||||
* @return PDO
|
||||
*/
|
||||
public function getDBH() {
|
||||
return $this->dbh;
|
||||
}
|
||||
/**
|
||||
* creates this view
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function create() {
|
||||
$sql = sprintf(self::CREATE, $this->name, $this->query->getQuery());
|
||||
$this->dbh->query($sql);
|
||||
}
|
||||
/**
|
||||
* drops this view
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function drop() {
|
||||
$this->dbh->query(sprintf(self::DROP, $this->name));
|
||||
}
|
||||
/**
|
||||
* executes the view
|
||||
*
|
||||
* @return Doctrine_Collection
|
||||
*/
|
||||
public function execute() {
|
||||
return $this->query->execute();
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSelectSql() {
|
||||
return sprintf(self::SELECT, $this->name);
|
||||
}
|
||||
}
|
||||
?>
|
20
tests/CacheQuerySqliteTestCase.php
Normal file
20
tests/CacheQuerySqliteTestCase.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
require_once("UnitTestCase.php");
|
||||
|
||||
class Doctrine_Cache_Query_SqliteTestCase extends Doctrine_UnitTestCase {
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->manager->setAttribute(Doctrine::ATTR_CACHE,Doctrine::CACHE_NONE);
|
||||
$dir = $this->session->getAttribute(Doctrine::ATTR_CACHE_DIR);
|
||||
|
||||
if(file_exists($dir.DIRECTORY_SEPARATOR."stats.cache"))
|
||||
unlink($dir.DIRECTORY_SEPARATOR."stats.cache");
|
||||
|
||||
$this->cache = new Doctrine_Cache_Query_Sqlite($this->objTable);
|
||||
$this->cache->deleteAll();
|
||||
}
|
||||
public function testConstructor() {
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
44
tests/ViewTestCase.php
Normal file
44
tests/ViewTestCase.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
class MyView extends Doctrine_View { }
|
||||
|
||||
class Doctrine_ViewTestCase extends Doctrine_UnitTestCase {
|
||||
public function testCreateView() {
|
||||
$query = new Doctrine_Query($this->session);
|
||||
$query->from('User');
|
||||
|
||||
$view = new MyView($query);
|
||||
|
||||
$this->assertEqual($view->getName(), 'MyView');
|
||||
$this->assertEqual($view->getQuery(), $query);
|
||||
$this->assertEqual($view, $query->getView());
|
||||
$this->assertTrue($view->getDBH() instanceof PDO);
|
||||
|
||||
$success = true;
|
||||
|
||||
try {
|
||||
$view->create();
|
||||
} catch(Exception $e) {
|
||||
$success = false;
|
||||
}
|
||||
$this->assertTrue($success);
|
||||
|
||||
$users = $view->execute();
|
||||
$count = $this->dbh->count();
|
||||
$this->assertTrue($users instanceof Doctrine_Collection);
|
||||
$this->assertEqual($users->count(), 8);
|
||||
$this->assertEqual($users[0]->name, 'zYne');
|
||||
$this->assertEqual($users[0]->getState(), Doctrine_Record::STATE_CLEAN);
|
||||
$this->assertEqual($count, $this->dbh->count());
|
||||
|
||||
$success = true;
|
||||
try {
|
||||
$view->drop();
|
||||
} catch(Exception $e) {
|
||||
$success = false;
|
||||
}
|
||||
$this->assertTrue($success);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
@ -17,6 +17,8 @@ require_once("PessimisticLockingTestCase.php");
|
||||
require_once("CacheSqliteTestCase.php");
|
||||
require_once("CollectionOffsetTestCase.php");
|
||||
require_once("QueryTestCase.php");
|
||||
require_once("CacheQuerySqliteTestCase.php");
|
||||
require_once("ViewTestCase.php");
|
||||
|
||||
error_reporting(E_ALL);
|
||||
|
||||
@ -48,7 +50,9 @@ $test->addTestCase(new Doctrine_PessimisticLockingTestCase());
|
||||
|
||||
$test->addTestCase(new Doctrine_QueryTestCase());
|
||||
|
||||
$test->addTestCase(new Doctrine_ViewTestCase());
|
||||
|
||||
//$test->addTestCase(new Doctrine_Cache_Query_SqliteTestCase());
|
||||
//$test->addTestCase(new Doctrine_Cache_FileTestCase());
|
||||
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user