forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwith.js
More file actions
22 lines (19 loc) · 712 Bytes
/
Copy pathwith.js
File metadata and controls
22 lines (19 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { columnRefToSQL } from './column'
import { exprToSQL } from './expr'
import { identifierToSql, literalToSQL } from './util'
/**
* @param {Array<Object>} withExpr
*/
function withToSQL(withExpr) {
if (!withExpr || withExpr.length === 0) return
const isRecursive = withExpr[0].recursive ? 'RECURSIVE ' : ''
const withExprStr = withExpr.map(cte => {
const { name, stmt, columns } = cte
const column = Array.isArray(columns) ? `(${columns.map(columnRefToSQL).join(', ')})` : ''
return `${name.type === 'default' ? identifierToSql(name.value) : literalToSQL(name)}${column} AS (${exprToSQL(stmt)})`
}).join(', ')
return `WITH ${isRecursive}${withExprStr}`
}
export {
withToSQL,
}