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
22 lines (20 loc) · 979 Bytes
/
aggregation.js
File metadata and controls
22 lines (20 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)
const fnName = expr.name
const overStr = overToSQL(over)
const separator = ' '
if (args.distinct) str = ['DISTINCT', str].join(separator)
if (args.orderby) str = `${str} ${orderOrPartitionByToSQL(args.orderby, 'order by')}`
if (args.separator) 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,
}