# Queue.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
    00021     final class Queue implements Query
    00022     {
    00023         private $queue = array();
    00024
    00028         public static function create()
    00029         {
    00030             return new self;
    00031         }
    00032
    00033         public function getId()
    00034         {
    00035             return sha1(serialize($this->queue));
    00036         }
    00037
    00038         public function setId($id)
    00039         {
    00040             throw new UnsupportedMethodException();
    00041         }
    00042
    00043         public function getQueue()
    00044         {
    00045             return $this->queue;
    00046         }
    00047
    00051         public function add(Query $query)
    00052         {
    00053             $this->queue[] = $query;
    00054
    00055             return $this;
    00056         }
    00057
    00061         public function remove(Query $query)
    00062         {
    00063             if (!$id = array_search($query, $this->queue))
    00064                 throw new MissingElementException();
    00065
    00066             unset($this->queue[$id]);
    00067
    00068             return $this;
    00069         }
    00070
    00074         public function drop()
    00075         {
    00076             $this->queue = array();
    00077
    00078             return $this;
    00079         }
    00080
    00084         public function run(DB $db)
    00085         {
    00086             $db->queryRaw($this->toDialectString($db->getDialect()));
    00087
    00088             return $this;
    00089         }
    00090
    00094         public function flush(DB $db)
    00095         {
    00096             return $this->run($db)->drop();
    00097         }
    00098
    00099         // to satisfy Query interface
    00100         public function toString()
    00101         {
    00102             return $this->toDialectString(ImaginaryDialect::me());
    00103         }
    00104
    00105         public function toDialectString(Dialect $dialect)
    00106         {
    00107             $out = array();
    00108
    00109             foreach ($this->queue as $query)
    00110                 $out[] = $query->toDialectString($dialect);
    00111
    00112             return implode(";\n", $out);
    00113         }
    00114     }
    00115 ?>