[Pkg-owncloud-commits] [owncloud] 11/172: dont update entity and dont run an update query if an entity wasnt changed at all

David Prévot taffit at moszumanska.debian.org
Sun May 18 20:09:34 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit 5199e4508ac0c1d3902434a9cd05987d053d40f0
Author: Bernhard Posselt <dev at bernhard-posselt.com>
Date:   Wed Apr 23 13:43:17 2014 +0200

    dont update entity and dont run an update query if an entity wasnt changed at all
---
 lib/public/appframework/db/entity.php    |  3 +++
 lib/public/appframework/db/mapper.php    |  7 ++++++-
 tests/lib/appframework/db/EntityTest.php | 11 ++++++++++-
 tests/lib/appframework/db/MapperTest.php | 14 ++++++++++++++
 4 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/lib/public/appframework/db/entity.php b/lib/public/appframework/db/entity.php
index 631bf85..8ab42bd 100644
--- a/lib/public/appframework/db/entity.php
+++ b/lib/public/appframework/db/entity.php
@@ -92,6 +92,9 @@ abstract class Entity {
 	protected function setter($name, $args) {
 		// setters should only work for existing attributes
 		if(property_exists($this, $name)){
+			if($this->$name === $args[0]) {
+				return;
+			}
 			$this->markFieldUpdated($name);
 
 			// if type definition exists, cast to correct type
diff --git a/lib/public/appframework/db/mapper.php b/lib/public/appframework/db/mapper.php
index f65a232..13a4c65 100644
--- a/lib/public/appframework/db/mapper.php
+++ b/lib/public/appframework/db/mapper.php
@@ -127,6 +127,12 @@ abstract class Mapper {
 	 * @param Entity $entity the entity that should be created
 	 */
 	public function update(Entity $entity){
+		// if entity wasn't changed it makes no sense to run a db query
+		$properties = $entity->getUpdatedFields();
+		if(count($properties) === 0) {
+			return $entity;
+		}
+
 		// entity needs an id
 		$id = $entity->getId();
 		if($id === null){
@@ -136,7 +142,6 @@ abstract class Mapper {
 
 		// get updated fields to save, fields have to be set using a setter to
 		// be saved
-		$properties = $entity->getUpdatedFields();
 		// dont update the id field
 		unset($properties['id']);
 
diff --git a/tests/lib/appframework/db/EntityTest.php b/tests/lib/appframework/db/EntityTest.php
index f674f43..9de44b9 100644
--- a/tests/lib/appframework/db/EntityTest.php
+++ b/tests/lib/appframework/db/EntityTest.php
@@ -42,8 +42,9 @@ class TestEntity extends Entity {
 	public $testId;
 	public $preName;
 
-	public function __construct(){
+	public function __construct($name=null){
 		$this->addType('testId', 'integer');		
+		$this->name = $name;
 	}
 };
 
@@ -211,4 +212,12 @@ class EntityTest extends \PHPUnit_Framework_TestCase {
 		$this->assertEquals('integer', gettype($entity->getId()));
 	}
 
+
+	public function testFieldsNotMarkedUpdatedIfNothingChanges() {
+		$entity = new TestEntity('hey');
+		$entity->setName('hey');
+		$this->assertEquals(0, count($entity->getUpdatedFields()));
+	}
+
+
 }
\ No newline at end of file
diff --git a/tests/lib/appframework/db/MapperTest.php b/tests/lib/appframework/db/MapperTest.php
index e21edbd..4ddc4ef 100644
--- a/tests/lib/appframework/db/MapperTest.php
+++ b/tests/lib/appframework/db/MapperTest.php
@@ -217,6 +217,20 @@ class MapperTest extends MapperTestUtility {
 	}
 
 
+	public function testUpdateNothingChangedNoQuery(){
+		$params = array('john', 'my at email');
+		$entity = new Example();
+		$entity->setId(3);
+		$entity->setEmail($params[1]);
+		$entity->resetUpdatedFields();
+
+		$this->db->expects($this->never())
+			->method('prepareQuery');
+
+		$this->mapper->update($entity);
+	}
+
+
 	public function testMapRowToEntity(){
 		$entity1 = $this->mapper->mapRow(array('pre_name' => 'test1', 'email' => 'test2'));
 		$entity2 = new Example();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list