00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00020 final class CommonDaoWorker extends BaseDaoWorker
00021 {
00023
00024 public function get(ObjectQuery $oq, $expires = Cache::DO_NOT_CACHE)
00025 {
00026 return $this->getByQuery($oq->toSelectQuery($this->dao), $expires);
00027 }
00028
00029 public function getById($id, $expires = Cache::EXPIRES_MEDIUM)
00030 {
00031 if (
00032 ($expires !== Cache::DO_NOT_CACHE)
00033 && ($object = $this->getCachedById($id))
00034 ) {
00035 if ($object === Cache::NOT_FOUND)
00036 throw new ObjectNotFoundException();
00037
00038 return $object;
00039 } else {
00040 $query =
00041 $this->dao->
00042 makeSelectHead()->
00043 andWhere(
00044 Expression::eq(
00045 DBField::create(
00046 $this->dao->getIdName(),
00047 $this->dao->getTable()
00048 ),
00049 $id
00050 )
00051 );
00052
00053 if ($object = $this->fetchObject($query)) {
00054 return
00055 $expires === Cache::DO_NOT_CACHE
00056 ? $object
00057 : $this->cacheById($object, $expires);
00058 } else {
00059 throw new ObjectNotFoundException(
00060 "there is no such object for '".$this->dao->getObjectName()
00061 .(
00062 defined('__LOCAL_DEBUG__')
00063 ?
00064 "' with query == "
00065 .$query->toDialectString(
00066 DBPool::me()->getByDao($this->dao)->
00067 getDialect()
00068 )
00069 : null
00070 )
00071 );
00072 }
00073 }
00074 }
00075
00076 public function getByLogic(
00077 LogicalObject $logic, $expires = Cache::DO_NOT_CACHE
00078 )
00079 {
00080 return
00081 $this->getByQuery(
00082 $this->dao->makeSelectHead()->andWhere($logic), $expires
00083 );
00084 }
00085
00086 public function getByQuery(
00087 SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00088 )
00089 {
00090 if (
00091 ($expires !== Cache::DO_NOT_CACHE)
00092 && ($object = $this->getCachedByQuery($query))
00093 ) {
00094 if ($object === Cache::NOT_FOUND)
00095 throw new ObjectNotFoundException();
00096
00097 return $object;
00098 } elseif ($object = $this->fetchObject($query)) {
00099 if ($expires === Cache::DO_NOT_CACHE)
00100 return $object;
00101 else
00102 return $this->cacheByQuery($query, $object, $expires);
00103 } else
00104 throw new ObjectNotFoundException(
00105 "there is no such object for '".$this->dao->getObjectName()
00106 .(
00107 defined('__LOCAL_DEBUG__')
00108 ?
00109 "' with query == "
00110 .$query->toDialectString(
00111 DBPool::me()->getByDao($this->dao)->
00112 getDialect()
00113 )
00114 : null
00115 )
00116 );
00117 }
00118
00119 public function getCustom(
00120 SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00121 )
00122 {
00123 $db = DBPool::getByDao($this->dao);
00124
00125 if ($query->getLimit() > 1)
00126 throw new WrongArgumentException(
00127 'can not handle non-single row queries'
00128 );
00129
00130 if (
00131 ($expires !== Cache::DO_NOT_CACHE)
00132 && ($object = $this->getCachedByQuery($query))
00133 ) {
00134 if ($object === Cache::NOT_FOUND)
00135 throw new ObjectNotFoundException();
00136
00137 return $object;
00138 } elseif ($object = $db->queryRow($query)) {
00139 if ($expires === Cache::DO_NOT_CACHE)
00140 return $object;
00141 else
00142 return $this->cacheByQuery($query, $object, $expires);
00143 } else {
00144 throw new ObjectNotFoundException(
00145 "zero"
00146 .(
00147 defined('__LOCAL_DEBUG__')
00148 ?
00149 "for query == "
00150 .$query->toDialectString(
00151 DBPool::me()->getByDao($this->dao)->
00152 getDialect()
00153 )
00154 : null
00155 )
00156 );
00157 }
00158 }
00160
00162
00163 public function getList(ObjectQuery $oq, $expires = Cache::DO_NOT_CACHE)
00164 {
00165 return
00166 $this->getListByQuery(
00167 $oq->toSelectQuery($this->dao), $expires
00168 );
00169 }
00170
00171 public function getListByIds($ids, $expires = Cache::EXPIRES_MEDIUM)
00172 {
00173 if ($expires !== Cache::DO_NOT_CACHE) {
00174
00175 $list = array();
00176 $toFetch = array();
00177
00178 foreach ($ids as $id) {
00179 $cached = $this->getCachedById($id);
00180
00181 if ($cached !== Cache::NOT_FOUND) {
00182 if ($cached)
00183 $list[] = $cached;
00184 else
00185 $toFetch[] = $id;
00186 }
00187 }
00188
00189 if (!$toFetch)
00190 return $list;
00191
00192 try {
00193 return
00194 array_merge(
00195 $list,
00196 $this->getListByLogic(
00197 Expression::in(
00198 new DBField(
00199 $this->dao->getIdName(),
00200 $this->dao->getTable()
00201 ),
00202 $toFetch
00203 ),
00204 $expires
00205 )
00206 );
00207 } catch (ObjectNotFoundException $e) {
00208
00209 return $list;
00210 }
00211 } elseif (count($ids)) {
00212 return
00213 $this->getListByLogic(
00214 Expression::in(
00215 new DBField(
00216 $this->dao->getIdName(),
00217 $this->dao->getTable()
00218 ),
00219 $ids
00220 ),
00221 Cache::DO_NOT_CACHE
00222 );
00223 } else
00224 return array();
00225
00226 Assert::isUnreachable();
00227 }
00228
00229 public function getListByQuery(
00230 SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00231 )
00232 {
00233 if (
00234 ($expires !== Cache::DO_NOT_CACHE)
00235 && ($list = $this->getCachedByQuery($query))
00236 ) {
00237 if ($list === Cache::NOT_FOUND)
00238 throw new ObjectNotFoundException();
00239
00240 return $list;
00241 } elseif ($list = $this->fetchList($query)) {
00242 if (Cache::DO_NOT_CACHE === $expires) {
00243 return $list;
00244 } else {
00245 return $this->cacheByQuery($query, $list, $expires);
00246 }
00247 } else {
00248 throw new ObjectNotFoundException(
00249 "empty list"
00250 .(
00251 defined('__LOCAL_DEBUG__')
00252 ?
00253 " for such query - "
00254 .$query->toDialectString(
00255 DBPool::me()->getByDao($this->dao)->
00256 getDialect()
00257 )
00258 : null
00259 )
00260 );
00261 }
00262 }
00263
00264 public function getListByLogic(
00265 LogicalObject $logic, $expires = Cache::DO_NOT_CACHE
00266 )
00267 {
00268 return
00269 $this->getListByQuery(
00270 $this->dao->makeSelectHead()->andWhere($logic), $expires
00271 );
00272 }
00273
00274 public function getPlainList($expires = Cache::EXPIRES_MEDIUM)
00275 {
00276 return $this->getListByQuery(
00277 $this->dao->makeSelectHead(), $expires
00278 );
00279 }
00281
00283
00284 public function getCustomList(
00285 SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00286 )
00287 {
00288 if (
00289 ($expires !== Cache::DO_NOT_CACHE)
00290 && ($list = $this->getCachedByQuery($query))
00291 ) {
00292 if ($list === Cache::NOT_FOUND)
00293 throw new ObjectNotFoundException();
00294
00295 return $list;
00296 } elseif ($list = DBPool::getByDao($this->dao)->querySet($query)) {
00297 if (Cache::DO_NOT_CACHE === $expires) {
00298 return $list;
00299 } else {
00300 return $this->cacheByQuery($query, $list, $expires);
00301 }
00302 } else {
00303 throw new ObjectNotFoundException(
00304 "empty list"
00305 .(
00306 defined('__LOCAL_DEBUG__')
00307 ?
00308 " for such query - "
00309 .$query->toDialectString(
00310 DBPool::me()->getByDao($this->dao)->
00311 getDialect()
00312 )
00313 : null
00314 )
00315 );
00316 }
00317 }
00318
00319 public function getCustomRowList(
00320 SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00321 )
00322 {
00323 if ($query->getFieldsCount() !== 1)
00324 throw new WrongArgumentException(
00325 'you should select only one row when using this method'
00326 );
00327
00328 if (
00329 ($expires !== Cache::DO_NOT_CACHE)
00330 && ($list = $this->getCachedByQuery($query))
00331 ) {
00332 if ($list === Cache::NOT_FOUND)
00333 throw new ObjectNotFoundException();
00334
00335 return $list;
00336 } elseif ($list = DBPool::getByDao($this->dao)->queryColumn($query)) {
00337 if (Cache::DO_NOT_CACHE === $expires) {
00338 return $list;
00339 } else {
00340 return $this->cacheByQuery($query, $list, $expires);
00341 }
00342 } else {
00343 throw new ObjectNotFoundException(
00344 "empty list"
00345 .(
00346 defined('__LOCAL_DEBUG__')
00347 ?
00348 " for such query - "
00349 .$query->toDialectString(
00350 DBPool::me()->getByDao($this->dao)->
00351 getDialect()
00352 )
00353 : null
00354 )
00355 );
00356 }
00357 }
00359
00361
00362 public function getCountedList(
00363 ObjectQuery $oq, $expires = Cache::DO_NOT_CACHE
00364 )
00365 {
00366 return
00367 $this->getQueryResult(
00368 $oq->toSelectQuery($this->dao), $expires
00369 );
00370 }
00371
00372 public function getQueryResult(
00373 SelectQuery $query, $expires = Cache::DO_NOT_CACHE
00374 )
00375 {
00376 if (
00377 ($expires !== Cache::DO_NOT_CACHE)
00378 && ($list = $this->getCachedByQuery($query))
00379 ) {
00380 return $list;
00381 } else {
00382 $list = $this->fetchList($query);
00383
00384 $count = clone $query;
00385
00386 $count =
00387 DBPool::getByDao($this->dao)->queryRow(
00388 $count->dropFields()->dropOrder()->limit(null, null)->
00389 get(SQLFunction::create('COUNT', '*')->setAlias('count'))
00390 );
00391
00392 return
00393 $this->cacheByQuery(
00394 $query,
00395
00396 QueryResult::create()->
00397 setList($list)->
00398 setCount($count['count'])->
00399 setQuery($query),
00400
00401 $expires
00402 );
00403 }
00404 }
00406
00408
00409 public function cacheById(
00410 Identifiable $object, $expires = Cache::EXPIRES_MEDIUM
00411 )
00412 {
00413 if ($expires !== Cache::DO_NOT_CACHE) {
00414
00415 Cache::me()->mark($this->className)->
00416 add(
00417 $this->className.'_'.$object->getId(),
00418 $object,
00419 $expires
00420 );
00421 }
00422
00423 return $object;
00424 }
00425
00426 public function cacheByQuery(
00427 SelectQuery $query,
00428 $object,
00429 $expires = Cache::DO_NOT_CACHE
00430 )
00431 {
00432 if ($expires !== Cache::DO_NOT_CACHE) {
00433
00434 Cache::me()->mark($this->className)->
00435 add(
00436 $this->className.self::SUFFIX_QUERY.$query->getId(),
00437 $object,
00438 $expires
00439 );
00440 }
00441
00442 return $object;
00443 }
00444
00445 public function cacheListByQuery(SelectQuery $query, $array)
00446 {
00447 throw new UnimplementedFeatureException();
00448 }
00450
00452
00453 public function dropById($id)
00454 {
00455 $result = parent::dropById($id);
00456
00457 $this->dao->uncacheLists();
00458
00459 return $result;
00460 }
00462
00464
00465 public function uncacheByIds($ids)
00466 {
00467 foreach ($ids as $id)
00468 $this->uncacheById($id);
00469
00470 return $this->dao->uncacheLists();
00471 }
00472
00473
00474 public function uncacheLists()
00475 {
00476 return $this->uncacheByQuery($this->dao->makeSelectHead());
00477 }
00479 }
00480 ?>