Skip to content

Commit e35a8e5

Browse files
committed
Pass options properly and test nesting
1 parent 3300c40 commit e35a8e5

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

src/types.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,35 @@ export function handleValue(x, parameters, types, options) {
9696

9797
const defaultHandlers = typeHandlers(types)
9898

99-
export function stringify(q, string, value, parameters, types, o) { // eslint-disable-line
99+
export function stringify(q, string, value, parameters, types, options) { // eslint-disable-line
100100
for (let i = 1; i < q.strings.length; i++) {
101-
string += (
102-
value && value[0] instanceof Query ? value.reduce((acc, x) => acc + ' ' + fragment(x, parameters, types, o), '') :
103-
value instanceof Query ? fragment(value, parameters, types, o) :
104-
value instanceof Identifier ? value.value :
105-
value instanceof Builder ? value.build(string, parameters, types, o) :
106-
handleValue(value, parameters, types, o)
107-
) + q.strings[i]
101+
string += (stringifyValue(string, value, parameters, types, options)) + q.strings[i]
108102
value = q.args[i]
109103
}
110104

111105
return string
112106
}
113107

108+
function stringifyValue(string, value, parameters, types, o) {
109+
return (
110+
value instanceof Builder ? value.build(string, parameters, types, o) :
111+
value instanceof Query ? fragment(value, parameters, types, o) :
112+
value instanceof Identifier ? value.value :
113+
value && value[0] instanceof Query ? value.reduce((acc, x) => acc + ' ' + fragment(x, parameters, types, o), '') :
114+
handleValue(value, parameters, types, o)
115+
)
116+
}
117+
114118
function fragment(q, parameters, types, options) {
115119
q.fragment = true
116120
return stringify(q, q.strings[0], q.args[0], parameters, types, options)
117121
}
118122

119123
function valuesBuilder(first, parameters, types, columns, options) {
120-
let value
121124
return first.map(row =>
122-
'(' + columns.map(column => {
123-
value = row[column]
124-
return (
125-
value instanceof Query ? fragment(value, parameters, types) :
126-
value instanceof Identifier ? value.value :
127-
handleValue(value, parameters, types, options)
128-
)
129-
}).join(',') + ')'
125+
'(' + columns.map(column =>
126+
stringifyValue('values', row[column], parameters, types, options)
127+
).join(',') + ')'
130128
).join(',')
131129
}
132130

@@ -146,7 +144,7 @@ function select(first, rest, parameters, types, options) {
146144
return columns.map(x => {
147145
value = first[x]
148146
return (
149-
value instanceof Query ? fragment(value, parameters, types) :
147+
value instanceof Query ? fragment(value, parameters, types, options) :
150148
value instanceof Identifier ? value.value :
151149
handleValue(value, parameters, types, options)
152150
) + ' as ' + escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)

tests/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,18 @@ t('Supports nested fragments with parameters', async() => {
21012101
]
21022102
})
21032103

2104+
t('Supports multiple nested fragments with parameters', async() => {
2105+
const [{ b }] = await sql`select * ${
2106+
sql`from ${
2107+
sql`(values (2, ${ 1 }::int)) as x(${ sql(['a', 'b']) })`
2108+
}`
2109+
}`
2110+
return [
2111+
1,
2112+
b
2113+
]
2114+
})
2115+
21042116
t('Supports arrays of fragments', async() => {
21052117
const [{ x }] = await sql`
21062118
${ [sql`select`, sql`1`, sql`as`, sql`x`] }

0 commit comments

Comments
 (0)