diff --git a/src/Dibi/Expression.php b/src/Dibi/Expression.php new file mode 100644 index 00000000..3f52432d --- /dev/null +++ b/src/Dibi/Expression.php @@ -0,0 +1,32 @@ +values = func_get_args(); + } + + + public function getValues() + { + return $this->values; + } +} diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index 56a59502..69c865f6 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -458,6 +458,9 @@ final class Translator } elseif ($value instanceof Literal) { return (string) $value; + } elseif ($value instanceof Expression) { + return call_user_func_array([$this->connection, 'translate'], $value->getValues()); + } else { $type = is_object($value) ? get_class($value) : gettype($value); return $this->errors[] = "**Unexpected $type**"; diff --git a/tests/dibi/Translator.phpt b/tests/dibi/Translator.phpt index 42e093d9..a486f80a 100644 --- a/tests/dibi/Translator.phpt +++ b/tests/dibi/Translator.phpt @@ -474,7 +474,7 @@ Assert::same( 'title' => ['SHA1(%s)', 'Test product'], ], [ 'product_id' => 1, - 'title' => ['SHA1(%s)', 'Test product'], + 'title' => new Dibi\Expression('SHA1(%s)', 'Test product'), ]) ); @@ -486,6 +486,22 @@ Assert::same( ]) ); +Assert::same( + reformat('UPDATE [products] [product_id]=1, [title]=SHA1(\'Test product\')'), + $conn->translate('UPDATE [products]', [ + 'product_id' => 1, + 'title' => new Dibi\Expression('SHA1(%s)', 'Test product'), + ]) +); + +Assert::same( + reformat('SELECT * FROM [products] WHERE [product_id]=1, [title]=SHA1(\'Test product\')'), + $conn->translate('SELECT * FROM [products] WHERE', [ + 'product_id' => 1, + 'title' => new Dibi\Expression('SHA1(%s)', 'Test product'), + ]) +); + $e = Assert::exception(function () use ($conn) { $array6 = [