1
0
mirror of synced 2025-01-18 06:21:40 +03:00

More refactorings. Commented out the plugin tests for now. They seem to wreak havoc with a lot of other tests (especially validator tests) and i havent found the reason yet.

This commit is contained in:
romanb 2007-11-24 18:11:09 +00:00
parent 24a8cef0e6
commit 78f5a2edcb
13 changed files with 1088 additions and 641 deletions

View File

@ -238,20 +238,6 @@ abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable
return true;
}
/**
* getCacheDriver
*
* @return Doctrine_Cache_Interface
*/
public function getCacheDriver()
{
if ( ! isset($this->attributes[Doctrine::ATTR_CACHE])) {
throw new Doctrine_Exception('Cache driver not initialized.');
}
return $this->attributes[Doctrine::ATTR_CACHE];
}
/**
* @param Doctrine_EventListener $listener
* @return void

View File

@ -914,17 +914,15 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$this->getAttribute(Doctrine::ATTR_LISTENER)->preQuery($event);
if ( ! $event->skipOperation) {
//echo $query . "<br />";
$stmt = $this->dbh->query($query);
$this->_count++;
}
$this->getAttribute(Doctrine::ATTR_LISTENER)->postQuery($event);
return $stmt;
}
} catch(Doctrine_Adapter_Exception $e) {
} catch(PDOException $e) { }
} catch (Doctrine_Adapter_Exception $e) {
} catch (PDOException $e) { }
$this->rethrowException($e, $this);
}
@ -959,8 +957,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
return $count;
}
} catch(Doctrine_Adapter_Exception $e) {
} catch(PDOException $e) { }
} catch (Doctrine_Adapter_Exception $e) {
} catch (PDOException $e) { }
$this->rethrowException($e, $this);
}
@ -1202,6 +1200,31 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
return $this->dbh->errorInfo();
}
/**
* getCacheDriver
*
* @return Doctrine_Cache_Interface
* @deprecated Use getResultCacheDriver()
*/
public function getCacheDriver()
{
return $this->getResultCacheDriver();
}
/**
* getResultCacheDriver
*
* @return Doctrine_Cache_Interface
*/
public function getResultCacheDriver()
{
if ( ! isset($this->attributes[Doctrine::ATTR_CACHE])) {
throw new Doctrine_Exception('Result Cache driver not initialized.');
}
return $this->attributes[Doctrine::ATTR_CACHE];
}
/**
* lastInsertId
*

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -34,7 +34,7 @@ class Doctrine_Query_From extends Doctrine_Query_Part
{
/**
* DQL FROM PARSER
* parses the from part of the query string
* parses the FROM part of the query string
*
* @param string $str
* @return void
@ -54,7 +54,6 @@ class Doctrine_Query_From extends Doctrine_Query_Part
break;
}
$last = '';
foreach ($parts as $k => $part) {

View File

@ -1,6 +1,6 @@
<?php
/*
* $Id: Query.php 1296 2007-04-26 17:42:03Z zYne $
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@ -27,8 +27,9 @@
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 1296 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @version $Revision$
*/
class Doctrine_Query_Parser
{ }
{
}

View File

