Author: voxus
Date: 2006-06-26 04:15:19 +0400 (Mon, 26 Jun 2006)
New Revision: 1806
Added:
trunk/main/Containers/Storable/
trunk/main/Containers/Storable/DependentDAO.class.php
trunk/main/Containers/Storable/IndependentLinkDAO.class.php
trunk/main/Containers/Storable/PartDAO.class.php
trunk/main/Containers/Storable/StorableContainer.class.php
trunk/main/Containers/Unified/
trunk/main/Containers/Unified/ManyToManyLinked.class.php
trunk/main/Containers/Unified/ManyToManyLinkedFull.class.php
trunk/main/Containers/Unified/ManyToManyLinkedLazy.class.php
trunk/main/Containers/Unified/ManyToManyLinkedWorker.class.php
trunk/main/Containers/Unified/OneToManyLinked.class.php
trunk/main/Containers/Unified/OneToManyLinkedFull.class.php
trunk/main/Containers/Unified/OneToManyLinkedLazy.class.php
trunk/main/Containers/Unified/OneToManyLinkedWorker.class.php
trunk/main/Containers/Unified/UnifiedContainer.class.php
trunk/main/Containers/Unified/UnifiedContainerWorker.class.php
Removed:
trunk/main/Containers/DependentDAO.class.php
trunk/main/Containers/IndependentLinkDAO.class.php
trunk/main/Containers/ManyToManyLinked.class.php
trunk/main/Containers/ManyToManyLinkedFull.class.php
trunk/main/Containers/ManyToManyLinkedLazy.class.php
trunk/main/Containers/ManyToManyLinkedWorker.class.php
trunk/main/Containers/OneToManyLinked.class.php
trunk/main/Containers/OneToManyLinkedFull.class.php
trunk/main/Containers/OneToManyLinkedLazy.class.php
trunk/main/Containers/OneToManyLinkedWorker.class.php
trunk/main/Containers/PartDAO.class.php
trunk/main/Containers/StorableContainer.class.php
trunk/main/Containers/UnifiedContainer.class.php
trunk/main/Containers/UnifiedContainerWorker.class.php
Modified:
trunk/doc/ChangeLog
trunk/global.inc.php.tpl
Log:
* main/Containers/ separated
Modified: trunk/doc/ChangeLog
===================================================================
--- trunk/doc/ChangeLog 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/doc/ChangeLog 2006-06-26 00:15:19 UTC (rev 1806)
@@ -2,6 +2,9 @@
2006-06-25 Konstantin V. Arkhipov
+ * main/Containers/: sub-directories for Storable and Unified
+ containers introduced.
+
* core/Base/Timestamp.class.php: extended support of malformed dates,
normalization introduced for such cases.
Modified: trunk/global.inc.php.tpl
===================================================================
--- trunk/global.inc.php.tpl 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/global.inc.php.tpl 2006-06-26 00:15:19 UTC (rev 1806)
@@ -74,7 +74,10 @@
.ONPHP_MAIN_PATH.'DAOs' .PATH_SEPARATOR
.ONPHP_MAIN_PATH.'Flow' .PATH_SEPARATOR
.ONPHP_MAIN_PATH.'SPL' .PATH_SEPARATOR
- .ONPHP_MAIN_PATH.'Containers' .PATH_SEPARATOR
+
+ .ONPHP_MAIN_PATH.'Containers'.DIRECTORY_SEPARATOR.'Storable'.PATH_SEPARATOR
+ .ONPHP_MAIN_PATH.'Containers'.DIRECTORY_SEPARATOR.'Unified'.PATH_SEPARATOR
+
.ONPHP_MAIN_PATH.'Utils' .PATH_SEPARATOR
// uncomment this one, if your php compiled without spl extension:
Deleted: trunk/main/Containers/DependentDAO.class.php
===================================================================
--- trunk/main/Containers/DependentDAO.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/DependentDAO.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,141 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Anton E. Lebedevich *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- abstract class DependentDAO extends GenericDAO implements PartDAO
- {
- abstract public function getParentIdField();
-
- /**
- * We must do it, because we have collision:
- * method getById() defined in interface
- * and in abstract class
- **/
- public function getById($id, $expires = Cache::EXPIRES_MEDIUM)
- {
- return parent::getById($id, $expires);
- }
-
- public function add($parentId, Identifiable $child)
- {
- $this->checkType($child);
-
- return
- $this->import($parentId,
- $child->setId(
- DBFactory::getDefaultInstance()->
- obtainSequence(
- $this->getSequence()
- )
- )
- );
- }
-
- public function dropByBothId($parentId, $childId)
- {
- return parent::dropById($childId);
- }
-
- public function dropByParentId($parentId)
- {
- DBFactory::getDefaultInstance()->queryNull(
- OSQL::delete()->from($this->getTable())->where(
- Expression::eq($this->getParentIdField(), $parentId)
- )
- );
-
- return $this;
- }
-
- public function insert($parentId, Identifiable $child)
- {
- throw new UnsupportedMethodException(
- 'insert is not applicaple to dependent objects'
- );
- }
-
- public function import($parentId, Identifiable $child)
- {
- $this->checkType($child);
-
- DBFactory::getDefaultInstance()->queryNull(
- $this->setQueryFields(
- OSQL::insert()->into($this->getTable())->
- set('id', $child->getId())->
- set($this->getParentIdField(), $parentId),
- $child
- )
- );
-
- $this->uncacheById($child->getId());
-
- return $this;
- }
-
- public function save($parentId, Identifiable $child)
- {
- $this->checkType($child);
-
- DBFactory::getDefaultInstance()->queryNull(
- $this->setQueryFields(
- OSQL::update($this->getTable())->
- where(Expression::eq('id', $child->getId())),
- $child
- )
- );
-
- $this->uncacheById($child->getId());
-
- return $this;
- }
-
- public function getListByParentId(
- $parentId, $expires = Cache::DO_NOT_CACHE
- )
- {
- return
- $this->getListByLogic(
- Expression::eq($this->getParentIdField(), $parentId),
- $expires
- );
- }
-
- public function getChildIdsList(
- $parentId, $expires = Cache::DO_NOT_CACHE
- )
- {
- return
- $this->getCustomRowList(
- OSQL::select()->from($this->getTable())->
- get('id')->
- where(
- Expression::eq(
- new DBField($this->getParentIdField()),
- new DBValue($parentId)
- )
- ),
-
- $expires
- );
- }
-
- private function checkType(Identifiable $child)
- {
- Assert::isTrue(
- get_class($child) === $this->getObjectName(),
- "child type doesn't match DAO type"
- );
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/IndependentLinkDAO.class.php
===================================================================
--- trunk/main/Containers/IndependentLinkDAO.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/IndependentLinkDAO.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,163 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Anton E. Lebedevich *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- abstract class IndependentLinkDAO extends Singleton implements PartDAO
- {
- abstract public function getParentIdField();
- abstract public function getChildIdField();
- abstract public function getChildDAO();
- abstract public function getTable();
-
- abstract public function getObjectName();
-
- public function getById($id, $expires = Cache::EXPIRES_MEDIUM)
- {
- return $this->getChildDAO()->getById($id, $expires);
- }
-
- private function checkType(Identifiable $child)
- {
- Assert::isTrue(
- get_class($child) === $this->getObjectName(),
- "child type doesn't match DAO type"
- );
- }
-
- public function add($parentId, Identifiable $child)
- {
- throw new UnsupportedMethodException(
- "independent link can't use objects without id"
- );
- }
-
- public function dropByBothId($parentId, $childId)
- {
- try {
- DBFactory::getDefaultInstance()->queryNull(
- OSQL::delete()->from($this->getTable())->where(
- Expression::expAnd(
- Expression::eq(
- new DBField($this->getParentIdField()),
- new DBValue($parentId)
- ),
- Expression::eq(
- new DBField($this->getChildIdField()),
- new DBValue($childId)
- )
- )
- )
- );
- } catch (DatabaseException $e) {
- throw $e->setMessage(
- "can't delete {$this->getObjectName()} ".
- "with parentId == {$parentId} ".
- "and childId == {$childId}"
- );
- }
-
- return $this;
- }
-
- public function dropByParentId($parentId)
- {
- try {
- DBFactory::getDefaultInstance()->queryNull(
- OSQL::delete()->from($this->getTable())->where(
- Expression::eq($this->getParentIdField(), $parentId)
- )
- );
- } catch (DatabaseException $e) {
- throw $e->setMessage(
- "can't delete {$this->getObjectName()} with parentId == {$parentId}"
- );
- }
-
- return $this;
- }
-
- public function save($parentId, Identifiable $child)
- {
- // do not throw anything here - we're too often calling it,
- // while we don't need to save links at all
- return $this;
- }
-
- public function import($parentId, Identifiable $child)
- {
- $this->checkType($child);
-
- DBFactory::getDefaultInstance()->queryNull(
- OSQL::insert()->into($this->getTable())->
- set($this->getChildIdField(), $child->getId())->
- set($this->getParentIdField(), $parentId)
- );
-
- return $this;
- }
-
- public function insert($parentId, Identifiable $child)
- {
- return $this->import($parentId, $child);
- }
-
- public function getListByParentId($parentId, $expires = Cache::DO_NOT_CACHE)
- {
- return $this->getChildDAO()->getListByQuery(
- $this->getChildDAO()->
- makeSelectHead()->
- join(
- $this->getTable(),
- Expression::expAnd(
- Expression::eq(
- new DBField(
- $this->getChildIdField(),
- $this->getTable()
- ),
- new DBField(
- 'id',
- $this->getChildDAO()->getTable()
- )
- ),
- Expression::eq(
- new DBField(
- $this->getParentIdField(),
- $this->getTable()
- ),
- new DBValue($parentId)
- )
- )
- ),
- $expires
- );
- }
-
- public function getChildIdsList($parentId, $expires = Cache::DO_NOT_CACHE)
- {
- return
- $this->getChildDAO()->
- getCustomRowList(
- OSQL::select()->from($this->getTable())->
- get($this->getChildIdField())->
- where(
- Expression::eq(
- new DBField($this->getParentIdField()),
- new DBValue($parentId)
- )
- ),
- $expires
- );
- }
- }
-?>
Deleted: trunk/main/Containers/ManyToManyLinked.class.php
===================================================================
--- trunk/main/Containers/ManyToManyLinked.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/ManyToManyLinked.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,39 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- abstract class ManyToManyLinked extends UnifiedContainer
- {
- abstract public function getHelperTable();
-
- public function __construct(
- Identifiable $parent, GenericDAO $dao, $lazy = true
- )
- {
- parent::__construct($parent, $dao, $lazy);
-
- $worker =
- $lazy
- ? 'ManyToManyLinkedLazy'
- : 'ManyToManyLinkedFull';
-
- $this->worker = new $worker($this);
- }
-
- public function getParentTableIdField()
- {
- return 'id';
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/ManyToManyLinkedFull.class.php
===================================================================
--- trunk/main/Containers/ManyToManyLinkedFull.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/ManyToManyLinkedFull.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,90 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005-2006 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- final class ManyToManyLinkedFull extends ManyToManyLinkedWorker
- {
- public function sync(&$insert, &$update = array(), &$delete)
- {
- $db = DBFactory::getDefaultInstance();
-
- $dao = $this->container->getDao();
-
- if ($insert)
- for ($i = 0, $size = count($insert); $i < $size; ++$i) {
- // check existence of new object
- try {
- $dao->getById($insert[$i]->getId());
- } catch (ObjectNotFoundException $e) {
- // ok, saving it then
- $dao->add($insert[$i]);
- }
-
- $db->queryNull(
- $this->makeInsertQuery($insert[$i]->getId())
- );
- }
-
- if ($update)
- for ($i = 0, $size = count($update); $i < $size; ++$i)
- $dao->save($update[$i]);
-
- if ($delete) {
- $ids = array();
-
- foreach ($delete as $object)
- $ids[] = $object->getId();
-
- $db->queryNull($this->makeDeleteQuery($ids));
-
- $dao->uncacheById($ids);
- }
-
- return $this;
- }
-
- public function makeFetchQuery()
- {
- $uc = $this->container;
-
- if ($this->oq)
- $query = $this->oq->toSelectQuery($uc->getDao());
- else
- $query = $uc->getDao()->makeSelectHead();
-
- return
- $query->
- distinct()->
- join(
- $uc->getHelperTable(),
- Expression::eq(
- new DBField(
- $uc->getParentTableIdField(),
- $uc->getDao()->getTable()
- ),
- new DBField(
- $uc->getChildIdField(),
- $uc->getHelperTable()
- )
- )
- )->
- where(
- Expression::eq(
- new DBField($uc->getParentIdField()),
- new DBValue($uc->getParentObject()->getId())
- )
- );
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/ManyToManyLinkedLazy.class.php
===================================================================
--- trunk/main/Containers/ManyToManyLinkedLazy.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/ManyToManyLinkedLazy.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,61 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005-2006 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- final class ManyToManyLinkedLazy extends ManyToManyLinkedWorker
- {
- public function sync(&$insert, &$update = array(), &$delete)
- {
- Assert::isTrue($update === array());
-
- $db = DBFactory::getDefaultInstance();
-
- $dao = $this->container->getDao();
-
- if ($insert)
- for ($i = 0, $size = count($insert); $i < $size; ++$i)
- $db->queryNull($this->makeInsertQuery($insert[$i]));
-
- if ($delete) {
- $db->queryNull($this->makeDeleteQuery($delete));
-
- foreach ($delete as $id)
- $dao->uncacheById($id);
- }
-
- return $this;
- }
-
- public function makeFetchQuery()
- {
- $uc = $this->container;
-
- if ($this->oq)
- $query = $this->oq->toSelectQuery($uc->getDao())->dropFields();
- else
- $query = OSQL::select()->from($uc->getHelperTable());
-
- return
- $query->
- get($uc->getChildIdField())->
- distinct()->
- where(
- Expression::eq(
- new DBField($uc->getParentIdField()),
- new DBValue($uc->getParentObject()->getId())
- )
- );
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/ManyToManyLinkedWorker.class.php
===================================================================
--- trunk/main/Containers/ManyToManyLinkedWorker.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/ManyToManyLinkedWorker.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,52 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- abstract class ManyToManyLinkedWorker extends UnifiedContainerWorker
- {
- protected function makeInsertQuery($childId)
- {
- $uc = $this->container;
-
- return
- OSQL::insert()->into($uc->getHelperTable())->
- set(
- $uc->getParentIdField(),
- $uc->getParentObject()->getId()
- )->
- set($uc->getChildIdField(), $childId);
- }
-
- // only unlinking, we don't want to drop original object
- protected function makeDeleteQuery(&$delete)
- {
- $uc = $this->container;
-
- return
- OSQL::delete()->from($uc->getHelperTable())->
- where(
- Expression::eq(
- new DBField($uc->getParentIdField()),
- new DBValue($uc->getParentObject()->getId())
- )
- )->
- andWhere(
- Expression::in(
- $uc->getChildIdField(),
- $delete
- )
- );
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/OneToManyLinked.class.php
===================================================================
--- trunk/main/Containers/OneToManyLinked.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/OneToManyLinked.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,52 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- abstract class OneToManyLinked extends UnifiedContainer
- {
- public function __construct(
- Identifiable $parent, GenericDAO $dao, $lazy = true
- )
- {
- parent::__construct($parent, $dao, $lazy);
-
- $worker =
- $lazy
- ? 'OneToManyLinkedLazy'
- : 'OneToManyLinkedFull';
-
- $this->worker = new $worker($this);
- }
-
- public function getParentIdField()
- {
- static $name = null;
-
- if ($name === null)
- $name = get_class($this->parent).'_id';
-
- return $name;
- }
-
- public function getChildIdField()
- {
- return 'id';
- }
-
- public function isUnlinkable()
- {
- return false;
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/OneToManyLinkedFull.class.php
===================================================================
--- trunk/main/Containers/OneToManyLinkedFull.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/OneToManyLinkedFull.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,67 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005-2006 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- final class OneToManyLinkedFull extends OneToManyLinkedWorker
- {
- public function makeFetchQuery()
- {
- $uc = $this->container;
-
- return
- $this->targetize(
- $this->oq
- ? $this->oq->toSelectQuery($uc->getDao())
- : $uc->getDao()->makeSelectHead()
- );
- }
-
- public function sync(&$insert, &$update = array(), &$delete)
- {
- $uc = $this->container;
- $dao = $uc->getDao();
-
- if ($insert)
- for ($i = 0, $size = count($insert); $i < $size; ++$i)
- $dao->add($insert[$i]);
-
- if ($update)
- for ($i = 0, $size = count($update); $i < $size; ++$i)
- $dao->save($update[$i]);
-
- if ($delete) {
- DBFactory::getDefaultInstance()->queryNull(
- OSQL::delete()->from($dao->getTable())->
- where(
- Expression::eq(
- new DBField($uc->getParentIdField()),
- $uc->getParentObject()->getId()
- )
- )->
- andWhere(
- Expression::in(
- $uc->getChildIdField(),
- ArrayUtils::getIdsArray($delete)
- )
- )
- );
-
- foreach ($delete as $object)
- $dao->uncacheById($object->getId());
- }
-
- return $this;
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/OneToManyLinkedLazy.class.php
===================================================================
--- trunk/main/Containers/OneToManyLinkedLazy.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/OneToManyLinkedLazy.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,85 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- final class OneToManyLinkedLazy extends OneToManyLinkedWorker
- {
- public function makeFetchQuery()
- {
- $uc = $this->container;
-
- return
- $this->targetize(
- $this->oq
- ?
- $this->oq->toSelectQuery($uc->getDao())->
- dropFields()->
- get($uc->getChildIdField())
- :
- OSQL::select()->from($uc->getDao()->getTable())->
- get($uc->getChildIdField())
- );
- }
-
- public function sync(&$insert, &$update = array(), &$delete)
- {
- Assert::isTrue($update === array());
-
- $db = &DBFactory::getDefaultInstance();
-
- $uc = $this->container;
- $dao = $uc->getDao();
-
- if ($insert)
- $db->queryNull($this->makeMassUpdateQuery($insert));
-
- if ($delete) {
- // unlink or drop
- $uc->isUnlinkable()
- ?
- $db->queryNull($this->makeMassUpdateQuery($delete))
- :
- $db->queryNull(
- OSQL::delete()->from($dao->getTable())->
- where(
- Expression::in(
- $uc->getChildIdField(),
- $delete
- )
- )
- );
-
- foreach ($delete as $id)
- $dao->uncacheById($id);
- }
-
- return $this;
- }
-
- private function makeMassUpdateQuery(&$ids)
- {
- $uc = $this->container;
-
- return
- OSQL::update($uc->getDao()->getTable())->
- set($uc->getParentIdField(), null)->
- where(
- Expression::in(
- $uc->getChildIdField(),
- $ids
- )
- );
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/OneToManyLinkedWorker.class.php
===================================================================
--- trunk/main/Containers/OneToManyLinkedWorker.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/OneToManyLinkedWorker.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,29 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- abstract class OneToManyLinkedWorker extends UnifiedContainerWorker
- {
- protected function targetize(SelectQuery $query)
- {
- return
- $query->where(
- Expression::eqId(
- new DBField($this->container->getParentIdField()),
- $this->container->getParentObject()
- )
- );
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/PartDAO.class.php
===================================================================
--- trunk/main/Containers/PartDAO.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/PartDAO.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,34 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Anton E. Lebedevich *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @ingroup Containers
- **/
- interface PartDAO
- {
- public function dropByParentId($parentId);
- public function dropByBothId($parentId, $childId);
-
- public function add($parentId, Identifiable $child);
- public function save($parentId, Identifiable $child);
- public function import($parentId, Identifiable $child);
- public function insert($parentId, Identifiable $child);
-
- public function getById($id, $expires = Cache::EXPIRES_MEDIUM);
- public function getListByParentId(
- $parentId, $expires = Cache::DO_NOT_CACHE
- );
- public function getChildIdsList(
- $parentId, $expires = Cache::DO_NOT_CACHE
- );
- }
-?>
\ No newline at end of file
Copied: trunk/main/Containers/Storable/DependentDAO.class.php (from rev 1805, trunk/main/Containers/DependentDAO.class.php)
===================================================================
--- trunk/main/Containers/Storable/DependentDAO.class.php (rev 0)
+++ trunk/main/Containers/Storable/DependentDAO.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,141 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Anton E. Lebedevich *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ abstract class DependentDAO extends GenericDAO implements PartDAO
+ {
+ abstract public function getParentIdField();
+
+ /**
+ * We must do it, because we have collision:
+ * method getById() defined in interface
+ * and in abstract class
+ **/
+ public function getById($id, $expires = Cache::EXPIRES_MEDIUM)
+ {
+ return parent::getById($id, $expires);
+ }
+
+ public function add($parentId, Identifiable $child)
+ {
+ $this->checkType($child);
+
+ return
+ $this->import($parentId,
+ $child->setId(
+ DBFactory::getDefaultInstance()->
+ obtainSequence(
+ $this->getSequence()
+ )
+ )
+ );
+ }
+
+ public function dropByBothId($parentId, $childId)
+ {
+ return parent::dropById($childId);
+ }
+
+ public function dropByParentId($parentId)
+ {
+ DBFactory::getDefaultInstance()->queryNull(
+ OSQL::delete()->from($this->getTable())->where(
+ Expression::eq($this->getParentIdField(), $parentId)
+ )
+ );
+
+ return $this;
+ }
+
+ public function insert($parentId, Identifiable $child)
+ {
+ throw new UnsupportedMethodException(
+ 'insert is not applicaple to dependent objects'
+ );
+ }
+
+ public function import($parentId, Identifiable $child)
+ {
+ $this->checkType($child);
+
+ DBFactory::getDefaultInstance()->queryNull(
+ $this->setQueryFields(
+ OSQL::insert()->into($this->getTable())->
+ set('id', $child->getId())->
+ set($this->getParentIdField(), $parentId),
+ $child
+ )
+ );
+
+ $this->uncacheById($child->getId());
+
+ return $this;
+ }
+
+ public function save($parentId, Identifiable $child)
+ {
+ $this->checkType($child);
+
+ DBFactory::getDefaultInstance()->queryNull(
+ $this->setQueryFields(
+ OSQL::update($this->getTable())->
+ where(Expression::eq('id', $child->getId())),
+ $child
+ )
+ );
+
+ $this->uncacheById($child->getId());
+
+ return $this;
+ }
+
+ public function getListByParentId(
+ $parentId, $expires = Cache::DO_NOT_CACHE
+ )
+ {
+ return
+ $this->getListByLogic(
+ Expression::eq($this->getParentIdField(), $parentId),
+ $expires
+ );
+ }
+
+ public function getChildIdsList(
+ $parentId, $expires = Cache::DO_NOT_CACHE
+ )
+ {
+ return
+ $this->getCustomRowList(
+ OSQL::select()->from($this->getTable())->
+ get('id')->
+ where(
+ Expression::eq(
+ new DBField($this->getParentIdField()),
+ new DBValue($parentId)
+ )
+ ),
+
+ $expires
+ );
+ }
+
+ private function checkType(Identifiable $child)
+ {
+ Assert::isTrue(
+ get_class($child) === $this->getObjectName(),
+ "child type doesn't match DAO type"
+ );
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Storable/IndependentLinkDAO.class.php (from rev 1805, trunk/main/Containers/IndependentLinkDAO.class.php)
===================================================================
--- trunk/main/Containers/Storable/IndependentLinkDAO.class.php (rev 0)
+++ trunk/main/Containers/Storable/IndependentLinkDAO.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,163 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Anton E. Lebedevich *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ abstract class IndependentLinkDAO extends Singleton implements PartDAO
+ {
+ abstract public function getParentIdField();
+ abstract public function getChildIdField();
+ abstract public function getChildDAO();
+ abstract public function getTable();
+
+ abstract public function getObjectName();
+
+ public function getById($id, $expires = Cache::EXPIRES_MEDIUM)
+ {
+ return $this->getChildDAO()->getById($id, $expires);
+ }
+
+ private function checkType(Identifiable $child)
+ {
+ Assert::isTrue(
+ get_class($child) === $this->getObjectName(),
+ "child type doesn't match DAO type"
+ );
+ }
+
+ public function add($parentId, Identifiable $child)
+ {
+ throw new UnsupportedMethodException(
+ "independent link can't use objects without id"
+ );
+ }
+
+ public function dropByBothId($parentId, $childId)
+ {
+ try {
+ DBFactory::getDefaultInstance()->queryNull(
+ OSQL::delete()->from($this->getTable())->where(
+ Expression::expAnd(
+ Expression::eq(
+ new DBField($this->getParentIdField()),
+ new DBValue($parentId)
+ ),
+ Expression::eq(
+ new DBField($this->getChildIdField()),
+ new DBValue($childId)
+ )
+ )
+ )
+ );
+ } catch (DatabaseException $e) {
+ throw $e->setMessage(
+ "can't delete {$this->getObjectName()} ".
+ "with parentId == {$parentId} ".
+ "and childId == {$childId}"
+ );
+ }
+
+ return $this;
+ }
+
+ public function dropByParentId($parentId)
+ {
+ try {
+ DBFactory::getDefaultInstance()->queryNull(
+ OSQL::delete()->from($this->getTable())->where(
+ Expression::eq($this->getParentIdField(), $parentId)
+ )
+ );
+ } catch (DatabaseException $e) {
+ throw $e->setMessage(
+ "can't delete {$this->getObjectName()} with parentId == {$parentId}"
+ );
+ }
+
+ return $this;
+ }
+
+ public function save($parentId, Identifiable $child)
+ {
+ // do not throw anything here - we're too often calling it,
+ // while we don't need to save links at all
+ return $this;
+ }
+
+ public function import($parentId, Identifiable $child)
+ {
+ $this->checkType($child);
+
+ DBFactory::getDefaultInstance()->queryNull(
+ OSQL::insert()->into($this->getTable())->
+ set($this->getChildIdField(), $child->getId())->
+ set($this->getParentIdField(), $parentId)
+ );
+
+ return $this;
+ }
+
+ public function insert($parentId, Identifiable $child)
+ {
+ return $this->import($parentId, $child);
+ }
+
+ public function getListByParentId($parentId, $expires = Cache::DO_NOT_CACHE)
+ {
+ return $this->getChildDAO()->getListByQuery(
+ $this->getChildDAO()->
+ makeSelectHead()->
+ join(
+ $this->getTable(),
+ Expression::expAnd(
+ Expression::eq(
+ new DBField(
+ $this->getChildIdField(),
+ $this->getTable()
+ ),
+ new DBField(
+ 'id',
+ $this->getChildDAO()->getTable()
+ )
+ ),
+ Expression::eq(
+ new DBField(
+ $this->getParentIdField(),
+ $this->getTable()
+ ),
+ new DBValue($parentId)
+ )
+ )
+ ),
+ $expires
+ );
+ }
+
+ public function getChildIdsList($parentId, $expires = Cache::DO_NOT_CACHE)
+ {
+ return
+ $this->getChildDAO()->
+ getCustomRowList(
+ OSQL::select()->from($this->getTable())->
+ get($this->getChildIdField())->
+ where(
+ Expression::eq(
+ new DBField($this->getParentIdField()),
+ new DBValue($parentId)
+ )
+ ),
+ $expires
+ );
+ }
+ }
+?>
Copied: trunk/main/Containers/Storable/PartDAO.class.php (from rev 1805, trunk/main/Containers/PartDAO.class.php)
===================================================================
--- trunk/main/Containers/Storable/PartDAO.class.php (rev 0)
+++ trunk/main/Containers/Storable/PartDAO.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,34 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Anton E. Lebedevich *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ interface PartDAO
+ {
+ public function dropByParentId($parentId);
+ public function dropByBothId($parentId, $childId);
+
+ public function add($parentId, Identifiable $child);
+ public function save($parentId, Identifiable $child);
+ public function import($parentId, Identifiable $child);
+ public function insert($parentId, Identifiable $child);
+
+ public function getById($id, $expires = Cache::EXPIRES_MEDIUM);
+ public function getListByParentId(
+ $parentId, $expires = Cache::DO_NOT_CACHE
+ );
+ public function getChildIdsList(
+ $parentId, $expires = Cache::DO_NOT_CACHE
+ );
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Storable/StorableContainer.class.php (from rev 1805, trunk/main/Containers/StorableContainer.class.php)
===================================================================
--- trunk/main/Containers/Storable/StorableContainer.class.php (rev 0)
+++ trunk/main/Containers/Storable/StorableContainer.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,272 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Anton E. Lebedevich *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @see UnifiedContainer for alternative
+ *
+ * @ingroup Containers
+ **/
+ class StorableContainer
+ {
+ // lazy loading state
+ const LOAD_IDS = 1;
+ const LOAD_OBJECTS = 2;
+ const LOADED = 3;
+
+ private $loaded = null;
+ private $parentId = null;
+
+ private $new = array(); // new without id
+ private $insert = array(); // new with id, independent links
+ private $saved = array(); // in database
+ private $deleted = array();
+
+ private $partDAO = null;
+ private $expires = null;
+
+ public static function create(
+ PartDAO $partDAO, $expires = Cache::EXPIRES_MEDIUM
+ )
+ {
+ return new StorableContainer($partDAO, $expires);
+ }
+
+ public function __construct(
+ PartDAO $partDAO, $expires = Cache::EXPIRES_MEDIUM
+ )
+ {
+ $this->partDAO = $partDAO;
+ $this->expires = $expires;
+
+ $this->loaded = self::LOADED;
+ }
+
+ public function getPartDAO()
+ {
+ return $this->partDAO;
+ }
+
+ public function add(Identifiable $object)
+ {
+ $this->checkLoaded();
+ if (!$object->getId())
+ $this->new[] = $object;
+ else
+ try {
+ $this->update($object);
+ } catch (WrongStateException $e) {
+ $this->insert[$object->getId()] = $object;
+ }
+
+ return $this;
+ }
+
+ public function update(Identifiable $object)
+ {
+ $this->checkLoaded();
+ if (($id = $object->getId()) && isset($this->saved[$id]))
+ $this->saved[$id] = $object;
+ else
+ throw new WrongStateException(
+ 'you should use add for new objects'
+ );
+
+ return $this;
+ }
+
+ public function getById($id)
+ {
+ $this->checkLoaded();
+ if (!isset($this->saved[$id]))
+ throw new WrongArgumentException(
+ 'id not found in saved objects'
+ );
+
+ if (!is_object($this->saved[$id]))
+ $this->saved[$id] = $this->partDAO->getById($id, $this->expires);
+
+ return $this->saved[$id];
+ }
+
+ public function dropById($id)
+ {
+ $this->checkLoaded();
+ if (!isset($this->saved[$id]))
+ throw new WrongArgumentException(
+ 'id not found in saved objects'
+ );
+
+ $this->deleted[$id] = $this->saved[$id];
+ unset($this->saved[$id]);
+
+ return $this;
+ }
+
+ public function getList()
+ {
+ $this->checkLoaded();
+
+ // populate saved
+ foreach ($this->saved as $id => &$val) {
+ $this->getById($id);
+ }
+
+ return $this->new + $this->saved + $this->insert;
+ }
+
+ public function getIdsList()
+ {
+ $this->checkLoaded();
+
+ if (count($this->new))
+ throw WrongStateException(
+ 'can not enumerate objects without id, save them first'
+ );
+
+ return array_keys($this->saved) + array_keys($this->insert);
+ }
+
+ public function getCount()
+ {
+ $this->checkLoaded();
+
+ return count($this->saved) + count($this->insert);
+ }
+
+ // DAO part
+ public function save($parentId)
+ {
+ if ($this->loaded != self::LOADED)
+ return $this;
+
+ foreach ($this->deleted as $id => &$deleted)
+ $this->partDAO->dropByBothId($parentId, $id);
+
+ $this->deleted = array();
+
+ foreach ($this->saved as &$object) {
+ if (is_object($object)) // TODO: compare with clone
+ $this->partDAO->save($parentId, $object);
+ }
+
+ $this->processAdd($parentId);
+
+ foreach ($this->insert as &$insert) {
+ $this->partDAO->insert($parentId, $insert);
+ $this->addSaved($insert);
+ }
+
+ $this->insert = array();
+
+ return $this;
+ }
+
+ public function import($parentId)
+ {
+ if ($this->loaded != self::LOADED)
+ throw WrongStateException(
+ 'can not import such container'
+ );
+
+ foreach ($this->saved as &$object)
+ $this->partDAO->import($parentId, $object);
+
+ $this->processAdd($parentId);
+
+ foreach ($this->insert as &$insert) {
+ $this->partDAO->import($parentId, $insert);
+ $this->addSaved($insert);
+ }
+
+ $this->insert = array();
+
+ return $this;
+ }
+
+ public static function getByParentId(
+ $parentId, PartDAO $childDAO, $expires = Cache::EXPIRES_MEDIUM
+ )
+ {
+ $container = new StorableContainer($childDAO, $expires);
+ $container->loaded = self::LOAD_OBJECTS;
+ $container->parentId = $parentId;
+
+ return $container;
+ }
+
+ public static function getIdsByParentId(
+ $parentId, PartDAO $childDAO, $expires = Cache::EXPIRES_MEDIUM
+ )
+ {
+ $container = new StorableContainer($childDAO, $expires);
+ $container->loaded = self::LOAD_IDS;
+ $container->parentId = $parentId;
+
+ return $container;
+ }
+
+ public static function dropByParentId($parentId, PartDAO $childDAO)
+ {
+ return $childDAO->dropByParentId($parentId);
+ }
+
+ private function checkLoaded()
+ {
+ if ($this->loaded == self::LOAD_IDS && $this->parentId) {
+
+ try {
+ $list = $this->partDAO->getChildIdsList($this->parentId);
+
+ foreach ($list as $id)
+ $this->saved[$id] = $id;
+
+ } catch (ObjectNotFoundException $e) {/* we don't care */}
+
+ } elseif ($this->loaded == self::LOAD_OBJECTS && $this->parentId) {
+
+ try {
+ $list = $this->partDAO->getListByParentId($this->parentId);
+ $this->saved = ArrayUtils::convertObjectList($list);
+
+ } catch (ObjectNotFoundException $e) {/* we don't care */}
+
+ } elseif ($this->loaded != self::LOADED)
+ throw new WrongStateException('container in a bad state');
+
+ $this->loaded = self::LOADED;
+
+ return $this;
+ }
+
+ private function processAdd($parentId)
+ {
+ foreach ($this->new as &$new) {
+ $this->partDAO->add($parentId, $new);
+ $this->addSaved($new);
+ }
+
+ $this->new = array();
+
+ return $this;
+ }
+
+ private function addSaved(Identifiable $object)
+ {
+ if ($object !== null && $object->getId())
+ $this->saved[$object->getId()] = $object;
+ else
+ throw new WrongArgumentException('saved object must have an id');
+
+ return $this;
+ }
+ }
+?>
Deleted: trunk/main/Containers/StorableContainer.class.php
===================================================================
--- trunk/main/Containers/StorableContainer.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/StorableContainer.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,272 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Anton E. Lebedevich *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @see UnifiedContainer for alternative
- *
- * @ingroup Containers
- **/
- class StorableContainer
- {
- // lazy loading state
- const LOAD_IDS = 1;
- const LOAD_OBJECTS = 2;
- const LOADED = 3;
-
- private $loaded = null;
- private $parentId = null;
-
- private $new = array(); // new without id
- private $insert = array(); // new with id, independent links
- private $saved = array(); // in database
- private $deleted = array();
-
- private $partDAO = null;
- private $expires = null;
-
- public static function create(
- PartDAO $partDAO, $expires = Cache::EXPIRES_MEDIUM
- )
- {
- return new StorableContainer($partDAO, $expires);
- }
-
- public function __construct(
- PartDAO $partDAO, $expires = Cache::EXPIRES_MEDIUM
- )
- {
- $this->partDAO = $partDAO;
- $this->expires = $expires;
-
- $this->loaded = self::LOADED;
- }
-
- public function getPartDAO()
- {
- return $this->partDAO;
- }
-
- public function add(Identifiable $object)
- {
- $this->checkLoaded();
- if (!$object->getId())
- $this->new[] = $object;
- else
- try {
- $this->update($object);
- } catch (WrongStateException $e) {
- $this->insert[$object->getId()] = $object;
- }
-
- return $this;
- }
-
- public function update(Identifiable $object)
- {
- $this->checkLoaded();
- if (($id = $object->getId()) && isset($this->saved[$id]))
- $this->saved[$id] = $object;
- else
- throw new WrongStateException(
- 'you should use add for new objects'
- );
-
- return $this;
- }
-
- public function getById($id)
- {
- $this->checkLoaded();
- if (!isset($this->saved[$id]))
- throw new WrongArgumentException(
- 'id not found in saved objects'
- );
-
- if (!is_object($this->saved[$id]))
- $this->saved[$id] = $this->partDAO->getById($id, $this->expires);
-
- return $this->saved[$id];
- }
-
- public function dropById($id)
- {
- $this->checkLoaded();
- if (!isset($this->saved[$id]))
- throw new WrongArgumentException(
- 'id not found in saved objects'
- );
-
- $this->deleted[$id] = $this->saved[$id];
- unset($this->saved[$id]);
-
- return $this;
- }
-
- public function getList()
- {
- $this->checkLoaded();
-
- // populate saved
- foreach ($this->saved as $id => &$val) {
- $this->getById($id);
- }
-
- return $this->new + $this->saved + $this->insert;
- }
-
- public function getIdsList()
- {
- $this->checkLoaded();
-
- if (count($this->new))
- throw WrongStateException(
- 'can not enumerate objects without id, save them first'
- );
-
- return array_keys($this->saved) + array_keys($this->insert);
- }
-
- public function getCount()
- {
- $this->checkLoaded();
-
- return count($this->saved) + count($this->insert);
- }
-
- // DAO part
- public function save($parentId)
- {
- if ($this->loaded != self::LOADED)
- return $this;
-
- foreach ($this->deleted as $id => &$deleted)
- $this->partDAO->dropByBothId($parentId, $id);
-
- $this->deleted = array();
-
- foreach ($this->saved as &$object) {
- if (is_object($object)) // TODO: compare with clone
- $this->partDAO->save($parentId, $object);
- }
-
- $this->processAdd($parentId);
-
- foreach ($this->insert as &$insert) {
- $this->partDAO->insert($parentId, $insert);
- $this->addSaved($insert);
- }
-
- $this->insert = array();
-
- return $this;
- }
-
- public function import($parentId)
- {
- if ($this->loaded != self::LOADED)
- throw WrongStateException(
- 'can not import such container'
- );
-
- foreach ($this->saved as &$object)
- $this->partDAO->import($parentId, $object);
-
- $this->processAdd($parentId);
-
- foreach ($this->insert as &$insert) {
- $this->partDAO->import($parentId, $insert);
- $this->addSaved($insert);
- }
-
- $this->insert = array();
-
- return $this;
- }
-
- public static function getByParentId(
- $parentId, PartDAO $childDAO, $expires = Cache::EXPIRES_MEDIUM
- )
- {
- $container = new StorableContainer($childDAO, $expires);
- $container->loaded = self::LOAD_OBJECTS;
- $container->parentId = $parentId;
-
- return $container;
- }
-
- public static function getIdsByParentId(
- $parentId, PartDAO $childDAO, $expires = Cache::EXPIRES_MEDIUM
- )
- {
- $container = new StorableContainer($childDAO, $expires);
- $container->loaded = self::LOAD_IDS;
- $container->parentId = $parentId;
-
- return $container;
- }
-
- public static function dropByParentId($parentId, PartDAO $childDAO)
- {
- return $childDAO->dropByParentId($parentId);
- }
-
- private function checkLoaded()
- {
- if ($this->loaded == self::LOAD_IDS && $this->parentId) {
-
- try {
- $list = $this->partDAO->getChildIdsList($this->parentId);
-
- foreach ($list as $id)
- $this->saved[$id] = $id;
-
- } catch (ObjectNotFoundException $e) {/* we don't care */}
-
- } elseif ($this->loaded == self::LOAD_OBJECTS && $this->parentId) {
-
- try {
- $list = $this->partDAO->getListByParentId($this->parentId);
- $this->saved = ArrayUtils::convertObjectList($list);
-
- } catch (ObjectNotFoundException $e) {/* we don't care */}
-
- } elseif ($this->loaded != self::LOADED)
- throw new WrongStateException('container in a bad state');
-
- $this->loaded = self::LOADED;
-
- return $this;
- }
-
- private function processAdd($parentId)
- {
- foreach ($this->new as &$new) {
- $this->partDAO->add($parentId, $new);
- $this->addSaved($new);
- }
-
- $this->new = array();
-
- return $this;
- }
-
- private function addSaved(Identifiable $object)
- {
- if ($object !== null && $object->getId())
- $this->saved[$object->getId()] = $object;
- else
- throw new WrongArgumentException('saved object must have an id');
-
- return $this;
- }
- }
-?>
Copied: trunk/main/Containers/Unified/ManyToManyLinked.class.php (from rev 1805, trunk/main/Containers/ManyToManyLinked.class.php)
===================================================================
--- trunk/main/Containers/Unified/ManyToManyLinked.class.php (rev 0)
+++ trunk/main/Containers/Unified/ManyToManyLinked.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,39 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ abstract class ManyToManyLinked extends UnifiedContainer
+ {
+ abstract public function getHelperTable();
+
+ public function __construct(
+ Identifiable $parent, GenericDAO $dao, $lazy = true
+ )
+ {
+ parent::__construct($parent, $dao, $lazy);
+
+ $worker =
+ $lazy
+ ? 'ManyToManyLinkedLazy'
+ : 'ManyToManyLinkedFull';
+
+ $this->worker = new $worker($this);
+ }
+
+ public function getParentTableIdField()
+ {
+ return 'id';
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Unified/ManyToManyLinkedFull.class.php (from rev 1805, trunk/main/Containers/ManyToManyLinkedFull.class.php)
===================================================================
--- trunk/main/Containers/Unified/ManyToManyLinkedFull.class.php (rev 0)
+++ trunk/main/Containers/Unified/ManyToManyLinkedFull.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,90 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005-2006 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ final class ManyToManyLinkedFull extends ManyToManyLinkedWorker
+ {
+ public function sync(&$insert, &$update = array(), &$delete)
+ {
+ $db = DBFactory::getDefaultInstance();
+
+ $dao = $this->container->getDao();
+
+ if ($insert)
+ for ($i = 0, $size = count($insert); $i < $size; ++$i) {
+ // check existence of new object
+ try {
+ $dao->getById($insert[$i]->getId());
+ } catch (ObjectNotFoundException $e) {
+ // ok, saving it then
+ $dao->add($insert[$i]);
+ }
+
+ $db->queryNull(
+ $this->makeInsertQuery($insert[$i]->getId())
+ );
+ }
+
+ if ($update)
+ for ($i = 0, $size = count($update); $i < $size; ++$i)
+ $dao->save($update[$i]);
+
+ if ($delete) {
+ $ids = array();
+
+ foreach ($delete as $object)
+ $ids[] = $object->getId();
+
+ $db->queryNull($this->makeDeleteQuery($ids));
+
+ $dao->uncacheById($ids);
+ }
+
+ return $this;
+ }
+
+ public function makeFetchQuery()
+ {
+ $uc = $this->container;
+
+ if ($this->oq)
+ $query = $this->oq->toSelectQuery($uc->getDao());
+ else
+ $query = $uc->getDao()->makeSelectHead();
+
+ return
+ $query->
+ distinct()->
+ join(
+ $uc->getHelperTable(),
+ Expression::eq(
+ new DBField(
+ $uc->getParentTableIdField(),
+ $uc->getDao()->getTable()
+ ),
+ new DBField(
+ $uc->getChildIdField(),
+ $uc->getHelperTable()
+ )
+ )
+ )->
+ where(
+ Expression::eq(
+ new DBField($uc->getParentIdField()),
+ new DBValue($uc->getParentObject()->getId())
+ )
+ );
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Unified/ManyToManyLinkedLazy.class.php (from rev 1805, trunk/main/Containers/ManyToManyLinkedLazy.class.php)
===================================================================
--- trunk/main/Containers/Unified/ManyToManyLinkedLazy.class.php (rev 0)
+++ trunk/main/Containers/Unified/ManyToManyLinkedLazy.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,61 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005-2006 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ final class ManyToManyLinkedLazy extends ManyToManyLinkedWorker
+ {
+ public function sync(&$insert, &$update = array(), &$delete)
+ {
+ Assert::isTrue($update === array());
+
+ $db = DBFactory::getDefaultInstance();
+
+ $dao = $this->container->getDao();
+
+ if ($insert)
+ for ($i = 0, $size = count($insert); $i < $size; ++$i)
+ $db->queryNull($this->makeInsertQuery($insert[$i]));
+
+ if ($delete) {
+ $db->queryNull($this->makeDeleteQuery($delete));
+
+ foreach ($delete as $id)
+ $dao->uncacheById($id);
+ }
+
+ return $this;
+ }
+
+ public function makeFetchQuery()
+ {
+ $uc = $this->container;
+
+ if ($this->oq)
+ $query = $this->oq->toSelectQuery($uc->getDao())->dropFields();
+ else
+ $query = OSQL::select()->from($uc->getHelperTable());
+
+ return
+ $query->
+ get($uc->getChildIdField())->
+ distinct()->
+ where(
+ Expression::eq(
+ new DBField($uc->getParentIdField()),
+ new DBValue($uc->getParentObject()->getId())
+ )
+ );
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Unified/ManyToManyLinkedWorker.class.php (from rev 1805, trunk/main/Containers/ManyToManyLinkedWorker.class.php)
===================================================================
--- trunk/main/Containers/Unified/ManyToManyLinkedWorker.class.php (rev 0)
+++ trunk/main/Containers/Unified/ManyToManyLinkedWorker.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,52 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ abstract class ManyToManyLinkedWorker extends UnifiedContainerWorker
+ {
+ protected function makeInsertQuery($childId)
+ {
+ $uc = $this->container;
+
+ return
+ OSQL::insert()->into($uc->getHelperTable())->
+ set(
+ $uc->getParentIdField(),
+ $uc->getParentObject()->getId()
+ )->
+ set($uc->getChildIdField(), $childId);
+ }
+
+ // only unlinking, we don't want to drop original object
+ protected function makeDeleteQuery(&$delete)
+ {
+ $uc = $this->container;
+
+ return
+ OSQL::delete()->from($uc->getHelperTable())->
+ where(
+ Expression::eq(
+ new DBField($uc->getParentIdField()),
+ new DBValue($uc->getParentObject()->getId())
+ )
+ )->
+ andWhere(
+ Expression::in(
+ $uc->getChildIdField(),
+ $delete
+ )
+ );
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Unified/OneToManyLinked.class.php (from rev 1805, trunk/main/Containers/OneToManyLinked.class.php)
===================================================================
--- trunk/main/Containers/Unified/OneToManyLinked.class.php (rev 0)
+++ trunk/main/Containers/Unified/OneToManyLinked.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,52 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ abstract class OneToManyLinked extends UnifiedContainer
+ {
+ public function __construct(
+ Identifiable $parent, GenericDAO $dao, $lazy = true
+ )
+ {
+ parent::__construct($parent, $dao, $lazy);
+
+ $worker =
+ $lazy
+ ? 'OneToManyLinkedLazy'
+ : 'OneToManyLinkedFull';
+
+ $this->worker = new $worker($this);
+ }
+
+ public function getParentIdField()
+ {
+ static $name = null;
+
+ if ($name === null)
+ $name = get_class($this->parent).'_id';
+
+ return $name;
+ }
+
+ public function getChildIdField()
+ {
+ return 'id';
+ }
+
+ public function isUnlinkable()
+ {
+ return false;
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Unified/OneToManyLinkedFull.class.php (from rev 1805, trunk/main/Containers/OneToManyLinkedFull.class.php)
===================================================================
--- trunk/main/Containers/Unified/OneToManyLinkedFull.class.php (rev 0)
+++ trunk/main/Containers/Unified/OneToManyLinkedFull.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,67 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005-2006 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ final class OneToManyLinkedFull extends OneToManyLinkedWorker
+ {
+ public function makeFetchQuery()
+ {
+ $uc = $this->container;
+
+ return
+ $this->targetize(
+ $this->oq
+ ? $this->oq->toSelectQuery($uc->getDao())
+ : $uc->getDao()->makeSelectHead()
+ );
+ }
+
+ public function sync(&$insert, &$update = array(), &$delete)
+ {
+ $uc = $this->container;
+ $dao = $uc->getDao();
+
+ if ($insert)
+ for ($i = 0, $size = count($insert); $i < $size; ++$i)
+ $dao->add($insert[$i]);
+
+ if ($update)
+ for ($i = 0, $size = count($update); $i < $size; ++$i)
+ $dao->save($update[$i]);
+
+ if ($delete) {
+ DBFactory::getDefaultInstance()->queryNull(
+ OSQL::delete()->from($dao->getTable())->
+ where(
+ Expression::eq(
+ new DBField($uc->getParentIdField()),
+ $uc->getParentObject()->getId()
+ )
+ )->
+ andWhere(
+ Expression::in(
+ $uc->getChildIdField(),
+ ArrayUtils::getIdsArray($delete)
+ )
+ )
+ );
+
+ foreach ($delete as $object)
+ $dao->uncacheById($object->getId());
+ }
+
+ return $this;
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Unified/OneToManyLinkedLazy.class.php (from rev 1805, trunk/main/Containers/OneToManyLinkedLazy.class.php)
===================================================================
--- trunk/main/Containers/Unified/OneToManyLinkedLazy.class.php (rev 0)
+++ trunk/main/Containers/Unified/OneToManyLinkedLazy.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,85 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ final class OneToManyLinkedLazy extends OneToManyLinkedWorker
+ {
+ public function makeFetchQuery()
+ {
+ $uc = $this->container;
+
+ return
+ $this->targetize(
+ $this->oq
+ ?
+ $this->oq->toSelectQuery($uc->getDao())->
+ dropFields()->
+ get($uc->getChildIdField())
+ :
+ OSQL::select()->from($uc->getDao()->getTable())->
+ get($uc->getChildIdField())
+ );
+ }
+
+ public function sync(&$insert, &$update = array(), &$delete)
+ {
+ Assert::isTrue($update === array());
+
+ $db = &DBFactory::getDefaultInstance();
+
+ $uc = $this->container;
+ $dao = $uc->getDao();
+
+ if ($insert)
+ $db->queryNull($this->makeMassUpdateQuery($insert));
+
+ if ($delete) {
+ // unlink or drop
+ $uc->isUnlinkable()
+ ?
+ $db->queryNull($this->makeMassUpdateQuery($delete))
+ :
+ $db->queryNull(
+ OSQL::delete()->from($dao->getTable())->
+ where(
+ Expression::in(
+ $uc->getChildIdField(),
+ $delete
+ )
+ )
+ );
+
+ foreach ($delete as $id)
+ $dao->uncacheById($id);
+ }
+
+ return $this;
+ }
+
+ private function makeMassUpdateQuery(&$ids)
+ {
+ $uc = $this->container;
+
+ return
+ OSQL::update($uc->getDao()->getTable())->
+ set($uc->getParentIdField(), null)->
+ where(
+ Expression::in(
+ $uc->getChildIdField(),
+ $ids
+ )
+ );
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Unified/OneToManyLinkedWorker.class.php (from rev 1805, trunk/main/Containers/OneToManyLinkedWorker.class.php)
===================================================================
--- trunk/main/Containers/Unified/OneToManyLinkedWorker.class.php (rev 0)
+++ trunk/main/Containers/Unified/OneToManyLinkedWorker.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,29 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @ingroup Containers
+ **/
+ abstract class OneToManyLinkedWorker extends UnifiedContainerWorker
+ {
+ protected function targetize(SelectQuery $query)
+ {
+ return
+ $query->where(
+ Expression::eqId(
+ new DBField($this->container->getParentIdField()),
+ $this->container->getParentObject()
+ )
+ );
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Unified/UnifiedContainer.class.php (from rev 1805, trunk/main/Containers/UnifiedContainer.class.php)
===================================================================
--- trunk/main/Containers/Unified/UnifiedContainer.class.php (rev 0)
+++ trunk/main/Containers/Unified/UnifiedContainer.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,266 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+/*
+ UnifiedContainer:
+
+ child's and parent's field names:
+ abstract public function getChildIdField()
+ abstract public function getParentIdField()
+
+ all we need from outer world:
+ public function __construct(
+ Identifiable $parent, UnifiedContainer $dao, $lazy = true
+ )
+
+ if you want to apply ObjectQuery's "filter":
+ public function setObjectQuery(ObjectQuery $oq)
+
+ first you should fetch whatever you want:
+ public function fetch()
+
+ then you can get it:
+ public function getList()
+
+ set you modified list:
+ public function setList($list)
+
+ finally, sync fetched data and stored one:
+ public function save()
+
+ OneToManyLinked <- UnifiedContainer:
+
+ indicates whether child can be free (parent_id nullable):
+ protected function isUnlinkable()
+
+ ManyToManyLinked <- UnifiedContainer:
+
+ helper's table name:
+ abstract public function getHelperTable()
+
+ id field name at parent's primary table:
+ protected function getParentTableIdField()
+*/
+
+ /**
+ * IdentifiableObject childs collection handling.
+ *
+ * @see StorableContainer for alternative
+ *
+ * @ingroup Containers
+ **/
+ abstract class UnifiedContainer
+ {
+ protected $worker = null;
+ protected $parent = null;
+
+ protected $dao = null;
+
+ protected $lazy = true;
+ protected $fetched = false;
+
+ protected $list = array();
+ protected $clones = array();
+
+ abstract protected function getChildIdField();
+ abstract protected function getParentIdField();
+
+ public function __construct(
+ Identifiable $parent, GenericDAO $dao, $lazy = true
+ )
+ {
+ Assert::isBoolean($lazy);
+
+ $this->parent = $parent;
+ $this->lazy = $lazy;
+ $this->dao = $dao;
+
+ $childClass = $dao->getObjectName();
+
+ Assert::isTrue(
+ new $childClass instanceof Identifiable,
+ "child object should be at least Identifiable"
+ );
+ }
+
+ public function getParentObject()
+ {
+ return $this->parent;
+ }
+
+ public function getDao()
+ {
+ return $this->dao;
+ }
+
+ public function isLazy()
+ {
+ return $this->lazy;
+ }
+
+ public function isFetched()
+ {
+ return $this->fetched;
+ }
+
+ public function setObjectQuery(ObjectQuery $oq)
+ {
+ Assert::isTrue(
+ $this->dao instanceof MappedDAO,
+ 'you should implement MappedDAO to be able to use ObjectQueries'
+ );
+
+ $this->worker->setObjectQuery($oq);
+
+ return $this;
+ }
+
+ public function setList($list)
+ {
+ Assert::isArray($list);
+
+ $this->list = $list;
+
+ return $this;
+ }
+
+ public function replaceList(/* array */ $list)
+ {
+ Assert::isArray($list);
+
+ return $this->importList($list);
+ }
+
+ public function getList()
+ {
+ return $this->list;
+ }
+
+ public function fetch()
+ {
+ if (!$this->parent->getId())
+ throw new WrongStateException(
+ 'save parent object first'
+ );
+
+ try {
+ $this->fetchList();
+ } catch (ObjectNotFoundException $e) {
+ // yummy
+ }
+
+ $this->fetched = true;
+
+ return $this;
+ }
+
+ public function save()
+ {
+ Assert::isArray(
+ $this->list,
+ "that's not an array :-/"
+ );
+
+ if (!$this->fetched)
+ throw new WrongStateException(
+ 'do not want to save non-fetched collection'
+ );
+
+ $list = $this->list;
+ $clones = $this->clones;
+
+ $ids = $insert = $delete = $update = array();
+
+ if ($this->lazy) {
+ foreach ($list as $id) {
+ if (!isset($clones[$id]))
+ $insert[] = $ids[$id] = $id;
+ else
+ $ids[$id] = $id;
+ }
+
+ foreach ($clones as $id) {
+ if (!isset($ids[$id]))
+ $delete[] = $id;
+ }
+ } else {
+ foreach ($list as $object) {
+ $id = $object->getId();
+
+ if (null === $id) {
+ $insert[] = $object;
+ } elseif (isset($clones[$id]) && $object != $clones[$id]) {
+ $update[] = $object;
+ } elseif (!isset($clones[$id])) {
+ $insert[] = $object;
+ }
+
+ if (null !== $id)
+ $ids[$id] = $object;
+ }
+
+ foreach ($clones as $id => $object) {
+ if (!isset($ids[$id]))
+ $delete[] = $object;
+ }
+ }
+
+ $db = DBFactory::getDefaultInstance();
+
+ $db->queueStart()->begin();
+
+ try {
+ $this->worker->sync($insert, $update, $delete);
+
+ $db->commit()->queueFlush();
+ } catch (DatabaseException $e) {
+ $db->queueDrop()->rollback();
+ throw $e;
+ }
+
+ $this->dao->uncacheLists();
+
+ return $this;
+ }
+
+ protected function fetchList()
+ {
+ $query = $this->worker->makeFetchQuery();
+
+ if ($this->lazy)
+ $list = $this->dao->getCustomRowList($query);
+ else
+ $list = $this->dao->getListByQuery($query);
+
+ return $this->importList($list);
+ }
+
+ private function importList(/* array */ $list)
+ {
+ if ($this->lazy) {
+ foreach ($list as $id) {
+ $this->list[$id] = $id;
+ $this->clones[$id] = $id;
+ }
+ } else {
+ $this->list = $list;
+
+ foreach ($list as $object)
+ $this->clones[$object->getId()] = clone $object;
+ }
+
+ $this->fetched = true;
+
+ return $this;
+ }
+ }
+?>
\ No newline at end of file
Copied: trunk/main/Containers/Unified/UnifiedContainerWorker.class.php (from rev 1805, trunk/main/Containers/UnifiedContainerWorker.class.php)
===================================================================
--- trunk/main/Containers/Unified/UnifiedContainerWorker.class.php (rev 0)
+++ trunk/main/Containers/Unified/UnifiedContainerWorker.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -0,0 +1,38 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ /**
+ * @see UnifiedContainer
+ *
+ * @ingroup Containers
+ **/
+ abstract class UnifiedContainerWorker
+ {
+ protected $oq = null;
+ protected $container = null;
+
+ abstract public function makeFetchQuery();
+ abstract public function sync(&$insert, &$update = array(), &$delete);
+
+ public function __construct(UnifiedContainer $uc)
+ {
+ $this->container = $uc;
+ }
+
+ public function setObjectQuery(ObjectQuery $oq)
+ {
+ $this->oq = $oq;
+
+ return $this;
+ }
+ }
+?>
\ No newline at end of file
Deleted: trunk/main/Containers/UnifiedContainer.class.php
===================================================================
--- trunk/main/Containers/UnifiedContainer.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/UnifiedContainer.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,266 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
-/*
- UnifiedContainer:
-
- child's and parent's field names:
- abstract public function getChildIdField()
- abstract public function getParentIdField()
-
- all we need from outer world:
- public function __construct(
- Identifiable $parent, UnifiedContainer $dao, $lazy = true
- )
-
- if you want to apply ObjectQuery's "filter":
- public function setObjectQuery(ObjectQuery $oq)
-
- first you should fetch whatever you want:
- public function fetch()
-
- then you can get it:
- public function getList()
-
- set you modified list:
- public function setList($list)
-
- finally, sync fetched data and stored one:
- public function save()
-
- OneToManyLinked <- UnifiedContainer:
-
- indicates whether child can be free (parent_id nullable):
- protected function isUnlinkable()
-
- ManyToManyLinked <- UnifiedContainer:
-
- helper's table name:
- abstract public function getHelperTable()
-
- id field name at parent's primary table:
- protected function getParentTableIdField()
-*/
-
- /**
- * IdentifiableObject childs collection handling.
- *
- * @see StorableContainer for alternative
- *
- * @ingroup Containers
- **/
- abstract class UnifiedContainer
- {
- protected $worker = null;
- protected $parent = null;
-
- protected $dao = null;
-
- protected $lazy = true;
- protected $fetched = false;
-
- protected $list = array();
- protected $clones = array();
-
- abstract protected function getChildIdField();
- abstract protected function getParentIdField();
-
- public function __construct(
- Identifiable $parent, GenericDAO $dao, $lazy = true
- )
- {
- Assert::isBoolean($lazy);
-
- $this->parent = $parent;
- $this->lazy = $lazy;
- $this->dao = $dao;
-
- $childClass = $dao->getObjectName();
-
- Assert::isTrue(
- new $childClass instanceof Identifiable,
- "child object should be at least Identifiable"
- );
- }
-
- public function getParentObject()
- {
- return $this->parent;
- }
-
- public function getDao()
- {
- return $this->dao;
- }
-
- public function isLazy()
- {
- return $this->lazy;
- }
-
- public function isFetched()
- {
- return $this->fetched;
- }
-
- public function setObjectQuery(ObjectQuery $oq)
- {
- Assert::isTrue(
- $this->dao instanceof MappedDAO,
- 'you should implement MappedDAO to be able to use ObjectQueries'
- );
-
- $this->worker->setObjectQuery($oq);
-
- return $this;
- }
-
- public function setList($list)
- {
- Assert::isArray($list);
-
- $this->list = $list;
-
- return $this;
- }
-
- public function replaceList(/* array */ $list)
- {
- Assert::isArray($list);
-
- return $this->importList($list);
- }
-
- public function getList()
- {
- return $this->list;
- }
-
- public function fetch()
- {
- if (!$this->parent->getId())
- throw new WrongStateException(
- 'save parent object first'
- );
-
- try {
- $this->fetchList();
- } catch (ObjectNotFoundException $e) {
- // yummy
- }
-
- $this->fetched = true;
-
- return $this;
- }
-
- public function save()
- {
- Assert::isArray(
- $this->list,
- "that's not an array :-/"
- );
-
- if (!$this->fetched)
- throw new WrongStateException(
- 'do not want to save non-fetched collection'
- );
-
- $list = $this->list;
- $clones = $this->clones;
-
- $ids = $insert = $delete = $update = array();
-
- if ($this->lazy) {
- foreach ($list as $id) {
- if (!isset($clones[$id]))
- $insert[] = $ids[$id] = $id;
- else
- $ids[$id] = $id;
- }
-
- foreach ($clones as $id) {
- if (!isset($ids[$id]))
- $delete[] = $id;
- }
- } else {
- foreach ($list as $object) {
- $id = $object->getId();
-
- if (null === $id) {
- $insert[] = $object;
- } elseif (isset($clones[$id]) && $object != $clones[$id]) {
- $update[] = $object;
- } elseif (!isset($clones[$id])) {
- $insert[] = $object;
- }
-
- if (null !== $id)
- $ids[$id] = $object;
- }
-
- foreach ($clones as $id => $object) {
- if (!isset($ids[$id]))
- $delete[] = $object;
- }
- }
-
- $db = DBFactory::getDefaultInstance();
-
- $db->queueStart()->begin();
-
- try {
- $this->worker->sync($insert, $update, $delete);
-
- $db->commit()->queueFlush();
- } catch (DatabaseException $e) {
- $db->queueDrop()->rollback();
- throw $e;
- }
-
- $this->dao->uncacheLists();
-
- return $this;
- }
-
- protected function fetchList()
- {
- $query = $this->worker->makeFetchQuery();
-
- if ($this->lazy)
- $list = $this->dao->getCustomRowList($query);
- else
- $list = $this->dao->getListByQuery($query);
-
- return $this->importList($list);
- }
-
- private function importList(/* array */ $list)
- {
- if ($this->lazy) {
- foreach ($list as $id) {
- $this->list[$id] = $id;
- $this->clones[$id] = $id;
- }
- } else {
- $this->list = $list;
-
- foreach ($list as $object)
- $this->clones[$object->getId()] = clone $object;
- }
-
- $this->fetched = true;
-
- return $this;
- }
- }
-?>
\ No newline at end of file
Deleted: trunk/main/Containers/UnifiedContainerWorker.class.php
===================================================================
--- trunk/main/Containers/UnifiedContainerWorker.class.php 2006-06-25 20:39:37 UTC (rev 1805)
+++ trunk/main/Containers/UnifiedContainerWorker.class.php 2006-06-26 00:15:19 UTC (rev 1806)
@@ -1,38 +0,0 @@
-<?php
-/***************************************************************************
- * Copyright (C) 2005 by Konstantin V. Arkhipov *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-/* $Id$ */
-
- /**
- * @see UnifiedContainer
- *
- * @ingroup Containers
- **/
- abstract class UnifiedContainerWorker
- {
- protected $oq = null;
- protected $container = null;
-
- abstract public function makeFetchQuery();
- abstract public function sync(&$insert, &$update = array(), &$delete);
-
- public function __construct(UnifiedContainer $uc)
- {
- $this->container = $uc;
- }
-
- public function setObjectQuery(ObjectQuery $oq)
- {
- $this->oq = $oq;
-
- return $this;
- }
- }
-?>
\ No newline at end of file
Received on Mon Jun 26 2006 - 04:15:21 MSD
This archive was generated by hypermail 2.2.0 : Sat Oct 27 2007 - 20:16:06 MSD