@@ -64,19 +64,25 @@ const Builder = module.exports.Builder = class Builder extends NotTagged {
6464 this . rest = rest
6565 }
6666
67- build ( before , parameters , types , transform ) {
67+ build ( before , parameters , types , options ) {
6868 const keyword = builders . map ( ( [ x , fn ] ) => ( { fn, i : before . search ( x ) } ) ) . sort ( ( a , b ) => a . i - b . i ) . pop ( )
6969 if ( keyword . i === - 1 )
7070 throw new Error ( 'Could not infer helper mode' )
7171
72- return keyword . fn ( this . first , this . rest , parameters , types , transform )
72+ return keyword . fn ( this . first , this . rest , parameters , types , options )
7373 }
7474}
7575
76- module . exports . handleValue = handleValue ; function handleValue ( x , parameters , types ) {
77- const value = x instanceof Parameter ? x . value : x
78- if ( value === undefined )
79- throw Errors . generic ( 'UNDEFINED_VALUE' , 'Undefined values are not allowed' )
76+ module . exports . handleValue = handleValue ; function handleValue ( x , parameters , types , options ) {
77+ let value = x instanceof Parameter ? x . value : x
78+ if ( value === undefined ) {
79+ x instanceof Parameter
80+ ? x . value = options . transform . undefined
81+ : value = x = options . transform . undefined
82+
83+ if ( value === undefined )
84+ throw Errors . generic ( 'UNDEFINED_VALUE' , 'Undefined values are not allowed' )
85+ }
8086
8187 return '$' + ( types . push (
8288 x instanceof Parameter
@@ -90,40 +96,59 @@ module.exports.handleValue = handleValue;function handleValue(x, parameters, typ
9096
9197const defaultHandlers = typeHandlers ( types )
9298
93- function valuesBuilder ( first , parameters , types , transform , columns ) {
99+ module . exports . stringify = stringify ; function stringify ( q , string , value , parameters , types , options ) { // eslint-disable-line
100+ for ( let i = 1 ; i < q . strings . length ; i ++ ) {
101+ string += (
102+ value instanceof Query ? fragment ( value , parameters , types ) :
103+ value instanceof Identifier ? value . value :
104+ value instanceof Builder ? value . build ( string , parameters , types , options ) :
105+ handleValue ( value , parameters , types , options )
106+ ) + q . strings [ i ]
107+ value = q . args [ i ]
108+ }
109+
110+ return string
111+ }
112+
113+ function fragment ( q , parameters , types ) {
114+ q . fragment = true
115+ return stringify ( q , q . strings [ 0 ] , q . args [ 0 ] , parameters , types )
116+ }
117+
118+ function valuesBuilder ( first , parameters , types , columns , options ) {
94119 let value
95120 return first . map ( row =>
96121 '(' + columns . map ( column => {
97122 value = row [ column ]
98123 return (
99- value instanceof Query ? value . strings [ 0 ] :
124+ value instanceof Query ? fragment ( value , parameters , types ) :
100125 value instanceof Identifier ? value . value :
101- handleValue ( value , parameters , types )
126+ handleValue ( value , parameters , types , options )
102127 )
103128 } ) . join ( ',' ) + ')'
104129 ) . join ( ',' )
105130}
106131
107- function values ( first , rest , parameters , types , transform ) {
132+ function values ( first , rest , parameters , types , options ) {
108133 const multi = Array . isArray ( first [ 0 ] )
109134 const columns = rest . length ? rest . flat ( ) : Object . keys ( multi ? first [ 0 ] : first )
110- return valuesBuilder ( multi ? first : [ first ] , parameters , types , transform , columns )
135+ return valuesBuilder ( multi ? first : [ first ] , parameters , types , columns , options )
111136}
112137
113- function select ( first , rest , parameters , types , transform ) {
138+ function select ( first , rest , parameters , types , options ) {
114139 typeof first === 'string' && ( first = [ first ] . concat ( rest ) )
115140 if ( Array . isArray ( first ) )
116- return first . map ( x => escapeIdentifier ( transform . column . to ? transform . column . to ( x ) : x ) ) . join ( ',' )
141+ return first . map ( x => escapeIdentifier ( options . transform . column . to ? options . transform . column . to ( x ) : x ) ) . join ( ',' )
117142
118143 let value
119144 const columns = rest . length ? rest . flat ( ) : Object . keys ( first )
120145 return columns . map ( x => {
121146 value = first [ x ]
122147 return (
123- value instanceof Query ? value . strings [ 0 ] :
148+ value instanceof Query ? fragment ( value , parameters , types ) :
124149 value instanceof Identifier ? value . value :
125- handleValue ( value , parameters , types )
126- ) + ' as ' + escapeIdentifier ( transform . column . to ? transform . column . to ( x ) : x )
150+ handleValue ( value , parameters , types , options )
151+ ) + ' as ' + escapeIdentifier ( options . transform . column . to ? options . transform . column . to ( x ) : x )
127152 } ) . join ( ',' )
128153}
129154
@@ -133,19 +158,19 @@ const builders = Object.entries({
133158 select,
134159 returning : select ,
135160
136- update ( first , rest , parameters , types , transform ) {
161+ update ( first , rest , parameters , types , options ) {
137162 return ( rest . length ? rest . flat ( ) : Object . keys ( first ) ) . map ( x =>
138- escapeIdentifier ( transform . column . to ? transform . column . to ( x ) : x ) +
139- '=' + handleValue ( first [ x ] , parameters , types )
163+ escapeIdentifier ( options . transform . column . to ? options . transform . column . to ( x ) : x ) +
164+ '=' + handleValue ( first [ x ] , parameters , types , options )
140165 )
141166 } ,
142167
143- insert ( first , rest , parameters , types , transform ) {
168+ insert ( first , rest , parameters , types , options ) {
144169 const columns = rest . length ? rest . flat ( ) : Object . keys ( Array . isArray ( first ) ? first [ 0 ] : first )
145170 return '(' + columns . map ( x =>
146- escapeIdentifier ( transform . column . to ? transform . column . to ( x ) : x )
171+ escapeIdentifier ( options . transform . column . to ? options . transform . column . to ( x ) : x )
147172 ) . join ( ',' ) + ')values' +
148- valuesBuilder ( Array . isArray ( first ) ? first : [ first ] , parameters , types , transform , columns )
173+ valuesBuilder ( Array . isArray ( first ) ? first : [ first ] , parameters , types , columns , options )
149174 }
150175} ) . map ( ( [ x , fn ] ) => ( [ new RegExp ( '(^|[\\s(])' + x + '($|[\\s(])' , 'i' ) , fn ] ) )
151176
0 commit comments