diff --git a/Doctrine/Query.php b/Doctrine/Query.php index 5345e7f73..52ea6bb15 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -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); diff --git a/Doctrine/View.php b/Doctrine/View.php new file mode 100644 index 000000000..9f7393509 --- /dev/null +++ b/Doctrine/View.php @@ -0,0 +1,103 @@ +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); + } +} +?> diff --git a/tests/CacheQuerySqliteTestCase.php b/tests/CacheQuerySqliteTestCase.php new file mode 100644 index 000000000..3066e8cd1 --- /dev/null +++ b/tests/CacheQuerySqliteTestCase.php @@ -0,0 +1,20 @@ +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() { + + } +} +?> diff --git a/tests/ViewTestCase.php b/tests/ViewTestCase.php new file mode 100644 index 000000000..f34789380 --- /dev/null +++ b/tests/ViewTestCase.php @@ -0,0 +1,44 @@ +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); + + + } +} +?> diff --git a/tests/run.php b/tests/run.php index 39ecc6136..274e719af 100644 --- a/tests/run.php +++ b/tests/run.php @@ -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());