Sequenceless.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 
00027     abstract class Sequenceless extends DB
00028     {
00029         protected $sequencePool = array();
00030         
00031         abstract protected function getInsertId();
00032 
00036         final public function obtainSequence($sequence)
00037         {
00038             $id = Identifier::create();
00039             
00040             $this->sequencePool[$sequence][] = $id;
00041             
00042             return $id;
00043         }
00044         
00045         final public function query(Query $query)
00046         {
00047             $result = $this->queryRaw(
00048                 $query->toDialectString($this->getDialect())
00049             );
00050             
00051             if (
00052                 ($query instanceof InsertQuery)
00053                 && !empty($this->sequencePool[$name = $query->getTable().'_id'])
00054             ) {
00055                 $id = current($this->sequencePool[$name]);
00056                 
00057                 Assert::isTrue(
00058                     $id instanceof Identifier,
00059                     'identifier was lost in the way'
00060                 );
00061                 
00062                 $id->setId($this->getInsertId())->finalize();
00063                 
00064                 unset(
00065                     $this->sequencePool[
00066                         $name
00067                     ][
00068                         key($this->sequencePool[$name])
00069                     ]
00070                 );
00071             }
00072             
00073             return $result;
00074         }
00075     }
00076 ?>