Skip to content

Commit a8af23a

Browse files
authored
Merge pull request #157 from vrana/pgsql-json
Treat #> as operator
2 parents 3a7aa19 + a5aa636 commit a8af23a

File tree

11 files changed

+79
-2
lines changed

11 files changed

+79
-2
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ vendor/bin/phpunit --testdox
2020
echo '' | vendor/bin/phpcs
2121
vendor/bin/phpstan analyze
2222
```
23+
24+
## Regenerating expected output
25+
26+
To regenerate expected tests output, run `bin/regenerate-expected-output`.

bin/regenerate-expected-output

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Doctrine\SqlFormatter\CliHighlighter;
5+
use Doctrine\SqlFormatter\HtmlHighlighter;
6+
use Doctrine\SqlFormatter\NullHighlighter;
7+
use Doctrine\SqlFormatter\SqlFormatter;
8+
use Doctrine\SqlFormatter\Tests\SqlFormatterTest;
9+
10+
require __DIR__ . '/../vendor/autoload.php';
11+
12+
$formatter = new SqlFormatter(new HtmlHighlighter());
13+
14+
updateExpected('format-highlight.html', [$formatter, 'format']);
15+
updateExpected('format.txt', [new SqlFormatter(new NullHighlighter()), 'format']);
16+
updateExpected('highlight.html', [$formatter, 'highlight']);
17+
updateExpected('clihighlight.txt', [new SqlFormatter(new CliHighlighter()), 'format']);
18+
updateExpected('compress.txt', [$formatter, 'compress']);
19+
20+
function updateExpected(string $filename, callable $highlight): void
21+
{
22+
$data = [];
23+
24+
foreach (SqlFormatterTest::fileSqlData() as $sql) {
25+
$data[] = rtrim($highlight($sql), "\n");
26+
}
27+
28+
file_put_contents(__DIR__ . '/../tests/' . $filename, implode("\n---\n", $data) . "\n");
29+
}

src/Tokenizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ private function makeTokenizeRegexes(): array
842842

843843
return [
844844
Token::TOKEN_TYPE_WHITESPACE => '\s+',
845-
Token::TOKEN_TYPE_COMMENT => '(?:--|#)[^\n]*+',
845+
Token::TOKEN_TYPE_COMMENT => '(?:--|#(?!>))[^\n]*+', // #>, #>> and <#> are PostgreSQL operators
846846
Token::TOKEN_TYPE_BLOCK_COMMENT => '/\*(?:[^*]+|\*(?!/))*+(?:\*|$)(?:/|$)',
847847
// 1. backtick quoted string using `` to escape
848848
// 2. square bracket quoted string (SQL Server) using ]] to escape

tests/SqlFormatterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testUsePre(): void
101101
}
102102

103103
/** @return string[] */
104-
private static function fileSqlData(): array
104+
public static function fileSqlData(): array
105105
{
106106
$contents = file_get_contents(__DIR__ . '/sql.sql');
107107
assert($contents !== false);

tests/TokenizerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,20 @@ public static function tokenizeData(): Generator
16581658
],
16591659
'/* foo...',
16601660
];
1661+
1662+
yield 'PostgreSQL operator' => [
1663+
[
1664+
new Token(Token::TOKEN_TYPE_RESERVED_TOPLEVEL, 'select'),
1665+
new Token(Token::TOKEN_TYPE_WHITESPACE, ' '),
1666+
new Token(Token::TOKEN_TYPE_WORD, 'json'),
1667+
new Token(Token::TOKEN_TYPE_WHITESPACE, ' '),
1668+
new Token(Token::TOKEN_TYPE_BOUNDARY, '#'),
1669+
new Token(Token::TOKEN_TYPE_BOUNDARY, '>'),
1670+
new Token(Token::TOKEN_TYPE_WHITESPACE, ' '),
1671+
new Token(Token::TOKEN_TYPE_RESERVED, 'null'),
1672+
],
1673+
'select json #> null',
1674+
];
16611675
}
16621676

16631677
public function testTokenizeLongConcat(): void

tests/clihighlight.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,3 +1205,9 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
12051205
CREATE TABLE t (
12061206
c VARCHAR(20)
12071207
) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
1208+
---
1209+
SELECT
1210+
'{}' :: json #> '{}'
1211+
---
1212+
SELECT
1213+
vector1 <#> vector2

tests/compress.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,7 @@ SELECT a FROM test STRAIGHT_JOIN test2 ON test.id = test2.id
111111
SELECT t.id, t.start, t.end, t.end AS e2, t.limit, t.begin, t.case, t.when, t.then, t.else FROM t WHERE t.start = t.end
112112
---
113113
CREATE TABLE t (c VARCHAR(20)) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
114+
---
115+
SELECT '{}'::json #> '{}'
116+
---
117+
SELECT vector1 <#> vector2

tests/format-highlight.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,3 +1205,9 @@
12051205
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">CREATE</span> <span style="font-weight:bold;">TABLE</span> <span style="color: #333;">t</span> (
12061206
<span style="color: #333;">c</span> <span style="font-weight:bold;">VARCHAR</span>(<span style="color: green;">20</span>)
12071207
) <span style="font-weight:bold;">DEFAULT</span> <span style="font-weight:bold;">CHARACTER</span> <span style="font-weight:bold;">SET</span> <span style="color: #333;">utf8mb4</span> <span style="font-weight:bold;">ENGINE</span> <span >=</span> <span style="color: #333;">InnoDB</span></pre>
1208+
---
1209+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
1210+
<span style="color: blue;">'{}'</span> <span >::</span> <span style="color: #333;">json</span> <span >#</span><span >&gt;</span> <span style="color: blue;">'{}'</span></pre>
1211+
---
1212+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
1213+
<span style="color: #333;">vector1</span> <span >&lt;</span><span >#</span><span >&gt;</span> <span style="color: #333;">vector2</span></pre>

tests/format.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,3 +1203,9 @@ WHERE
12031203
CREATE TABLE t (
12041204
c VARCHAR(20)
12051205
) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
1206+
---
1207+
SELECT
1208+
'{}' :: json #> '{}'
1209+
---
1210+
SELECT
1211+
vector1 <#> vector2

tests/highlight.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,7 @@
425425
<span style="font-weight:bold;">WHERE</span> <span style="color: #333;">t</span><span >.</span><span style="color: #333;">start</span> <span >=</span> <span style="color: #333;">t</span><span >.</span><span style="color: #333;">end</span></pre>
426426
---
427427
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">CREATE</span> <span style="font-weight:bold;">TABLE</span> <span style="color: #333;">t</span> (<span style="color: #333;">c</span> <span style="font-weight:bold;">VARCHAR</span>(<span style="color: green;">20</span>)) <span style="font-weight:bold;">DEFAULT</span> <span style="font-weight:bold;">CHARACTER</span> <span style="font-weight:bold;">SET</span> <span style="color: #333;">utf8mb4</span> <span style="font-weight:bold;">ENGINE</span> <span >=</span> <span style="color: #333;">InnoDB</span></pre>
428+
---
429+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: blue;">'{}'</span><span >::</span><span style="color: #333;">json</span> <span >#</span><span >&gt;</span> <span style="color: blue;">'{}'</span></pre>
430+
---
431+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #333;">vector1</span> <span >&lt;</span><span >#</span><span >&gt;</span> <span style="color: #333;">vector2</span></pre>

0 commit comments

Comments
 (0)