From e93bab27e96ae810c59f414ea44550816438e560 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 21 Sep 2017 15:04:53 +0200 Subject: [PATCH] Translator: Dibi\Expression should be used instead of array --- src/Dibi/Translator.php | 11 +++++++---- tests/dibi/Translator.phpt | 8 ++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index 63f9578e..01fb9145 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -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); diff --git a/tests/dibi/Translator.phpt b/tests/dibi/Translator.phpt index 9f49fd29..35303860 100644 --- a/tests/dibi/Translator.phpt +++ b/tests/dibi/Translator.phpt @@ -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], ];