-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqliteMultiQueryParser.php
More file actions
84 lines (75 loc) · 1.82 KB
/
SqliteMultiQueryParser.php
File metadata and controls
84 lines (75 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php declare(strict_types = 1);
namespace Nextras\MultiQueryParser;
use Iterator;
class SqliteMultiQueryParser extends BaseMultiQueryParser
{
public function parseStringStream(Iterator $stream): Iterator
{
$patternIterator = new PatternIterator($stream, $this->getQueryPattern());
foreach ($patternIterator as $match) {
if (isset($match['query']) && $match['query'] !== '') {
yield $match['query'];
}
}
}
private function getQueryPattern(): string
{
// (*PRUNE) must appear inline (not inside DEFINE subroutines) because PCRE confines
// backtracking verbs to the subroutine scope. The inner bodies are defined once in
// DEFINE and referenced after the inline (*PRUNE) to avoid pattern duplication.
return /** @lang PhpRegExp */ '~
(?(DEFINE)
(?<sqI> (?: \'\' | [^\'] )*+ \' )
(?<dqI> (?: "" | [^"] )*+ " )
(?<btI> (?: `` | [^`] )*+ ` )
(?<bkI> [^\]]*+ (?: \]\] [^\]]*+ )* \] )
(?<bcI> (?: [^*]++ | \*(?!/) )*+ \*/ )
(?<lc> -- [^\n]*+ )
(?<skip>
(?:
\s
| /\* (*PRUNE) (?&bcI)
| (?&lc)
)*+
)
(?<stmt>
(?&skip)
(?:
[^;\'"`[/-]++
| \' (*PRUNE) (?&sqI)
| " (*PRUNE) (?&dqI)
| ` (*PRUNE) (?&btI)
| \[ (*PRUNE) (?&bkI)
| /\* (*PRUNE) (?&bcI)
| (?&lc)
| (?!;) .
)++
;
)
)
(?&skip)
(?:
(?:
(?<query>
(?:
[^bB;\'"`[/-]++
| \' (*PRUNE) (?&sqI)
| " (*PRUNE) (?&dqI)
| ` (*PRUNE) (?&btI)
| \[ (*PRUNE) (?&bkI)
| /\* (*PRUNE) (?&bcI)
| (?i:BEGIN) (?!\s*(?:(?i:TRANSACTION|DEFERRED|IMMEDIATE|EXCLUSIVE)\b|;|\z)) (*PRUNE) (?: (?i:\s*END)\s* | (?&stmt) )*
| (?&lc)
| (?!;) .
)*+
)
(?: ; | \z )
)
|
(?:
\z
)
)
~xsAS';
}
}