forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.js
More file actions
43 lines (39 loc) · 1.29 KB
/
func.js
File metadata and controls
43 lines (39 loc) · 1.29 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
import { exprToSQL } from './expr'
import { hasVal, identifierToSql, toUpper } from './util'
import { overToSQL } from './over'
function castToSQL(expr) {
const { target, expr: expression, symbol, as: alias } = expr
const { length, dataType, parentheses, scale } = target
let str = ''
if (length) str = scale ? `${length}, ${scale}` : length
if (parentheses) str = `(${str})`
let prefix = exprToSQL(expression)
let symbolChar = '::'
let suffix = ''
if (symbol === 'as') {
prefix = `CAST(${prefix}`
suffix = ')'
symbolChar = ` ${symbol.toUpperCase()} `
}
if (alias) suffix += ` AS ${identifierToSql(alias)}`
return `${prefix}${symbolChar}${dataType}${str}${suffix}`
}
function extractFunToSQL(stmt) {
const { args, type } = stmt
const { field, cast_type: castType, source } = args
const result = [`${toUpper(type)}(${toUpper(field)}`, 'FROM', toUpper(castType), exprToSQL(source)]
return `${result.filter(hasVal).join(' ')})`
}
function funcToSQL(expr) {
const { args, name } = expr
if (!args) return name
const { parentheses, over } = expr
const str = `${name}(${exprToSQL(args).join(', ')})`
const overStr = overToSQL(over)
return [parentheses ? `(${str})` : str, overStr].filter(hasVal).join(' ')
}
export {
castToSQL,
extractFunToSQL,
funcToSQL,
}