forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit.js
More file actions
28 lines (23 loc) · 785 Bytes
/
limit.js
File metadata and controls
28 lines (23 loc) · 785 Bytes
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
import { connector, toUpper, hasVal } from './util'
import { exprToSQL } from './expr'
function composePrefixValSuffix(stmt) {
if (!stmt) return []
return [toUpper(stmt.prefix), exprToSQL(stmt.value), toUpper(stmt.suffix)]
}
function fetchOffsetToSQL(stmt) {
const { fetch, offset } = stmt
const result = [...composePrefixValSuffix(offset), ...composePrefixValSuffix(fetch)]
return result.filter(hasVal).join(' ')
}
function limitOffsetToSQL(limit) {
const { seperator, value } = limit
return connector('LIMIT', value.map(exprToSQL).join(`${seperator === 'offset' ? ' ' : ''}${toUpper(seperator)} `))
}
function limitToSQL(limit) {
if (!limit) return ''
if (limit.fetch) return fetchOffsetToSQL(limit)
return limitOffsetToSQL(limit)
}
export {
limitToSQL,
}