forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.js
More file actions
48 lines (45 loc) · 1.91 KB
/
select.js
File metadata and controls
48 lines (45 loc) · 1.91 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
import { exprToSQL, getExprListSQL, orderOrPartitionByToSQL } from './expr'
import { columnsToSQL } from './column'
import { limitToSQL } from './limit'
import { withToSQL } from './with'
import { tablesToSQL } from './tables'
import { hasVal, commonOptionConnector, connector, topToSQL, toUpper } from './util'
/**
* @param {Object} stmt
* @param {?Array} stmt.with
* @param {?Array} stmt.options
* @param {?string} stmt.distinct
* @param {?Array|string} stmt.columns
* @param {?Array} stmt.from
* @param {?Object} stmt.where
* @param {?Array} stmt.groupby
* @param {?Object} stmt.having
* @param {?Array} stmt.orderby
* @param {?Array} stmt.limit
* @return {string}
*/
function selectToSQL(stmt) {
const {
as_struct_val: asStructVal, columns, distinct, from, for_sys_time_as_of: forSystem = {}, for_update: forUpdate, groupby, having, limit, options, orderby, parentheses_symbol: parentheses, top, window: windowInfo, with: withInfo, where,
} = stmt
const clauses = [withToSQL(withInfo), 'SELECT', toUpper(asStructVal)]
clauses.push(topToSQL(top))
if (Array.isArray(options)) clauses.push(options.join(' '))
clauses.push(distinct, columnsToSQL(columns, from))
// FROM + joins
clauses.push(commonOptionConnector('FROM', tablesToSQL, from))
const { keyword, expr } = forSystem || {}
clauses.push(commonOptionConnector(keyword, exprToSQL, expr))
clauses.push(commonOptionConnector('WHERE', exprToSQL, where))
clauses.push(connector('GROUP BY', getExprListSQL(groupby).join(', ')))
clauses.push(commonOptionConnector('HAVING', exprToSQL, having))
clauses.push(commonOptionConnector('WINDOW', exprToSQL, windowInfo))
clauses.push(orderOrPartitionByToSQL(orderby, 'order by'))
clauses.push(limitToSQL(limit))
clauses.push(toUpper(forUpdate))
const sql = clauses.filter(hasVal).join(' ')
return parentheses ? `(${sql})` : sql
}
export {
selectToSQL,
}