BaseDaoWorker.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2008 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     abstract class BaseDaoWorker implements BaseDAO
00016     {
00017         const SUFFIX_LIST   = '_list_';
00018         const SUFFIX_INDEX  = '_lists_index_';
00019         const SUFFIX_QUERY  = '_query_';
00020         const SUFFIX_RESULT = '_result_';
00021         
00022         protected $dao = null;
00023         
00024         protected $className = null;
00025         
00026         protected $watermark = null;
00027         
00028         public function __construct(GenericDAO $dao)
00029         {
00030             $this->dao = $dao;
00031             
00032             $this->className = $dao->getObjectName();
00033             
00034             if (($cache = Cache::me()) instanceof WatermarkedPeer)
00035                 $this->watermark =
00036                     $cache->mark($this->className)->getActualWatermark();
00037         }
00038         
00042         public function setDao(GenericDAO $dao)
00043         {
00044             $this->dao = $dao;
00045             
00046             return $this;
00047         }
00048         
00050 
00051         public function drop(Identifiable $object)
00052         {
00053             return $this->dropById($object->getId());
00054         }
00055         
00056         public function dropById($id)
00057         {
00058             $result =
00059                 DBPool::getByDao($this->dao)->queryCount(
00060                     OSQL::delete()->from($this->dao->getTable())->
00061                     where(Expression::eq($this->dao->getIdName(), $id))
00062                 );
00063             
00064             $this->dao->uncacheById($id);
00065             
00066             return $result;
00067         }
00068         
00069         public function dropByIds(array $ids)
00070         {
00071             $result =
00072                 DBPool::getByDao($this->dao)->queryCount(
00073                     OSQL::delete()->from($this->dao->getTable())->
00074                     where(Expression::in($this->dao->getIdName(), $ids))
00075                 );
00076             
00077             $this->dao->uncacheByIds($ids);
00078             
00079             return $result;
00080         }
00082         
00084 
00085         public function uncacheById($id)
00086         {
00087             return
00088                 Cache::me()->mark($this->className)->
00089                     delete($this->makeIdKey($id));
00090         }
00091         
00092         public function uncacheByQuery(SelectQuery $query)
00093         {
00094             return
00095                 Cache::me()->mark($this->className)->
00096                     delete($this->makeQueryKey($query, self::SUFFIX_QUERY));
00097         }
00099         
00101 
00102         public function getCachedById($id)
00103         {
00104             return
00105                 Cache::me()->mark($this->className)->
00106                     get($this->makeIdKey($id));
00107         }
00108         
00109         protected function getCachedByQuery(SelectQuery $query)
00110         {
00111             return
00112                 Cache::me()->mark($this->className)->
00113                     get($this->makeQueryKey($query, self::SUFFIX_QUERY));
00114         }
00116         
00118 
00119         protected function fetchObject(SelectQuery $query)
00120         {
00121             if ($row = DBPool::getByDao($this->dao)->queryRow($query)) {
00122                 return $this->dao->makeObject($row);
00123             }
00124             
00125             return null;
00126         }
00127         
00128         protected function cachedFetchObject(
00129             SelectQuery $query,
00130             $expires,
00131             $byId = true
00132         )
00133         {
00134             if ($row = DBPool::getByDao($this->dao)->queryRow($query)) {
00135                 $object = $this->dao->makeOnlyObject($row);
00136                 
00137                 if ($byId)
00138                     $object = $this->cacheById($object, $expires);
00139                 else
00140                     $object = $this->cacheByQuery($query, $object, $expires);
00141                 
00142                 return $this->dao->completeObject($object);
00143             }
00144             
00145             return null;
00146         }
00147         
00148         protected function fetchList(SelectQuery $query)
00149         {
00150             $list = array();
00151             
00152             if ($rows = DBPool::getByDao($this->dao)->querySet($query)) {
00153                 $proto = $this->dao->getProtoClass();
00154                 
00155                 $proto->beginPrefetch();
00156                 
00157                 foreach ($rows as $row)
00158                     $list[] = $this->dao->makeObject($row);
00159                 
00160                 $proto->endPrefetch($list);
00161             }
00162             
00163             return $list;
00164         }
00166         
00167         protected function makeIdKey($id)
00168         {
00169             return $this->className.'_'.$id.$this->watermark;
00170         }
00171         
00172         protected function makeQueryKey(SelectQuery $query, $suffix)
00173         {
00174             return
00175                 $this->className
00176                 .$suffix
00177                 .$query->getId()
00178                 .$this->watermark;
00179         }
00180     }
00181 ?>