From b764cf89eca333d1d8a429323ea2da039f73c2a6 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Thu, 27 Jun 2024 09:15:29 +0800 Subject: [PATCH] feat: support select into and temporary table in tsql --- pegjs/transactsql.pegjs | 18 +++++++++++++++++- test/transactsql.spec.js | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/pegjs/transactsql.pegjs b/pegjs/transactsql.pegjs index a1f7d49e..320c58ca 100644 --- a/pegjs/transactsql.pegjs +++ b/pegjs/transactsql.pegjs @@ -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? __ @@ -1426,6 +1427,10 @@ select_stmt_nake options: opts, distinct: d, columns: c, + into: { + ...(ci || {}), + position: ci && 'column', + }, from: f, for: fx, where: w, @@ -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 @@ -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 @@ -3133,7 +3150,6 @@ ___ comment = block_comment / line_comment - / pound_sign_comment block_comment = "/*" (!"*/" !"/*" char / block_comment)* "*/" diff --git a/test/transactsql.spec.js b/test/transactsql.spec.js index d326a4af..d400b7f1 100644 --- a/test/transactsql.spec.js +++ b/test/transactsql.spec.js @@ -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]) + }) + }) })