Event.php 4.98 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;
zYne's avatar
zYne committed
45 46 47
    /**
     * @var Doctrine_Db $invoker        the handler which invoked this event
     */
zYne's avatar
zYne committed
48
    protected $invoker;
zYne's avatar
zYne committed
49 50 51
    /**
     * @var string $query               the sql query associated with this event (if any)
     */
zYne's avatar
zYne committed
52
    protected $query;
zYne's avatar
zYne committed
53 54 55 56 57 58 59 60
    /**
     * @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
61
    protected $startedMicrotime;
zYne's avatar
zYne committed
62 63 64
    /**
     * @var integer $endedMicrotime     the time point in which this event was ended
     */
zYne's avatar
zYne committed
65
    protected $endedMicrotime;
zYne's avatar
zYne committed
66 67 68 69 70 71 72 73
    /**
     * 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)
     */
    public function __construct($invoker, $code, $query = null)
lsmith's avatar
lsmith committed
74
    {
zYne's avatar
zYne committed
75
        $this->invoker = $invoker;
zYne's avatar
zYne committed
76
        $this->code    = $code;
zYne's avatar
zYne committed
77 78
        $this->query   = $query;
    }
zYne's avatar
zYne committed
79 80 81 82 83
    /**
     * getQuery
     *
     * @return string       returns the query associated with this event (if any)
     */
lsmith's avatar
lsmith committed
84 85
    public function getQuery()
    {
zYne's avatar
zYne committed
86 87
        return $this->query;
    }
zYne's avatar
zYne committed
88 89 90 91 92 93 94
    /**
     * getName
     * returns the name of this event
     *
     * @return string       the name of this event
     */
    public function getName() 
lsmith's avatar
lsmith committed
95
    {
zYne's avatar
zYne committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        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
114
    }
zYne's avatar
zYne committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    /**
     * 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
130 131
    public function start()
    {
zYne's avatar
zYne committed
132 133
        $this->startedMicrotime = microtime(true);
    }
zYne's avatar
zYne committed
134 135 136 137 138 139
    /**
     * hasEnded
     * whether or not this event has ended
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
140 141
    public function hasEnded()
    {
zYne's avatar
zYne committed
142 143
        return ($this->endedMicrotime != null);
    }
zYne's avatar
zYne committed
144 145 146 147 148 149
    /**
     * end
     * ends the internal timer of this event
     *
     * @return void
     */
lsmith's avatar
lsmith committed
150 151
    public function end()
    {
zYne's avatar
zYne committed
152 153
        $this->endedMicrotime = microtime(true);
    }
zYne's avatar
zYne committed
154 155 156 157 158 159
    /**
     * getInvoker
     * returns the handler that invoked this event
     *
     * @return Doctrine_Db   the handler that invoked this event
     */
lsmith's avatar
lsmith committed
160 161
    public function getInvoker()
    {
zYne's avatar
zYne committed
162 163 164
        return $this->invoker;
    }
    /**
165
     * Get the elapsed time (in microseconds) that the event ran.  If the event has
zYne's avatar
zYne committed
166 167 168 169
     * not yet ended, return false.
     *
     * @return mixed
     */
lsmith's avatar
lsmith committed
170 171
    public function getElapsedSecs()
    {
lsmith's avatar
lsmith committed
172
        if (is_null($this->endedMicrotime)) {
zYne's avatar
zYne committed
173
            return false;
lsmith's avatar
lsmith committed
174
        }
zYne's avatar
zYne committed
175 176 177 178
        return ($this->endedMicrotime - $this->startedMicrotime);
    }

}