Skip to content

Commit de53e58

Browse files
committed
task: expose helper methods on SelectStatement
1 parent 31f5820 commit de53e58

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

src/lib/postgresql/src/Flow/PostgreSql/AST/Nodes/Statement/SelectStatement.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ public function __construct(
2020
) {
2121
}
2222

23+
public function hasCte() : bool
24+
{
25+
return $this->stmt->hasWithClause();
26+
}
27+
28+
public function hasIntoClause() : bool
29+
{
30+
return $this->stmt->hasIntoClause();
31+
}
32+
33+
public function hasLimit() : bool
34+
{
35+
return $this->stmt->hasLimitCount();
36+
}
37+
38+
public function hasLockingClause() : bool
39+
{
40+
return \count($this->stmt->getLockingClause()) > 0;
41+
}
42+
43+
public function hasOffset() : bool
44+
{
45+
return $this->stmt->hasLimitOffset();
46+
}
47+
2348
public function raw() : SelectStmt
2449
{
2550
return $this->stmt;
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flow\PostgreSql\Tests\Unit\AST\Nodes\Statement;
6+
7+
use function Flow\PostgreSql\DSL\sql_parse;
8+
9+
use Flow\PostgreSql\AST\Nodes\Statement\SelectStatement;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class SelectStatementTest extends TestCase
13+
{
14+
protected function setUp() : void
15+
{
16+
if (!\extension_loaded('pg_query')) {
17+
self::markTestSkipped('pg_query extension is not loaded. For local development use `nix-shell --arg with-pg-query-ext true` to enable it in the shell.');
18+
}
19+
}
20+
21+
public function test_has_cte_returns_false_when_no_with_clause() : void
22+
{
23+
$statement = sql_parse('SELECT * FROM users')->statements()->first();
24+
self::assertInstanceOf(SelectStatement::class, $statement);
25+
26+
self::assertFalse($statement->hasCte());
27+
}
28+
29+
public function test_has_cte_returns_true_when_with_clause_exists() : void
30+
{
31+
$statement = sql_parse('WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users')->statements()->first();
32+
self::assertInstanceOf(SelectStatement::class, $statement);
33+
34+
self::assertTrue($statement->hasCte());
35+
}
36+
37+
public function test_has_into_clause_returns_false_when_no_into_clause() : void
38+
{
39+
$statement = sql_parse('SELECT * FROM users')->statements()->first();
40+
self::assertInstanceOf(SelectStatement::class, $statement);
41+
42+
self::assertFalse($statement->hasIntoClause());
43+
}
44+
45+
public function test_has_into_clause_returns_true_when_into_clause_exists() : void
46+
{
47+
$statement = sql_parse('SELECT * INTO new_users FROM users')->statements()->first();
48+
self::assertInstanceOf(SelectStatement::class, $statement);
49+
50+
self::assertTrue($statement->hasIntoClause());
51+
}
52+
53+
public function test_has_limit_returns_false_when_no_limit() : void
54+
{
55+
$statement = sql_parse('SELECT * FROM users')->statements()->first();
56+
self::assertInstanceOf(SelectStatement::class, $statement);
57+
58+
self::assertFalse($statement->hasLimit());
59+
}
60+
61+
public function test_has_limit_returns_true_when_limit_exists() : void
62+
{
63+
$statement = sql_parse('SELECT * FROM users LIMIT 10')->statements()->first();
64+
self::assertInstanceOf(SelectStatement::class, $statement);
65+
66+
self::assertTrue($statement->hasLimit());
67+
}
68+
69+
public function test_has_locking_clause_returns_false_when_no_locking() : void
70+
{
71+
$statement = sql_parse('SELECT * FROM users')->statements()->first();
72+
self::assertInstanceOf(SelectStatement::class, $statement);
73+
74+
self::assertFalse($statement->hasLockingClause());
75+
}
76+
77+
public function test_has_locking_clause_returns_true_when_for_share_exists() : void
78+
{
79+
$statement = sql_parse('SELECT * FROM users FOR SHARE')->statements()->first();
80+
self::assertInstanceOf(SelectStatement::class, $statement);
81+
82+
self::assertTrue($statement->hasLockingClause());
83+
}
84+
85+
public function test_has_locking_clause_returns_true_when_for_update_exists() : void
86+
{
87+
$statement = sql_parse('SELECT * FROM users FOR UPDATE')->statements()->first();
88+
self::assertInstanceOf(SelectStatement::class, $statement);
89+
90+
self::assertTrue($statement->hasLockingClause());
91+
}
92+
93+
public function test_has_offset_returns_false_when_no_offset() : void
94+
{
95+
$statement = sql_parse('SELECT * FROM users')->statements()->first();
96+
self::assertInstanceOf(SelectStatement::class, $statement);
97+
98+
self::assertFalse($statement->hasOffset());
99+
}
100+
101+
public function test_has_offset_returns_true_when_limit_and_offset_exist() : void
102+
{
103+
$statement = sql_parse('SELECT * FROM users LIMIT 10 OFFSET 5')->statements()->first();
104+
self::assertInstanceOf(SelectStatement::class, $statement);
105+
106+
self::assertTrue($statement->hasLimit());
107+
self::assertTrue($statement->hasOffset());
108+
}
109+
110+
public function test_has_offset_returns_true_when_offset_exists() : void
111+
{
112+
$statement = sql_parse('SELECT * FROM users OFFSET 5')->statements()->first();
113+
self::assertInstanceOf(SelectStatement::class, $statement);
114+
115+
self::assertTrue($statement->hasOffset());
116+
}
117+
}

0 commit comments

Comments
 (0)