[Beeframework-svn] SF.net SVN: beeframework:[37] trunk
Brought to you by:
b_hartmann,
m_plomer
From: <m_p...@us...> - 2013-05-21 14:20:18
|
Revision: 37 http://sourceforge.net/p/beeframework/code/37 Author: m_plomer Date: 2013-05-21 14:20:15 +0000 (Tue, 21 May 2013) Log Message: ----------- - updated ordered behavior with proper group handling Modified Paths: -------------- trunk/examples/classes/Persistence/Doctrine/OrderedColorsDao.php trunk/examples/classes/Persistence/Doctrine/OrderedColorsEntity.php trunk/examples/classes/Persistence/Pdo/OrderedColorsDao.php trunk/examples/classes/Persistence/Pdo/SimpleHierarchyDao.php trunk/examples/conf/context.xml trunk/examples/index.php trunk/framework/Bee/Persistence/Behaviors/Ordered/Strategy.php trunk/framework/Bee/Persistence/Doctrine/Behaviors/DelegateBase.php trunk/framework/Bee/Persistence/Doctrine/Behaviors/GenericOrderedDelegate.php trunk/framework/Bee/Persistence/Pdo/Behaviors/DelegateBase.php trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericNestedSetDelegate.php trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericOrderedDelegate.php trunk/framework/Bee/Persistence/Pdo/SimpleDaoBase.php Added Paths: ----------- trunk/framework/Bee/Persistence/Behaviors/AbstractFieldBasedDelegate.php Modified: trunk/examples/classes/Persistence/Doctrine/OrderedColorsDao.php =================================================================== --- trunk/examples/classes/Persistence/Doctrine/OrderedColorsDao.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/examples/classes/Persistence/Doctrine/OrderedColorsDao.php 2013-05-21 14:20:15 UTC (rev 37) @@ -35,8 +35,9 @@ public function __construct(\Doctrine_Connection $conn) { $this->setDoctrineConnection($conn); - $pdoOrderedDelagate = new GenericOrderedDelegate(self::ENTITY_CLASS_NAME, $conn); - $this->orderedStrategy = new OrderedStrategy($pdoOrderedDelagate); + $delagate = new GenericOrderedDelegate(self::ENTITY_CLASS_NAME, $conn); + $delagate->setGroupFieldName('color_grp'); + $this->orderedStrategy = new OrderedStrategy($delagate); } /** @@ -57,19 +58,20 @@ }); } - public function addColor($colorName, $colorHex) { - self::getLog()->info("adding color ($colorName, $colorHex)"); - return $this->doInTransaction(function(OrderedColorsDao $dao, \Logger $log) use ($colorName, $colorHex) { + public function addColor($colorName, $colorHex, $grpId) { + self::getLog()->info("adding color ($colorName, $colorHex, $grpId)"); + return $this->doInTransaction(function(OrderedColorsDao $dao, \Logger $log) use ($colorName, $colorHex, $grpId) { $log->debug('inserting'); $entity = new OrderedColorsEntity(); $entity->setName($colorName); $entity->setHexValue($colorHex); + $entity->setColorGrp($grpId); $entity->save($dao->getDoctrineConnection()); $log->debug('moving to end of list'); $pos = $dao->getOrderedStrategy()->moveToEnd($entity); - $log->debug("committing ($entity->id, $colorName, $colorHex, $pos)"); + $log->debug("committing ($entity->id, $colorName, $colorHex, $grpId, $pos)"); return $entity; }); } Modified: trunk/examples/classes/Persistence/Doctrine/OrderedColorsEntity.php =================================================================== --- trunk/examples/classes/Persistence/Doctrine/OrderedColorsEntity.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/examples/classes/Persistence/Doctrine/OrderedColorsEntity.php 2013-05-21 14:20:15 UTC (rev 37) @@ -1,20 +1,20 @@ <?php namespace Persistence\Doctrine; -/* - * Copyright 2008-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ + /* + * Copyright 2008-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /** * User: mp @@ -31,46 +31,73 @@ $this->hasColumn('name', 'string', 255); $this->hasColumn('hex_value', 'string', 10); + $this->hasColumn('color_grp', 'int'); $this->hasColumn('pos', 'int'); + + $drvName = \Doctrine_Manager::connection()->getAttribute(\PDO::ATTR_DRIVER_NAME); + if($drvName == 'mysql') { + // sqlite does generally not support ordered updates + $this->index('grp_pos', array( + 'fields' => array( + 'color_grp', + 'pos' => array('sorting' => 'ASC') + ), + 'type' => 'unique', + )); + } } /** - * @param mixed $hexValue + * @param string $hexValue */ public function setHexValue($hexValue) { $this->hex_value = $hexValue; } /** - * @return mixed + * @return string */ public function getHexValue() { return $this->hex_value; } /** - * @param mixed $name + * @param string $name */ public function setName($name) { $this->name = $name; } /** - * @return mixed + * @return string */ public function getName() { return $this->name; } /** - * @param mixed $pos + * @param int $colorGrp */ + public function setColorGrp($colorGrp) { + $this->color_grp = $colorGrp; + } + + /** + * @return int + */ + public function getColorGrp() { + return $this->color_grp; + } + + /** + * @param int $pos + */ public function setPos($pos) { $this->pos = $pos; } /** - * @return mixed + * @return int */ public function getPos() { return $this->pos; Modified: trunk/examples/classes/Persistence/Pdo/OrderedColorsDao.php =================================================================== --- trunk/examples/classes/Persistence/Pdo/OrderedColorsDao.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/examples/classes/Persistence/Pdo/OrderedColorsDao.php 2013-05-21 14:20:15 UTC (rev 37) @@ -26,7 +26,7 @@ * Date: 07.05.13 * Time: 12:12 */ - + class OrderedColorsDao extends SimpleDaoBase { /** @@ -36,20 +36,39 @@ public function __construct(\PDO $pdoConnection) { parent::__construct($pdoConnection); - $pdoOrderedDelagate = new GenericOrderedDelegate('ordered_colors_pdo', $pdoConnection); - $this->orderedStrategy = new OrderedStrategy($pdoOrderedDelagate); + $delagate = new GenericOrderedDelegate('ordered_colors_pdo', $pdoConnection); + $delagate->setGroupFieldName('color_grp'); + $this->orderedStrategy = new OrderedStrategy($delagate); } /** * */ public function createTable() { - $this->getPdoConnection()->exec('CREATE TABLE IF NOT EXISTS "ordered_colors_pdo" ( - "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, - "name" text NOT NULL, - "hex_value" text NOT NULL, - "pos" integer DEFAULT NULL)' - ); + $drvName = $this->getPdoConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME); + switch ($drvName) { + case 'mysql': + $this->getPdoConnection()->exec('CREATE TABLE IF NOT EXISTS `ordered_colors_pdo` ( + id bigint(20) NOT NULL AUTO_INCREMENT, + name varchar(255) NOT NULL, + hex_value varchar(7) NOT NULL, + color_grp int(11) NOT NULL, + pos int(11) DEFAULT NULL, + PRIMARY KEY (id), + UNIQUE KEY grp_pos_idx (color_grp, pos)) + ENGINE=InnoDB CHARSET=utf8' + ); + break; + case 'sqlite': + $this->getPdoConnection()->exec('CREATE TABLE IF NOT EXISTS "ordered_colors_pdo" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "name" text NOT NULL, + "hex_value" text NOT NULL, + "color_grp" integer NOT NULL, + "pos" integer DEFAULT NULL)' + ); + break; + } } /** @@ -60,24 +79,24 @@ } public function deleteAllColors() { - $this->doInTransaction(function(OrderedColorsDao $dao, \Logger $log) { + $this->doInTransaction(function (OrderedColorsDao $dao, \Logger $log) { $log->debug('deleting all colors'); $dao->getPdoConnection()->exec('DELETE FROM ordered_colors_pdo'); }); } - public function addColor($colorName, $colorHex) { - self::getLog()->info("adding color ($colorName, $colorHex)"); - return $this->doInTransaction(function(OrderedColorsDao $dao, \Logger $log) use ($colorName, $colorHex) { + public function addColor($colorName, $colorHex, $grpId) { + self::getLog()->info("adding color ($colorName, $colorHex, $grpId)"); + return $this->doInTransaction(function (OrderedColorsDao $dao, \Logger $log) use ($colorName, $colorHex, $grpId) { $log->debug('inserting'); - $insertStmt = $dao->getPdoConnection()->prepare('INSERT INTO ordered_colors_pdo (name, hex_value) VALUES (:name, :hex_value)'); - $insertStmt->execute(array(':name' => $colorName, ':hex_value' => $colorHex)); + $insertStmt = $dao->getPdoConnection()->prepare('INSERT INTO ordered_colors_pdo (name, hex_value, color_grp) VALUES (:name, :hex_value, :grp_id)'); + $insertStmt->execute(array(':name' => $colorName, ':hex_value' => $colorHex, ':grp_id' => $grpId)); $id = $dao->getPdoConnection()->lastInsertId(); $log->debug('moving to end of list'); $pos = $dao->getOrderedStrategy()->moveToEnd($id); - $log->debug("committing ($id, $colorName, $colorHex, $pos)"); + $log->debug("committing ($id, $colorName, $colorHex, $grpId, $pos)"); return $id; }); Modified: trunk/examples/classes/Persistence/Pdo/SimpleHierarchyDao.php =================================================================== --- trunk/examples/classes/Persistence/Pdo/SimpleHierarchyDao.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/examples/classes/Persistence/Pdo/SimpleHierarchyDao.php 2013-05-21 14:20:15 UTC (rev 37) @@ -35,27 +35,48 @@ public function __construct(\PDO $pdoConnection) { parent::__construct($pdoConnection); - $pdoNestedSetDelagate = new GenericNestedSetDelegate('simple_hierarchy', $pdoConnection); - $this->nestedSetStrategy = new NestedSetStrategy($pdoNestedSetDelagate); + $delagate = new GenericNestedSetDelegate('simple_hierarchy_pdo', $pdoConnection); + $delagate->setGroupFieldName('root_id'); + $this->nestedSetStrategy = new NestedSetStrategy($delagate); } /** * */ public function createTable() { - $this->getPdoConnection()->exec('CREATE TABLE IF NOT EXISTS "simple_hierarchy" ( - "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, - "name" text NOT NULL, - "lft" integer DEFAULT NULL, - "rgt" integer DEFAULT NULL, - "lvl" integer DEFAULT NULL)' - ); + $drvName = $this->getPdoConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME); + switch ($drvName) { + case 'mysql': + $this->getPdoConnection()->exec('CREATE TABLE IF NOT EXISTS `simple_hierarchy_pdo` ( + id bigint(20) NOT NULL AUTO_INCREMENT, + name varchar(255) NOT NULL, + root_id int(11) NOT NULL, + lft int(11) DEFAULT NULL, + rgt int(11) DEFAULT NULL, + lvl int(11) DEFAULT NULL, + PRIMARY KEY (id), + UNIQUE KEY grp_lft (root_id, lft), + UNIQUE KEY grp_rgt (root_id, rgt)) + ENGINE=InnoDB CHARSET=utf8' + ); + break; + case 'sqlite': + $this->getPdoConnection()->exec('CREATE TABLE IF NOT EXISTS "simple_hierarchy_pdo" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "name" text NOT NULL, + "root_id" integer NOT NULL, + "lft" integer DEFAULT NULL, + "rgt" integer DEFAULT NULL, + "lvl" integer DEFAULT NULL)' + ); + break; + } } public function deleteAll() { $this->doInTransaction(function(SimpleHierarchyDao $dao, \Logger $log) { $log->debug('deleting all hierarchy entries'); - $dao->getPdoConnection()->exec('DELETE FROM simple_hierarchy'); + $dao->getPdoConnection()->exec('DELETE FROM simple_hierarchy_pdo'); }); } @@ -70,7 +91,7 @@ return $this->doInTransaction(function(SimpleHierarchyDao $dao, \Logger $log) use ($name) { $log->info("adding entry ($name)"); $log->debug('inserting'); - $insertStmt = $dao->getPdoConnection()->prepare('INSERT INTO simple_hierarchy (name) VALUES (:name)'); + $insertStmt = $dao->getPdoConnection()->prepare('INSERT INTO simple_hierarchy_pdo (name) VALUES (:name)'); $insertStmt->execute(array(':name' => $name)); $id = $dao->getPdoConnection()->lastInsertId(); Modified: trunk/examples/conf/context.xml =================================================================== --- trunk/examples/conf/context.xml 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/examples/conf/context.xml 2013-05-21 14:20:15 UTC (rev 37) @@ -8,10 +8,10 @@ <!-- Persistence - PDO Connection --> <bean id="pdoConnection" class="PDO"> <!-- LOCAL DB --> - <constructor-arg index="0" value="sqlite:db/examples.sqlite" /> - <constructor-arg index="1" value="sa" /> - <!--<constructor-arg index="0" value="mysql:host=127.0.0.1;dbname=bee_test" />--> - <!--<constructor-arg index="1" value="root" />--> + <!--<constructor-arg index="0" value="sqlite:db/examples.sqlite" />--> + <!--<constructor-arg index="1" value="sa" />--> + <constructor-arg index="0" value="mysql:host=127.0.0.1;dbname=bee_test" /> + <constructor-arg index="1" value="root" /> <constructor-arg index="2" value="" /> </bean> Modified: trunk/examples/index.php =================================================================== --- trunk/examples/index.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/examples/index.php 2013-05-21 14:20:15 UTC (rev 37) @@ -24,6 +24,8 @@ * Time: 12:25 */ +unlink('db/examples.sqlite'); + require_once '../framework/Bee/Framework.php'; require_once 'vendor/autoload.php'; @@ -51,13 +53,18 @@ $dao->createTable(); $dao->deleteAllColors(); -$redId = $dao->addColor('Red', '#ff0000'); -$greenId = $dao->addColor('Green', '#00ff00'); -$blueId = $dao->addColor('Blue', '#0000ff'); -$dao->addColor('Yellow', '#ffff00'); -$purpleId = $dao->addColor('Purple', '#ff00ff'); -$cyanId = $dao->addColor('Cyan', '#00ffff'); +$redId = $dao->addColor('Red', '#ff0000', 1); +$greenId = $dao->addColor('Green', '#00ff00', 1); +$blueId = $dao->addColor('Blue', '#0000ff', 1); +$dao->addColor('Yellow', '#ffff00', 1); +$purpleId = $dao->addColor('Purple', '#ff00ff', 1); +$cyanId = $dao->addColor('Cyan', '#00ffff', 1); +$redId2 = $dao->addColor('Red2', '#ff0000', 2); +$greenId2 = $dao->addColor('Green2', '#00ff00', 2); +$blueId2 = $dao->addColor('Blue2', '#0000ff', 2); +$yellowId2 = $dao->addColor('Yellow2', '#ffff00', 2); + $dao->doInTransaction(function(OrderedColorsDaoPdo $dao, \Logger $log) use ($greenId, $purpleId) { $dao->getOrderedStrategy()->moveAfter($greenId, $purpleId); }); @@ -86,13 +93,18 @@ $dao->createTable(); $dao->deleteAllColors(); -$redId = $dao->addColor('Red', '#ff0000'); -$greenId = $dao->addColor('Green', '#00ff00'); -$blueId = $dao->addColor('Blue', '#0000ff'); -$dao->addColor('Yellow', '#ffff00'); -$purpleId = $dao->addColor('Purple', '#ff00ff'); -$cyanId = $dao->addColor('Cyan', '#00ffff'); +$redId = $dao->addColor('Red', '#ff0000', 1); +$greenId = $dao->addColor('Green', '#00ff00', 1); +$blueId = $dao->addColor('Blue', '#0000ff', 1); +$dao->addColor('Yellow', '#ffff00', 1); +$purpleId = $dao->addColor('Purple', '#ff00ff', 1); +$cyanId = $dao->addColor('Cyan', '#00ffff', 1); +$redId2 = $dao->addColor('Red2', '#ff0000', 2); +$greenId2 = $dao->addColor('Green2', '#00ff00', 2); +$blueId2 = $dao->addColor('Blue2', '#0000ff', 2); +$yellowId2 = $dao->addColor('Yellow2', '#ffff00', 2); + $dao->doInTransaction(function(OrderedColorsDaoDoctrine $dao, \Logger $log) use ($greenId, $purpleId) { $dao->getOrderedStrategy()->moveAfter($greenId, $purpleId); }); Added: trunk/framework/Bee/Persistence/Behaviors/AbstractFieldBasedDelegate.php =================================================================== --- trunk/framework/Bee/Persistence/Behaviors/AbstractFieldBasedDelegate.php (rev 0) +++ trunk/framework/Bee/Persistence/Behaviors/AbstractFieldBasedDelegate.php 2013-05-21 14:20:15 UTC (rev 37) @@ -0,0 +1,85 @@ +<?php +namespace Bee\Persistence\Behaviors; +/* + * Copyright 2008-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +use Bee\Persistence\Pdo\FeatureDetector; + +/** + * User: mp + * Date: 21.05.13 + * Time: 10:19 + */ + +abstract class AbstractFieldBasedDelegate { + + /** + * @var string + */ + private $idFieldName = 'id'; + + /** + * @var string + */ + private $groupFieldName; + + /** + * @var string + */ + private $queryDomain; + + /** + * @param string $queryDomain + */ + public function __construct($queryDomain) { + \Bee_Utils_Assert::hasText($queryDomain, 'Query domain (table / entity name) required, must not be empty'); + $this->queryDomain = $queryDomain; + } + + /** + * @param string $idFieldName + */ + public function setIdFieldName($idFieldName) { + $this->idFieldName = $idFieldName; + } + + /** + * @return string + */ + public function getIdFieldName() { + return $this->idFieldName; + } + + /** + * @param string $domainFieldName + */ + public function setGroupFieldName($domainFieldName) { + $this->groupFieldName = $domainFieldName; + } + + /** + * @return string + */ + public function getGroupFieldName() { + return $this->groupFieldName; + } + + /** + * @return string + */ + public function getQueryDomain() { + return $this->queryDomain; + } +} Modified: trunk/framework/Bee/Persistence/Behaviors/Ordered/Strategy.php =================================================================== --- trunk/framework/Bee/Persistence/Behaviors/Ordered/Strategy.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/framework/Bee/Persistence/Behaviors/Ordered/Strategy.php 2013-05-21 14:20:15 UTC (rev 37) @@ -97,6 +97,7 @@ * @return int */ public function moveRelative($subject, $ref, $before = true, $groupRestriction = false) { + // determine previous position of subject $oldPos = $this->delegate->getPosition($subject, $groupRestriction); @@ -113,10 +114,10 @@ } } else { // no, "move behind nothing" means "move to beginning", "move before nothing" means "move to the end" - $newPos = 0; + $newPos = 1; if ($before) { $maxPos = $this->delegate->getMaxPosition($subject, $groupRestriction); - $newPos = $maxPos !== false ? $maxPos + 1 : 0; + $newPos = $maxPos !== false ? $maxPos + 1 : 1; } } Modified: trunk/framework/Bee/Persistence/Doctrine/Behaviors/DelegateBase.php =================================================================== --- trunk/framework/Bee/Persistence/Doctrine/Behaviors/DelegateBase.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/framework/Bee/Persistence/Doctrine/Behaviors/DelegateBase.php 2013-05-21 14:20:15 UTC (rev 37) @@ -15,7 +15,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +use Bee\Persistence\Behaviors\AbstractFieldBasedDelegate; use Bee\Persistence\Pdo\FeatureDetector; +use Bee\Persistence\Pdo\Utils; /** * User: mp @@ -23,29 +25,20 @@ * Time: 02:19 */ -class DelegateBase extends FeatureDetector { +class DelegateBase extends AbstractFieldBasedDelegate { + /** * @var \Doctrine_Connection */ private $doctrineConnection; /** - * @var string - */ - private $entityClass; - - /** - * @var string - */ - private $idFieldName = 'id'; - - /** * @param string $entityClass * @param \Doctrine_Connection $doctrineConnection * @return \Bee\Persistence\Doctrine\Behaviors\DelegateBase */ public function __construct($entityClass, \Doctrine_Connection $doctrineConnection) { - $this->entityClass = $entityClass; + parent::__construct($entityClass); $this->doctrineConnection = $doctrineConnection; } @@ -57,38 +50,17 @@ } /** - * @return string - */ - public function getEntityClass() { - return $this->entityClass; - } - - /** - * @param string $idFieldName - */ - public function setIdFieldName($idFieldName) { - $this->idFieldName = $idFieldName; - } - - /** - * @return string - */ - public function getIdFieldName() { - return $this->idFieldName; - } - - /** * @return \Doctrine_Query */ protected function getEntityBaseQuery() { - return $this->getDoctrineConnection()->createQuery()->from($this->entityClass); + return $this->getDoctrineConnection()->createQuery()->from($this->getQueryDomain()); } /** * @return \Doctrine_Query */ protected function getEntityUpdateBaseQuery() { - return $this->getDoctrineConnection()->createQuery()->update($this->entityClass); + return $this->getDoctrineConnection()->createQuery()->update($this->getQueryDomain()); } /** @@ -99,7 +71,7 @@ */ protected function addIdentityRestriction(\Doctrine_Query $qry, $orderedEntity) { if($orderedEntity instanceof \Doctrine_Record) { - $id = $orderedEntity->get($this->idFieldName); + $id = $orderedEntity->get($this->getIdFieldName()); } else if(is_numeric($orderedEntity)) { $id = $orderedEntity; } else { @@ -108,11 +80,34 @@ return $qry->addWhere('id = :id', array(':id' => $id)); } + protected function addGroupRestriction(\Doctrine_Query $qry, $orderedEntity, $restriction = false) { + if ($this->getGroupFieldName()) { + if ($restriction === false) { + $restriction = $this->getGroup($orderedEntity); + } + $this->doAddGroupRestriction($qry, $restriction); + } + return $qry; + } + + protected function doAddGroupRestriction(\Doctrine_Query $qry, $restriction = false) { + return $qry->addWhere($this->getGroupFieldName().' = :grp_id', array(':grp_id' => $restriction)); + } + /** * @param string $feature * @return bool */ protected function pdoSupportsFeature($feature) { - return self::supports($feature, $this->getDoctrineConnection()->getDbh()); + return FeatureDetector::supports($feature, $this->getDoctrineConnection()->getDbh()); } + + /** + * @param $entity + * @return mixed + */ + protected function getGroup($entity) { + $grpQry = $this->getDoctrineConnection()->createQuery()->select($this->getGroupFieldName())->from($this->getQueryDomain()); + return $this->addIdentityRestriction($grpQry, $entity)->fetchOne(array(), \Doctrine_Core::HYDRATE_SINGLE_SCALAR); + } } Modified: trunk/framework/Bee/Persistence/Doctrine/Behaviors/GenericOrderedDelegate.php =================================================================== --- trunk/framework/Bee/Persistence/Doctrine/Behaviors/GenericOrderedDelegate.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/framework/Bee/Persistence/Doctrine/Behaviors/GenericOrderedDelegate.php 2013-05-21 14:20:15 UTC (rev 37) @@ -16,6 +16,7 @@ * limitations under the License. */ use Bee\Persistence\Behaviors\Ordered\IDelegate; +use Bee\Persistence\Pdo\FeatureDetector; use Bee\Persistence\Pdo\Utils; /** @@ -61,7 +62,7 @@ * @return int */ public function getMaxPosition($orderedEntity, $restriction = false) { - $qry = $this->addCustomRestricitons($this->getEntityBaseQuery($orderedEntity, $restriction), $restriction); + $qry = $this->addGroupRestriction($this->getEntityBaseQuery($orderedEntity, $restriction), $orderedEntity, $restriction); $result = $this->addMaxPositionSelect($qry)->fetchOne(array(), \Doctrine_Core::HYDRATE_SINGLE_SCALAR); return Utils::numericOrFalse($result); } @@ -76,7 +77,7 @@ public function shiftPosition($orderedEntity, $newPos, $oldPos, $restriction = false) { $params = array(); - $qry = $this->addCustomRestricitons($this->getEntityUpdateBaseQuery(), $restriction); + $qry = $this->addGroupRestriction($this->getEntityUpdateBaseQuery(), $orderedEntity, $restriction); if($oldPos !== false) { if($newPos !== false) { @@ -108,8 +109,8 @@ // if this is a single table update, add ORDER clause to avoid unique constraint violation (if driver supports it) - if ($oldPos !== false && $this->pdoSupportsFeature(self::FEATURE_ORDERED_UPDATE) /*&& stripos($qryDomain, ' JOIN ') === false*/) { - $qry->orderBy($this->getPosExpression() . ($newPos < $oldPos ? ' DESC' : ' ASC')); + if ($oldPos !== false && $this->pdoSupportsFeature(FeatureDetector::FEATURE_ORDERED_UPDATE) /*&& stripos($qryDomain, ' JOIN ') === false*/) { + $qry->orderBy($this->getPosExpression() . ($newPos !== false && ($newPos < $oldPos) ? ' DESC' : ' ASC')); } $qry->execute($params); @@ -121,7 +122,7 @@ * @param mixed $restriction */ public function setPosition($orderedEntity, $newPos, $restriction = false) { - $qry = $this->addCustomRestricitons($this->getEntityUpdateBaseQuery(), $restriction); + $qry = $this->addGroupRestriction($this->getEntityUpdateBaseQuery(), $orderedEntity, $restriction); $this->addIdentityRestriction($qry, $orderedEntity); $qry->set($this->getPosExpression(), is_null($newPos) ? 'NULL' : $newPos)->execute(); } @@ -134,7 +135,7 @@ protected function getIdentityBaseQuery($orderedEntity, $restriction = false) { $qry = $this->getEntityBaseQuery(); $this->addIdentityRestriction($qry, $orderedEntity); - return $this->addCustomRestricitons($qry, $restriction); + return $this->addGroupRestriction($qry, $orderedEntity, $restriction); } /** @@ -152,14 +153,4 @@ protected function addMaxPositionSelect(\Doctrine_Query $qry) { return $qry->select('MAX('.$this->getPosExpression().')'); } - - /** - * @param \Doctrine_Query $qry - * @param mixed $restriction - * @return \Doctrine_Query - */ - protected function addCustomRestricitons(\Doctrine_Query $qry, $restriction = false) { - // no default implementation - return $qry; - } } Modified: trunk/framework/Bee/Persistence/Pdo/Behaviors/DelegateBase.php =================================================================== --- trunk/framework/Bee/Persistence/Pdo/Behaviors/DelegateBase.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/framework/Bee/Persistence/Pdo/Behaviors/DelegateBase.php 2013-05-21 14:20:15 UTC (rev 37) @@ -1,6 +1,7 @@ <?php namespace Bee\Persistence\Pdo\Behaviors; +use Bee\Persistence\Behaviors\AbstractFieldBasedDelegate; use Bee\Persistence\Pdo\FeatureDetector; use Bee\Persistence\Pdo\Utils; @@ -27,7 +28,7 @@ * Date: 05.05.13 * Time: 23:52 */ -abstract class DelegateBase extends FeatureDetector { +abstract class DelegateBase extends AbstractFieldBasedDelegate { const GROUP_QUERY_TEMPLATE = 'SELECT %1$s FROM %2$s WHERE %3$s'; @@ -37,28 +38,12 @@ private $pdo; /** - * @var string - */ - private $queryDomain; - - /** - * @var string - */ - private $idFieldName = 'id'; - - /** - * @var string - */ - private $groupFieldName; - - /** * @param string $queryDomain * @param \PDO $pdo * @return \Bee\Persistence\Pdo\Behaviors\DelegateBase */ public function __construct($queryDomain, \PDO $pdo) { - \Bee_Utils_Assert::hasText($queryDomain, 'Query domain (table name / table joins) required, must not be empty'); - $this->queryDomain = $queryDomain; + parent::__construct($queryDomain); $this->pdo = $pdo; } @@ -70,46 +55,11 @@ } /** - * @return string - */ - public function getQueryDomain() { - return $this->queryDomain; - } - - /** - * @param string $idFieldName - */ - public function setIdFieldName($idFieldName) { - $this->idFieldName = $idFieldName; - } - - /** - * @return string - */ - public function getIdFieldName() { - return $this->idFieldName; - } - - /** - * @param string $domainFieldName - */ - public function setGroupFieldName($domainFieldName) { - $this->groupFieldName = $domainFieldName; - } - - /** - * @return string - */ - public function getGroupFieldName() { - return $this->groupFieldName; - } - - /** * @param string $feature * @return bool */ protected function pdoSupportsFeature($feature) { - return self::supports($feature, $this->getPdo()); + return FeatureDetector::supports($feature, $this->getPdo()); } /** @@ -120,7 +70,7 @@ */ protected function getIdentityRestrictionString($entity, array &$params) { $params[':id'] = $entity; - return $this->idFieldName . ' = :id'; + return $this->getIdFieldName() . ' = :id'; } /** @@ -134,9 +84,7 @@ if ($this->getGroupFieldName()) { if ($restriction === false) { // determine group value - $grpParams = array(); - $qryString = sprintf(self::GROUP_QUERY_TEMPLATE, $this->getGroupFieldName(), $this->getQueryDomain(), $this->getIdentityRestrictionString($entity, $params)); - $restriction = Utils::fetchOne($this->getPdo()->prepare($qryString), $grpParams); + $restriction = $this->getGroup($entity); } $result = $this->doCreateRestrictionString($params, $restriction); } @@ -152,4 +100,15 @@ $params[':group_id'] = $restriction; return $this->getGroupFieldName() . ' = :group_id'; } + + /** + * @param $entity + * @return mixed + */ + protected function getGroup($entity) { + // determine group value + $params = array(); + $qryString = sprintf(self::GROUP_QUERY_TEMPLATE, $this->getGroupFieldName(), $this->getQueryDomain(), $this->getIdentityRestrictionString($entity, $params)); + return Utils::fetchOne($this->getPdo()->prepare($qryString), $params); + } } Modified: trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericNestedSetDelegate.php =================================================================== --- trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericNestedSetDelegate.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericNestedSetDelegate.php 2013-05-21 14:20:15 UTC (rev 37) @@ -3,6 +3,7 @@ use Bee\Persistence\Behaviors\NestedSet\IDelegate; use Bee\Persistence\Behaviors\NestedSet\NodeInfo; +use Bee\Persistence\Pdo\FeatureDetector; use Bee\Persistence\Pdo\Utils; /* @@ -138,7 +139,7 @@ $domRes = $this->getDomainRestrictionString($nestedSetEntity, $params, $restriction); // order updates only if supported by the driver and not operating on a joined relation - $orderUpdate = $this->pdoSupportsFeature(self::FEATURE_ORDERED_UPDATE) && stripos($qryDomain, ' JOIN ') === false; + $orderUpdate = $this->pdoSupportsFeature(FeatureDetector::FEATURE_ORDERED_UPDATE) && stripos($qryDomain, ' JOIN ') === false; // update left positions $qryString = sprintf($qryTempl, $qryDomain, $this->leftFieldName, $domRes); Modified: trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericOrderedDelegate.php =================================================================== --- trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericOrderedDelegate.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericOrderedDelegate.php 2013-05-21 14:20:15 UTC (rev 37) @@ -2,6 +2,7 @@ namespace Bee\Persistence\Pdo\Behaviors; use Bee\Persistence\Behaviors\Ordered\IDelegate; +use Bee\Persistence\Pdo\FeatureDetector; use Bee\Persistence\Pdo\Utils; /* @@ -69,7 +70,7 @@ /** * @param $orderedEntity * @param $restriction - * @return mixed + * @return int|bool */ public function getMaxPosition($orderedEntity, $restriction = false) { $params = array(); @@ -103,8 +104,8 @@ $this->getDomainRestrictionString($orderedEntity, $params, $restriction)); // if this is a single table update, add ORDER clause to avoid unique constraint violation (if driver supports it) - if ($oldPos !== false && $this->pdoSupportsFeature(self::FEATURE_ORDERED_UPDATE) && stripos($qryDomain, ' JOIN ') === false) { - $qryString .= ' ORDER BY ' . $this->getPosExpression() . ($newPos < $oldPos ? ' DESC' : ' ASC'); + if ($oldPos !== false && $this->pdoSupportsFeature(FeatureDetector::FEATURE_ORDERED_UPDATE) && stripos($qryDomain, ' JOIN ') === false) { + $qryString .= ' ORDER BY ' . $this->getPosExpression() . ($newPos !== false && ($newPos < $oldPos) ? ' DESC' : ' ASC'); } $this->getPdo()->prepare($qryString)->execute($params); Modified: trunk/framework/Bee/Persistence/Pdo/SimpleDaoBase.php =================================================================== --- trunk/framework/Bee/Persistence/Pdo/SimpleDaoBase.php 2013-05-20 19:06:48 UTC (rev 36) +++ trunk/framework/Bee/Persistence/Pdo/SimpleDaoBase.php 2013-05-21 14:20:15 UTC (rev 37) @@ -75,6 +75,5 @@ $this->pdoConnection->rollBack(); throw $e; } - } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |