Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

コメントを付与したSQLが実行されない問題の修正 #33

Merged
merged 10 commits into from
Dec 18, 2024
9 changes: 7 additions & 2 deletions src/SqlQueryRowList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use function array_pop;
use function count;
use function explode;
use function preg_replace;
use function str_starts_with;
use function strpos;
use function strtolower;
use function trim;
Expand Down Expand Up @@ -52,8 +54,11 @@ public function __invoke(array ...$queries): iterable
}

$lastQuery = $result
? strtolower(trim((string) $result->queryString, "\\ \t\n\r\0\x0B")) : '';
if ($result instanceof PDOStatement && strpos($lastQuery, 'select') === 0) {
? strtolower(trim(
(string) preg_replace('/\/\*.*?\*\/|--.*$/m', '', (string) $result->queryString),
"\\ \t\n\r\0\x0B",
)) : '';
if ($result instanceof PDOStatement && str_starts_with($lastQuery, 'select')) {
return (array) $result->fetchAll(PDO::FETCH_ASSOC);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Fake/sql/todo_item_by_id_with_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* todo_item_by_id_with_comment.sql */
SELECT
*
FROM
todo /* table */
WHERE
id = :id /* conditions */
KazuyaUchida marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions tests/Fake/sql/todo_item_by_id_with_line_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- todo_item_by_id_with_line_comment.sql
SELECT * FROM todo WHERE id = :id -- comment
KazuyaUchida marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions tests/Fake/sql/todo_item_by_id_with_multiple_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* todo_item_by_id_with_multiple_comment.sql */ SELECT * FROM todo WHERE id = :id -- conditions
KazuyaUchida marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 30 additions & 7 deletions tests/SqlQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ protected function setUp(): void
public function testInvoke(): void
{
$sql = (string) file_get_contents(__DIR__ . '/Fake/sql/todo_item_by_id.sql');
$query = new SqlQueryRowList($this->pdo, $sql);
$row = ((array) $query(['id' => 1]))[0];
assert(is_array($row));
assert(isset($row['title']));
assert(isset($row['id']));
$this->assertSame('run', $row['title']);
$this->assertSame('1', $row['id']);
$this->testSql($sql);
}

public function testNotFound(): void
Expand Down Expand Up @@ -66,4 +60,33 @@ public function testMultipleQuery(): void
$this->assertSame('test', $row['title']);
$this->assertSame('2', $row['id']);
}

public function testWithComment(): void
{
$sql = (string) file_get_contents(__DIR__ . '/Fake/sql/todo_item_by_id_with_comment.sql');
$this->testSql($sql);
}

public function testWithLineComment(): void
{
$sql = (string) file_get_contents(__DIR__ . '/Fake/sql/todo_item_by_id_with_line_comment.sql');
$this->testSql($sql);
}

public function testWithMultipleComment(): void
{
$sql = (string) file_get_contents(__DIR__ . '/Fake/sql/todo_item_by_id_with_multiple_comment.sql');
$this->testSql($sql);
}

private function testSql(string $sql): void
{
$query = new SqlQueryRowList($this->pdo, $sql);
$row = ((array) $query(['id' => 1]))[0];
assert(is_array($row));
assert(isset($row['title']));
assert(isset($row['id']));
$this->assertSame('run', $row['title']);
$this->assertSame('1', $row['id']);
}
KazuyaUchida marked this conversation as resolved.
Show resolved Hide resolved
}