1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/lib/Doctrine/Lib.php

173 lines
6.9 KiB
PHP
Raw Normal View History

2006-05-30 12:42:10 +04:00
<?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>.
*/
/**
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
*
* Doctrine_Lib has not commonly used static functions, mostly for debugging purposes
*/
2006-05-30 12:42:10 +04:00
class Doctrine_Lib {
/**
* @param integer $state the state of record
* @see Doctrine_Record::STATE_* constants
* @return string string representation of given state
*/
public static function getRecordStateAsString($state) {
switch($state):
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;
endswitch;
}
/**
* returns a string representation of Doctrine_Record object
* @param Doctrine_Record $record
* @return string
*/
2006-06-20 01:31:22 +04:00
public static function getRecordAsString(Doctrine_Record $record) {
2006-05-30 12:42:10 +04:00
$r[] = "<pre>";
$r[] = "Component : ".$record->getTable()->getComponentName();
$r[] = "ID : ".$record->obtainIdentifier();
2006-05-30 12:42:10 +04:00
$r[] = "References : ".count($record->getReferences());
$r[] = "State : ".Doctrine_Lib::getRecordStateAsString($record->getState());
$r[] = "OID : ".$record->getOID();
$r[] = "</pre>";
return implode("\n",$r)."<br />";
}
/**
* getStateAsString
2006-08-22 03:19:15 +04:00
* returns a given connection state as string
* @param integer $state connection state
2006-05-30 12:42:10 +04:00
*/
2006-08-22 03:19:15 +04:00
public static function getConnectionStateAsString($state) {
2006-05-30 12:42:10 +04:00
switch($state):
case Doctrine_Connection_Transaction::STATE_OPEN:
2006-05-30 12:42:10 +04:00
return "open";
break;
case Doctrine_Connection_Transaction::STATE_CLOSED:
2006-05-30 12:42:10 +04:00
return "closed";
break;
case Doctrine_Connection_Transaction::STATE_BUSY:
2006-05-30 12:42:10 +04:00
return "busy";
break;
case Doctrine_Connection_Transaction::STATE_ACTIVE:
2006-05-30 12:42:10 +04:00
return "active";
break;
endswitch;
}
/**
2006-08-22 03:19:15 +04:00
* returns a string representation of Doctrine_Connection object
* @param Doctrine_Connection $connection
2006-05-30 12:42:10 +04:00
* @return string
*/
2006-08-22 03:19:15 +04:00
public static function getConnectionAsString(Doctrine_Connection $connection) {
2006-05-30 12:42:10 +04:00
$r[] = "<pre>";
2006-08-22 03:19:15 +04:00
$r[] = "Doctrine_Connection object";
2006-10-31 21:05:15 +03:00
$r[] = "State : ".Doctrine_Lib::getConnectionStateAsString($connection->getTransaction()->getState());
$r[] = "Open Transactions : ".$connection->getTransaction()->getTransactionLevel();
$r[] = "Table in memory : ".$connection->count();
2006-05-30 12:42:10 +04:00
$queries = false;
2006-11-06 21:00:35 +03:00
if($connection->getDBH() instanceof Doctrine_Db) {
2006-05-30 12:42:10 +04:00
$handler = "Doctrine Database Handler";
2006-08-22 03:19:15 +04:00
$queries = count($connection->getDBH()->getQueries());
$sum = array_sum($connection->getDBH()->getExecTimes());
} elseif($connection->getDBH() instanceof PDO) {
2006-05-30 12:42:10 +04:00
$handler = "PHP Native PDO Driver";
} else
$handler = "Unknown Database Handler";
$r[] = "DB Handler : ".$handler;
if($queries) {
$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
*/
2006-06-20 01:31:22 +04:00
public static function getTableAsString(Doctrine_Table $table) {
2006-05-30 12:42:10 +04:00
$r[] = "<pre>";
2006-06-25 22:34:53 +04:00
$r[] = "Component : ".$table->getComponentName();
$r[] = "Table : ".$table->getTableName();
2006-05-30 12:42:10 +04:00
$r[] = "</pre>";
return implode("\n",$r)."<br>";
}
2006-06-09 02:11:36 +04:00
/**
* @return string
*/
2006-06-20 01:31:22 +04:00
public static function formatSql($sql) {
2006-06-09 02:11:36 +04:00
$e = explode("\n",$sql);
$color = "367FAC";
$l = $sql;
$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);
2006-06-30 03:04:39 +04:00
$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);
2006-06-09 02:11:36 +04:00
$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);
return $l;
}
2006-05-30 12:42:10 +04:00
/**
* returns a string representation of Doctrine_Collection object
* @param Doctrine_Collection $collection
* @return string
*/
2006-06-20 01:31:22 +04:00
public static function getCollectionAsString(Doctrine_Collection $collection) {
2006-05-30 12:42:10 +04:00
$r[] = "<pre>";
$r[] = get_class($collection);
foreach($collection as $key => $record) {
$r[] = "Key : ".$key." ID : ".$record->obtainIdentifier();
2006-05-30 12:42:10 +04:00
}
$r[] = "</pre>";
return implode("\n",$r);
}
}