mirror of
https://github.com/dg/dibi.git
synced 2025-08-04 13:17:58 +02:00
* modified DibiException (getDbError, ...)
* fix dibi::dumpResult()
This commit is contained in:
@@ -13,11 +13,11 @@
|
|||||||
* @license GNU GENERAL PUBLIC LICENSE v2
|
* @license GNU GENERAL PUBLIC LICENSE v2
|
||||||
* @package dibi
|
* @package dibi
|
||||||
* @category Database
|
* @category Database
|
||||||
* @version 0.7b $Revision: 26 $ $Date: 2007-01-29 06:08:52 +0100 (po, 29 I 2007) $
|
* @version 0.7c $Revision: 27 $ $Date: 2007-01-30 22:50:04 +0100 (út, 30 I 2007) $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
define('DIBI','Version 0.7b $Revision: 26 $');if(version_compare(PHP_VERSION,'5.0.3','<'))die('dibi needs PHP 5.0.3 or newer');abstract
|
define('DIBI','Version 0.7c $Revision: 27 $');if(version_compare(PHP_VERSION,'5.0.3','<'))die('dibi needs PHP 5.0.3 or newer');abstract
|
||||||
class
|
class
|
||||||
DibiDriver{protected$config;public$formats=array('TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);static
|
DibiDriver{protected$config;public$formats=array('TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);static
|
||||||
public
|
public
|
||||||
@@ -111,12 +111,14 @@ fetchAll(){@$this->seek(0);$rec=$this->fetch();if(!$rec)return
|
|||||||
array();$arr=array();if(count($rec)==1){$key=key($rec);do{$arr[]=$rec[$key];}while($rec=$this->fetch());}else{do{$arr[]=$rec;}while($rec=$this->fetch());}return$arr;}final
|
array();$arr=array();if(count($rec)==1){$key=key($rec);do{$arr[]=$rec[$key];}while($rec=$this->fetch());}else{do{$arr[]=$rec;}while($rec=$this->fetch());}return$arr;}final
|
||||||
function
|
function
|
||||||
fetchAssoc($assocBy){@$this->seek(0);$rec=$this->fetch();if(!$rec)return
|
fetchAssoc($assocBy){@$this->seek(0);$rec=$this->fetch();if(!$rec)return
|
||||||
array();$assocBy=func_get_args();$arr=array();do{foreach($assocBy
|
array();$assocBy=func_get_args();foreach($assocBy
|
||||||
|
as$n=>$assoc)if(!array_key_exists($assoc,$rec))unset($assocBy[$n]);$arr=array();do{foreach($assocBy
|
||||||
as$n=>$assoc){$val[$n]=$rec[$assoc];unset($rec[$assoc]);}foreach($assocBy
|
as$n=>$assoc){$val[$n]=$rec[$assoc];unset($rec[$assoc]);}foreach($assocBy
|
||||||
as$n=>$assoc){if($n==0)$tmp=&$arr[$val[$n]];else$tmp=&$tmp[$assoc][$val[$n]];if($tmp===NULL)$tmp=$rec;}}while($rec=$this->fetch());return$arr;}final
|
as$n=>$assoc){if($n==0)$tmp=&$arr[$val[$n]];else$tmp=&$tmp[$assoc][$val[$n]];if($tmp===NULL)$tmp=$rec;}}while($rec=$this->fetch());return$arr;}final
|
||||||
function
|
function
|
||||||
fetchPairs($key,$value){@$this->seek(0);$rec=$this->fetch();if(!$rec)return
|
fetchPairs($key,$value){@$this->seek(0);$rec=$this->fetch();if(!$rec)return
|
||||||
array();$arr=array();do{$arr[$rec[$key]]=$rec[$value];}while($rec=$this->fetch());return$arr;}public
|
array();if(!array_key_exists($key,$rec)||!array_key_exists($value,$rec))return
|
||||||
|
FALSE;$arr=array();do{$arr[$rec[$key]]=$rec[$value];}while($rec=$this->fetch());return$arr;}public
|
||||||
function
|
function
|
||||||
__destruct(){@$this->free();}public
|
__destruct(){@$this->free();}public
|
||||||
function
|
function
|
||||||
@@ -163,9 +165,7 @@ function
|
|||||||
__construct($driver,$subst){$this->driver=$driver;$this->subK=array_keys($subst);$this->subV=array_values($subst);}public
|
__construct($driver,$subst){$this->driver=$driver;$this->subK=array_keys($subst);$this->subV=array_values($subst);}public
|
||||||
function
|
function
|
||||||
translate($args){$this->hasError=FALSE;$command=null;$mod=&$this->modifier;$mod=FALSE;$this->ifLevel=$this->ifLevelStart=0;$comment=&$this->comment;$comment=FALSE;$sql=array();foreach($args
|
translate($args){$this->hasError=FALSE;$command=null;$mod=&$this->modifier;$mod=FALSE;$this->ifLevel=$this->ifLevelStart=0;$comment=&$this->comment;$comment=FALSE;$sql=array();foreach($args
|
||||||
as$arg){if('if'==$mod){$mod=FALSE;$this->ifLevel++;if(!$comment&&!$arg){$sql[]="\0";$this->ifLevelStart=$this->ifLevel;$comment=TRUE;}continue;}if(is_string($arg)&&(!$mod||'sql'==$mod)){$mod=FALSE;$sql[]=$this->formatValue($arg,'sql');continue;}if(!$mod&&is_array($arg)&&is_string(key($arg))){if(!$command)$command=strtoupper(substr(ltrim($args[0]),0,6));$mod=('INSERT'==$command||'REPLAC'==$command)?'v':'a';}if(!$comment)$sql[]=$this->formatValue($arg,$mod);$mod=FALSE;}if($comment)$sql[]="\0";$sql=implode(' ',$sql);$sql=preg_replace('#\x00.*?\x00#s','',$sql);$this->sql=$sql;return!$this->hasError;if($this->hasError)throw
|
as$arg){if('if'==$mod){$mod=FALSE;$this->ifLevel++;if(!$comment&&!$arg){$sql[]="\0";$this->ifLevelStart=$this->ifLevel;$comment=TRUE;}continue;}if(is_string($arg)&&(!$mod||'sql'==$mod)){$mod=FALSE;$sql[]=$this->formatValue($arg,'sql');continue;}if(!$mod&&is_array($arg)&&is_string(key($arg))){if(!$command)$command=strtoupper(substr(ltrim($args[0]),0,6));$mod=('INSERT'==$command||'REPLAC'==$command)?'v':'a';}if(!$comment)$sql[]=$this->formatValue($arg,$mod);$mod=FALSE;}if($comment)$sql[]="\0";$sql=implode(' ',$sql);$sql=preg_replace('#\x00.*?\x00#s','',$sql);$this->sql=$sql;return!$this->hasError;}private
|
||||||
new
|
|
||||||
DibiException('Errors during generating SQL',array('sql'=>$sql));return$sql;}private
|
|
||||||
function
|
function
|
||||||
formatValue($value,$modifier){if(is_array($value)){$vx=$kx=array();switch($modifier){case'a':foreach($value
|
formatValue($value,$modifier){if(is_array($value)){$vx=$kx=array();switch($modifier){case'a':foreach($value
|
||||||
as$k=>$v){$pair=explode('%',$k,2);$vx[]=$this->quote($pair[0]).'='.$this->formatValue($v,isset($pair[1])?$pair[1]:FALSE);}return
|
as$k=>$v){$pair=explode('%',$k,2);$vx[]=$this->quote($pair[0]).'='.$this->formatValue($v,isset($pair[1])?$pair[1]:FALSE);}return
|
||||||
@@ -195,14 +195,15 @@ function
|
|||||||
__isset($nm){$this->__get($nm);}}class
|
__isset($nm){$this->__get($nm);}}class
|
||||||
DibiException
|
DibiException
|
||||||
extends
|
extends
|
||||||
Exception{private$info;public
|
Exception{private$sql,$dbError;public
|
||||||
function
|
function
|
||||||
__construct($message,$info=NULL){$this->info=$info;if(isset($info['message']))$message="$message: $info[message]";parent::__construct($message);}public
|
__construct($message,$dbError=NULL,$sql=NULL){$this->dbError=$dbError;$this->sql=$sql;parent::__construct($message);}public
|
||||||
function
|
function
|
||||||
getSql(){return
|
getSql(){return$this->sql;}public
|
||||||
isset($this->info['sql'])?$this->info['sql']:NULL;}public
|
|
||||||
function
|
function
|
||||||
__toString(){$s=parent::__toString();if(isset($this->info['sql']))$s.="\nSQL: ".$this->info['sql'];return$s;}}
|
getDbError(){return$this->dbError;}public
|
||||||
|
function
|
||||||
|
__toString(){$s=parent::__toString();if($this->dbError){$s.="\nERROR: ";if(isset($this->dbError['code']))$s.="[".$this->dbError['code']."] ";$s.=$this->dbError['message'];}if($this->sql)$s.="\nSQL: ".$this->sql;return$s;}}
|
||||||
interface
|
interface
|
||||||
IDibiVariable{public
|
IDibiVariable{public
|
||||||
function
|
function
|
||||||
@@ -246,13 +247,13 @@ function
|
|||||||
query($args){$conn=self::getConnection();if(!is_array($args))$args=func_get_args();$trans=new
|
query($args){$conn=self::getConnection();if(!is_array($args))$args=func_get_args();$trans=new
|
||||||
DibiTranslator($conn,self::$substs);if(!$trans->translate($args)){if(self::$logFile)self::log("ERROR: SQL generate error"."\n-- SQL: ".$trans->sql.";\n-- ".date('Y-m-d H:i:s '));if(dibi::$throwExceptions)throw
|
DibiTranslator($conn,self::$substs);if(!$trans->translate($args)){if(self::$logFile)self::log("ERROR: SQL generate error"."\n-- SQL: ".$trans->sql.";\n-- ".date('Y-m-d H:i:s '));if(dibi::$throwExceptions)throw
|
||||||
new
|
new
|
||||||
DibiException('SQL generate error',array('sql'=>$trans->sql));else{trigger_error("dibi: SQL generate error: $trans->sql",E_USER_WARNING);return
|
DibiException('SQL generate error',NULL,$trans->sql);else{trigger_error("dibi: SQL generate error: $trans->sql",E_USER_WARNING);return
|
||||||
FALSE;}}self::$sql=$trans->sql;$timer=-microtime(true);$res=$conn->query(self::$sql);if($res===FALSE){if(self::$logFile){$info=$conn->errorInfo();self::log("ERROR: [$info[code]] $info[message]"."\n-- SQL: ".self::$sql.";\n-- ".date('Y-m-d H:i:s '));}if(dibi::$throwExceptions){$info=$conn->errorInfo();$info['sql']=self::$sql;throw
|
FALSE;}}self::$sql=$trans->sql;$timer=-microtime(true);$res=$conn->query(self::$sql);if($res===FALSE){if(self::$logFile){$info=$conn->errorInfo();if($info['code'])$info['message']="[$info[code]] $info[message]";self::log("ERROR: $info[message]"."\n-- SQL: ".self::$sql.";\n-- ".date('Y-m-d H:i:s '));}if(dibi::$throwExceptions){$info=$conn->errorInfo();throw
|
||||||
new
|
new
|
||||||
DibiException('Query error',$info);}else{$info=$conn->errorInfo();trigger_error("dibi: [$info[code]] $info[message]",E_USER_WARNING);return
|
DibiException('Query error',$info,self::$sql);}else{$info=$conn->errorInfo();if($info['code'])$info['message']="[$info[code]] $info[message]";trigger_error("dibi: $info[message]",E_USER_WARNING);return
|
||||||
FALSE;}}if(self::$logFile&&self::$logAll){$timer+=microtime(true);$msg=$res
|
FALSE;}}if(self::$logFile&&self::$logAll){$timer+=microtime(true);$msg=$res
|
||||||
instanceof
|
instanceof
|
||||||
DibiResult?'object('.get_class($res).') rows: '.$res->rowCount():'OK';self::log("OK: ".self::$sql.";\n-- result: $msg"."\n-- takes: ".sprintf('%0.3f',$timer*1000).' ms'.";\n-- ".date('Y-m-d H:i:s '));}return$res;}static
|
DibiResult?'object('.get_class($res).') rows: '.$res->rowCount():'OK';self::log("OK: ".self::$sql.";\n-- result: $msg"."\n-- takes: ".sprintf('%0.3f',$timer*1000).' ms'."\n-- ".date('Y-m-d H:i:s '));}return$res;}static
|
||||||
public
|
public
|
||||||
function
|
function
|
||||||
test($args){if(!is_array($args))$args=func_get_args();$trans=new
|
test($args){if(!is_array($args))$args=func_get_args();$trans=new
|
||||||
@@ -270,10 +271,10 @@ function
|
|||||||
dumpHighlight($matches){if(!empty($matches[1]))return'<em style="color:gray">'.$matches[1].'</em>';if(!empty($matches[2]))return'<strong style="color:red">'.$matches[2].'</strong>';if(!empty($matches[3]))return'<strong style="color:blue">'.$matches[3].'</strong>';if(!empty($matches[4]))return'<strong style="color:green">'.$matches[4].'</strong>';}static
|
dumpHighlight($matches){if(!empty($matches[1]))return'<em style="color:gray">'.$matches[1].'</em>';if(!empty($matches[2]))return'<strong style="color:red">'.$matches[2].'</strong>';if(!empty($matches[3]))return'<strong style="color:blue">'.$matches[3].'</strong>';if(!empty($matches[4]))return'<strong style="color:green">'.$matches[4].'</strong>';}static
|
||||||
public
|
public
|
||||||
function
|
function
|
||||||
dump($sql,$return=FALSE){static$keywords2='ALL|DISTINCT|AS|ON|INTO|AND|OR|AS';static$keywords1='SELECT|UPDATE|INSERT|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN';$sql=preg_replace("#\\b(?:$keywords1)\\b#","\n\$0",$sql);$sql=trim($sql);$sql=preg_replace('# {2,}#',' ',$sql);$sql=wordwrap($sql,100);$sql=htmlSpecialChars($sql);$sql=preg_replace("#\n{2,}#","\n",$sql);$sql=preg_replace_callback("#(/\*.+?\*/)|(\*\*.+?\*\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#",array('dibi','dumpHighlight'),$sql);$sql='<pre class="dibi">'.$sql."</pre>\n";if(!$return)echo$sql;return$sql;}static
|
dump($sql,$return=FALSE){static$keywords2='ALL|DISTINCT|AS|ON|INTO|AND|OR|AS';static$keywords1='SELECT|UPDATE|INSERT|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN';$sql=preg_replace("#\\b(?:$keywords1)\\b#","\n\$0",$sql);$sql=trim($sql);$sql=preg_replace('# {2,}#',' ',$sql);$sql=wordwrap($sql,100);$sql=htmlSpecialChars($sql);$sql=preg_replace("#\n{2,}#","\n",$sql);$sql=preg_replace_callback("#(/\*.+?\*/)|(\*\*.+?\*\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#",array('dibi','dumpHighlight'),$sql);$sql='<pre class="dump">'.$sql."</pre>\n";if(!$return)echo$sql;return$sql;}static
|
||||||
public
|
public
|
||||||
function
|
function
|
||||||
dumpResult(DibiResult$res){echo'<table class="dump"><tr>';echo'<th>Row</th>';$fieldCount=$res->fieldCount();for($i=0;$i<$fieldCount;$i++){$info=$res->fieldMeta($i);echo'<th>'.htmlSpecialChars($info['name']).'</th>';}echo'</tr>';foreach($res
|
dumpResult(DibiResult$res){echo'<table class="dump"><tr>';echo'<th>#row</th>';foreach($res->getFields()as$field)echo'<th>'.$field.'</th>';echo'</tr>';foreach($res
|
||||||
as$row=>$fields){echo'<tr><th>',$row,'</th>';foreach($fields
|
as$row=>$fields){echo'<tr><th>',$row,'</th>';foreach($fields
|
||||||
as$field){if(is_object($field))$field=$field->__toString();echo'<td>',htmlSpecialChars($field),'</td>';}echo'</tr>';}echo'</table>';}static
|
as$field){if(is_object($field))$field=$field->__toString();echo'<td>',htmlSpecialChars($field),'</td>';}echo'</tr>';}echo'</table>';}static
|
||||||
public
|
public
|
||||||
|
@@ -14,11 +14,11 @@
|
|||||||
* @license GNU GENERAL PUBLIC LICENSE v2
|
* @license GNU GENERAL PUBLIC LICENSE v2
|
||||||
* @package dibi
|
* @package dibi
|
||||||
* @category Database
|
* @category Database
|
||||||
* @version 0.7b $Revision$ $Date$
|
* @version 0.7c $Revision$ $Date$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
define('DIBI', 'Version 0.7b $Revision$');
|
define('DIBI', 'Version 0.7c $Revision$');
|
||||||
|
|
||||||
|
|
||||||
if (version_compare(PHP_VERSION , '5.0.3', '<'))
|
if (version_compare(PHP_VERSION , '5.0.3', '<'))
|
||||||
@@ -143,7 +143,7 @@ class dibi
|
|||||||
* @param array|string connection parameters
|
* @param array|string connection parameters
|
||||||
* @param string connection name
|
* @param string connection name
|
||||||
* @return void
|
* @return void
|
||||||
* @throw DibiException
|
* @throw DibiException
|
||||||
*/
|
*/
|
||||||
static public function connect($config, $name = '1')
|
static public function connect($config, $name = '1')
|
||||||
{
|
{
|
||||||
@@ -189,7 +189,7 @@ class dibi
|
|||||||
* Retrieve active connection
|
* Retrieve active connection
|
||||||
*
|
*
|
||||||
* @return object DibiDriver object.
|
* @return object DibiDriver object.
|
||||||
* @throw DibiException
|
* @throw DibiException
|
||||||
*/
|
*/
|
||||||
static public function getConnection()
|
static public function getConnection()
|
||||||
{
|
{
|
||||||
@@ -206,7 +206,7 @@ class dibi
|
|||||||
*
|
*
|
||||||
* @param string connection registy name
|
* @param string connection registy name
|
||||||
* @return void
|
* @return void
|
||||||
* @throw DibiException
|
* @throw DibiException
|
||||||
*/
|
*/
|
||||||
static public function activate($name)
|
static public function activate($name)
|
||||||
{
|
{
|
||||||
@@ -227,7 +227,7 @@ class dibi
|
|||||||
*
|
*
|
||||||
* @param array|mixed one or more arguments
|
* @param array|mixed one or more arguments
|
||||||
* @return int|DibiResult
|
* @return int|DibiResult
|
||||||
* @throw DibiException
|
* @throw DibiException
|
||||||
*/
|
*/
|
||||||
static public function query($args)
|
static public function query($args)
|
||||||
{
|
{
|
||||||
@@ -248,7 +248,7 @@ class dibi
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (dibi::$throwExceptions)
|
if (dibi::$throwExceptions)
|
||||||
throw new DibiException('SQL generate error', array('sql' => $trans->sql));
|
throw new DibiException('SQL generate error', NULL, $trans->sql);
|
||||||
else {
|
else {
|
||||||
trigger_error("dibi: SQL generate error: $trans->sql", E_USER_WARNING);
|
trigger_error("dibi: SQL generate error: $trans->sql", E_USER_WARNING);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
@@ -264,8 +264,9 @@ class dibi
|
|||||||
if ($res === FALSE) { // query error
|
if ($res === FALSE) { // query error
|
||||||
if (self::$logFile) { // log to file
|
if (self::$logFile) { // log to file
|
||||||
$info = $conn->errorInfo();
|
$info = $conn->errorInfo();
|
||||||
|
if ($info['code']) $info['message'] = "[$info[code]] $info[message]";
|
||||||
self::log(
|
self::log(
|
||||||
"ERROR: [$info[code]] $info[message]"
|
"ERROR: $info[message]"
|
||||||
. "\n-- SQL: " . self::$sql
|
. "\n-- SQL: " . self::$sql
|
||||||
. ";\n-- " . date('Y-m-d H:i:s ')
|
. ";\n-- " . date('Y-m-d H:i:s ')
|
||||||
);
|
);
|
||||||
@@ -273,11 +274,11 @@ class dibi
|
|||||||
|
|
||||||
if (dibi::$throwExceptions) {
|
if (dibi::$throwExceptions) {
|
||||||
$info = $conn->errorInfo();
|
$info = $conn->errorInfo();
|
||||||
$info['sql'] = self::$sql;
|
throw new DibiException('Query error', $info, self::$sql);
|
||||||
throw new DibiException('Query error', $info);
|
|
||||||
} else {
|
} else {
|
||||||
$info = $conn->errorInfo();
|
$info = $conn->errorInfo();
|
||||||
trigger_error("dibi: [$info[code]] $info[message]", E_USER_WARNING);
|
if ($info['code']) $info['message'] = "[$info[code]] $info[message]";
|
||||||
|
trigger_error("dibi: $info[message]", E_USER_WARNING);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -290,7 +291,7 @@ class dibi
|
|||||||
"OK: " . self::$sql
|
"OK: " . self::$sql
|
||||||
. ";\n-- result: $msg"
|
. ";\n-- result: $msg"
|
||||||
. "\n-- takes: " . sprintf('%0.3f', $timer * 1000) . ' ms'
|
. "\n-- takes: " . sprintf('%0.3f', $timer * 1000) . ' ms'
|
||||||
. ";\n-- " . date('Y-m-d H:i:s ')
|
. "\n-- " . date('Y-m-d H:i:s ')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -389,7 +390,7 @@ class dibi
|
|||||||
|
|
||||||
// syntax highlight
|
// syntax highlight
|
||||||
$sql = preg_replace_callback("#(/\*.+?\*/)|(\*\*.+?\*\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#", array('dibi', 'dumpHighlight'), $sql);
|
$sql = preg_replace_callback("#(/\*.+?\*/)|(\*\*.+?\*\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#", array('dibi', 'dumpHighlight'), $sql);
|
||||||
$sql = '<pre class="dibi">' . $sql . "</pre>\n";
|
$sql = '<pre class="dump">' . $sql . "</pre>\n";
|
||||||
|
|
||||||
// print & return
|
// print & return
|
||||||
if (!$return) echo $sql;
|
if (!$return) echo $sql;
|
||||||
@@ -407,12 +408,9 @@ class dibi
|
|||||||
static public function dumpResult(DibiResult $res)
|
static public function dumpResult(DibiResult $res)
|
||||||
{
|
{
|
||||||
echo '<table class="dump"><tr>';
|
echo '<table class="dump"><tr>';
|
||||||
echo '<th>Row</th>';
|
echo '<th>#row</th>';
|
||||||
$fieldCount = $res->fieldCount();
|
foreach ($res->getFields() as $field)
|
||||||
for ($i = 0; $i < $fieldCount; $i++) {
|
echo '<th>' . $field . '</th>';
|
||||||
$info = $res->fieldMeta($i);
|
|
||||||
echo '<th>'.htmlSpecialChars($info['name']).'</th>';
|
|
||||||
}
|
|
||||||
echo '</tr>';
|
echo '</tr>';
|
||||||
|
|
||||||
foreach ($res as $row => $fields) {
|
foreach ($res as $row => $fields) {
|
||||||
|
@@ -49,9 +49,9 @@ abstract class DibiDriver
|
|||||||
/**
|
/**
|
||||||
* DibiDriver factory: creates object and connects to a database
|
* DibiDriver factory: creates object and connects to a database
|
||||||
*
|
*
|
||||||
* @param array connect configuration
|
* @param array connect configuration
|
||||||
* @return DibiDriver
|
* @return DibiDriver
|
||||||
* @throw DibiException
|
* @throw DibiException
|
||||||
*/
|
*/
|
||||||
/*abstract disallowed since PHP 5.2*/ static public function connect($config) {}
|
/*abstract disallowed since PHP 5.2*/ static public function connect($config) {}
|
||||||
|
|
||||||
|
@@ -28,31 +28,44 @@ if (!defined('DIBI')) die();
|
|||||||
class DibiException extends Exception
|
class DibiException extends Exception
|
||||||
{
|
{
|
||||||
private
|
private
|
||||||
$info;
|
$sql,
|
||||||
|
$dbError;
|
||||||
|
|
||||||
|
|
||||||
public function __construct($message, $info=NULL) {
|
public function __construct($message, $dbError=NULL, $sql=NULL)
|
||||||
|
{
|
||||||
$this->info = $info;
|
$this->dbError = $dbError;
|
||||||
|
$this->sql = $sql;
|
||||||
if (isset($info['message']))
|
|
||||||
$message = "$message: $info[message]";
|
|
||||||
|
|
||||||
parent::__construct($message);
|
parent::__construct($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function getSql()
|
public function getSql()
|
||||||
{
|
{
|
||||||
return isset($this->info['sql']) ? $this->info['sql'] : NULL;
|
return $this->sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getDbError()
|
||||||
|
{
|
||||||
|
return $this->dbError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
{
|
{
|
||||||
$s = parent::__toString();
|
$s = parent::__toString();
|
||||||
if (isset($this->info['sql']))
|
|
||||||
$s .= "\nSQL: " . $this->info['sql'];
|
if ($this->dbError) {
|
||||||
|
$s .= "\nERROR: ";
|
||||||
|
if (isset($this->dbError['code']))
|
||||||
|
$s .= "[" . $this->dbError['code'] . "] ";
|
||||||
|
|
||||||
|
$s .= $this->dbError['message'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->sql) $s .= "\nSQL: " . $this->sql;
|
||||||
|
|
||||||
return $s;
|
return $s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -200,6 +200,11 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
|||||||
return array(); // empty resultset
|
return array(); // empty resultset
|
||||||
|
|
||||||
$assocBy = func_get_args();
|
$assocBy = func_get_args();
|
||||||
|
|
||||||
|
// check function parameters - !!! ignore or throw error?
|
||||||
|
foreach ($assocBy as $n => $assoc) //
|
||||||
|
if (!array_key_exists($assoc, $rec)) unset($assocBy[$n]);
|
||||||
|
|
||||||
$arr = array();
|
$arr = array();
|
||||||
|
|
||||||
do { // make associative arrays
|
do { // make associative arrays
|
||||||
@@ -234,6 +239,9 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
|||||||
if (!$rec)
|
if (!$rec)
|
||||||
return array(); // empty resultset
|
return array(); // empty resultset
|
||||||
|
|
||||||
|
if (!array_key_exists($key, $rec) ||
|
||||||
|
!array_key_exists($value, $rec)) return FALSE;
|
||||||
|
|
||||||
$arr = array();
|
$arr = array();
|
||||||
do {
|
do {
|
||||||
$arr[ $rec[$key] ] = $rec[$value];
|
$arr[ $rec[$key] ] = $rec[$value];
|
||||||
|
@@ -52,7 +52,7 @@ class DibiTranslator
|
|||||||
*
|
*
|
||||||
* @param array
|
* @param array
|
||||||
* @return string
|
* @return string
|
||||||
* @throw DibiException
|
* @throw DibiException
|
||||||
*/
|
*/
|
||||||
public function translate($args)
|
public function translate($args)
|
||||||
{
|
{
|
||||||
@@ -115,10 +115,6 @@ class DibiTranslator
|
|||||||
$this->sql = $sql;
|
$this->sql = $sql;
|
||||||
|
|
||||||
return !$this->hasError;
|
return !$this->hasError;
|
||||||
if ($this->hasError)
|
|
||||||
throw new DibiException('Errors during generating SQL', array('sql' => $sql));
|
|
||||||
|
|
||||||
return $sql;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -4,55 +4,53 @@ require_once '../dibi/dibi.php';
|
|||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// connects using DSN
|
|
||||||
|
// connects to MySQL using DSN
|
||||||
dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=utf8');
|
dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=utf8');
|
||||||
|
|
||||||
|
|
||||||
|
// connects to MySQL / MySQLi
|
||||||
|
dibi::connect(array(
|
||||||
|
'driver' => 'mysql', // or 'mysqli'
|
||||||
|
'host' => 'localhost',
|
||||||
|
'username' => 'root',
|
||||||
|
'password' => 'xxx',
|
||||||
|
'database' => 'dibi',
|
||||||
|
'charset' => 'utf8',
|
||||||
|
));
|
||||||
|
|
||||||
|
|
||||||
|
// connects to ODBC
|
||||||
|
dibi::connect(array(
|
||||||
|
'driver' => 'odbc',
|
||||||
|
'username' => 'root',
|
||||||
|
'password' => '***',
|
||||||
|
'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Database.mdb',
|
||||||
|
));
|
||||||
|
|
||||||
|
|
||||||
|
// connects to SQlite
|
||||||
|
dibi::connect(array(
|
||||||
|
'driver' => 'sqlite',
|
||||||
|
'database' => 'mydb.sdb',
|
||||||
|
));
|
||||||
|
|
||||||
|
|
||||||
|
// connects to PostgreSql
|
||||||
|
dibi::connect(array(
|
||||||
|
'driver' => 'postgre',
|
||||||
|
'string' => 'host=localhost port=5432 dbname=mary',
|
||||||
|
'persistent' => TRUE,
|
||||||
|
));
|
||||||
|
|
||||||
|
|
||||||
} catch (DibiException $e) {
|
} catch (DibiException $e) {
|
||||||
echo "DibiException: " . $e->getMessage();
|
|
||||||
die();
|
echo "DibiException: <pre>", $e;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// connects to mysql
|
|
||||||
dibi::connect(array(
|
|
||||||
'driver' => 'mysql',
|
|
||||||
'host' => 'localhost',
|
|
||||||
'username' => 'root',
|
|
||||||
'password' => 'xxx', // change to real password!
|
|
||||||
'database' => 'test',
|
|
||||||
'charset' => 'utf8',
|
|
||||||
));
|
|
||||||
|
|
||||||
/* connects to ODBC
|
|
||||||
dibi::connect(array(
|
|
||||||
'driver' => 'odbc',
|
|
||||||
'username' => 'root',
|
|
||||||
'password' => '***',
|
|
||||||
'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Database.mdb',
|
|
||||||
));
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* connects to SQlite
|
|
||||||
dibi::connect(array(
|
|
||||||
'driver' => 'sqlite',
|
|
||||||
'database' => 'mydb.sdb',
|
|
||||||
));
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* connects to PostgreSql
|
|
||||||
dibi::connect(array(
|
|
||||||
'driver' => 'postgre',
|
|
||||||
'string' => 'host=localhost port=5432 dbname=mary',
|
|
||||||
'persistent' => TRUE,
|
|
||||||
));
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
// check status
|
|
||||||
if (!dibi::isConnected()) {
|
|
||||||
echo 'dibi::isConnected(): Not connected';
|
|
||||||
} else {
|
|
||||||
echo 'Connected';
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
@@ -4,7 +4,7 @@ require_once '../dibi/dibi.php';
|
|||||||
|
|
||||||
// required since PHP 5.1.0
|
// required since PHP 5.1.0
|
||||||
if (function_exists('date_default_timezone_set'))
|
if (function_exists('date_default_timezone_set'))
|
||||||
date_default_timezone_set('Europe/Prague'); // or 'GMT'
|
date_default_timezone_set('Europe/Prague');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -55,12 +55,13 @@ class TDateTime implements IDibiVariable
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// connects to mysqli
|
// CHANGE TO REAL PARAMETERS!
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
'driver' => 'mysql',
|
'driver' => 'mysql',
|
||||||
'host' => 'localhost',
|
'host' => 'localhost',
|
||||||
'username' => 'root',
|
'username' => 'root',
|
||||||
'password' => 'xxx', // change to real password!
|
'password' => 'xxx',
|
||||||
|
'database' => 'dibi',
|
||||||
'charset' => 'utf8',
|
'charset' => 'utf8',
|
||||||
));
|
));
|
||||||
|
|
||||||
@@ -68,7 +69,7 @@ dibi::connect(array(
|
|||||||
|
|
||||||
// generate and dump SQL
|
// generate and dump SQL
|
||||||
dibi::test("
|
dibi::test("
|
||||||
INSERT INTO [test]", array(
|
INSERT INTO [mytable]", array(
|
||||||
'A' => 12,
|
'A' => 12,
|
||||||
'B' => NULL,
|
'B' => NULL,
|
||||||
'C' => new TDateTime(31542), // using out class
|
'C' => new TDateTime(31542), // using out class
|
||||||
@@ -76,4 +77,3 @@ INSERT INTO [test]", array(
|
|||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
?>
|
|
38
examples/dump.php
Normal file
38
examples/dump.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once '../dibi/dibi.php';
|
||||||
|
|
||||||
|
if (function_exists('date_default_timezone_set'))
|
||||||
|
date_default_timezone_set('Europe/Prague');
|
||||||
|
|
||||||
|
|
||||||
|
// CHANGE TO REAL PARAMETERS!
|
||||||
|
dibi::connect(array(
|
||||||
|
'driver' => 'mysql',
|
||||||
|
'host' => 'localhost',
|
||||||
|
'username' => 'root',
|
||||||
|
'password' => 'xxx',
|
||||||
|
'database' => 'dibi',
|
||||||
|
'charset' => 'utf8',
|
||||||
|
));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$res = dibi::query('SELECT * FROM [mytable]');
|
||||||
|
|
||||||
|
// get last SQL
|
||||||
|
$sql = dibi::$sql;
|
||||||
|
|
||||||
|
|
||||||
|
// dump it
|
||||||
|
echo '<h1>dibi::dump()</h1>';
|
||||||
|
|
||||||
|
dibi::dump($sql);
|
||||||
|
|
||||||
|
|
||||||
|
// dump result table
|
||||||
|
echo '<h1>dibi::dumpResult()</h1>';
|
||||||
|
|
||||||
|
dibi::dumpResult($res);
|
||||||
|
|
||||||
|
|
@@ -4,18 +4,18 @@
|
|||||||
require_once '../dibi/dibi.php';
|
require_once '../dibi/dibi.php';
|
||||||
|
|
||||||
|
|
||||||
// mysql
|
// CHANGE TO REAL PARAMETERS!
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
'driver' => 'mysql',
|
'driver' => 'mysql',
|
||||||
'host' => 'localhost',
|
'host' => 'localhost',
|
||||||
'username' => 'root',
|
'username' => 'root',
|
||||||
'password' => 'xxx', // change to real password!
|
'password' => 'xxx',
|
||||||
'database' => 'test',
|
'database' => 'dibi',
|
||||||
'charset' => 'utf8',
|
'charset' => 'utf8',
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
$res = dibi::query('SELECT * FROM table');
|
$res = dibi::query('SELECT * FROM [mytable]');
|
||||||
if (!$res) die('SQL error');
|
if (!$res) die('SQL error');
|
||||||
|
|
||||||
|
|
||||||
@@ -46,4 +46,3 @@ foreach ($res->getIterator(2, 3) as $row => $fields) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
?>
|
|
@@ -1,4 +1,3 @@
|
|||||||
<pre>
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once '../dibi/dibi.php';
|
require_once '../dibi/dibi.php';
|
||||||
@@ -19,26 +18,56 @@ dibi::$logMode = 'a';
|
|||||||
dibi::$logAll = TRUE;
|
dibi::$logAll = TRUE;
|
||||||
|
|
||||||
|
|
||||||
// mysql
|
|
||||||
|
// CHANGE TO REAL PARAMETERS!
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
'driver' => 'mysql',
|
'driver' => 'mysql',
|
||||||
'host' => 'localhost',
|
'host' => 'localhost',
|
||||||
'username' => 'root',
|
'username' => 'root',
|
||||||
'password' => 'xxx', // change to real password!
|
'password' => 'xxx',
|
||||||
'database' => 'xxx',
|
'database' => 'dibi',
|
||||||
'charset' => 'utf8',
|
'charset' => 'utf8',
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] = %i', 38);
|
// generate user-level errors
|
||||||
|
dibi::$throwExceptions = FALSE;
|
||||||
|
echo '<h1>User-level errors</h1>';
|
||||||
|
|
||||||
|
|
||||||
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] < %i', 38);
|
$res = dibi::query('SELECT * FROM [mytable] WHERE [inumber] = %i', 38);
|
||||||
|
|
||||||
|
|
||||||
$res = dibi::query('SELECT * FROM [*nucleus_item] WHERE [inumber] < %i', 38);
|
$res = dibi::query('SELECT * FROM [mytable] WHERE [inumber] < %i', 38);
|
||||||
|
|
||||||
echo 'See file ', dibi::$logFile;
|
|
||||||
|
|
||||||
?>
|
$res = dibi::query('SELECT FROM [mytable] WHERE [inumber] < %i', 38);
|
||||||
|
|
||||||
|
echo "<br />See file ", dibi::$logFile;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// generate DibiException
|
||||||
|
dibi::$throwExceptions = TRUE;
|
||||||
|
echo '<h1>DibiException</h1>';
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$res = dibi::query('SELECT FROM [mytable] WHERE [inumber] < %i', 38);
|
||||||
|
|
||||||
|
} catch (DibiException $e) {
|
||||||
|
|
||||||
|
echo '<pre>', $e, '</pre>';
|
||||||
|
|
||||||
|
echo '<h2>$e->getSql()</h2>';
|
||||||
|
$sql = $e->getSql();
|
||||||
|
echo "SQL: $sql\n";
|
||||||
|
|
||||||
|
echo '<h2>$e->getDbError()</h2>';
|
||||||
|
$error = $e->getDbError();
|
||||||
|
echo '<pre>';
|
||||||
|
print_r($error);
|
||||||
|
echo '</pre>';
|
||||||
|
|
||||||
|
}
|
@@ -4,18 +4,18 @@
|
|||||||
require_once '../dibi/dibi.php';
|
require_once '../dibi/dibi.php';
|
||||||
|
|
||||||
|
|
||||||
// mysql
|
// CHANGE TO REAL PARAMETERS!
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
'driver' => 'mysqli',
|
'driver' => 'mysql',
|
||||||
'host' => 'localhost',
|
'host' => 'localhost',
|
||||||
'username' => 'root',
|
'username' => 'root',
|
||||||
'password' => 'xxx', // change to real password!
|
'password' => 'xxx',
|
||||||
'database' => 'dgx',
|
'database' => 'dibi',
|
||||||
'charset' => 'utf8',
|
'charset' => 'utf8',
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] <> %i', 38);
|
$res = dibi::query('SELECT * FROM [mytable] WHERE [inumber] <> %i', 38);
|
||||||
if (!$res) die('SQL error');
|
if (!$res) die('SQL error');
|
||||||
|
|
||||||
|
|
||||||
@@ -30,5 +30,3 @@ $res->setType(TRUE);
|
|||||||
$record = $res->fetch();
|
$record = $res->fetch();
|
||||||
var_dump($record);
|
var_dump($record);
|
||||||
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
@@ -11,12 +11,13 @@ if (function_exists('date_default_timezone_set'))
|
|||||||
date_default_timezone_set('Europe/Prague'); // or 'GMT'
|
date_default_timezone_set('Europe/Prague'); // or 'GMT'
|
||||||
|
|
||||||
|
|
||||||
// mysql
|
// CHANGE TO REAL PARAMETERS!
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
'driver' => 'mysql',
|
'driver' => 'mysql',
|
||||||
'host' => 'localhost',
|
'host' => 'localhost',
|
||||||
'username' => 'root',
|
'username' => 'root',
|
||||||
'password' => 'xxx', // change to real password!
|
'password' => 'xxx',
|
||||||
|
'database' => 'dibi',
|
||||||
'charset' => 'utf8',
|
'charset' => 'utf8',
|
||||||
));
|
));
|
||||||
|
|
||||||
@@ -59,16 +60,16 @@ LIMIT 10");
|
|||||||
|
|
||||||
|
|
||||||
// dibi detects INSERT or REPLACE command
|
// dibi detects INSERT or REPLACE command
|
||||||
dibi::test("INSERT INTO [test]", $arr4);
|
dibi::test("INSERT INTO [mytable]", $arr4);
|
||||||
|
|
||||||
|
|
||||||
// dibi detects UPDATE command
|
// dibi detects UPDATE command
|
||||||
$n = 123;
|
$n = 123;
|
||||||
dibi::test("UPDATE [test] SET", $arr4, " WHERE [id]=%i", $n);
|
dibi::test("UPDATE [mytable] SET", $arr4, " WHERE [id]=%i", $n);
|
||||||
|
|
||||||
|
|
||||||
// array with modifier %a - assoc
|
// array with modifier %a - assoc
|
||||||
dibi::test("UPDATE [test] SET%a", $arr4, " WHERE [id]=%i", $n);
|
dibi::test("UPDATE [mytable] SET%a", $arr4, " WHERE [id]=%i", $n);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -7,12 +7,13 @@ pre.dibi { padding-bottom: 10px; }
|
|||||||
require_once '../dibi/dibi.php';
|
require_once '../dibi/dibi.php';
|
||||||
|
|
||||||
|
|
||||||
// mysql
|
// CHANGE TO REAL PARAMETERS!
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
'driver' => 'mysql',
|
'driver' => 'mysql',
|
||||||
'host' => 'localhost',
|
'host' => 'localhost',
|
||||||
'username' => 'root',
|
'username' => 'root',
|
||||||
'password' => 'xxx', // change to real password!
|
'password' => 'xxx',
|
||||||
|
'database' => 'dibi',
|
||||||
'charset' => 'utf8',
|
'charset' => 'utf8',
|
||||||
));
|
));
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ $user = $cond1 ? 'Davidek' : NULL;
|
|||||||
|
|
||||||
dibi::test('
|
dibi::test('
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM [table]
|
FROM [mytable]
|
||||||
%if', isset($user), 'WHERE [user]=%s', $user, '%end'
|
%if', isset($user), 'WHERE [user]=%s', $user, '%end'
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -41,11 +42,11 @@ FROM %if', $cond1, '[one_table] %else [second_table]'
|
|||||||
// nested condition
|
// nested condition
|
||||||
dibi::test('
|
dibi::test('
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM [test]
|
FROM [mytable]
|
||||||
WHERE
|
WHERE
|
||||||
%if', isset($user), '[user]=%s', $user, '
|
%if', isset($user), '[user]=%s', $user, '
|
||||||
%if', $cond2, 'AND [admin]=1 %end
|
%if', $cond2, 'AND [admin]=1 %end
|
||||||
%else LIMIT 10 %end'
|
%else LIMIT 10 %end'
|
||||||
);
|
);
|
||||||
|
|
||||||
?>
|
|
||||||
|
@@ -3,12 +3,13 @@
|
|||||||
require_once '../dibi/dibi.php';
|
require_once '../dibi/dibi.php';
|
||||||
|
|
||||||
|
|
||||||
// connects to mysqli
|
// CHANGE TO REAL PARAMETERS!
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
'driver' => 'mysql',
|
'driver' => 'mysql',
|
||||||
'host' => 'localhost',
|
'host' => 'localhost',
|
||||||
'username' => 'root',
|
'username' => 'root',
|
||||||
'password' => 'xxx', // change to real password!
|
'password' => 'xxx',
|
||||||
|
'database' => 'dibi',
|
||||||
'charset' => 'utf8',
|
'charset' => 'utf8',
|
||||||
));
|
));
|
||||||
|
|
||||||
@@ -29,4 +30,3 @@ dibi::addSubst('', 'my_');
|
|||||||
dibi::test("UPDATE [database.::table] SET [text]='Hello World'");
|
dibi::test("UPDATE [database.::table] SET [text]='Hello World'");
|
||||||
|
|
||||||
|
|
||||||
?>
|
|
@@ -1 +1 @@
|
|||||||
Dibi Version 0.7b
|
Dibi Version 0.7c
|
||||||
|
Reference in New Issue
Block a user