From 0c865150763432d46634f39d7668a8a86d95fd43 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 2 Feb 2007 03:51:43 +0000 Subject: [PATCH] * modified DibiException (getDbError, ...) * fix dibi::dumpResult() --- dibi.compact/dibi.compact.php | 37 +++++++-------- dibi/dibi.php | 36 +++++++-------- dibi/libs/driver.php | 4 +- dibi/libs/exception.php | 35 ++++++++++----- dibi/libs/resultset.php | 8 ++++ dibi/libs/translator.php | 6 +-- examples/connect.php | 84 +++++++++++++++++------------------ examples/date.type.demo.php | 10 ++--- examples/dump.php | 38 ++++++++++++++++ examples/fetch.php | 9 ++-- examples/logger.php | 47 ++++++++++++++++---- examples/metatypes.php | 12 +++-- examples/sql-builder.php | 11 ++--- examples/sql-condition.php | 11 ++--- examples/table-prefix.php | 6 +-- version.txt | 2 +- 16 files changed, 218 insertions(+), 138 deletions(-) create mode 100644 examples/dump.php diff --git a/dibi.compact/dibi.compact.php b/dibi.compact/dibi.compact.php index a055ca35..ba8da5ce 100644 --- a/dibi.compact/dibi.compact.php +++ b/dibi.compact/dibi.compact.php @@ -13,11 +13,11 @@ * @license GNU GENERAL PUBLIC LICENSE v2 * @package dibi * @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 DibiDriver{protected$config;public$formats=array('TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);static 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 function 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){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 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 __destruct(){@$this->free();}public function @@ -163,9 +165,7 @@ function __construct($driver,$subst){$this->driver=$driver;$this->subK=array_keys($subst);$this->subV=array_values($subst);}public 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 -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 -new -DibiException('Errors during generating SQL',array('sql'=>$sql));return$sql;}private +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 function 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 @@ -195,14 +195,15 @@ function __isset($nm){$this->__get($nm);}}class DibiException extends -Exception{private$info;public +Exception{private$sql,$dbError;public 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 -getSql(){return -isset($this->info['sql'])?$this->info['sql']:NULL;}public +getSql(){return$this->sql;}public 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 IDibiVariable{public function @@ -246,13 +247,13 @@ function 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 new -DibiException('SQL generate error',array('sql'=>$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 +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();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 -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 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 function test($args){if(!is_array($args))$args=func_get_args();$trans=new @@ -270,10 +271,10 @@ function dumpHighlight($matches){if(!empty($matches[1]))return''.$matches[1].'';if(!empty($matches[2]))return''.$matches[2].'';if(!empty($matches[3]))return''.$matches[3].'';if(!empty($matches[4]))return''.$matches[4].'';}static public 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='
'.$sql."
\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='
'.$sql."
\n";if(!$return)echo$sql;return$sql;}static public function -dumpResult(DibiResult$res){echo'';echo'';$fieldCount=$res->fieldCount();for($i=0;$i<$fieldCount;$i++){$info=$res->fieldMeta($i);echo'';}echo'';foreach($res +dumpResult(DibiResult$res){echo'
Row'.htmlSpecialChars($info['name']).'
';echo'';foreach($res->getFields()as$field)echo'';echo'';foreach($res as$row=>$fields){echo'';foreach($fields as$field){if(is_object($field))$field=$field->__toString();echo'';}echo'';}echo'
#row'.$field.'
',$row,'',htmlSpecialChars($field),'
';}static public diff --git a/dibi/dibi.php b/dibi/dibi.php index 9b2e4a4e..c52dd096 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -14,11 +14,11 @@ * @license GNU GENERAL PUBLIC LICENSE v2 * @package dibi * @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', '<')) @@ -143,7 +143,7 @@ class dibi * @param array|string connection parameters * @param string connection name * @return void - * @throw DibiException + * @throw DibiException */ static public function connect($config, $name = '1') { @@ -189,7 +189,7 @@ class dibi * Retrieve active connection * * @return object DibiDriver object. - * @throw DibiException + * @throw DibiException */ static public function getConnection() { @@ -206,7 +206,7 @@ class dibi * * @param string connection registy name * @return void - * @throw DibiException + * @throw DibiException */ static public function activate($name) { @@ -227,7 +227,7 @@ class dibi * * @param array|mixed one or more arguments * @return int|DibiResult - * @throw DibiException + * @throw DibiException */ static public function query($args) { @@ -248,7 +248,7 @@ class dibi ); if (dibi::$throwExceptions) - throw new DibiException('SQL generate error', array('sql' => $trans->sql)); + throw new DibiException('SQL generate error', NULL, $trans->sql); else { trigger_error("dibi: SQL generate error: $trans->sql", E_USER_WARNING); return FALSE; @@ -264,8 +264,9 @@ class dibi if ($res === FALSE) { // query error if (self::$logFile) { // log to file $info = $conn->errorInfo(); + if ($info['code']) $info['message'] = "[$info[code]] $info[message]"; self::log( - "ERROR: [$info[code]] $info[message]" + "ERROR: $info[message]" . "\n-- SQL: " . self::$sql . ";\n-- " . date('Y-m-d H:i:s ') ); @@ -273,11 +274,11 @@ class dibi if (dibi::$throwExceptions) { $info = $conn->errorInfo(); - $info['sql'] = self::$sql; - throw new DibiException('Query error', $info); + throw new DibiException('Query error', $info, self::$sql); } else { $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; } } @@ -290,7 +291,7 @@ class dibi "OK: " . self::$sql . ";\n-- result: $msg" . "\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 $sql = preg_replace_callback("#(/\*.+?\*/)|(\*\*.+?\*\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#", array('dibi', 'dumpHighlight'), $sql); - $sql = '
' . $sql . "
\n"; + $sql = '
' . $sql . "
\n"; // print & return if (!$return) echo $sql; @@ -407,12 +408,9 @@ class dibi static public function dumpResult(DibiResult $res) { echo ''; - echo ''; - $fieldCount = $res->fieldCount(); - for ($i = 0; $i < $fieldCount; $i++) { - $info = $res->fieldMeta($i); - echo ''; - } + echo ''; + foreach ($res->getFields() as $field) + echo ''; echo ''; foreach ($res as $row => $fields) { diff --git a/dibi/libs/driver.php b/dibi/libs/driver.php index f5d2b97a..d879edbc 100644 --- a/dibi/libs/driver.php +++ b/dibi/libs/driver.php @@ -49,9 +49,9 @@ abstract class DibiDriver /** * DibiDriver factory: creates object and connects to a database * - * @param array connect configuration + * @param array connect configuration * @return DibiDriver - * @throw DibiException + * @throw DibiException */ /*abstract disallowed since PHP 5.2*/ static public function connect($config) {} diff --git a/dibi/libs/exception.php b/dibi/libs/exception.php index 5ac25c30..9634a8c7 100644 --- a/dibi/libs/exception.php +++ b/dibi/libs/exception.php @@ -28,31 +28,44 @@ if (!defined('DIBI')) die(); class DibiException extends Exception { private - $info; + $sql, + $dbError; - public function __construct($message, $info=NULL) { - - $this->info = $info; - - if (isset($info['message'])) - $message = "$message: $info[message]"; - + public function __construct($message, $dbError=NULL, $sql=NULL) + { + $this->dbError = $dbError; + $this->sql = $sql; parent::__construct($message); } public function getSql() { - return isset($this->info['sql']) ? $this->info['sql'] : NULL; + return $this->sql; + } + + + public function getDbError() + { + return $this->dbError; } public function __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; } diff --git a/dibi/libs/resultset.php b/dibi/libs/resultset.php index 973c18d5..73e5ec6a 100644 --- a/dibi/libs/resultset.php +++ b/dibi/libs/resultset.php @@ -200,6 +200,11 @@ abstract class DibiResult implements IteratorAggregate, Countable return array(); // empty resultset $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(); do { // make associative arrays @@ -234,6 +239,9 @@ abstract class DibiResult implements IteratorAggregate, Countable if (!$rec) return array(); // empty resultset + if (!array_key_exists($key, $rec) || + !array_key_exists($value, $rec)) return FALSE; + $arr = array(); do { $arr[ $rec[$key] ] = $rec[$value]; diff --git a/dibi/libs/translator.php b/dibi/libs/translator.php index 98f0c6bd..99307d35 100644 --- a/dibi/libs/translator.php +++ b/dibi/libs/translator.php @@ -52,7 +52,7 @@ class DibiTranslator * * @param array * @return string - * @throw DibiException + * @throw DibiException */ public function translate($args) { @@ -115,10 +115,6 @@ class DibiTranslator $this->sql = $sql; return !$this->hasError; - if ($this->hasError) - throw new DibiException('Errors during generating SQL', array('sql' => $sql)); - - return $sql; } diff --git a/examples/connect.php b/examples/connect.php index bc782136..296b1fe2 100644 --- a/examples/connect.php +++ b/examples/connect.php @@ -4,55 +4,53 @@ require_once '../dibi/dibi.php'; try { - // connects using DSN + + // connects to MySQL using DSN 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) { - echo "DibiException: " . $e->getMessage(); - die(); + + echo "DibiException:
", $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';
-}
 
-?>
\ No newline at end of file
diff --git a/examples/date.type.demo.php b/examples/date.type.demo.php
index b2d6f388..eceeb196 100644
--- a/examples/date.type.demo.php
+++ b/examples/date.type.demo.php
@@ -4,7 +4,7 @@ require_once '../dibi/dibi.php';
 
 // required since PHP 5.1.0
 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(
     'driver'   => 'mysql',
     'host'     => 'localhost',
     'username' => 'root',
-    'password' => 'xxx',  // change to real password!
+    'password' => 'xxx',
+    'database' => 'dibi',
     'charset'  => 'utf8',
 ));
 
@@ -68,7 +69,7 @@ dibi::connect(array(
 
 // generate and dump SQL
 dibi::test("
-INSERT INTO [test]", array(
+INSERT INTO [mytable]", array(
     'A' => 12,
     'B' => NULL,
     'C' => new TDateTime(31542),  // using out class
@@ -76,4 +77,3 @@ INSERT INTO [test]", array(
 ));
 
 
-?>
\ No newline at end of file
diff --git a/examples/dump.php b/examples/dump.php
new file mode 100644
index 00000000..c99b3159
--- /dev/null
+++ b/examples/dump.php
@@ -0,0 +1,38 @@
+ '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 '

dibi::dump()

'; + +dibi::dump($sql); + + +// dump result table +echo '

dibi::dumpResult()

'; + +dibi::dumpResult($res); + + diff --git a/examples/fetch.php b/examples/fetch.php index fb1ff849..b2ec10a7 100644 --- a/examples/fetch.php +++ b/examples/fetch.php @@ -4,18 +4,18 @@ require_once '../dibi/dibi.php'; -// mysql +// CHANGE TO REAL PARAMETERS! dibi::connect(array( 'driver' => 'mysql', 'host' => 'localhost', 'username' => 'root', - 'password' => 'xxx', // change to real password! - 'database' => 'test', + 'password' => 'xxx', + 'database' => 'dibi', 'charset' => 'utf8', )); -$res = dibi::query('SELECT * FROM table'); +$res = dibi::query('SELECT * FROM [mytable]'); if (!$res) die('SQL error'); @@ -46,4 +46,3 @@ foreach ($res->getIterator(2, 3) as $row => $fields) { -?> \ No newline at end of file diff --git a/examples/logger.php b/examples/logger.php index 9567f1b3..927c9d69 100644 --- a/examples/logger.php +++ b/examples/logger.php @@ -1,4 +1,3 @@ -
  'mysql',
     'host'     => 'localhost',
     'username' => 'root',
-    'password' => 'xxx',  // change to real password!
-    'database' => 'xxx',
+    'password' => 'xxx',
+    'database' => 'dibi',
     'charset'  => 'utf8',
 ));
 
 
 
-$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] = %i', 38);
+// generate user-level errors
+dibi::$throwExceptions = FALSE;
+echo '

User-level errors

'; -$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; -?> \ No newline at end of file +$res = dibi::query('SELECT FROM [mytable] WHERE [inumber] < %i', 38); + +echo "
See file ", dibi::$logFile; + + + +// generate DibiException +dibi::$throwExceptions = TRUE; +echo '

DibiException

'; + +try { + + $res = dibi::query('SELECT FROM [mytable] WHERE [inumber] < %i', 38); + +} catch (DibiException $e) { + + echo '
', $e, '
'; + + echo '

$e->getSql()

'; + $sql = $e->getSql(); + echo "SQL: $sql\n"; + + echo '

$e->getDbError()

'; + $error = $e->getDbError(); + echo '
';
+    print_r($error);
+    echo '
'; + +} \ No newline at end of file diff --git a/examples/metatypes.php b/examples/metatypes.php index 97cdd2bb..305bb0bc 100644 --- a/examples/metatypes.php +++ b/examples/metatypes.php @@ -4,18 +4,18 @@ require_once '../dibi/dibi.php'; -// mysql +// CHANGE TO REAL PARAMETERS! dibi::connect(array( - 'driver' => 'mysqli', + 'driver' => 'mysql', 'host' => 'localhost', 'username' => 'root', - 'password' => 'xxx', // change to real password! - 'database' => 'dgx', + 'password' => 'xxx', + 'database' => 'dibi', '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'); @@ -30,5 +30,3 @@ $res->setType(TRUE); $record = $res->fetch(); var_dump($record); - -?> diff --git a/examples/sql-builder.php b/examples/sql-builder.php index a1f5e4eb..57f911b6 100644 --- a/examples/sql-builder.php +++ b/examples/sql-builder.php @@ -11,12 +11,13 @@ if (function_exists('date_default_timezone_set')) date_default_timezone_set('Europe/Prague'); // or 'GMT' -// mysql +// CHANGE TO REAL PARAMETERS! dibi::connect(array( 'driver' => 'mysql', 'host' => 'localhost', 'username' => 'root', - 'password' => 'xxx', // change to real password! + 'password' => 'xxx', + 'database' => 'dibi', 'charset' => 'utf8', )); @@ -59,16 +60,16 @@ LIMIT 10"); // dibi detects INSERT or REPLACE command -dibi::test("INSERT INTO [test]", $arr4); +dibi::test("INSERT INTO [mytable]", $arr4); // dibi detects UPDATE command $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 -dibi::test("UPDATE [test] SET%a", $arr4, " WHERE [id]=%i", $n); +dibi::test("UPDATE [mytable] SET%a", $arr4, " WHERE [id]=%i", $n); diff --git a/examples/sql-condition.php b/examples/sql-condition.php index f6b51a12..37d3aea2 100644 --- a/examples/sql-condition.php +++ b/examples/sql-condition.php @@ -7,12 +7,13 @@ pre.dibi { padding-bottom: 10px; } require_once '../dibi/dibi.php'; -// mysql +// CHANGE TO REAL PARAMETERS! dibi::connect(array( 'driver' => 'mysql', 'host' => 'localhost', 'username' => 'root', - 'password' => 'xxx', // change to real password! + 'password' => 'xxx', + 'database' => 'dibi', 'charset' => 'utf8', )); @@ -26,7 +27,7 @@ $user = $cond1 ? 'Davidek' : NULL; dibi::test(' SELECT * -FROM [table] +FROM [mytable] %if', isset($user), 'WHERE [user]=%s', $user, '%end' ); @@ -41,11 +42,11 @@ FROM %if', $cond1, '[one_table] %else [second_table]' // nested condition dibi::test(' SELECT * -FROM [test] +FROM [mytable] WHERE %if', isset($user), '[user]=%s', $user, ' %if', $cond2, 'AND [admin]=1 %end %else LIMIT 10 %end' ); -?> + diff --git a/examples/table-prefix.php b/examples/table-prefix.php index 5c53d2d3..16bb257d 100644 --- a/examples/table-prefix.php +++ b/examples/table-prefix.php @@ -3,12 +3,13 @@ require_once '../dibi/dibi.php'; -// connects to mysqli +// CHANGE TO REAL PARAMETERS! dibi::connect(array( 'driver' => 'mysql', 'host' => 'localhost', 'username' => 'root', - 'password' => 'xxx', // change to real password! + 'password' => 'xxx', + 'database' => 'dibi', 'charset' => 'utf8', )); @@ -29,4 +30,3 @@ dibi::addSubst('', 'my_'); dibi::test("UPDATE [database.::table] SET [text]='Hello World'"); -?> \ No newline at end of file diff --git a/version.txt b/version.txt index c962eef5..97934c05 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -Dibi Version 0.7b +Dibi Version 0.7c
Row'.htmlSpecialChars($info['name']).'#row' . $field . '