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
66 lines (59 loc) · 1.96 KB
/
window.js
File metadata and controls
66 lines (59 loc) · 1.96 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
import { hasVal, toUpper } from './util'
import { exprToSQL, orderOrPartitionByToSQL } from './expr'
import { overToSQL } from './over'
function windowFrameExprToSQL(windowFrameExpr) {
if (!windowFrameExpr) return
const { type } = windowFrameExpr
if (type === 'rows') {
return [toUpper(type), exprToSQL(windowFrameExpr.expr)].filter(hasVal).join(' ')
}
return exprToSQL(windowFrameExpr)
}
function windowSpecificationToSQL(windowSpec) {
const {
name,
partitionby,
orderby,
window_frame_clause: windowFrame,
} = windowSpec
const result = [
name,
orderOrPartitionByToSQL(partitionby, 'partition by'),
orderOrPartitionByToSQL(orderby, 'order by'),
windowFrameExprToSQL(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 constructArgsList(expr) {
const { args, name, consider_nulls = '', separator = ', ' } = expr
const argsList = args ? exprToSQL(args).join(separator) : ''
// cover Syntax from FN_NAME(...args [RESPECT NULLS]) [RESPECT NULLS]
const result = [name, '(', argsList, ')', consider_nulls && ' ', consider_nulls]
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,
}