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
42 lines (39 loc) · 1.55 KB
/
Copy pathselect.js
File metadata and controls
42 lines (39 loc) · 1.55 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
import { exprToSQL, getExprListSQL, orderOrPartitionByToSQL } from './expr'
import { columnsToSQL } from './column'
import { withToSql } from './with'
import { tablesToSQL } from './tables'
import { hasVal, commonOptionConnector, connector } 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 { with: withInfo, options, distinct, columns, from, where, groupby, having, orderby, limit } = stmt
const clauses = [withToSql(withInfo), 'SELECT']
if (Array.isArray(options)) clauses.push(options.join(' '))
clauses.push(distinct, columnsToSQL(columns, from))
// FROM + joins
clauses.push(commonOptionConnector('FROM', tablesToSQL, from))
clauses.push(commonOptionConnector('WHERE', exprToSQL, where))
clauses.push(connector('GROUP BY', getExprListSQL(groupby).join(', ')))
clauses.push(commonOptionConnector('HAVING', exprToSQL, having))
clauses.push(orderOrPartitionByToSQL(orderby, 'order by'))
if (limit) {
const { seperator, value } = limit
clauses.push(connector('LIMIT', value.map(exprToSQL).join(`${seperator === 'offset' ? ' ' : ''}${seperator.toUpperCase()} `)))
}
return clauses.filter(hasVal).join(' ')
}
export {
selectToSQL,
}