Event.php 5.43 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
/**
 * Doctrine_Db_Event
 *
24 25 26 27 28 29 30 31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @package     Doctrine
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
32 33
class Doctrine_Db_Event
{
zYne's avatar
zYne committed
34 35 36
    /**
     * EVENT CODE CONSTANTS
     */
zYne's avatar
zYne committed
37 38
    const QUERY     = 1;
    const EXEC      = 2;
39 40 41 42 43
    const EXECUTE   = 3;
    const PREPARE   = 4;
    const BEGIN     = 5;
    const COMMIT    = 6;
    const ROLLBACK  = 7;
44
    const CONNECT   = 8;
45 46
    const FETCH     = 9;
    const FETCHALL  = 10;
zYne's avatar
zYne committed
47 48 49
    /**
     * @var Doctrine_Db $invoker        the handler which invoked this event
     */
zYne's avatar
zYne committed
50
    protected $invoker;
zYne's avatar
zYne committed
51 52 53
    /**
     * @var string $query               the sql query associated with this event (if any)
     */
zYne's avatar
zYne committed
54
    protected $query;
55 56 57 58
    /**
     * @var string $params               the parameters associated with the query (if any)
     */
    protected $params;
zYne's avatar
zYne committed
59 60 61 62 63 64 65 66
    /**
     * @see Doctrine_Db_Event constants
     * @var integer $code               the event code
     */
    protected $code;
    /**
     * @var integer $startedMicrotime   the time point in which this event was started
     */
zYne's avatar
zYne committed
67
    protected $startedMicrotime;
zYne's avatar
zYne committed
68 69 70
    /**
     * @var integer $endedMicrotime     the time point in which this event was ended
     */
zYne's avatar
zYne committed
71
    protected $endedMicrotime;
zYne's avatar
zYne committed
72 73 74 75 76 77 78
    /**
     * constructor
     *
     * @param Doctrine_Db $invoker      the handler which invoked this event
     * @param integer $code             the event code
     * @param string $query             the sql query associated with this event (if any)
     */
79
    public function __construct($invoker, $code, $query = null, $params = array())
lsmith's avatar
lsmith committed
80
    {
zYne's avatar
zYne committed
81
        $this->invoker = $invoker;
zYne's avatar
zYne committed
82
        $this->code    = $code;
83
        $this->query   = $query;
84
        $this->params = $params;
zYne's avatar
zYne committed
85
    }
zYne's avatar
zYne committed
86 87 88 89 90
    /**
     * getQuery
     *
     * @return string       returns the query associated with this event (if any)
     */
lsmith's avatar
lsmith committed
91 92
    public function getQuery()
    {
zYne's avatar
zYne committed
93 94
        return $this->query;
    }
zYne's avatar
zYne committed
95 96 97 98 99 100 101
    /**
     * getName
     * returns the name of this event
     *
     * @return string       the name of this event
     */
    public function getName() 
lsmith's avatar
lsmith committed
102
    {
zYne's avatar
zYne committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        switch ($this->code) {
            case self::QUERY:
                return 'query';
            case self::EXEC:
                return 'exec';
            case self::EXECUTE:
                return 'execute';
            case self::PREPARE:
                return 'prepare';
            case self::BEGIN:
                return 'begin';
            case self::COMMIT:
                return 'commit';
            case self::ROLLBACK:
                return 'rollback';
            case self::CONNECT:
                return 'connect';
        }
zYne's avatar
zYne committed
121
    }
zYne's avatar
zYne committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    /**
     * getCode
     *
     * @return integer      returns the code associated with this event
     */
    public function getCode()
    {
        return $this->code;
    }
    /**
     * start
     * starts the internal timer of this event
     *
     * @return void
     */
lsmith's avatar
lsmith committed
137 138
    public function start()
    {
zYne's avatar
zYne committed
139 140
        $this->startedMicrotime = microtime(true);
    }
zYne's avatar
zYne committed
141 142 143 144 145 146
    /**
     * hasEnded
     * whether or not this event has ended
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
147 148
    public function hasEnded()
    {
zYne's avatar
zYne committed
149 150
        return ($this->endedMicrotime != null);
    }
zYne's avatar
zYne committed
151 152 153 154 155 156
    /**
     * end
     * ends the internal timer of this event
     *
     * @return void
     */
lsmith's avatar
lsmith committed
157 158
    public function end()
    {
zYne's avatar
zYne committed
159 160
        $this->endedMicrotime = microtime(true);
    }
zYne's avatar
zYne committed
161 162 163 164 165 166
    /**
     * getInvoker
     * returns the handler that invoked this event
     *
     * @return Doctrine_Db   the handler that invoked this event
     */
lsmith's avatar
lsmith committed
167 168
    public function getInvoker()
    {
zYne's avatar
zYne committed
169 170
        return $this->invoker;
    }
171 172 173 174 175 176 177 178 179 180
    /**
     * getParams
     * returns the parameters of the query
     *
     * @return array   parameters of the query
     */
    public function getParams()
    {
        return $this->params;
    }
zYne's avatar
zYne committed
181
    /**
182
     * Get the elapsed time (in microseconds) that the event ran.  If the event has
zYne's avatar
zYne committed
183 184 185 186
     * not yet ended, return false.
     *
     * @return mixed
     */
lsmith's avatar
lsmith committed
187 188
    public function getElapsedSecs()
    {
lsmith's avatar
lsmith committed
189
        if (is_null($this->endedMicrotime)) {
zYne's avatar
zYne committed
190
            return false;
lsmith's avatar
lsmith committed
191
        }
zYne's avatar
zYne committed
192 193 194 195
        return ($this->endedMicrotime - $this->startedMicrotime);
    }

}