@@ -2,11 +2,19 @@ import { Buffer } from 'https://deno.land/
[email protected] /node/buffer.ts'
2
2
import { Query } from './query.js'
3
3
import { Errors } from './errors.js'
4
4
5
+ export const serialize = function serialize ( x ) {
6
+ return typeof x === 'string' ? x :
7
+ x instanceof Date ? types . date . serialize ( x ) :
8
+ x instanceof Uint8Array ? types . bytea . serialize ( x ) :
9
+ ( x === true || x === false ) ? types . boolean . serialize ( x ) :
10
+ '' + x
11
+ }
12
+
5
13
export const types = {
6
14
string : {
7
15
to : 25 ,
8
16
from : null , // defaults to string
9
- serialize : x => '' + x
17
+ serialize
10
18
} ,
11
19
number : {
12
20
to : 0 ,
@@ -67,10 +75,9 @@ export class Builder extends NotTagged {
67
75
68
76
build ( before , parameters , types , options ) {
69
77
const keyword = builders . map ( ( [ x , fn ] ) => ( { fn, i : before . search ( x ) } ) ) . sort ( ( a , b ) => a . i - b . i ) . pop ( )
70
- if ( keyword . i === - 1 )
71
- throw new Error ( 'Could not infer helper mode' )
72
-
73
- return keyword . fn ( this . first , this . rest , parameters , types , options )
78
+ return keyword . i === - 1
79
+ ? escapeIdentifiers ( this . first , options )
80
+ : keyword . fn ( this . first , this . rest , parameters , types , options )
74
81
}
75
82
}
76
83
@@ -138,7 +145,7 @@ function values(first, rest, parameters, types, options) {
138
145
function select ( first , rest , parameters , types , options ) {
139
146
typeof first === 'string' && ( first = [ first ] . concat ( rest ) )
140
147
if ( Array . isArray ( first ) )
141
- return first . map ( x => escapeIdentifier ( options . transform . column . to ? options . transform . column . to ( x ) : x ) ) . join ( ',' )
148
+ return escapeIdentifiers ( first , options )
142
149
143
150
let value
144
151
const columns = rest . length ? rest . flat ( ) : Object . keys ( first )
@@ -171,9 +178,7 @@ const builders = Object.entries({
171
178
172
179
insert ( first , rest , parameters , types , options ) {
173
180
const columns = rest . length ? rest . flat ( ) : Object . keys ( Array . isArray ( first ) ? first [ 0 ] : first )
174
- return '(' + columns . map ( x =>
175
- escapeIdentifier ( options . transform . column . to ? options . transform . column . to ( x ) : x )
176
- ) . join ( ',' ) + ')values' +
181
+ return '(' + escapeIdentifiers ( columns , options ) + ')values' +
177
182
valuesBuilder ( Array . isArray ( first ) ? first : [ first ] , parameters , types , columns , options )
178
183
}
179
184
} ) . map ( ( [ x , fn ] ) => ( [ new RegExp ( '((?:^|[\\s(])' + x + '(?:$|[\\s(]))(?![\\s\\S]*\\1)' , 'i' ) , fn ] ) )
@@ -210,16 +215,18 @@ function typeHandlers(types) {
210
215
} , { parsers : { } , serializers : { } } )
211
216
}
212
217
218
+ function escapeIdentifiers ( xs , { transform : { column } } ) {
219
+ return xs . map ( x => escapeIdentifier ( column . to ? column . to ( x ) : x ) ) . join ( ',' )
220
+ }
221
+
213
222
export const escapeIdentifier = function escape ( str ) {
214
223
return '"' + str . replace ( / " / g, '""' ) . replace ( / \. / g, '"."' ) + '"'
215
224
}
216
225
217
226
export const inferType = function inferType ( x ) {
218
227
return x instanceof Parameter
219
228
? x . type
220
- : Array . isArray ( x )
221
- ? inferType ( x [ 0 ] )
222
- : 0
229
+ : 0
223
230
}
224
231
225
232
const escapeBackslash = / \\ / g
0 commit comments