diff --git a/src/QueryInterceptor.php b/src/QueryInterceptor.php index 52944b0..8911e4a 100644 --- a/src/QueryInterceptor.php +++ b/src/QueryInterceptor.php @@ -46,7 +46,8 @@ public function invoke(MethodInvocation $invocation) } /** - * @param array $param + * @param MethodInvocation $invocation + * @param array $param * * @return mixed */ @@ -56,13 +57,17 @@ private function getQueryResult(MethodInvocation $invocation, QueryInterface $qu $result = $query($param); $object = $invocation->getThis(); if ($object instanceof ResourceObject) { + /** @var array|object|scalar|null $result */ return $this->returnRo($object, $invocation, $result); } return $result; } - /** @param mixed $result */ + /** + * @param MethodInvocation $invocation + * @param mixed $result + */ private function returnRo(ResourceObject $ro, MethodInvocation $invocation, $result): ResourceObject { if (! $result) { diff --git a/src/SqlQueryRowList.php b/src/SqlQueryRowList.php index fa62e10..fa4015f 100644 --- a/src/SqlQueryRowList.php +++ b/src/SqlQueryRowList.php @@ -12,12 +12,16 @@ use function array_pop; use function count; use function explode; +use function preg_replace; use function strpos; use function strtolower; use function trim; class SqlQueryRowList implements RowListInterface { + public const QUERY_CLEANUP_REGEX = '/\/\*.*?\*\/|--.*$/m'; + public const TRIM_CHARACTERS_REGEX = "\\ \t\n\r\0\x0B"; + /** @var ExtendedPdoInterface */ private $pdo; @@ -37,7 +41,7 @@ public function __invoke(array ...$queries): iterable $this->sql .= ';'; } - $sqls = explode(';', trim($this->sql, "\\ \t\n\r\0\x0B")); + $sqls = explode(';', trim($this->sql, self::TRIM_CHARACTERS_REGEX)); array_pop($sqls); $numQueris = count($queries); if (count($sqls) !== $numQueris) { @@ -52,7 +56,10 @@ public function __invoke(array ...$queries): iterable } $lastQuery = $result - ? strtolower(trim((string) $result->queryString, "\\ \t\n\r\0\x0B")) : ''; + ? strtolower(trim( + (string) preg_replace(self::QUERY_CLEANUP_REGEX, '', (string) $result->queryString), + self::TRIM_CHARACTERS_REGEX, + )) : ''; if ($result instanceof PDOStatement && strpos($lastQuery, 'select') === 0) { return (array) $result->fetchAll(PDO::FETCH_ASSOC); } diff --git a/tests/Fake/sql/todo_item_by_id_with_comment.sql b/tests/Fake/sql/todo_item_by_id_with_comment.sql new file mode 100644 index 0000000..d13cf2d --- /dev/null +++ b/tests/Fake/sql/todo_item_by_id_with_comment.sql @@ -0,0 +1,7 @@ +/* todo_item_by_id_with_comment.sql */ +SELECT + * +FROM + todo /* table */ +WHERE + id = :id /* conditions */ diff --git a/tests/Fake/sql/todo_item_by_id_with_line_comment.sql b/tests/Fake/sql/todo_item_by_id_with_line_comment.sql new file mode 100644 index 0000000..355dc27 --- /dev/null +++ b/tests/Fake/sql/todo_item_by_id_with_line_comment.sql @@ -0,0 +1,2 @@ +-- todo_item_by_id_with_line_comment.sql +SELECT * FROM todo WHERE id = :id -- comment diff --git a/tests/Fake/sql/todo_item_by_id_with_multiple_comment.sql b/tests/Fake/sql/todo_item_by_id_with_multiple_comment.sql new file mode 100644 index 0000000..eb9f05f --- /dev/null +++ b/tests/Fake/sql/todo_item_by_id_with_multiple_comment.sql @@ -0,0 +1 @@ +/* todo_item_by_id_with_multiple_comment.sql */ SELECT * FROM todo WHERE id = :id -- conditions diff --git a/tests/SqlQueryTest.php b/tests/SqlQueryTest.php index 1f41012..8ad5dc8 100644 --- a/tests/SqlQueryTest.php +++ b/tests/SqlQueryTest.php @@ -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 @@ -66,4 +60,35 @@ 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); + $result = (array) $query(['id' => 1]); + $this->assertNotEmpty($result); + $row = $result[0]; + $this->assertIsArray($row); + $this->assertArrayHasKey('title', $row); + $this->assertArrayHasKey('id', $row); + $this->assertSame('run', $row['title']); + $this->assertSame('1', $row['id']); + } }