Skip to content

Commit

Permalink
[fix] where IN clause with Raw object #1079
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelaon committed Jan 22, 2024
1 parent b409028 commit 02877db
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Medoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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':
Expand Down
56 changes: 56 additions & 0 deletions tests/WhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<<<EOD
SELECT "user_name"
FROM "account"
WHERE
"id" IN (LOWER("FOO"), LOWER("BAR"))
EOD,
$this->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(
<<<EOD
SELECT "user_name"
FROM "account"
WHERE
"id" NOT IN (LOWER("FOO"), LOWER("BAR"))
EOD,
$this->database->queryString
);
}

/**
* @covers ::select()
* @covers ::dataImplode()
Expand Down

0 comments on commit 02877db

Please sign in to comment.