diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 6572a3d2..885c3be0 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -242,12 +242,8 @@ class DibiConnection extends DibiObject { $args = func_get_args(); $this->connect(); - $trans = new DibiTranslator($this->driver); - if ($trans->translate($args)) { - return $this->nativeQuery($trans->sql); - } else { - throw new DibiException('SQL translate error: ' . $trans->sql); - } + $translator = new DibiTranslator($this->driver); + return $this->nativeQuery($translator->translate($args)); } @@ -262,12 +258,8 @@ class DibiConnection extends DibiObject { $args = func_get_args(); $this->connect(); - $trans = new DibiTranslator($this->driver); - if ($trans->translate($args)) { - return $trans->sql; - } else { - throw new DibiException('SQL translate error: ' . $trans->sql); - } + $translator = new DibiTranslator($this->driver); + return $translator->translate($args); } @@ -281,10 +273,31 @@ class DibiConnection extends DibiObject { $args = func_get_args(); $this->connect(); - $trans = new DibiTranslator($this->driver); - $ok = $trans->translate($args); - dibi::dump($trans->sql); - return $ok; + try { + $translator = new DibiTranslator($this->driver); + dibi::dump($translator->translate($args)); + return TRUE; + + } catch (DibiException $e) { + dibi::dump($e->getSql()); + return FALSE; + } + } + + + + /** + * Generates (translates) and returns SQL query as DibiDataSource. + * @param array|mixed one or more arguments + * @return DibiDataSource + * @throws DibiException + */ + final public function dataSource($args) + { + $args = func_get_args(); + $this->connect(); + $translator = new DibiTranslator($this->driver); + return new DibiDataSource($translator->translate($args), $this); } diff --git a/dibi/libs/DibiDataSource.php b/dibi/libs/DibiDataSource.php index 8b328b18..20ad30e6 100644 --- a/dibi/libs/DibiDataSource.php +++ b/dibi/libs/DibiDataSource.php @@ -20,26 +20,12 @@ -/** - * Provides an interface between a dataset and data-aware components. - * @package dibi - * @deprecated - */ -interface IDataSource extends Countable, IteratorAggregate -{ - //function IteratorAggregate::getIterator(); - //function Countable::count(); -} - - - /** * Default implementation of IDataSource for dibi. * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl * @package dibi - * @deprecated */ class DibiDataSource extends DibiObject implements IDataSource { diff --git a/dibi/libs/DibiException.php b/dibi/libs/DibiException.php index 70524b4b..27ae3848 100644 --- a/dibi/libs/DibiException.php +++ b/dibi/libs/DibiException.php @@ -27,31 +27,14 @@ * @copyright Copyright (c) 2005, 2009 David Grudl * @package dibi */ -class DibiException extends Exception +class DibiException extends Exception implements /*Nette\*/IDebuggable { -} - - - - -/** - * database server exception. - * - * @author David Grudl - * @copyright Copyright (c) 2005, 2009 David Grudl - * @package dibi - */ -class DibiDriverException extends DibiException implements /*Nette\*/IDebuggable -{ - /** @var string */ - private static $errorMsg; - /** @var string */ private $sql; /** - * Construct an dibi driver exception. + * Construct an dibi exception. * @param string Message describing the exception * @param int Some code * @param string SQL command @@ -104,12 +87,30 @@ class DibiDriverException extends DibiException implements /*Nette\*/IDebuggable return $panels; } +} + + +/** + * database server exception. + * + * @author David Grudl + * @copyright Copyright (c) 2005, 2009 David Grudl + * @package dibi + */ +class DibiDriverException extends DibiException +{ + /********************* error catching ****************d*g**/ + /** @var string */ + private static $errorMsg; + + + /** * Starts catching potential errors/warnings * @return void diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index 8259986b..2f28a16e 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -27,7 +27,7 @@ * @copyright Copyright (c) 2005, 2009 David Grudl * @package dibi */ -class DibiFluent extends DibiObject implements Countable, IteratorAggregate +class DibiFluent extends DibiObject implements IDataSource { /** @var array */ public static $masks = array( @@ -133,7 +133,7 @@ class DibiFluent extends DibiObject implements Countable, IteratorAggregate } elseif (is_string(key($arg))) { // associative array $args = array('%a', $arg); } - } + } // case $arg === FALSE is handled below } if (array_key_exists($clause, $this->clauses)) { @@ -413,6 +413,16 @@ class DibiFluent extends DibiObject implements Countable, IteratorAggregate + /** + * @return DibiDataSource + */ + public function toDataSource() + { + return new DibiDataSource($this->connection->sql($this->_export()), $this->connection); + } + + + /** * Returns SQL query. * @return string diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index fb25350e..6d2e1430 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -40,7 +40,7 @@ * @copyright Copyright (c) 2005, 2009 David Grudl * @package dibi */ -class DibiResult extends DibiObject implements Countable, IteratorAggregate +class DibiResult extends DibiObject implements IDataSource { /** @var array IDibiDriver */ private $driver; diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index 43d79272..0b249693 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -29,9 +29,6 @@ */ final class DibiTranslator extends DibiObject { - /** @var string */ - public $sql; - /** @var IDibiDriver */ private $driver; @@ -81,7 +78,8 @@ final class DibiTranslator extends DibiObject /** * Generates SQL. * @param array - * @return bool + * @return string + * @throws DibiException */ public function translate(array $args) { @@ -174,13 +172,16 @@ final class DibiTranslator extends DibiObject $sql = implode(' ', $sql); + if ($this->hasError) { + throw new DibiException('SQL translate error', 0, $sql); + } + // apply limit if ($this->limit > -1 || $this->offset > 0) { $this->driver->applyLimit($sql, $this->limit, $this->offset); } - $this->sql = $sql; - return !$this->hasError; + return $sql; } @@ -197,15 +198,14 @@ final class DibiTranslator extends DibiObject if (is_array($value) || $value instanceof ArrayObject) { $vx = $kx = array(); - $operator = ', '; switch ($modifier) { case 'and': case 'or': // key=val AND key IS NULL AND ... - $operator = ' ' . strtoupper($modifier) . ' '; if (empty($value)) { return '1'; + } - } else foreach ($value as $k => $v) { + foreach ($value as $k => $v) { if (is_string($k)) { $pair = explode('%', $k, 2); // split into identifier & modifier $k = $this->delimite($pair[0]) . ' '; @@ -225,12 +225,12 @@ final class DibiTranslator extends DibiObject $vx[] = $this->formatValue($v, 'sql'); } } - return implode($operator, $vx); + return implode(' ' . strtoupper($modifier) . ' ', $vx); case 'n': // identifier names foreach ($value as $k => $v) { if (is_string($k)) { - $vx[] = $this->delimite($k) . ' AS ' . $v; + $vx[] = $this->delimite($k) . (empty($v) ? '' : ' AS ' . $v); } else { $vx[] = $this->delimite($v); } @@ -244,7 +244,7 @@ final class DibiTranslator extends DibiObject $vx[] = $this->delimite($pair[0]) . '=' . $this->formatValue($v, isset($pair[1]) ? $pair[1] : FALSE); } - return implode($operator, $vx); + return implode(', ', $vx); case 'l': // (val, val, ...) @@ -276,8 +276,7 @@ final class DibiTranslator extends DibiObject case 'sql': $translator = new self($this->driver); - $translator->translate($value); - return $translator->sql; + return $translator->translate($value); default: // value, value, value - all with the same modifier foreach ($value as $v) { @@ -357,7 +356,6 @@ final class DibiTranslator extends DibiObject case 'a': case 'l': case 'v': - case 'by': $this->hasError = TRUE; return '**Unexpected type ' . gettype($value) . '**'; diff --git a/dibi/libs/interfaces.php b/dibi/libs/interfaces.php index 36a6e6c1..03155c48 100644 --- a/dibi/libs/interfaces.php +++ b/dibi/libs/interfaces.php @@ -39,6 +39,20 @@ interface IDibiVariable +/** + * Provides an interface between a dataset and data-aware components. + * @package dibi + */ +interface IDataSource extends Countable, IteratorAggregate +{ + //function IteratorAggregate::getIterator($offset = NULL, $limit = NULL); + //function Countable::count(); +} + + + + + /** * Defines method that must profiler implement. * @package dibi