diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php
index 8a13cd37..ef4e31bb 100644
--- a/dibi/drivers/postgre.php
+++ b/dibi/drivers/postgre.php
@@ -409,6 +409,11 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
*/
public function getTables()
{
+ $version = pg_version($this->connection);
+ if ($version['server'] < 8) {
+ throw new NotSupportedException('Reflection requires PostgreSQL 8.');
+ }
+
$this->query("
SELECT table_name as name, CAST(table_type = 'VIEW' AS INTEGER) as view
FROM information_schema.tables
@@ -431,8 +436,9 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
$_table = $this->escape($table, dibi::FIELD_TEXT);
$this->query("
SELECT indkey
- FROM pg_index, pg_class
- WHERE pg_class.relname = $_table AND pg_class.oid = pg_index.indrelid AND pg_index.indisprimary
+ FROM pg_class
+ LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid AND pg_index.indisprimary
+ WHERE pg_class.relname = $_table
");
$primary = (int) pg_fetch_object($this->resultSet)->indkey;
@@ -469,7 +475,38 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
*/
public function getIndexes($table)
{
- throw new NotImplementedException;
+ $_table = $this->escape($table, dibi::FIELD_TEXT);
+ $this->query("
+ SELECT ordinal_position, column_name
+ FROM information_schema.columns
+ WHERE table_name = $_table AND table_schema = current_schema()
+ ORDER BY ordinal_position
+ ");
+
+ $columns = array();
+ while ($row = $this->fetch(TRUE)) {
+ $columns[$row['ordinal_position']] = $row['column_name'];
+ }
+
+ $this->query("
+ SELECT pg_class2.relname, indisunique, indisprimary, indkey
+ FROM pg_class
+ LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid
+ INNER JOIN pg_class as pg_class2 on pg_class2.oid = pg_index.indexrelid
+ WHERE pg_class.relname = $_table
+ ");
+
+ $res = array();
+ while ($row = $this->fetch(TRUE)) {
+ $res[$row['relname']]['name'] = $row['relname'];
+ $res[$row['relname']]['unique'] = $row['indisunique'] === 't';
+ $res[$row['relname']]['primary'] = $row['indisprimary'] === 't';
+ foreach (explode(' ', $row['indkey']) as $index) {
+ $res[$row['relname']]['columns'][] = $columns[$index];
+ }
+ }
+ $this->free();
+ return array_values($res);
}
diff --git a/dibi/libs/DibiTable.php b/dibi/libs/DibiTable.php
index d624c914..f03f865b 100644
--- a/dibi/libs/DibiTable.php
+++ b/dibi/libs/DibiTable.php
@@ -52,7 +52,7 @@ abstract class DibiTable extends DibiObject
/** @var array */
protected $blankRow = array();
- /** @var array of pairs [type, format] */
+ /** @var mixed; TRUE means autodetect, or array of pairs [type, format] */
protected $types = array();
@@ -325,7 +325,12 @@ abstract class DibiTable extends DibiObject
*/
protected function complete($res)
{
- $res->setTypes($this->types);
+ if (is_array($this->types)) {
+ $res->setTypes($this->types);
+
+ } elseif ($this->types === TRUE) {
+ $res->detectTypes();
+ }
return $res;
}
diff --git a/examples/Nette/Debug.php b/examples/Nette/Debug.php
new file mode 100644
index 00000000..e704468b
--- /dev/null
+++ b/examples/Nette/Debug.php
@@ -0,0 +1,609 @@
+file=$file;$this->line=$line;$this->context=$context;}}final
+class
+Framework{const
+VERSION='0.8';const
+REVISION='106 released on 2008/10/27 11:13:57';final
+public
+function
+__construct(){throw
+new
+LogicException("Cannot instantiate static class ".get_class($this));}public
+static
+function
+compareVersion($version){return
+version_compare($version,self::VERSION);}public
+static
+function
+promo($xhtml=TRUE){echo'':'>'),'';}}final
+class
+Debug{public
+static$counters=array();public
+static$html;public
+static$maxDepth=3;public
+static$maxLen=150;private
+static$enabled=FALSE;public
+static$useFirebug;private
+static$logFile;private
+static$logHandle;private
+static$sendEmails;private
+static$emailHeaders=array('To'=>'','From'=>'noreply@%host%','X-Mailer'=>'Nette Framework','Subject'=>'PHP: An error occurred on the server %host%','Body'=>'[%date%]');public
+static$mailer=array(__CLASS__,'sendEmail');public
+static$emailProbability=0.01;public
+static$keysToHide=array('password','passwd','pass','pwd','creditcard','credit card','cc','pin');private
+static$colophons=array(array(__CLASS__,'getDefaultColophons'));private
+static$keyFilter=array();public
+static$time;private
+static$fireCounter;const
+LOG='LOG';const
+INFO='INFO';const
+WARN='WARN';const
+ERROR='ERROR';final
+public
+function
+__construct(){throw
+new
+LogicException("Cannot instantiate static class ".get_class($this));}public
+static
+function
+dump($var,$return=FALSE){self::$keyFilter=FALSE;$output="
".self::_dump($var,0)."\n";if(!self::$html){$output=htmlspecialchars_decode(strip_tags($output),ENT_NOQUOTES);}if($return){return$output;}else{echo$output;return$var;}}private +static +function +_dump(&$var,$level){if(is_bool($var)){return"bool(".($var?'TRUE':'FALSE').")\n";}elseif($var===NULL){return"NULL\n";}elseif(is_int($var)){return"int($var)\n";}elseif(is_float($var)){return"float($var)\n";}elseif(is_string($var)){if(self::$maxLen&&strlen($var)>self::$maxLen){$s=htmlSpecialChars(substr($var,0,self::$maxLen),ENT_NOQUOTES).' ... ';}else{$s=htmlSpecialChars($var,ENT_NOQUOTES);}return"string(".strlen($var).") \"$s\"\n";}elseif(is_array($var)){$s="array(".count($var).") {\n";$space=str_repeat(' ',$level);static$marker;if($marker===NULL)$marker=uniqid("\x00",TRUE);if(isset($var[$marker])){$s.="$space *RECURSION*\n";}elseif($level
getMessage())?>
+getMessage())?>
+File: getFile())?> Line: getLine()?>
+getFile(),$ex->getLine())?>+ + + + + + +
+ + + ',htmlSpecialChars(basename($row['file'])),' (',$row['line'],')'?> + + <PHP inner-code> + + + ')">source ▶ + + + + + (')">arguments ▶) +
+ + +',(isset($params[$k])?'$'.$params[$k]->name:"#$k"),' | ';echo'',self::safeDump($v,isset($params[$k])?$params[$k]->name:NULL)," |
">+ + +
$',htmlspecialchars($k),' | ',self::safeDump($v,$k)," |
',htmlspecialchars($k),' | ',self::safeDump($v,$k)," |
',htmlspecialchars($v)," | \n";}?> +
empty
+ +',htmlspecialchars($k),' | ',self::dump($v,TRUE)," |
',htmlspecialchars($k),' | ',htmlspecialchars($v)," |
empty
+ +',htmlspecialchars($k),' | ',self::dump($v,TRUE)," |
';?>+ +
no headers
+ + + + +".self::_dump($var,0)."\n";}public +static +function +enableProfiler(){register_shutdown_function(array(__CLASS__,'paintProfiler'));}public +static +function +paintProfiler(){$colophons=self::$colophons;if(self::$useFirebug){foreach(self::$colophons +as$callback){foreach((array)call_user_func($callback,'profiler')as$line)self::fireLog(strip_tags($line));}}if(!isset($_SERVER['HTTP_X_REQUESTED_WITH'])||$_SERVER['HTTP_X_REQUESTED_WITH']!=='XMLHttpRequest'){?> + + + + + +
dibi::connect() example IDibiVariable example primary\n"; // Finds rows by primary key foreach ($products->find(1, 3) as $row) { - print_r($row); + Debug::dump($row); } @@ -61,26 +62,26 @@ $count = $products->delete(1); // deletes multiple rows $count = $products->delete(array(1, 2, 3)); -var_dump($count); // number of deleted rows +Debug::dump($count); // number of deleted rows // update row #2 in a table $data = (object) NULL; $data->title = 'New title'; $count = $products->update(2, $data); -var_dump($count); // number of updated rows +Debug::dump($count); // number of updated rows // update multiple rows in a table $count = $products->update(array(3, 5), $data); -var_dump($count); // number of updated rows +Debug::dump($count); // number of updated rows // inserts row into a table $data = array(); $data['title'] = 'New product'; $id = $products->insert($data); -var_dump($id); // generated id +Debug::dump($id); // generated id // inserts or updates row into a table @@ -97,4 +98,4 @@ $products->delete($key); // select all using fluent interface -var_dump($products->select('*')->orderBy('title')->fetchAll()); +Debug::dump($products->select('*')->orderBy('title')->fetchAll()); diff --git a/examples/dump.php b/examples/dump.php index 8e06ee0e..d3a9a6a9 100644 --- a/examples/dump.php +++ b/examples/dump.php @@ -1,6 +1,7 @@dibi dump example
fetchShuffle(); -print_r($all); +Debug::dump($all); diff --git a/examples/fetch.php b/examples/fetch.php index dd7a5150..6c67de55 100644 --- a/examples/fetch.php +++ b/examples/fetch.php @@ -2,6 +2,7 @@'; // fetch a single value $value = dibi::fetchSingle('SELECT [title] FROM [products]'); -print_r($value); // Chair +Debug::dump($value); // Chair echo '
'; // fetch complete result set $all = dibi::fetchAll('SELECT * FROM [products]'); -print_r($all); +Debug::dump($all); echo '
'; // fetch complete result set like association array $res = dibi::query('SELECT * FROM [products]'); $assoc = $res->fetchAssoc('title'); // key -print_r($assoc); +Debug::dump($assoc); echo '
'; // fetch complete result set like pairs key => value $pairs = $res->fetchPairs('product_id', 'title'); -print_r($pairs); +Debug::dump($pairs); echo '
'; // fetch row by row foreach ($res as $n => $row) { - print_r($row); + Debug::dump($row); } echo '
'; // fetch row by row with defined offset foreach ($res->getIterator(2) as $n => $row) { - print_r($row); + Debug::dump($row); } // fetch row by row with defined offset and limit foreach ($res->getIterator(2, 1) as $n => $row) { - print_r($row); + Debug::dump($row); } @@ -81,13 +82,13 @@ INNER JOIN [customers] USING ([customer_id]) '); $assoc = $res->fetchAssoc('customers.name,products.title'); // key -print_r($assoc); +Debug::dump($assoc); echo '
'; $assoc = $res->fetchAssoc('customers.name,#,products.title'); // key -print_r($assoc); +Debug::dump($assoc); echo '
'; $assoc = $res->fetchAssoc('customers.name,=,products.title'); // key -print_r($assoc); +Debug::dump($assoc); echo '
'; diff --git a/examples/fluent.test.php b/examples/fluent.test.php index c7aad120..9aa4b7c4 100644 --- a/examples/fluent.test.php +++ b/examples/fluent.test.php @@ -1,6 +1,7 @@dibi dump example
dibi logger example setType('customer_id', Dibi::FIELD_INTEGER); $res->setType('added', Dibi::FIELD_DATETIME, 'H:i j.n.Y'); $row = $res->fetch(); -var_dump($row); +Debug::dump($row); // outputs: // object(DibiRow)#3 (3) { // customer_id => int(1) diff --git a/examples/nette-debug.php b/examples/nette-debug.php index 4767df9c..f93c568e 100644 --- a/examples/nette-debug.php +++ b/examples/nette-debug.php @@ -1,7 +1,7 @@Nette::Debug & dibi example
-Dibi can display and log exceptions via Nette::Debug. You first need to install Nette Framework. You can download it from here:
+Dibi can display and log exceptions via Nette::Debug, part of Nette Framework.
Last query:
diff --git a/examples/sql-builder.php b/examples/sql-builder.php index 739fa01a..7b286e2d 100644 --- a/examples/sql-builder.php +++ b/examples/sql-builder.php @@ -5,6 +5,7 @@ pre.dibi { padding-bottom: 10px; }dibi prefix & substitute example