File tree Expand file tree Collapse file tree 2 files changed +41
-3
lines changed Expand file tree Collapse file tree 2 files changed +41
-3
lines changed Original file line number Diff line number Diff line change 8
8
9
9
var defaults = require ( './defaults' ) ;
10
10
11
+ function escapeElement ( elementRepresentation ) {
12
+ var escaped = elementRepresentation
13
+ . replace ( / \\ / g, '\\\\' )
14
+ . replace ( / " / g, '\\"' ) ;
15
+
16
+ return '"' + escaped + '"' ;
17
+ }
18
+
11
19
// convert a JS array to a postgres array literal
12
20
// uses comma separator so won't work for types like box that use
13
21
// a different array separator.
@@ -25,7 +33,7 @@ function arrayString(val) {
25
33
}
26
34
else
27
35
{
28
- result = result + JSON . stringify ( prepareValue ( val [ i ] ) ) ;
36
+ result += escapeElement ( prepareValue ( val [ i ] ) ) ;
29
37
}
30
38
}
31
39
result = result + '}' ;
@@ -104,15 +112,15 @@ function dateToString(date) {
104
112
}
105
113
106
114
function dateToStringUTC ( date ) {
107
-
115
+
108
116
var ret = pad ( date . getUTCFullYear ( ) , 4 ) + '-' +
109
117
pad ( date . getUTCMonth ( ) + 1 , 2 ) + '-' +
110
118
pad ( date . getUTCDate ( ) , 2 ) + 'T' +
111
119
pad ( date . getUTCHours ( ) , 2 ) + ':' +
112
120
pad ( date . getUTCMinutes ( ) , 2 ) + ':' +
113
121
pad ( date . getUTCSeconds ( ) , 2 ) + '.' +
114
122
pad ( date . getUTCMilliseconds ( ) , 3 ) ;
115
-
123
+
116
124
return ret + "+00:00" ;
117
125
}
118
126
Original file line number Diff line number Diff line change 1
1
var helper = require ( __dirname + "/test-helper" ) ;
2
2
var pg = helper . pg ;
3
3
4
+ test ( 'serializing arrays' , function ( ) {
5
+ pg . connect ( helper . config , assert . calls ( function ( err , client , done ) {
6
+ assert . isNull ( err ) ;
7
+
8
+ test ( 'nulls' , function ( ) {
9
+ client . query ( 'SELECT $1::text[] as array' , [ [ null ] ] , assert . success ( function ( result ) {
10
+ var array = result . rows [ 0 ] . array ;
11
+ assert . lengthIs ( array , 1 ) ;
12
+ assert . isNull ( array [ 0 ] ) ;
13
+ } ) ) ;
14
+ } ) ;
15
+
16
+ test ( 'elements containing JSON-escaped characters' , function ( ) {
17
+ var param = '\\"\\"' ;
18
+
19
+ for ( var i = 1 ; i <= 0x1f ; i ++ ) {
20
+ param += String . fromCharCode ( i ) ;
21
+ }
22
+
23
+ client . query ( 'SELECT $1::text[] as array' , [ [ param ] ] , assert . success ( function ( result ) {
24
+ var array = result . rows [ 0 ] . array ;
25
+ assert . lengthIs ( array , 1 ) ;
26
+ assert . equal ( array [ 0 ] , param ) ;
27
+ } ) ) ;
28
+
29
+ done ( ) ;
30
+ } ) ;
31
+ } ) ) ;
32
+ } ) ;
33
+
4
34
test ( 'parsing array results' , function ( ) {
5
35
pg . connect ( helper . config , assert . calls ( function ( err , client , done ) {
6
36
assert . isNull ( err ) ;
You can’t perform that action at this time.
0 commit comments