From 84bbfccbe29c854fc53ab51dd4a9f28a6ada7d32 Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 14 Dec 2006 13:26:16 +0000 Subject: [PATCH] DQL now supports UPDATE queries --- lib/Doctrine.php | 4 +- lib/Doctrine/Connection/UnitOfWork.php | 5 +- lib/Doctrine/Export.php | 5 +- lib/Doctrine/Hydrate.php | 19 +++---- lib/Doctrine/Manager.php | 7 ++- lib/Doctrine/Query.php | 74 +++++++++----------------- lib/Doctrine/Query/Set.php | 34 ++++++++---- lib/Doctrine/Query/Where.php | 13 +++-- lib/Doctrine/Record.php | 3 ++ 9 files changed, 89 insertions(+), 75 deletions(-) diff --git a/lib/Doctrine.php b/lib/Doctrine.php index f10882f57..b21950f73 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -314,7 +314,7 @@ final class Doctrine { * @param string $directory */ public static function import($directory) { - + Doctrine_Import::import(); } /** * export @@ -323,7 +323,7 @@ final class Doctrine { * @param string $directory */ public static function export($directory) { - + Doctrine_Export::export(); } /** * compile diff --git a/lib/Doctrine/Connection/UnitOfWork.php b/lib/Doctrine/Connection/UnitOfWork.php index 553dae870..acdc02c98 100644 --- a/lib/Doctrine/Connection/UnitOfWork.php +++ b/lib/Doctrine/Connection/UnitOfWork.php @@ -276,7 +276,10 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module implemen $params = array_merge($params, $id); - $sql = "UPDATE ".$record->getTable()->getTableName()." SET ".implode(", ",$set)." WHERE ".implode(" = ? AND ",$record->getTable()->getPrimaryKeys())." = ?"; + $sql = 'UPDATE ' . $record->getTable()->getTableName() + . ' SET ' . implode(', ', $set) + . ' WHERE ' . implode(' = ? AND ', $record->getTable()->getPrimaryKeys()) + . ' = ?'; $stmt = $this->conn->getDBH()->prepare($sql); $stmt->execute($params); diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index 08ea6abbb..567c7aba5 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -585,8 +585,11 @@ class Doctrine_Export extends Doctrine_Connection_Module { } /** * export + * method for exporting Doctrine_Record classes to a schema + * + * @return void */ - public function export() { + public static function export() { $parent = new ReflectionClass('Doctrine_Record'); $conn = Doctrine_Manager::getInstance()->getCurrentConnection(); $old = $conn->getAttribute(Doctrine::ATTR_CREATE_TABLES); diff --git a/lib/Doctrine/Hydrate.php b/lib/Doctrine/Hydrate.php index b0a69e096..eb3fd4919 100644 --- a/lib/Doctrine/Hydrate.php +++ b/lib/Doctrine/Hydrate.php @@ -96,15 +96,16 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { * @var array $parts SQL query string parts */ protected $parts = array( - "select" => array(), - "from" => array(), - "join" => array(), - "where" => array(), - "groupby" => array(), - "having" => array(), - "orderby" => array(), - "limit" => false, - "offset" => false, + 'select' => array(), + 'from' => array(), + 'set' => array(), + 'join' => array(), + 'where' => array(), + 'groupby' => array(), + 'having' => array(), + 'orderby' => array(), + 'limit' => false, + 'offset' => false, ); /** * constructor diff --git a/lib/Doctrine/Manager.php b/lib/Doctrine/Manager.php index 581cac099..bf8839fed 100644 --- a/lib/Doctrine/Manager.php +++ b/lib/Doctrine/Manager.php @@ -137,8 +137,13 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera } /** * connection + * + * if the adapter parameter is set this method acts as * a short cut for Doctrine_Manager::getInstance()->openConnection($adapter, $name); * + * if the adapter paramater is not set this method acts as + * a short cut for Doctrine_Manager::getInstance()->getCurrentConnection() + * * @param PDO|Doctrine_Adapter_Interface $adapter database driver * @param string $name name of the connection, if empty numeric key is used * @throws Doctrine_Manager_Exception if trying to bind a connection with an existing name @@ -150,7 +155,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera } else { return Doctrine_Manager::getInstance()->openConnection($adapter, $name); } - } + } /** * openConnection * opens a new connection and saves it to Doctrine_Manager->connections diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 29d71e64d..5ddf0d71e 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -320,7 +320,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { - $method = "parse".ucwords($name); + $method = 'parse' . ucwords($name); switch($name) { case 'select': @@ -399,48 +399,19 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { return $this->parts[$name]; } /** - * sets a query part + * set + * sets a query SET part + * this method should only be used with UPDATE queries * - * @param $name query part name - * @param $value query part value - * @return boolean + * @param $name name of the field + * @param $value field value + * @return Doctrine_Query */ public function set($name, $value) { - /** - - if(isset($this->parts[$name])) { - $method = "parse".ucwords($name); - switch($name): - case "where": - case "having": - $this->parts[$name] = array($this->$method($value)); - break; - case "limit": - case "offset": - if($value == null) - $value = false; - - $this->parts[$name] = $value; - break; - case "from": - $this->parts['select'] = array(); - $this->parts['join'] = array(); - $this->joins = array(); - $this->tables = array(); - $this->fetchModes = array(); - $this->tableIndexes = array(); - $this->tableAliases = array(); - default: - $this->parts[$name] = array(); - $this->$method($value); - endswitch; - - return true; - } - return false; - */ $class = new Doctrine_Query_Set($this); - $class->parse($name, $value); + $this->parts['set'][] = $class->parse($name . ' = ' . $value); + + return $this; } /** * @return boolean @@ -512,6 +483,10 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { } } + if( ! empty($this->parts['set'])) { + $q .= ' SET ' . implode(', ', $this->parts['set']); + } + $string = $this->applyInheritance(); if( ! empty($string)) @@ -745,8 +720,8 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $this->clear(); $query = trim($query); - $query = str_replace("\n"," ",$query); - $query = str_replace("\r"," ",$query); + $query = str_replace("\n", ' ', $query); + $query = str_replace("\r", ' ', $query); $parts = $this->splitQuery($query); @@ -756,7 +731,6 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { case 'DELETE': $this->type = self::DELETE; break; - case 'SELECT': $this->type = self::SELECT; $this->parseSelect($part); @@ -766,27 +740,31 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $k = 'FROM'; case 'FROM': - case 'SET': - $class = 'Doctrine_Query_'.ucwords(strtolower($k)); + $class = 'Doctrine_Query_' . ucwords(strtolower($k)); $parser = new $class($this); $parser->parse($part); break; + case 'SET': + $class = 'Doctrine_Query_' . ucwords(strtolower($k)); + $parser = new $class($this); + $this->parts['set'][] = $parser->parse($part); + break; case 'GROUP': case 'ORDER': - $k .= "by"; + $k .= 'by'; case 'WHERE': case 'HAVING': - $class = "Doctrine_Query_".ucwords(strtolower($k)); + $class = 'Doctrine_Query_' . ucwords(strtolower($k)); $parser = new $class($this); $name = strtolower($k); $this->parts[$name][] = $parser->parse($part); break; case 'LIMIT': - $this->parts["limit"] = trim($part); + $this->parts['limit'] = trim($part); break; case 'OFFSET': - $this->parts["offset"] = trim($part); + $this->parts['offset'] = trim($part); break; } } diff --git a/lib/Doctrine/Query/Set.php b/lib/Doctrine/Query/Set.php index 83b6408ee..7c7164e43 100644 --- a/lib/Doctrine/Query/Set.php +++ b/lib/Doctrine/Query/Set.php @@ -21,17 +21,33 @@ /** * Doctrine_Query * - * @package Doctrine - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @category Object Relational Mapping - * @link www.phpdoctrine.com - * @since 1.0 - * @version $Revision$ - * @author Konsta Vesterinen - */ + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + * @author Konsta Vesterinen + */ class Doctrine_Query_Set extends Doctrine_Query_Part { public function parse($dql) { - $e = Doctrine_Query::sqlExplode($dql, '='); + $parts = Doctrine_Query::sqlExplode($dql, ','); + + $result = array(); + foreach($parts as $part) { + $set = Doctrine_Query::sqlExplode($part, '='); + + $e = explode('.', trim($set[0])); + $field = array_pop($e); + + $reference = implode('.', $e); + + $alias = $this->query->getTableAlias($reference); + + $result[] = $alias . '.' . $field . ' = ' . $set[1]; + } + + return implode(', ', $result); } } ?> diff --git a/lib/Doctrine/Query/Where.php b/lib/Doctrine/Query/Where.php index 8f0ab1860..cd448ddb1 100644 --- a/lib/Doctrine/Query/Where.php +++ b/lib/Doctrine/Query/Where.php @@ -37,7 +37,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition { $value = implode('', $slice); $operator = trim(substr($where, strlen($r), -strlen($value))); - $reference = implode(".",$a); + $reference = implode('.', $a); $count = count($a); @@ -51,7 +51,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition { $values = Doctrine_Query::sqlExplode($value, ','); $field = array_pop($a); - $reference = implode(".",$a); + $reference = implode('.', $a); $table = $this->query->load($reference, false); array_pop($a); $reference2 = implode('.', $a); @@ -189,7 +189,12 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition { return $operator . ' ('.$this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')'; } - + /** + * getOperator + * + * @param string $func + * @return string + */ public function getOperator($func) { switch($func) { case 'contains': @@ -206,7 +211,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition { } public function __toString() { - return ( ! empty($this->parts))?implode(" AND ", $this->parts):''; + return ( ! empty($this->parts))?implode(' AND ', $this->parts):''; } } diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index b43da620a..4975f40c9 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -1369,6 +1369,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } public function hasIndex($name ) { + } + public function actsAsTree($treeImplName, $args) { + } /** * addListener