Lib.php 7.08 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
lsmith's avatar
lsmith committed
2
/*
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *  $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_Lib has not commonly used static functions, mostly for debugging purposes
zYne's avatar
zYne committed
23 24 25 26 27 28 29 30 31
 *
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
lsmith's avatar
lsmith committed
32 33
class Doctrine_Lib
{
doctrine's avatar
doctrine committed
34 35 36 37 38
    /**
     * @param integer $state                the state of record
     * @see Doctrine_Record::STATE_* constants
     * @return string                       string representation of given state
     */
lsmith's avatar
lsmith committed
39 40
    public static function getRecordStateAsString($state)
    {
lsmith's avatar
lsmith committed
41
        switch ($state) {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
            case Doctrine_Record::STATE_PROXY:
                return "proxy";
                break;
            case Doctrine_Record::STATE_CLEAN:
                return "persistent clean";
                break;
            case Doctrine_Record::STATE_DIRTY:
                return "persistent dirty";
                break;
            case Doctrine_Record::STATE_TDIRTY:
                return "transient dirty";
                break;
            case Doctrine_Record::STATE_TCLEAN:
                return "transient clean";
                break;
lsmith's avatar
lsmith committed
57
        };
doctrine's avatar
doctrine committed
58 59 60 61 62 63
    }
    /**
     * returns a string representation of Doctrine_Record object
     * @param Doctrine_Record $record
     * @return string
     */
lsmith's avatar
lsmith committed
64 65
    public static function getRecordAsString(Doctrine_Record $record)
    {
doctrine's avatar
doctrine committed
66 67
        $r[] = "<pre>";
        $r[] = "Component  : ".$record->getTable()->getComponentName();
68
        $r[] = "ID         : ".$record->obtainIdentifier();
doctrine's avatar
doctrine committed
69 70 71 72 73 74 75 76
        $r[] = "References : ".count($record->getReferences());
        $r[] = "State      : ".Doctrine_Lib::getRecordStateAsString($record->getState());
        $r[] = "OID        : ".$record->getOID();
        $r[] = "</pre>";
        return implode("\n",$r)."<br />";
    }
    /**
     * getStateAsString
zYne's avatar
zYne committed
77 78
     * returns a given connection state as string
     * @param integer $state        connection state
doctrine's avatar
doctrine committed
79
     */
lsmith's avatar
lsmith committed
80 81
    public static function getConnectionStateAsString($state)
    {
lsmith's avatar
lsmith committed
82
        switch ($state) {
83 84 85 86 87 88 89 90 91
            case Doctrine_Transaction::STATE_SLEEP:
                return "open";
                break;
            case Doctrine_Transaction::STATE_BUSY:
                return "busy";
                break;
            case Doctrine_Transaction::STATE_ACTIVE:
                return "active";
                break;
lsmith's avatar
lsmith committed
92
        };
doctrine's avatar
doctrine committed
93 94
    }
    /**
zYne's avatar
zYne committed
95 96
     * returns a string representation of Doctrine_Connection object
     * @param Doctrine_Connection $connection
doctrine's avatar
doctrine committed
97 98
     * @return string
     */
lsmith's avatar
lsmith committed
99 100
    public static function getConnectionAsString(Doctrine_Connection $connection)
    {
doctrine's avatar
doctrine committed
101
        $r[] = "<pre>";
zYne's avatar
zYne committed
102
        $r[] = "Doctrine_Connection object";
zYne's avatar
zYne committed
103 104 105
        $r[] = "State               : ".Doctrine_Lib::getConnectionStateAsString($connection->getTransaction()->getState());
        $r[] = "Open Transactions   : ".$connection->getTransaction()->getTransactionLevel();
        $r[] = "Table in memory     : ".$connection->count();
doctrine's avatar
doctrine committed
106 107

        $queries = false;
lsmith's avatar
lsmith committed
108
        if ($connection->getDBH() instanceof Doctrine_Db) {
doctrine's avatar
doctrine committed
109
            $handler = "Doctrine Database Handler";
zYne's avatar
zYne committed
110 111
            $queries = count($connection->getDBH()->getQueries());
            $sum     = array_sum($connection->getDBH()->getExecTimes());
lsmith's avatar
lsmith committed
112
        } elseif ($connection->getDBH() instanceof PDO) {
doctrine's avatar
doctrine committed
113
            $handler = "PHP Native PDO Driver";
lsmith's avatar
lsmith committed
114
        } else {
doctrine's avatar
doctrine committed
115
            $handler = "Unknown Database Handler";
lsmith's avatar
lsmith committed
116
        }
doctrine's avatar
doctrine committed
117 118

        $r[] = "DB Handler          : ".$handler;
lsmith's avatar
lsmith committed
119
        if ($queries) {
doctrine's avatar
doctrine committed
120 121 122 123 124 125 126 127 128 129 130 131
            $r[] = "Executed Queries    : ".$queries;
            $r[] = "Sum of Exec Times   : ".$sum;
        }

        $r[] = "</pre>";
        return implode("\n",$r)."<br>";
    }
    /**
     * returns a string representation of Doctrine_Table object
     * @param Doctrine_Table $table
     * @return string
     */
