GenericDAO.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2007 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 /* $Id: GenericDAO.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00018     abstract class GenericDAO extends Singleton implements BaseDAO
00019     {
00020         // override later, BC, <0.9
00021         protected $mapping = array();
00022 
00023         protected $identityMap  = array();
00024         
00025         protected $selectHead   = null;
00026         
00027         abstract public function getTable();
00028         abstract public function getObjectName();
00029         
00030         abstract protected function makeObject(&$array, $prefix = null);
00031         
00032         public function createObject()
00033         {
00034             $className = $this->getObjectName();
00035             
00036             return new $className;
00037         }
00038         
00045         public function getLinkName()
00046         {
00047             return null;
00048         }
00049         
00050         public function getIdName()
00051         {
00052             return 'id';
00053         }
00054         
00055         public function getSequence()
00056         {
00057             return $this->getTable().'_id';
00058         }
00059         
00063         public function makeSelectHead()
00064         {
00065             if (null === $this->selectHead) {
00066                 $table = $this->getTable();
00067                 
00068                 $this->selectHead =
00069                     OSQL::select()->
00070                     from($table);
00071                 
00072                 foreach ($this->getFields() as $field)
00073                     $this->selectHead->get(new DBField($field, $table));
00074             }
00075             
00076             return clone $this->selectHead;
00077         }
00078         
00080         public function getMapping()
00081         {
00082             if (!$this->mapping)
00083                 throw new WrongStateException('empty mapping');
00084             
00085             return $this->mapping;
00086         }
00087         
00089         public function getFields()
00090         {
00091             static $fields = array();
00092             
00093             $name = $this->getObjectName();
00094             
00095             if (!isset($fields[$name])) {
00096                 foreach ($this->getMapping() as $property => $field)
00097                     $fields[$name][] = $field === null ? $property : $field;
00098             }
00099             
00100             return $fields[$name];
00101         }
00102         
00104 
00105         public function get(ObjectQuery $oq, $expires = Cache::DO_NOT_CACHE)
00106         {
00107             return Cache::worker($this)->get($oq, $expires);
00108         }
00109 
00110         public function getById($id, $expires = Cache::EXPIRES_MEDIUM)
00111         {
00112             if (isset($this->identityMap[$id]))
00113                 return $this->identityMap[$id];
00114             
00115             return $this->addObjectToMap(
00116                 Cache::worker($this)->getById($id, $expires)
00117             );
00118         }
00119         
00120         public function getByLogic(
00121             LogicalObject $logic, $expires = Cache::DO_NOT_CACHE
00122         )
00123         {
00124             return $this->addObjectToMap(
00125                 Cache::worker($this)->getByLogic($logic, $expires)
00126             );
00127         }
00128         
00129         public function getByQuery(
00130             SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00131         )
00132         {
00133             return $this->addObjectToMap(
00134                 Cache::worker($this)->getByQuery($query, $expires)
00135             );
00136         }
00137         
00138         public function getCustom(
00139             SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00140         )
00141         {
00142             return Cache::worker($this)->getCustom($query, $expires);
00143         }
00144         
00145         public function getList(ObjectQuery $oq, $expires = Cache::DO_NOT_CACHE)
00146         {
00147             return Cache::worker($this)->getList($oq, $expires);
00148         }
00149         
00150         public function getListByIds(
00151             /* array */ $ids, $expires = Cache::EXPIRES_MEDIUM
00152         )
00153         {
00154             $mapped = $remain = array();
00155             
00156             foreach ($ids as $id) {
00157                 if (isset($this->identityMap[$id])) {
00158                     $mapped[] = $this->identityMap[$id];
00159                 } else {
00160                     $remain[] = $id;
00161                 }
00162             }
00163             
00164             if ($remain) {
00165                 $list = $this->addObjectListToMap(
00166                     Cache::worker($this)->getListByIds($remain, $expires)
00167                 );
00168                 
00169                 return array_merge($mapped, $list);
00170             }
00171             
00172             return $mapped;
00173         }
00174         
00175         public function getListByQuery(
00176             SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00177         )
00178         {
00179             return $this->addObjectListToMap(
00180                 Cache::worker($this)->getListByQuery($query, $expires)
00181             );
00182         }
00183         
00184         public function getListByLogic(
00185             LogicalObject $logic, $expires = Cache::DO_NOT_CACHE
00186         )
00187         {
00188             return $this->addObjectListToMap(
00189                 Cache::worker($this)->getListByLogic($logic, $expires)
00190             );
00191         }
00192         
00193         public function getPlainList($expires = Cache::EXPIRES_MEDIUM)
00194         {
00195             return $this->addObjectListToMap(
00196                 Cache::worker($this)->getPlainList($expires)
00197             );
00198         }
00199         
00200         public function getCustomList(
00201             SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00202         )
00203         {
00204             return Cache::worker($this)->getCustomList($query, $expires);
00205         }
00206         
00207         public function getCustomRowList(
00208             SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00209         )
00210         {
00211             return Cache::worker($this)->getCustomRowList($query, $expires);
00212         }
00213         
00214         public function getCountedList(
00215             ObjectQuery $oq, $expires = Cache::DO_NOT_CACHE
00216         )
00217         {
00218             return Cache::worker($this)->getCountedList($oq, $expires);
00219         }
00220         
00221         public function getQueryResult(
00222             SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00223         )
00224         {
00225             return Cache::worker($this)->getQueryResult($query, $expires);
00226         }
00227         
00228         public function cacheById(
00229             Identifiable $object, $expires = Cache::EXPIRES_MEDIUM
00230         )
00231         {
00232             return Cache::worker($this)->cacheById($object, $expires);
00233         }
00234         
00235         public function cacheByQuery(
00236             SelectQuery $query,
00237             /* Identifiable */ $object,
00238             $expires = Cache::DO_NOT_CACHE
00239         )
00240         {
00241             return Cache::worker($this)->cacheByQuery($query, $object, $expires);
00242         }
00243         
00244         public function cacheListByQuery(SelectQuery $query, /* array */ $array)
00245         {
00246             return Cache::worker($this)->cacheListByQuery($query, $array);
00247         }
00248         
00249         public function getCachedById($id)
00250         {
00251             return Cache::worker($this)->getCachedById($id);
00252         }
00253         
00254         public function getCachedByQuery(SelectQuery $query)
00255         {
00256             return Cache::worker($this)->getCachedByQuery($query);
00257         }
00258         
00259         public function drop(Identifiable $object)
00260         {
00261             $this->checkObjectType($object);
00262             
00263             return $this->dropById($object->getId());
00264         }
00265         
00266         public function dropById($id)
00267         {
00268             unset($this->identityMap[$id]);
00269             
00270             return Cache::worker($this)->dropById($id);
00271         }
00272         
00273         public function dropByIds(/* array */ $ids)
00274         {
00275             foreach ($ids as $id)
00276                 unset($this->identityMap[$id]);
00277             
00278             return Cache::worker($this)->dropByIds($ids);
00279         }
00280         
00281         public function uncacheById($id)
00282         {
00283             unset($this->identityMap[$id]);
00284             
00285             return Cache::worker($this)->uncacheById($id);
00286         }
00287         
00288         public function uncacheByIds($ids)
00289         {
00290             foreach ($ids as $id)
00291                 unset($this->identityMap[$id]);
00292             
00293             return Cache::worker($this)->uncacheByIds($ids);
00294         }
00295         
00296         public function uncacheByQuery(SelectQuery $query)
00297         {
00298             return Cache::worker($this)->uncacheByQuery($query);
00299         }
00300         
00301         public function uncacheLists()
00302         {
00303             $this->identityMap = array();
00304             
00305             return Cache::worker($this)->uncacheLists();
00306         }
00308         
00312         public function dropIdentityMap()
00313         {
00314             $this->identityMap = array();
00315             
00316             return $this;
00317         }
00318         
00319         /* void */ protected function checkObjectType(Identifiable $object)
00320         {
00321             Assert::isTrue(
00322                 get_class($object) === $this->getObjectName(),
00323                 'strange object given, i can not inject it'
00324             );
00325         }
00326         
00327         private function addObjectToMap($object)
00328         {
00329             return $this->identityMap[$object->getId()] = $object;
00330         }
00331         
00332         private function addObjectListToMap($list)
00333         {
00334             foreach ($list as $object)
00335                 $this->identityMap[$object->getId()] = $object;
00336             
00337             return $list;
00338         }
00339     }
00340 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4