1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-03 20:57:36 +02:00

Translator: Dibi\Expression should be used instead of array

This commit is contained in:
David Grudl
2017-09-21 15:04:53 +02:00
parent 499e3aea40
commit e93bab27e9
2 changed files with 11 additions and 8 deletions

View File

@@ -236,7 +236,7 @@ final class Translator
foreach ($value as $k => $v) {
$pair = explode('%', $k, 2); // split into identifier & modifier
$vx[] = $this->identifiers->{$pair[0]} . '='
. $this->formatValue($v, $pair[1] ?? (is_array($v) ? 'ex' : null));
. $this->formatValue($v, $pair[1] ?? (is_array($v) ? 'ex!' : null));
}
return implode(', ', $vx);
@@ -245,7 +245,7 @@ final class Translator
case 'l': // (val, val, ...)
foreach ($value as $k => $v) {
$pair = explode('%', (string) $k, 2); // split into identifier & modifier
$vx[] = $this->formatValue($v, $pair[1] ?? (is_array($v) ? 'ex' : null));
$vx[] = $this->formatValue($v, $pair[1] ?? (is_array($v) ? 'ex!' : null));
}
return '(' . (($vx || $modifier === 'l') ? implode(', ', $vx) : 'NULL') . ')';
@@ -254,7 +254,7 @@ final class Translator
foreach ($value as $k => $v) {
$pair = explode('%', $k, 2); // split into identifier & modifier
$kx[] = $this->identifiers->{$pair[0]};
$vx[] = $this->formatValue($v, $pair[1] ?? (is_array($v) ? 'ex' : null));
$vx[] = $this->formatValue($v, $pair[1] ?? (is_array($v) ? 'ex!' : null));
}
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
@@ -275,7 +275,7 @@ final class Translator
$pair = explode('%', $k, 2); // split into identifier & modifier
$kx[] = $this->identifiers->{$pair[0]};
foreach ($v as $k2 => $v2) {
$vx[$k2][] = $this->formatValue($v2, $pair[1] ?? (is_array($v2) ? 'ex' : null));
$vx[$k2][] = $this->formatValue($v2, $pair[1] ?? (is_array($v2) ? 'ex!' : null));
}
}
foreach ($vx as $k => $v) {
@@ -296,6 +296,9 @@ final class Translator
}
return implode(', ', $vx);
case 'ex!':
trigger_error('Use Dibi\Expression instead of array: ' . implode(', ', array_filter($value, 'is_scalar')), E_USER_WARNING);
// break omitted
case 'ex':
case 'sql':
return $this->connection->translate(...$value);

View File

@@ -475,7 +475,7 @@ Assert::same(
reformat('INSERT INTO [products] ([product_id], [title]) VALUES (1, SHA1(\'Test product\')) , (1, SHA1(\'Test product\'))'),
$conn->translate('INSERT INTO [products]', [
'product_id' => 1,
'title' => ['SHA1(%s)', 'Test product'],
'title' => new Dibi\Expression('SHA1(%s)', 'Test product'),
], [
'product_id' => 1,
'title' => new Dibi\Expression('SHA1(%s)', 'Test product'),
@@ -486,7 +486,7 @@ Assert::same(
reformat('UPDATE [products] [product_id]=1, [title]=SHA1(\'Test product\')'),
$conn->translate('UPDATE [products]', [
'product_id' => 1,
'title' => ['SHA1(%s)', 'Test product'],
'title' => new Dibi\Expression('SHA1(%s)', 'Test product'),
])
);
@@ -510,7 +510,7 @@ Assert::same(
$e = Assert::exception(function () use ($conn) {
$array6 = [
'id' => [1, 2, 3, 4],
'text' => ['ahoj', 'jak', 'se', ['SUM(%i)', '5']],
'text' => ['ahoj', 'jak', 'se', new Dibi\Expression('SUM(%i)', '5')],
'num%i' => ['1', ''],
];
$conn->translate('INSERT INTO test %m', $array6);
@@ -519,7 +519,7 @@ Assert::same('INSERT INTO test **Multi-insert array "num%i" is different**', $e-
$array6 = [
'id' => [1, 2, 3, 4],
'text' => ['ahoj', 'jak', 'se', ['SUM(%i)', '5']],
'text' => ['ahoj', 'jak', 'se', new Dibi\Expression('SUM(%i)', '5')],
'num%i' => ['1', '-1', 10.3, 1],
];