lsmith's avatar
lsmith committed
132 133
    public static function getTableAsString(Doctrine_Table $table)
    {
doctrine's avatar
doctrine committed
134
        $r[] = "<pre>";
135 136
        $r[] = "Component   : ".$table->getComponentName();
        $r[] = "Table       : ".$table->getTableName();
doctrine's avatar
doctrine committed
137 138 139
        $r[] = "</pre>";
        return implode("\n",$r)."<br>";
    }
doctrine's avatar
doctrine committed
140 141 142
    /**
     * @return string
     */
lsmith's avatar
lsmith committed
143 144
    public static function formatSql($sql)
    {
doctrine's avatar
doctrine committed
145 146 147
        $e = explode("\n",$sql);
        $color = "367FAC";
        $l = $sql;
zYne's avatar
zYne committed
148 149 150 151 152 153 154 155 156 157 158 159 160
        $l = str_replace("SELECT ", "<font color='$color'><b>SELECT </b></font><br \>  ",$l);
        $l = str_replace("FROM ", "<font color='$color'><b>FROM </b></font><br \>",$l);
        $l = str_replace(" LEFT JOIN ", "<br \><font color='$color'><b> LEFT JOIN </b></font>",$l);
        $l = str_replace(" INNER JOIN ", "<br \><font color='$color'><b> INNER JOIN </b></font>",$l);
        $l = str_replace(" WHERE ", "<br \><font color='$color'><b> WHERE </b></font>",$l);
        $l = str_replace(" GROUP BY ", "<br \><font color='$color'><b> GROUP BY </b></font>",$l);
        $l = str_replace(" HAVING ", "<br \><font color='$color'><b> HAVING </b></font>",$l);
        $l = str_replace(" AS ", "<font color='$color'><b> AS </b></font><br \>  ",$l);
        $l = str_replace(" ON ", "<font color='$color'><b> ON </b></font>",$l);
        $l = str_replace(" ORDER BY ", "<font color='$color'><b> ORDER BY </b></font><br \>",$l);
        $l = str_replace(" LIMIT ", "<font color='$color'><b> LIMIT </b></font><br \>",$l);
        $l = str_replace(" OFFSET ", "<font color='$color'><b> OFFSET </b></font><br \>",$l);
        $l = str_replace("  ", "<dd>",$l);
lsmith's avatar
lsmith committed
161

doctrine's avatar
doctrine committed
162 163
        return $l;
    }
doctrine's avatar
doctrine committed
164 165 166 167 168
    /**
     * returns a string representation of Doctrine_Collection object
     * @param Doctrine_Collection $collection
     * @return string
     */
lsmith's avatar
lsmith committed
169 170
    public static function getCollectionAsString(Doctrine_Collection $collection)
    {
doctrine's avatar
doctrine committed
171 172 173
        $r[] = "<pre>";
        $r[] = get_class($collection);

lsmith's avatar
lsmith committed
174
        foreach ($collection as $key => $record) {
175
            $r[] = "Key : ".$key." ID : ".$record->obtainIdentifier();
doctrine's avatar
doctrine committed
176 177 178 179 180
        }
        $r[] = "</pre>";
        return implode("\n",$r);
    }
}