forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-struct.js
More file actions
47 lines (43 loc) · 1.19 KB
/
array-struct.js
File metadata and controls
47 lines (43 loc) · 1.19 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
import { columnsToSQL } from './column'
import { exprToSQL } from './expr'
import { arrayStructTypeToSQL, hasVal, toUpper } from './util'
function arrayExprListToSQL(expr) {
const {
array_path: arrayPath,
brackets,
expr_list: exprList,
parentheses,
} = expr
if (!exprList) return `[${columnsToSQL(arrayPath)}]`
const result = Array.isArray(exprList) ? exprList.map(col => `(${columnsToSQL(col)})`).filter(hasVal).join(', ') : exprToSQL(exprList)
if (brackets) return `[${result}]`
return parentheses ? `(${result})` : result
}
function arrayStructValueToSQL(expr) {
const {
expr_list: exprList,
type,
} = expr
switch (toUpper(type)) {
case 'STRUCT':
return `(${columnsToSQL(exprList)})`
case 'ARRAY':
return arrayExprListToSQL(expr)
default:
return ''
}
}
function arrayStructExprToSQL(expr) {
const { definition, keyword } = expr
const result = [toUpper(keyword)]
if (definition && typeof definition === 'object') {
result.length = 0
result.push(arrayStructTypeToSQL(definition))
}
result.push(arrayStructValueToSQL(expr))
return result.filter(hasVal).join('')
}
export {
arrayStructExprToSQL,
arrayStructValueToSQL,
}