Skip to content

Commit 88b5d6f

Browse files
committed
Build deno & cjs
1 parent fcca4a4 commit 88b5d6f

File tree

5 files changed

+113
-88
lines changed

5 files changed

+113
-88
lines changed

cjs/src/types.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,35 +98,33 @@ const defaultHandlers = typeHandlers(types)
9898

9999
module.exports.stringify = stringify;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), '') :
103-
value instanceof Query ? fragment(value, parameters, types) :
104-
value instanceof Identifier ? value.value :
105-
value instanceof Builder ? value.build(string, parameters, types, options) :
106-
handleValue(value, parameters, types, options)
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

114-
function fragment(q, parameters, types) {
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+
118+
function fragment(q, parameters, types, options) {
115119
q.fragment = true
116-
return stringify(q, q.strings[0], q.args[0], parameters, types)
120+
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)
@@ -157,6 +155,7 @@ const builders = Object.entries({
157155
values,
158156
in: values,
159157
select,
158+
as: select,
160159
returning: select,
161160

162161
update(first, rest, parameters, types, options) {

cjs/tests/index.js

Lines changed: 12 additions & 0 deletions
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`] }

deno/src/types.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,35 +99,33 @@ const defaultHandlers = typeHandlers(types)
9999

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

112106
return string
113107
}
114108

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

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

@@ -147,7 +145,7 @@ function select(first, rest, parameters, types, options) {
147145
return columns.map(x => {
148146
value = first[x]
149147
return (
150-
value instanceof Query ? fragment(value, parameters, types) :
148+
value instanceof Query ? fragment(value, parameters, types, options) :
151149
value instanceof Identifier ? value.value :
152150
handleValue(value, parameters, types, options)
153151
) + ' as ' + escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)
@@ -158,6 +156,7 @@ const builders = Object.entries({
158156
values,
159157
in: values,
160158
select,
159+
as: select,
161160
returning: select,
162161

163162
update(first, rest, parameters, types, options) {

deno/tests/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,18 @@ t('Supports nested fragments with parameters', async() => {
21032103
]
21042104
})
21052105

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

0 commit comments

Comments
 (0)