@@ -37,7 +37,7 @@ export default function Postgres(url, options) {
37
37
, typeArrayMap = { }
38
38
39
39
function postgres ( xs , ...args ) {
40
- return query ( getConnection ( ) , xs , args )
40
+ return query ( false , getConnection ( ) , xs , args )
41
41
}
42
42
43
43
Object . assign ( postgres , {
@@ -74,7 +74,7 @@ export default function Postgres(url, options) {
74
74
savepoint = ''
75
75
} , connection ) {
76
76
begin && ( connection . savepoints = 0 )
77
- addTypes ( scoped )
77
+ addTypes ( scoped , connection )
78
78
scoped . savepoint = ( name , fn ) => new Promise ( ( resolve , reject ) => {
79
79
transaction ( {
80
80
savepoint : 'savepoint s' + connection . savepoints ++ + '_' + ( fn ? name : '' ) ,
@@ -84,7 +84,7 @@ export default function Postgres(url, options) {
84
84
} , connection )
85
85
} )
86
86
87
- query ( connection , raw ( begin || savepoint ) )
87
+ query ( true , connection , begin || savepoint )
88
88
. then ( ( ) => {
89
89
const result = fn ( scoped )
90
90
return ( Array . isArray ( result )
@@ -98,11 +98,11 @@ export default function Postgres(url, options) {
98
98
: resolve ( x )
99
99
)
100
100
. catch ( ( err ) => {
101
- query ( connection , raw (
101
+ query ( true , connection ,
102
102
begin
103
103
? 'rollback'
104
104
: 'rollback to ' + savepoint
105
- ) )
105
+ )
106
106
. then ( ( ) => reject ( err ) )
107
107
} )
108
108
. then ( ( ) => {
@@ -111,7 +111,7 @@ export default function Postgres(url, options) {
111
111
} )
112
112
113
113
function scoped ( xs , ...args ) {
114
- return query ( connection , xs , args ) . catch ( err => {
114
+ return query ( false , connection , xs , args ) . catch ( err => {
115
115
reject ( err )
116
116
throw err
117
117
} )
@@ -128,15 +128,15 @@ export default function Postgres(url, options) {
128
128
while ( ( x = queries . shift ( ) ) && ( c = getConnection ( x . fn ) ) ) {
129
129
x . fn
130
130
? transaction ( x , c )
131
- : c . send ( x . query , parse ( x . xs , x . args ) )
131
+ : send ( c , x . query , x . xs , x . args )
132
132
}
133
133
}
134
134
135
- function query ( connection , xs , args ) {
136
- if ( ! Array . isArray ( xs ) || ! Array . isArray ( xs . raw ) )
135
+ function query ( raw , connection , xs , args ) {
136
+ if ( ! raw && ( ! Array . isArray ( xs ) || ! Array . isArray ( xs . raw ) ) )
137
137
throw errors . generic ( { message : 'Query not called as a tagged template literal' , code : 'NOT_TAGGED_CALL' } )
138
138
139
- const query = { }
139
+ const query = { raw }
140
140
141
141
const promise = new Promise ( ( resolve , reject ) => {
142
142
query . resolve = resolve
@@ -156,7 +156,7 @@ export default function Postgres(url, options) {
156
156
157
157
function send ( connection , query , xs , args ) {
158
158
connection
159
- ? connection . send ( query , parse ( xs , args ) )
159
+ ? connection . send ( query , query . raw ? parseRaw ( xs , args ) : parse ( xs , args ) )
160
160
: queries . push ( { query, xs, args } )
161
161
}
162
162
@@ -179,11 +179,11 @@ export default function Postgres(url, options) {
179
179
180
180
function instance ( { fn } , connection ) {
181
181
let queries = 0
182
- addTypes ( scoped )
182
+ addTypes ( scoped , connection )
183
183
const container = fn ( scoped )
184
184
function scoped ( xs , ...args ) {
185
185
queries ++
186
- const promise = query ( connection , xs , args )
186
+ const promise = query ( false , connection , xs , args )
187
187
promise . then ( finished , finished )
188
188
return promise
189
189
}
@@ -230,11 +230,11 @@ export default function Postgres(url, options) {
230
230
function fetchArrayTypes ( connection ) {
231
231
return arrayTypesPromise || ( arrayTypesPromise =
232
232
new Promise ( ( resolve , reject ) => {
233
- send ( connection , { resolve, reject } , raw ( `
233
+ send ( connection , { resolve, reject, raw : true } , `
234
234
select oid, typelem
235
235
from pg_catalog.pg_type
236
236
where typcategory = 'A'
237
- ` ) )
237
+ ` )
238
238
} ) . then ( types => {
239
239
types . forEach ( ( { oid, typelem } ) => addArrayType ( oid , typelem ) )
240
240
ready = true
@@ -251,9 +251,10 @@ export default function Postgres(url, options) {
251
251
options . serializers [ oid ] = ( xs ) => arraySerializer ( xs , serializer )
252
252
}
253
253
254
- function addTypes ( instance ) {
254
+ function addTypes ( instance , connection ) {
255
255
Object . assign ( instance , {
256
256
notify : ( channel , payload ) => instance `select pg_notify(${ channel } , ${ String ( payload ) } )` ,
257
+ unsafe : ( xs , args ) => query ( true , connection || getConnection ( ) , xs , args ) ,
257
258
array,
258
259
rows,
259
260
row,
@@ -276,7 +277,7 @@ export default function Postgres(url, options) {
276
277
? listeners [ x ] . push ( fn )
277
278
: ( listeners [ x ] = [ fn ] )
278
279
279
- return query ( getListener ( ) , raw ( 'listen "' + x + '"' ) ) . then ( ( ) => x )
280
+ return query ( true , getListener ( ) , 'listen "' + x + '"' ) . then ( ( ) => x )
280
281
}
281
282
282
283
function getListener ( ) {
@@ -311,6 +312,21 @@ export default function Postgres(url, options) {
311
312
. then ( ( ) => clearTimeout ( destroy ) )
312
313
}
313
314
315
+ function parseRaw ( str , args = [ ] ) {
316
+ const types = [ ]
317
+ , xargs = args . map ( x => {
318
+ const type = getType ( x )
319
+ types . push ( type . type )
320
+ return type
321
+ } )
322
+
323
+ return {
324
+ sig : types + str ,
325
+ str,
326
+ args : xargs
327
+ }
328
+ }
329
+
314
330
function parse ( xs , args = [ ] ) {
315
331
const xargs = [ ]
316
332
, types = [ ]
@@ -345,16 +361,21 @@ export default function Postgres(url, options) {
345
361
}
346
362
347
363
function parseValue ( x , xargs , types ) {
364
+ const type = getType ( x )
365
+ types . push ( type . type )
366
+ return '$' + xargs . push ( type )
367
+ }
368
+
369
+ function getType ( x ) {
348
370
const value = x . value ? x . value : x
349
371
, type = x . type || ( Array . isArray ( value ) ? typeArrayMap [ inferType ( value ) ] : inferType ( value ) )
350
372
351
- types . push ( type )
352
- return '$' + xargs . push ( {
373
+ return {
353
374
type,
354
375
value : type
355
376
? ( options . serializers [ type ] || types . string . serialize ) ( value )
356
377
: value
357
- } )
378
+ }
358
379
}
359
380
}
360
381
0 commit comments