Skip to content

Commit

Permalink
Ensure correct parameter types in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Sep 25, 2023
1 parent 43a18a6 commit b5e11d1
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Internal/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Loupe\Loupe\Internal;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use Loupe\Loupe\Configuration;
use Loupe\Loupe\IndexResult;
use Loupe\Loupe\Internal\Filter\Parser;
Expand Down Expand Up @@ -193,10 +194,10 @@ public function upsert(
$query .= ' WHERE ' . implode(' AND ', $where);
}

$existing = $this->getConnection()->executeQuery($query, $parameters)->fetchAssociative();
$existing = $this->getConnection()->executeQuery($query, $parameters, $this->extractDbalTypes($parameters))->fetchAssociative();

if ($existing === false) {
$this->getConnection()->insert($table, $insertData);
$this->getConnection()->insert($table, $insertData, $this->extractDbalTypes($insertData));

return (int) $this->getConnection()->lastInsertId();
}
Expand Down Expand Up @@ -224,8 +225,23 @@ public function upsert(
$query .= ' WHERE ' . implode(' AND ', $where);
}

$this->getConnection()->executeStatement($query, $parameters);
$this->getConnection()->executeStatement($query, $parameters, $this->extractDbalTypes($parameters));

return $insertIdColumn !== '' ? (int) $existing[$insertIdColumn] : null;
}

private function extractDbalTypes(array $data): array
{
$types = [];

foreach ($data as $k => $v) {
$types[$k] = match (\gettype($v)) {
'boolean' => ParameterType::BOOLEAN,
'integer' => ParameterType::INTEGER,
default => ParameterType::STRING
};
}

return $types;
}
}

0 comments on commit b5e11d1

Please sign in to comment.