Skip to content

Commit 6bb6c50

Browse files
authored
Identify create virtual table sqlite statements (#73)
1 parent 9148c03 commit 6bb6c50

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

src/parser.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,11 @@ function stateMachineStatementParser(
854854
return;
855855
}
856856

857+
// Table/View modifiers
857858
if (
858-
['psql', 'sqlite'].includes(dialect) &&
859-
['TEMP', 'TEMPORARY'].includes(token.value.toUpperCase())
859+
(dialect === 'psql' && ['TEMP', 'TEMPORARY'].includes(token.value.toUpperCase())) ||
860+
(dialect === 'sqlite' &&
861+
['TEMP', 'TEMPORARY', 'VIRTUAL'].includes(token.value.toUpperCase()))
860862
) {
861863
setPrevToken(token);
862864
return;

test/parser/single-statements.spec.ts

+128
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,134 @@ describe('parser', () => {
187187
expect(actual).to.eql(expected);
188188
});
189189

190+
['TEMP', 'TEMPORARY'].forEach((type) => {
191+
describe(`it should parse "CREATE ${type} TABLE" statement`, () => {
192+
['psql', 'sqlite'].forEach((dialect) => {
193+
it(`for ${dialect}`, () => {
194+
const actual = parse(
195+
`CREATE ${type} TABLE Persons (PersonID int, Name varchar(255));`,
196+
true,
197+
dialect as 'psql' | 'sqlite',
198+
);
199+
200+
actual.tokens = aggregateUnknownTokens(actual.tokens);
201+
202+
const expected = {
203+
type: 'QUERY',
204+
start: 0,
205+
end: 54 + type.length + 1,
206+
body: [
207+
// nodes
208+
{
209+
start: 0,
210+
end: 54 + type.length + 1,
211+
type: 'CREATE_TABLE',
212+
executionType: 'MODIFICATION',
213+
endStatement: ';',
214+
parameters: [],
215+
},
216+
],
217+
tokens: [
218+
{
219+
type: 'keyword',
220+
value: 'CREATE',
221+
start: 0,
222+
end: 5,
223+
},
224+
{
225+
type: 'unknown',
226+
value: ` ${type} `,
227+
start: 6,
228+
end: 6 + type.length + 1,
229+
},
230+
{
231+
type: 'keyword',
232+
value: 'TABLE',
233+
start: 6 + type.length + 2,
234+
end: 6 + type.length + 1 + 5,
235+
},
236+
{
237+
type: 'unknown',
238+
value: ' Persons (PersonID int, Name varchar(255))',
239+
start: 6 + type.length + 1 + 5 + 1,
240+
end: 6 + type.length + 1 + 5 + 42,
241+
},
242+
{
243+
type: 'semicolon',
244+
value: ';',
245+
start: 6 + type.length + 1 + 5 + 42 + 1,
246+
end: 6 + type.length + 1 + 5 + 42 + 1,
247+
},
248+
],
249+
};
250+
251+
expect(actual).to.eql(expected);
252+
});
253+
});
254+
});
255+
});
256+
257+
it('should parse "CREATE VIRTUAL TABLE" statement for sqlite', () => {
258+
const actual = parse(
259+
`CREATE VIRTUAL TABLE Persons (PersonID int, Name varchar(255));`,
260+
true,
261+
'sqlite',
262+
);
263+
264+
actual.tokens = aggregateUnknownTokens(actual.tokens);
265+
266+
const expected = {
267+
type: 'QUERY',
268+
start: 0,
269+
end: 62,
270+
body: [
271+
// nodes
272+
{
273+
start: 0,
274+
end: 62,
275+
type: 'CREATE_TABLE',
276+
executionType: 'MODIFICATION',
277+
endStatement: ';',
278+
parameters: [],
279+
},
280+
],
281+
tokens: [
282+
{
283+
type: 'keyword',
284+
value: 'CREATE',
285+
start: 0,
286+
end: 5,
287+
},
288+
{
289+
type: 'unknown',
290+
value: ` VIRTUAL `,
291+
start: 6,
292+
end: 14,
293+
},
294+
{
295+
type: 'keyword',
296+
value: 'TABLE',
297+
start: 15,
298+
end: 19,
299+
},
300+
{
301+
type: 'unknown',
302+
value: ' Persons (PersonID int, Name varchar(255))',
303+
start: 20,
304+
end: 61,
305+
},
306+
{
307+
type: 'semicolon',
308+
value: ';',
309+
start: 62,
310+
end: 62,
311+
},
312+
],
313+
};
314+
315+
expect(actual).to.eql(expected);
316+
});
317+
190318
it('should parse "CREATE DATABASE" statement', () => {
191319
const actual = parse('CREATE DATABASE Profile;');
192320
actual.tokens = aggregateUnknownTokens(actual.tokens);

0 commit comments

Comments
 (0)