forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregation.js
More file actions
25 lines (23 loc) · 1.26 KB
/
aggregation.js
File metadata and controls
25 lines (23 loc) · 1.26 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
import { exprToSQL, orderOrPartitionByToSQL } from './expr'
import { hasVal, literalToSQL, toUpper } from './util'
import { overToSQL } from './over'
function aggrToSQL(expr) {
/** @type {Object} */
const { args, filter, over, within_group_orderby } = expr
let str = exprToSQL(args.expr)
str = Array.isArray(str) ? str.join(', ') : str
const fnName = expr.name
const overStr = overToSQL(over)
const separator = ' '
if (args.distinct) str = ['DISTINCT', str].join(separator)
if (args.separator && args.separator.delimiter) str = [str, literalToSQL(args.separator.delimiter)].join(`${args.separator.symbol} `)
if (args.separator && args.separator.expr) str = [str, exprToSQL(args.separator.expr)].join(' ')
if (args.orderby) str = [str, orderOrPartitionByToSQL(args.orderby, 'order by')].join(' ')
if (args.separator && args.separator.value) str = [str, toUpper(args.separator.keyword), literalToSQL(args.separator.value)].filter(hasVal).join(' ')
const withinGroup = within_group_orderby ? `WITHIN GROUP (${orderOrPartitionByToSQL(within_group_orderby, 'order by')})` : ''
const filterStr = filter ? `FILTER (WHERE ${exprToSQL(filter.where)})` : ''
return [`${fnName}(${str})`, withinGroup, overStr, filterStr].filter(hasVal).join(' ')
}
export {
aggrToSQL,
}