Skip to content

Commit

Permalink
Merge pull request #1878 from taozhi8833998/feat-set-stmt-tsql
Browse files Browse the repository at this point in the history
feat: support more set stmt in tsql
  • Loading branch information
taozhi8833998 authored Apr 24, 2024
2 parents 34934d7 + 62fbd14 commit 75f9c9d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
75 changes: 69 additions & 6 deletions pegjs/transactsql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,18 +1231,81 @@ rename_stmt
}
};
}
set_stmt_keywords
= 'result'i __ KW_SET __ 'caching'i {
return {
type: 'origin',
value: 'result set caching'
}
}
/ 'statistics'i __ d:('io'i / 'xml'i / 'profile'i / 'time'i) {
return {
type: 'origin',
value: `statistics ${d.toLowerCase()}`
}
}

set_stmt_keyword
= d:('datefirst'i / 'dateformat'i / 'deadlock_priority'i / 'lock_timeout'i / 'concat_null_yields_null'i / 'cursor_close_on_commit'i
/ 'fips_flagger'i / 'identity_insert'i / 'language'i / 'offsets'i / 'quoted_identifier'i / 'arithabort'i / 'arithignore'i / 'fmtonly'i
/ 'nocount'i / 'noexec'i / 'numberic_roundabort'i / 'parseonly'i / 'query_governor_cost_limit'i / 'rowcount'i / 'textsize'i / 'ansi_defaults'i
'ansi_null_dflt_off'i / 'ansi_null_dflt_on'i / 'ansi_nulls'i / 'ansi_padding'i / 'ansi_warnings'i / 'forceplan'i / 'showplan_all'i / 'showplan_text'i
/ 'showplan_xml'i / 'implicit_transactions'i / 'remote_proc_transactions'i / 'xact_abort'i) {
return {
type: 'origin',
value: d.toLowerCase()
}
}

set_transaction_iso_level
= 'read'i __ d:('uncommitted'i / 'committed'i) {
return {
type: 'origin',
value: `read ${d.toLowerCase()}`
}
}
/ 'REPEATABLE'i __ 'read'i {
return {
type: 'origin',
value: 'repeatable read'
}
}
/ d:('snapshot'i / 'serializable'i) {
return {
type: 'origin',
value: d.toLowerCase()
}
}

set_stmt
= KW_SET __
kw: (KW_GLOBAL / KW_SESSION / KW_LOCAL / KW_PERSIST / KW_PERSIST_ONLY)? __
a: assign_stmt {
a.keyword = kw
= KW_SET __ 'transaction'i __ 'isolation'i __ 'level'i __ e:set_transaction_iso_level{
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'set',
expr: a
expr: {
type: 'assign',
left: {
type: 'origin',
value: 'transaction isolation level'
},
right: e
}
}
}
}
/ KW_SET __ va:(set_stmt_keywords / set_stmt_keyword) __ e:proc_expr {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'set',
expr: {
type: 'assign',
left: va,
right: e
}
}
}
}
Expand Down Expand Up @@ -3103,7 +3166,7 @@ proc_stmt
}

assign_stmt
= va:(var_decl / without_prefix_var_decl) __ s: (KW_ASSIGN / KW_ASSIGIN_EQUAL) __ e:proc_expr {
= va:(var_decl / without_prefix_var_decl) __ s: (KW_ASSIGN / KW_ASSIGIN_EQUAL)? __ e:proc_expr {
return {
type: 'assign',
left: va,
Expand Down
3 changes: 2 additions & 1 deletion src/assign.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { exprToSQL } from './expr'
import { hasVal } from './util'

function assignToSQL(expr) {
/** @type {Object} */
const { left, right, symbol, keyword } = expr
left.keyword = keyword
const leftVar = exprToSQL(left)
const rightVal = exprToSQL(right)
return `${leftVar} ${symbol} ${rightVal}`
return [leftVar, symbol, rightVal].filter(hasVal).join(' ')
}

export {
Expand Down
10 changes: 10 additions & 0 deletions test/transactsql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ describe('transactsql', () => {
expect(getParsedSql(sql)).to.be.equal(`SELECT [node_1].[cnt] AS [cnt], [node_6].[wua_server_env_raw] AS [wua_server_env_raw] FROM [view_soc_246_node_1] AS [node_1] LEFT JOIN [dimension_soc_246_node_6] FOR SYSTEM_TIME ${opt.toUpperCase()} AS [node_6] ON [node_1].[id] = [node_6].[wua_server_env_raw]`);
}
})
it('should support set statements', () => {
let sql = 'SET NOCOUNT ON;'
expect(getParsedSql(sql)).to.be.equal('SET NOCOUNT ON')
sql = 'SET DATEFORMAT dmy;'
expect(getParsedSql(sql)).to.be.equal('SET DATEFORMAT dmy')
sql = 'SET STATISTICS IO OFF'
expect(getParsedSql(sql)).to.be.equal('SET STATISTICS IO OFF')
sql = 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED'
expect(getParsedSql(sql)).to.be.equal('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED')
})
describe('if else', () => {
it('should support if only statement', () => {
const sql = `IF EXISTS(SELECT 1 from sys.views where name='MyView' and type='v')
Expand Down

0 comments on commit 75f9c9d

Please sign in to comment.