From b52b2c2f79b9d92237820735d792366eb1b6b30c Mon Sep 17 00:00:00 2001 From: kruglov Date: Tue, 13 Aug 2024 13:04:44 +0300 Subject: [PATCH] Added query with 'with' support --- src/ClickHouseStatement.php | 3 ++- tests/SelectTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ClickHouseStatement.php b/src/ClickHouseStatement.php index e10237f..e5728a0 100644 --- a/src/ClickHouseStatement.php +++ b/src/ClickHouseStatement.php @@ -124,7 +124,8 @@ public function execute($params = null): Result new \ArrayIterator( mb_stripos($statement, 'select') === 0 || mb_stripos($statement, 'show') === 0 || - mb_stripos($statement, 'describe') === 0 + mb_stripos($statement, 'describe') === 0 || + preg_match('/with(.*)\)\s*select/ms', mb_strtolower($statement)) == 1 ? $this->client->select($statement)->rows() : $this->client->write($statement)->rows() ) diff --git a/tests/SelectTest.php b/tests/SelectTest.php index 80da456..0491f18 100644 --- a/tests/SelectTest.php +++ b/tests/SelectTest.php @@ -243,4 +243,20 @@ public function testTrimChar(): void $this->assertEquals('t2', $result->fetchOne()); } + + public function testWith(): void + { + $result = $this->connection->executeQuery(" + WITH subselect as ( + SELECT id + FROM test_select_table + WHERE payload = 'v4' + ) + SELECT * + FROM test_select_table tbl + JOIN subselect sub ON sub.id = tbl.id + "); + + $this->assertEquals(2, $result->columnCount()); + } }