forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.js
More file actions
71 lines (63 loc) · 1.99 KB
/
window.js
File metadata and controls
71 lines (63 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { hasVal, toUpper } from './util'
import { exprToSQL, orderOrPartitionByToSQL } from './expr'
import { overToSQL } from './over'
function windowSpecificationToSQL(windowSpec) {
const {
name,
partitionby,
orderby,
window_frame_clause: windowFrame,
} = windowSpec
const result = [
name,
orderOrPartitionByToSQL(partitionby, 'partition by'),
orderOrPartitionByToSQL(orderby, 'order by'),
toUpper(windowFrame),
]
return result.filter(hasVal).join(' ')
}
function asWindowSpecToSQL(asWindowSpec) {
if (typeof asWindowSpec === 'string') return asWindowSpec
const { window_specification: windowSpec } = asWindowSpec
return `(${windowSpecificationToSQL(windowSpec)})`
}
function namedWindowExprToSQL(namedWindowExpr) {
const { name, as_window_specification: asWindowSpec } = namedWindowExpr
return `${name} AS ${asWindowSpecToSQL(asWindowSpec)}`
}
function namedWindowExprListToSQL(namedWindowExprInfo) {
const { expr } = namedWindowExprInfo
return expr.map(namedWindowExprToSQL).join(', ')
}
function isConsiderNullsInArgs(fnName) {
// position of IGNORE/RESPECT NULLS varies by function
switch (fnName) {
case 'NTH_VALUE':
case 'LEAD':
case 'LAG':
return false
default:
return true
}
}
function constructArgsList(expr) {
const { args, name, consider_nulls = '' } = expr
const argsList = args ? exprToSQL(args).join(', ') : ''
// cover Syntax from FN_NAME(...args [RESPECT NULLS]) [RESPECT NULLS]
const isConsidernulls = isConsiderNullsInArgs(name)
const result = [name, '(', argsList, !isConsidernulls && ')', consider_nulls && ' ', consider_nulls, isConsidernulls && ')']
return result.filter(hasVal).join('')
}
function windowFuncToSQL(expr) {
const { over } = expr
const str = constructArgsList(expr)
const overStr = overToSQL(over)
return [str, overStr].filter(hasVal).join(' ')
}
export {
asWindowSpecToSQL,
namedWindowExprToSQL,
namedWindowExprListToSQL,
windowFuncToSQL,
windowSpecificationToSQL,
}