diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 25a19a6..7c0d37e 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -239,6 +239,26 @@ class DibiConnection extends DibiObject + /** + * Generates and returns SQL query. + * @param array|mixed one or more arguments + * @return string + * @throws DibiException + */ + final public function sql($args) + { + $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); + } + } + + + /** * Generates and prints SQL query. * @param array|mixed one or more arguments diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index 1359698..738cc36 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -122,6 +122,9 @@ class DibiFluent extends DibiObject } elseif (is_string($arg) && preg_match('#^[a-z][a-z0-9_.]*$#i', $arg)) { // identifier $args = array('%n', $arg); + } elseif ($arg instanceof self) { + $args = array_merge(array('('), $arg->_export(), array(')')); + } elseif (is_array($arg)) { // any array if (isset(self::$modifiers[$clause])) { $args = array(self::$modifiers[$clause], $arg); @@ -383,14 +386,12 @@ class DibiFluent extends DibiObject /** - * Returns (highlighted) SQL query. + * Returns SQL query. * @return string */ final public function __toString() { - ob_start(); - $this->test(); - return ob_get_clean(); + return $this->connection->sql($this->_export()); } }