mirror of
https://github.com/dg/dibi.git
synced 2025-08-07 06:36:44 +02:00
added Dibi\Expression [Closes #264]
This commit is contained in:
32
src/Dibi/Expression.php
Normal file
32
src/Dibi/Expression.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This file is part of the "dibi" - smart database abstraction layer.
|
||||||
|
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Dibi;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQL expression.
|
||||||
|
*/
|
||||||
|
class Expression
|
||||||
|
{
|
||||||
|
use Strict;
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
private $values;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->values = func_get_args();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getValues()
|
||||||
|
{
|
||||||
|
return $this->values;
|
||||||
|
}
|
||||||
|
}
|
@@ -458,6 +458,9 @@ final class Translator
|
|||||||
} elseif ($value instanceof Literal) {
|
} elseif ($value instanceof Literal) {
|
||||||
return (string) $value;
|
return (string) $value;
|
||||||
|
|
||||||
|
} elseif ($value instanceof Expression) {
|
||||||
|
return call_user_func_array([$this->connection, 'translate'], $value->getValues());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$type = is_object($value) ? get_class($value) : gettype($value);
|
$type = is_object($value) ? get_class($value) : gettype($value);
|
||||||
return $this->errors[] = "**Unexpected $type**";
|
return $this->errors[] = "**Unexpected $type**";
|
||||||
|
@@ -474,7 +474,7 @@ Assert::same(
|
|||||||
'title' => ['SHA1(%s)', 'Test product'],
|
'title' => ['SHA1(%s)', 'Test product'],
|
||||||
], [
|
], [
|
||||||
'product_id' => 1,
|
'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) {
|
$e = Assert::exception(function () use ($conn) {
|
||||||
$array6 = [
|
$array6 = [
|
||||||
|
Reference in New Issue
Block a user