Skip to content

Commit

Permalink
统一所有数据库预处理,支持:问号、冒号参数,绑定值时参数名是否传问号都生效
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Aug 30, 2021
1 parent c905a2e commit f079486
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
29 changes: 16 additions & 13 deletions src/Db/Drivers/Swoole/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,12 @@ public function execute(array $inputParameters = null): bool
}
else
{
$bindValues = $this->bindValues ?? [];
$this->bindValues = [];
if (null !== $inputParameters)
if (null === $inputParameters)
{
$inputParameters = $this->bindValues;
}
$this->bindValues = $bindValues = [];
if ($inputParameters)
{
$sqlParamsMap = $this->sqlParamsMap;
if ($sqlParamsMap)
Expand All @@ -198,21 +201,21 @@ public function execute(array $inputParameters = null): bool
{
$bindValues[$index] = $inputParameters[$paramName];
}
elseif (isset($inputParameters[$key = ':' . $paramName]))
{
$bindValues[$index] = $inputParameters[$key];
}
elseif (isset($inputParameters[$index + 1]))
{
$bindValues[$index] = $inputParameters[$index + 1];
}
}
}
elseif ($inputParameters)
else
{
foreach ($inputParameters as $k => $v)
{
$bindValues[$k] = $v;
}
$bindValues = array_values($inputParameters);
}
}
if ($bindValues)
{
ksort($bindValues);
$bindValues = array_values($bindValues);
}
$this->queryResult = $queryResult = $pgDb->execute($this->statementName, $bindValues);
if (false === $queryResult)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Db/Util/SqlUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public static function parseSqlWithParams(string $sql, ?array &$map): string

$i = 0;

return preg_replace_callback('/(:[a-zA-Z0-9_]+|\?)/', function (array $match) use (&$map, &$i): string {
$map[] = $match[0];
return preg_replace_callback('/(:([a-zA-Z0-9_]+)|\?)/', function (array $match) use (&$map, &$i): string {
$map[] = $match[2] ?? $match[0];

return '$' . (++$i);
}, $sql);
Expand Down
21 changes: 20 additions & 1 deletion tests/Unit/Db/DbBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,34 @@ public function testPreparePositional(): void
public function testPrepareNamed(): void
{
$db = Db::getInstance($this->poolName);
$stmt = $db->prepare('select * from tb_article where id = :id');

// 有冒号
$stmt = $db->prepare('select tb_article.*, :v as v from tb_article where id = :id');
$stmt->bindValue(':id', 1);
$stmt->bindValue(':v', 2);
Assert::assertTrue($stmt->execute());
Assert::assertEquals([
[
'id' => '1',
'title' => 'title',
'content' => 'content',
'time' => '2019-06-21 00:00:00',
'v' => 2,
],
], $stmt->fetchAll());

// 无冒号
$stmt = $db->prepare('select tb_article.*, :v as v from tb_article where id = :id');
$stmt->bindValue('id', 1);
$stmt->bindValue('v', 2);
Assert::assertTrue($stmt->execute());
Assert::assertEquals([
[
'id' => '1',
'title' => 'title',
'content' => 'content',
'time' => '2019-06-21 00:00:00',
'v' => 2,
],
], $stmt->fetchAll());
}
Expand Down

0 comments on commit f079486

Please sign in to comment.