Skip to content

Commit

Permalink
feat: support select into and temporary table in tsql
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Jun 27, 2024
1 parent 3254d62 commit b764cf8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pegjs/transactsql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ select_stmt_nake
d:KW_DISTINCT? __
top:top_clause? __
c:column_clause __
ci:into_clause? __
f:from_clause? __
w:where_clause? __
g:group_by_clause? __
Expand All @@ -1426,6 +1427,10 @@ select_stmt_nake
options: opts,
distinct: d,
columns: c,
into: {
...(ci || {}),
position: ci && 'column',
},
from: f,
for: fx,
where: w,
Expand Down Expand Up @@ -1515,6 +1520,14 @@ alias_clause
= KW_AS __ i:alias_ident { return i; }
/ KW_AS? __ i:ident { return i; }

into_clause
= KW_INTO __ f:ident {
return {
type: 'into',
expr: f
}
}

from_clause
= KW_FROM __ l:table_ref_list __ op:pivot_operator? {
if (l[0]) l[0].operator = op
Expand Down Expand Up @@ -1850,6 +1863,10 @@ table_name
v.table = v.name;
return v;
}
/ p:('##' / '#') n:ident {
return { db: null, table: `${p}${n}` }
}

or_and_expr
= head:expr tail:(__ (KW_AND / KW_OR) __ expr)* {
const len = tail.length
Expand Down Expand Up @@ -3133,7 +3150,6 @@ ___
comment
= block_comment
/ line_comment
/ pound_sign_comment

block_comment
= "/*" (!"*/" !"/*" char / block_comment)* "*/"
Expand Down
22 changes: 22 additions & 0 deletions test/transactsql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,26 @@ describe('transactsql', () => {
expect(getParsedSql(sql)).to.be.equal("SELECT * FROM (VALUES (0,0), (1,NULL), (NULL,2), (3,4)) AS [t(a, b)]")
})
})
const SQL_LIST = [
{
title: 'select from temp table',
sql: [
'SELECT * FROM #TempLocationCol',
'SELECT * FROM [#TempLocationCol]'
]
},
{
title: 'select into clause',
sql: [
'SELECT * INTO #temp_table FROM tableName',
'SELECT * INTO [#temp_table] FROM [tableName]'
]
}
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
it(`should support ${title}`, () => {
expect(getParsedSql(sql[0], tsqlOpt)).to.equal(sql[1])
})
})
})

0 comments on commit b764cf8

Please sign in to comment.