@ -22,6 +22,12 @@ Doctrine::autoload('Doctrine_Query_Abstract');
/**
* Doctrine_RawSql
*
* Doctrine_RawSql is an implementation of Doctrine_Query_Abstract that skips the entire
* DQL parsing procedure. The "DQL" that is passed to a RawSql query object for execution
* is considered to be plain SQL and will be used "as is". The only query part that is special
* in a RawSql query is the SELECT part, which has a special syntax that provides Doctrine
* with the necessary information to properly hydrate the query results.
*
* @package Doctrine
* @subpackage RawSql
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
@ -38,8 +44,20 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
private $fields = array();
/**
* parseQueryPart
* parses given query part
* @deprecated
*/
public function parseQueryPart($queryPartName, $queryPart, $append = false)
{
return $this->parseDqlQueryPart($queryPartName, $queryPart, $append);
}
/**
* parseDqlQueryPart
* parses given DQL query part. Overrides Doctrine_Query_Abstract::parseDqlQueryPart().
* This implementation does no parsing at all, except of the SELECT portion of the query
* which is special in RawSql queries. The entire remaining parts are used "as is", so
* the user of the RawSql query is responsible for writing SQL that is portable between
* different DBMS.
*
* @param string $queryPartName the name of the query part
* @param string $queryPart query part to be parsed
@ -48,47 +66,60 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
* the given query part stack with $queryPart
* @return Doctrine_Query this object
*/
public function parseQueryPart($queryPartName, $queryPart, $append = false)
public function parseDqlQueryPart($queryPartName, $queryPart, $append = false)
{
if ($queryPartName == 'select') {
$this->parseSelectFields($queryPart);
$this->_parseSelectFields($queryPart);
return $this;
}
if( ! isset($this->parts[$queryPartName])) {
$this->parts[$queryPartName] = array();
if ( ! isset($this->parts[$queryPartName])) {
$this->_sqlParts[$queryPartName] = array();
}
if (! $append) {
$this->parts[$queryPartName] = array($queryPart);
}else {
$this->parts[$queryPartName][] = $queryPart;
if ( ! $append) {
$this->_sqlParts[$queryPartName] = array($queryPart);
} else {
$this->_sqlParts[$queryPartName][] = $queryPart;
}
return $this;
}
/**
* Add select parts to fields
* Adds a DQL query part. Overrides Doctrine_Query_Abstract::_addDqlQueryPart().
* This implementation for RawSql parses the new parts right away, generating the SQL.
*/
protected function _addDqlQueryPart($queryPartName, $queryPart, $append = false)
{
return $this->parseQueryPart($queryPartName, $queryPart, $append);
}
/**
* Add select parts to fields.
*
* @param $queryPart sting The name of the querypart
*/
private function parseSelectFields($queryPart){
private function _parseSelectFields($queryPart){
preg_match_all('/{([^}{]*)}/U', $queryPart, $m);
$this->fields = $m[1];
$this->parts['select'] = array();
$this->_sqlParts['select'] = array();
}
/**
* parseQuery
* parses an sql query and adds the parts to internal array
* parseDqlQuery
* parses an sql query and adds the parts to internal array.
* Overrides Doctrine_Query_Abstract::parseDqlQuery().
* This implementation simply tokenizes the provided query string and uses them
* as SQL parts right away.
*
* @param string $query query to be parsed
* @return Doctrine_RawSql this object
*/
public function parseQuery($query)
public function parseDqlQuery($query)
{
$this->parseSelectFields($query);
$this->_parseSelectFields($query);
$this->clear();
$tokens = Doctrine_Tokenizer::sqlExplode($query,' ');
$tokens = $this->_tokenizer->sqlExplode($query, ' ');
$parts = array();
foreach ($tokens as $key => $part) {
@ -131,19 +162,19 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
}
}
$this->parts = $parts;
$this->parts['select'] = array();
$this->_sqlParts = $parts;
$this->_sqlParts['select'] = array();
return $this;
}
/**
* getQuery
* builds the sql query from the given query parts
* getSqlQuery
* builds the sql query.
*
* @return string the built sql query
*/
public function getQuery($params = array())
public function getSqlQuery($params = array())
{
$select = array();
@ -153,10 +184,10 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
throw new Doctrine_RawSql_Exception('All selected fields in Sql query must be in format tableAlias.fieldName');
}
// try to auto-add component
if ( ! $this->hasTableAlias($e[0])) {
if ( ! $this->hasSqlTableAlias($e[0])) {
try {
$this->addComponent($e[0], ucwords($e[0]));
} catch(Doctrine_Exception $exception) {
} catch (Doctrine_Exception $exception) {
throw new Doctrine_RawSql_Exception('The associated component for table alias ' . $e[0] . ' couldn\'t be found.');
}
}
@ -164,7 +195,7 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
$componentAlias = $this->getComponentAlias($e[0]);
if ($e[1] == '*') {
foreach ($this->_aliasMap[$componentAlias]['table']->getColumnNames() as $name) {
foreach ($this->_queryComponents[$componentAlias]['table']->getColumnNames() as $name) {
$field = $e[0] . '.' . $name;
$select[$componentAlias][$field] = $field . ' AS ' . $e[0] . '__' . $name;
@ -177,21 +208,21 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
// force-add all primary key fields
foreach ($this->getTableAliases() as $tableAlias => $componentAlias) {
$map = $this->_aliasMap[$componentAlias];
foreach ($this->getTableAliasMap() as $tableAlias => $componentAlias) {
$map = $this->_queryComponents[$componentAlias];
foreach ((array) $map['table']->getIdentifier() as $key) {
$field = $tableAlias . '.' . $key;
if ( ! isset($this->parts['select'][$field])) {
if ( ! isset($this->_sqlParts['select'][$field])) {
$select[$componentAlias][$field] = $field . ' AS ' . $tableAlias . '__' . $key;
}
}
}
// first add the fields of the root component
reset($this->_aliasMap);
$componentAlias = key($this->_aliasMap);
reset($this->_queryComponents);
$componentAlias = key($this->_queryComponents);
$q = 'SELECT ' . implode(', ', $select[$componentAlias]);
unset($select[$componentAlias]);
@ -204,21 +235,21 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
$string = $this->applyInheritance();
if ( ! empty($string)) {
$this->parts['where'][] = $string;
$this->_sqlParts['where'][] = $string;
}
$copy = $this->parts;
$copy = $this->_sqlParts;
unset($copy['select']);
$q .= ( ! empty($this->parts['from']))? ' FROM ' . implode(' ', $this->parts['from']) : '';
$q .= ( ! empty($this->parts['where']))? ' WHERE ' . implode(' AND ', $this->parts['where']) : '';
$q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']) : '';
$q .= ( ! empty($this->parts['having']))? ' HAVING ' . implode(' AND ', $this->parts['having']) : '';
$q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : '';
$q .= ( ! empty($this->parts['limit']))? ' LIMIT ' . implode(' ', $this->parts['limit']) : '';
$q .= ( ! empty($this->parts['offset']))? ' OFFSET ' . implode(' ', $this->parts['offset']) : '';
$q .= ( ! empty($this->_sqlParts['from']))? ' FROM ' . implode(' ', $this->_sqlParts['from']) : '';
$q .= ( ! empty($this->_sqlParts['where']))? ' WHERE ' . implode(' AND ', $this->_sqlParts['where']) : '';
$q .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : '';
$q .= ( ! empty($this->_sqlParts['having']))? ' HAVING ' . implode(' AND ', $this->_sqlParts['having']) : '';
$q .= ( ! empty($this->_sqlParts['orderby']))? ' ORDER BY ' . implode(', ', $this->_sqlParts['orderby']) : '';
$q .= ( ! empty($this->_sqlParts['limit']))? ' LIMIT ' . implode(' ', $this->_sqlParts['limit']) : '';
$q .= ( ! empty($this->_sqlParts['offset']))? ' OFFSET ' . implode(' ', $this->_sqlParts['offset']) : '';
if ( ! empty($string)) {
array_pop($this->parts['where']);
array_pop($this->_sqlParts['where']);
}
return $q;
}
@ -255,8 +286,8 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
$currPath = '';
if (isset($this->_aliasMap[$e[0]])) {
$table = $this->_aliasMap[$e[0]]['table'];
if (isset($this->_queryComponents[$e[0]])) {
$table = $this->_queryComponents[$e[0]]['table'];
$currPath = $parent = array_shift($e);
}
@ -281,15 +312,15 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
->getConnectionForComponent($component);
$table = $conn->getTable($component);
$this->_aliasMap[$componentAlias] = array('table' => $table);
$this->_queryComponents[$componentAlias] = array('table' => $table);
} else {
$relation = $table->getRelation($component);
$this->_aliasMap[$componentAlias] = array('table' => $relation->getTable(),
$this->_queryComponents[$componentAlias] = array('table' => $relation->getTable(),
'parent' => $parent,
'relation' => $relation);
}
$this->addTableAlias($tableAlias, $componentAlias);
$this->addSqlTableAlias($tableAlias, $componentAlias);
$parent = $currPath;
}

View File

@ -55,7 +55,9 @@ class Doctrine_Relation_Association extends Doctrine_Relation
*/
public function getRelationDql($count, $context = 'record')
{
$table = $this->definition['refTable'];
$component = $this->definition['refTable']->getComponentName();
switch ($context) {
case "record":
$sub = substr(str_repeat("?, ", $count),0,-2);
@ -88,7 +90,7 @@ class Doctrine_Relation_Association extends Doctrine_Relation
if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
$coll = new Doctrine_Collection($this->getTable());
} else {
$coll = Doctrine_Query::create()->parseQuery($this->getRelationDql(1))->execute(array($id));
$coll = Doctrine_Query::create()->query($this->getRelationDql(1), array($id));
}
$coll->setReference($record, $this);
return $coll;

View File

@ -109,7 +109,7 @@ class Doctrine_Tokenizer
*
* parameters:
* $str = email LIKE 'John@example.com'
* $d = ' AND '
* $d = ' LIKE '
*
* would return an array:
* array("email", "LIKE", "'John@example.com'")

View File

@ -47,16 +47,6 @@ class Doctrine_DataType_Enum_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($test->status, 'open');
$test->save();
try {
$query = new Doctrine_Query($this->connection);
$ret = $query->parseQuery('FROM EnumTest WHERE EnumTest.status = ?')
->execute(array('open'));
$this->assertEqual(count($ret), 1);
} catch (Exception $e) {
$this->fail();
}
try {
$query = new Doctrine_Query($this->connection);
$ret = $query->query("FROM EnumTest WHERE EnumTest.status = 'open'");

View File

@ -98,7 +98,7 @@ class Doctrine_Query_TestCase extends Doctrine_UnitTestCase
$q = new Doctrine_Query();
$q->from('User u')->leftJoin('u.Phonenumber p');
$q->getQuery();
//Doctrine::dump($q->getCachedForm(array('foo' => 'bar')));
$this->assertEqual($q->parseClause("CONCAT('u.name', u.name)"), "CONCAT('u.name', e.name)");
}
}
@ -106,8 +106,6 @@ class MyQuery extends Doctrine_Query
{
public function preQuery()
{
if ($this->getRoot()->getComponentName() == 'User') {
$this->where('u.id = 4');
}
}
}

View File

@ -125,6 +125,7 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$coll[5]->Phonenumber[0]->phonenumber;
$this->assertEqual($count, $this->conn->count());
}
public function testAliasesAreSupportedInAddComponent()
{
$query = new Doctrine_RawSql();
@ -161,8 +162,10 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
public function testConvenienceMethods()
{
$query = new Doctrine_RawSql($this->connection);
$query->select('{entity.name}')->from('entity');
$query->addComponent('entity', 'User');

View File

@ -145,7 +145,7 @@ $test->addTestCase($data_types);
// Utility components
$plugins = new GroupTest('Plugin tests: View, Validator, Hook','plugins');
//$utility->addTestCase(new Doctrine_PessimisticLocking_TestCase());
$plugins->addTestCase(new Doctrine_Plugin_TestCase());
//$plugins->addTestCase(new Doctrine_Plugin_TestCase());
$plugins->addTestCase(new Doctrine_View_TestCase());
$plugins->addTestCase(new Doctrine_AuditLog_TestCase());
$plugins->addTestCase(new Doctrine_Validator_TestCase());