From 02877db1c059e0a16f7e68264d885d84fcafb949 Mon Sep 17 00:00:00 2001 From: Angel Date: Mon, 22 Jan 2024 10:22:23 +0800 Subject: [PATCH] [fix] where IN clause with Raw object #1079 --- src/Medoo.php | 29 +++++++++++++++-------- tests/WhereTest.php | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/Medoo.php b/src/Medoo.php index b8cb0302..c44447cf 100644 --- a/src/Medoo.php +++ b/src/Medoo.php @@ -894,15 +894,20 @@ protected function dataImplode(array $data, array &$map, string $conjunctor): st break; case 'array': - $placeholders = []; + $values = []; foreach ($value as $index => $item) { - $stackKey = $mapKey . $index . '_i'; - $placeholders[] = $stackKey; - $map[$stackKey] = $this->typeMap($item, gettype($item)); + if ($raw = $this->buildRaw($item, $map)) { + $values[] = $raw; + } else { + $stackKey = $mapKey . $index . '_i'; + + $values[] = $stackKey; + $map[$stackKey] = $this->typeMap($item, gettype($item)); + } } - $stack[] = $column . ' NOT IN (' . implode(', ', $placeholders) . ')'; + $stack[] = $column . ' NOT IN (' . implode(', ', $values) . ')'; break; case 'object': @@ -981,16 +986,20 @@ protected function dataImplode(array $data, array &$map, string $conjunctor): st break; case 'array': - $placeholders = []; + $values = []; foreach ($value as $index => $item) { - $stackKey = $mapKey . $index . '_i'; + if ($raw = $this->buildRaw($item, $map)) { + $values[] = $raw; + } else { + $stackKey = $mapKey . $index . '_i'; - $placeholders[] = $stackKey; - $map[$stackKey] = $this->typeMap($item, gettype($item)); + $values[] = $stackKey; + $map[$stackKey] = $this->typeMap($item, gettype($item)); + } } - $stack[] = $column . ' IN (' . implode(', ', $placeholders) . ')'; + $stack[] = $column . ' IN (' . implode(', ', $values) . ')'; break; case 'object': diff --git a/tests/WhereTest.php b/tests/WhereTest.php index 32610bc5..35b2ff64 100644 --- a/tests/WhereTest.php +++ b/tests/WhereTest.php @@ -232,6 +232,62 @@ public function testArrayStringValuesWhere($type) ); } + /** + * @covers ::select() + * @covers ::dataImplode() + * @covers ::whereClause() + * @dataProvider typesProvider + */ + public function testRawArrayValuesWhere($type) + { + $this->setType($type); + + $this->database->select("account", "user_name", [ + 'id' => [ + Medoo::raw('LOWER("FOO")'), + Medoo::raw('LOWER("BAR")') + ] + ]); + + $this->assertQuery( + <<database->queryString + ); + } + + /** + * @covers ::select() + * @covers ::dataImplode() + * @covers ::whereClause() + * @dataProvider typesProvider + */ + public function testRawNotInArrayValuesWhere($type) + { + $this->setType($type); + + $this->database->select("account", "user_name", [ + 'id[!]' => [ + Medoo::raw('LOWER("FOO")'), + Medoo::raw('LOWER("BAR")') + ] + ]); + + $this->assertQuery( + <<database->queryString + ); + } + /** * @covers ::select() * @covers ::dataImplode()