Completed
Push — master ( e0dde8...a7d2aa )
by Carsten
09:47
created

SqlTokenizer::isComment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 12
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db\sqlite;
9
10
/**
11
 * SqlTokenizer splits SQLite query into individual SQL tokens.
12
 * It's used to obtain a `CHECK` constraint information from a `CREATE TABLE` SQL code.
13
 *
14
 * @see http://www.sqlite.org/draft/tokenreq.html
15
 * @see https://sqlite.org/lang.html
16
 * @author Sergey Makinen <[email protected]>
17
 * @since 2.0.13
18
 */
19
class SqlTokenizer extends \yii\db\SqlTokenizer
20
{
21
    /**
22
     * @inheritDoc
23
     */
24 13
    protected function isWhitespace(&$length)
25
    {
26 13
        static $whitespaces = [
27
            "\f" => true,
28
            "\n" => true,
29
            "\r" => true,
30
            "\t" => true,
31
            ' ' => true,
32
        ];
33
34 13
        $length = 1;
35 13
        return isset($whitespaces[$this->substring($length)]);
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41 13
    protected function isComment(&$length)
42
    {
43 13
        static $comments = [
44
            '--' => true,
45
            '/*' => true,
46
        ];
47
48 13
        $length = 2;
49 13
        if (!isset($comments[$this->substring($length)])) {
50 13
            return false;
51
        }
52
53 1
        if ($this->substring($length) === '--') {
54 1
            $length = $this->indexAfter("\n") - $this->offset;
55
        } else {
56 1
            $length = $this->indexAfter('*/') - $this->offset;
57
        }
58 1
        return true;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64 13
    protected function isOperator(&$length, &$content)
65
    {
66 13
        static $operators = [
67
            '!=',
68
            '%',
69
            '&',
70
            '(',
71
            ')',
72
            '*',
73
            '+',
74
            ',',
75
            '-',
76
            '.',
77
            '/',
78
            ';',
79
            '<',
80
            '<<',
81
            '<=',
82
            '<>',
83
            '=',
84
            '==',
85
            '>',
86
            '>=',
87
            '>>',
88
            '|',
89
            '||',
90
            '~',
91
        ];
92
93 13
        return $this->startsWithAnyLongest($operators, true, $length);
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99 13
    protected function isIdentifier(&$length, &$content)
100
    {
101 13
        static $identifierDelimiters = [
102
            '"' => '"',
103
            '[' => ']',
104
            '`' => '`',
105
        ];
106
107 13
        if (!isset($identifierDelimiters[$this->substring(1)])) {
108 13
            return false;
109
        }
110
111 13
        $delimiter = $identifierDelimiters[$this->substring(1)];
112 13
        $offset = $this->offset;
113 13
        while (true) {
114 13
            $offset = $this->indexAfter($delimiter, $offset + 1);
115 13
            if ($delimiter === ']' || $this->substring(1, true, $offset) !== $delimiter) {
116 13
                break;
117
            }
118
        }
119 13
        $length = $offset - $this->offset;
120 13
        $content = $this->substring($length - 2, true, $this->offset + 1);
121 13
        if ($delimiter !== ']') {
122 13
            $content = strtr($content, ["$delimiter$delimiter" => $delimiter]);
123
        }
124 13
        return true;
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130 13
    protected function isStringLiteral(&$length, &$content)
131
    {
132 13
        if ($this->substring(1) !== "'") {
133 13
            return false;
134
        }
135
136 4
        $offset = $this->offset;
137 4
        while (true) {
138 4
            $offset = $this->indexAfter("'", $offset + 1);
139 4
            if ($this->substring(1, true, $offset) !== "'") {
140 4
                break;
141
            }
142
        }
143 4
        $length = $offset - $this->offset;
144 4
        $content = strtr($this->substring($length - 2, true, $this->offset + 1), ["''" => "'"]);
145 4
        return true;
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151 13
    protected function isKeyword($string, &$content)
152
    {
153 13
        static $keywords = [
154
            'ABORT' => true,
155
            'ACTION' => true,
156
            'ADD' => true,
157
            'AFTER' => true,
158
            'ALL' => true,
159
            'ALTER' => true,
160
            'ANALYZE' => true,
161
            'AND' => true,
162
            'AS' => true,
163
            'ASC' => true,
164
            'ATTACH' => true,
165
            'AUTOINCREMENT' => true,
166
            'BEFORE' => true,
167
            'BEGIN' => true,
168
            'BETWEEN' => true,
169
            'BY' => true,
170
            'CASCADE' => true,
171
            'CASE' => true,
172
            'CAST' => true,
173
            'CHECK' => true,
174
            'COLLATE' => true,
175
            'COLUMN' => true,
176
            'COMMIT' => true,
177
            'CONFLICT' => true,
178
            'CONSTRAINT' => true,
179
            'CREATE' => true,
180
            'CROSS' => true,
181
            'CURRENT_DATE' => true,
182
            'CURRENT_TIME' => true,
183
            'CURRENT_TIMESTAMP' => true,
184
            'DATABASE' => true,
185
            'DEFAULT' => true,
186
            'DEFERRABLE' => true,
187
            'DEFERRED' => true,
188
            'DELETE' => true,
189
            'DESC' => true,
190
            'DETACH' => true,
191
            'DISTINCT' => true,
192
            'DROP' => true,
193
            'EACH' => true,
194
            'ELSE' => true,
195
            'END' => true,
196
            'ESCAPE' => true,
197
            'EXCEPT' => true,
198
            'EXCLUSIVE' => true,
199
            'EXISTS' => true,
200
            'EXPLAIN' => true,
201
            'FAIL' => true,
202
            'FOR' => true,
203
            'FOREIGN' => true,
204
            'FROM' => true,
205
            'FULL' => true,
206
            'GLOB' => true,
207
            'GROUP' => true,
208
            'HAVING' => true,
209
            'IF' => true,
210
            'IGNORE' => true,
211
            'IMMEDIATE' => true,
212
            'IN' => true,
213
            'INDEX' => true,
214
            'INDEXED' => true,
215
            'INITIALLY' => true,
216
            'INNER' => true,
217
            'INSERT' => true,
218
            'INSTEAD' => true,
219
            'INTERSECT' => true,
220
            'INTO' => true,
221
            'IS' => true,
222
            'ISNULL' => true,
223
            'JOIN' => true,
224
            'KEY' => true,
225
            'LEFT' => true,
226
            'LIKE' => true,
227
            'LIMIT' => true,
228
            'MATCH' => true,
229
            'NATURAL' => true,
230
            'NO' => true,
231
            'NOT' => true,
232
            'NOTNULL' => true,
233
            'NULL' => true,
234
            'OF' => true,
235
            'OFFSET' => true,
236
            'ON' => true,
237
            'OR' => true,
238
            'ORDER' => true,
239
            'OUTER' => true,
240
            'PLAN' => true,
241
            'PRAGMA' => true,
242
            'PRIMARY' => true,
243
            'QUERY' => true,
244
            'RAISE' => true,
245
            'RECURSIVE' => true,
246
            'REFERENCES' => true,
247
            'REGEXP' => true,
248
            'REINDEX' => true,
249
            'RELEASE' => true,
250
            'RENAME' => true,
251
            'REPLACE' => true,
252
            'RESTRICT' => true,
253
            'RIGHT' => true,
254
            'ROLLBACK' => true,
255
            'ROW' => true,
256
            'SAVEPOINT' => true,
257
            'SELECT' => true,
258
            'SET' => true,
259
            'TABLE' => true,
260
            'TEMP' => true,
261
            'TEMPORARY' => true,
262
            'THEN' => true,
263
            'TO' => true,
264
            'TRANSACTION' => true,
265
            'TRIGGER' => true,
266
            'UNION' => true,
267
            'UNIQUE' => true,
268
            'UPDATE' => true,
269
            'USING' => true,
270
            'VACUUM' => true,
271
            'VALUES' => true,
272
            'VIEW' => true,
273
            'VIRTUAL' => true,
274
            'WHEN' => true,
275
            'WHERE' => true,
276
            'WITH' => true,
277
            'WITHOUT' => true,
278
        ];
279
280 13
        $string = mb_strtoupper($string, 'UTF-8');
281 13
        if (!isset($keywords[$string])) {
282 13
            return false;
283
        }
284
285 13
        $content = $string;
286 13
        return true;
287
    }
288
}
289