From 98d43e08151651ef3b494b66c863f5b857bbfebb Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 26 Apr 2009 15:35:39 +0000 Subject: [PATCH] - Oracle driver: implemented applyLimit() & getTables() - DibiDataSource: removed key word AS - DibiProfiler: fixed bug with unbuffered queries - DibiTranslator: empty %and generates '1=1' --- dibi/drivers/oracle.php | 22 +++++++++++++++++++--- dibi/libs/DibiDataSource.php | 2 +- dibi/libs/DibiProfiler.php | 10 ++++++++-- dibi/libs/DibiTranslator.php | 2 +- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index e69fc386..68ef16e6 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -263,8 +263,13 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver */ public function applyLimit(&$sql, $limit, $offset) { - if ($limit < 0 && $offset < 1) return; - $sql .= ' LIMIT ' . $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : ''); + if ($offset > 0) { + // see http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html + $sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS __rnum FROM (' . $sql . ') t ' . ($limit >= 0 ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '') . ') WHERE __rnum > '. (int) $offset; + + } elseif ($limit >= 0) { + $sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit; + } } @@ -363,7 +368,18 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver */ public function getTables() { - throw new NotImplementedException; + $this->query('SELECT * FROM cat'); + $res = array(); + while ($row = $this->fetch(FALSE)) { + if ($row[1] === 'TABLE' || $row[1] === 'VIEW') { + $res[] = array( + 'name' => $row[0], + 'view' => $row[1] === 'VIEW', + ); + } + } + $this->free(); + return $res; } diff --git a/dibi/libs/DibiDataSource.php b/dibi/libs/DibiDataSource.php index 6a81733a..1613b9f6 100644 --- a/dibi/libs/DibiDataSource.php +++ b/dibi/libs/DibiDataSource.php @@ -70,7 +70,7 @@ class DibiDataSource extends DibiObject implements IDataSource if (strpos($sql, ' ') === FALSE) { $this->sql = $sql; // table name } else { - $this->sql = '(' . $sql . ') AS t'; // SQL command + $this->sql = '(' . $sql . ') t'; // SQL command } $this->connection = $connection; } diff --git a/dibi/libs/DibiProfiler.php b/dibi/libs/DibiProfiler.php index 70cbe158..09f3cd43 100644 --- a/dibi/libs/DibiProfiler.php +++ b/dibi/libs/DibiProfiler.php @@ -108,11 +108,17 @@ class DibiProfiler extends DibiObject implements IDibiProfiler if (($event & $this->filter) === 0) return; if ($event & self::QUERY) { + try { + $count = $res instanceof DibiResult ? count($res) : '-'; + } catch (Exception $e) { + $count = '?'; + } + if ($this->useFirebug && !headers_sent()) { self::$table[] = array( sprintf('%0.3f', dibi::$elapsedTime * 1000), trim($sql), - $res instanceof DibiResult ? count($res) : '-', + $count, $connection->getConfig('driver') . '/' . $connection->getConfig('name') ); @@ -138,7 +144,7 @@ class DibiProfiler extends DibiObject implements IDibiProfiler if ($this->file) { $this->writeFile( "OK: " . $sql - . ($res instanceof DibiResult ? ";\n-- rows: " . count($res) : '') + . ($res instanceof DibiResult ? ";\n-- rows: " . $count : '') . "\n-- takes: " . sprintf('%0.3f', dibi::$elapsedTime * 1000) . ' ms' . "\n-- driver: " . $connection->getConfig('driver') . '/' . $connection->getConfig('name') . "\n-- " . date('Y-m-d H:i:s') diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index a9fecfd9..575f3cf0 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -210,7 +210,7 @@ final class DibiTranslator extends DibiObject case 'and': case 'or': // key=val AND key IS NULL AND ... if (empty($value)) { - return $this->driver->escape(TRUE, 'b'); + return '1=1'; } foreach ($value as $k => $v) {