@@ -106,6 +106,53 @@ describe("makeQueryTemplate", () => {
106106 assert . deepStrictEqual ( params , [ "val1" ] ) ;
107107 } ) ;
108108
109+ it ( "makeQueryTemplate filter and escape filters column" , ( ) => {
110+ const source = { name : "db" , dialect : "postgres" , escape : ( i ) => `_${ i } _` } ;
111+ const operations = {
112+ ...baseOperations ,
113+ filter : [
114+ {
115+ type : "eq" ,
116+ operands : [
117+ { type : "column" , value : "col2" } ,
118+ { type : "resolved" , value : "val1" }
119+ ]
120+ }
121+ ]
122+ } ;
123+
124+ const [ parts , ...params ] = makeQueryTemplate ( operations , source ) ;
125+ assert . deepStrictEqual (
126+ parts . join ( "?" ) ,
127+ "SELECT t._col1_,t._col2_ FROM table1 t\nWHERE t._col2_ = ?"
128+ ) ;
129+ assert . deepStrictEqual ( params , [ "val1" ] ) ;
130+ } ) ;
131+
132+ it ( "makeQueryTemplate filter and escape filters column only once" , ( ) => {
133+ const source = { name : "db" , dialect : "postgres" , escape : ( i ) => `_${ i } _` } ;
134+ const operations = {
135+ ...baseOperations ,
136+ filter : [
137+ {
138+ type : "eq" ,
139+ operands : [
140+ { type : "column" , value : "col2" } ,
141+ { type : "resolved" , value : "val1" }
142+ ]
143+ }
144+ ]
145+ } ;
146+
147+ makeQueryTemplate ( operations , source ) ;
148+ const [ parts , ...params ] = makeQueryTemplate ( operations , source ) ;
149+ assert . deepStrictEqual (
150+ parts . join ( "?" ) ,
151+ "SELECT t._col1_,t._col2_ FROM table1 t\nWHERE t._col2_ = ?"
152+ ) ;
153+ assert . deepStrictEqual ( params , [ "val1" ] ) ;
154+ } ) ;
155+
109156 it ( "makeQueryTemplate filter list" , ( ) => {
110157 const source = { name : "db" , dialect : "postgres" } ;
111158 const operations = {
@@ -164,6 +211,24 @@ describe("makeQueryTemplate", () => {
164211 assert . deepStrictEqual ( params , [ ] ) ;
165212 } ) ;
166213
214+ it ( "makeQueryTemplate sort and escape sort column" , ( ) => {
215+ const source = { name : "db" , dialect : "mysql" , escape : ( i ) => `_${ i } _` } ;
216+ const operations = {
217+ ...baseOperations ,
218+ sort : [
219+ { column : "col1" , direction : "asc" } ,
220+ { column : "col2" , direction : "desc" }
221+ ]
222+ } ;
223+
224+ const [ parts , ...params ] = makeQueryTemplate ( operations , source ) ;
225+ assert . deepStrictEqual (
226+ parts . join ( "?" ) ,
227+ "SELECT t._col1_,t._col2_ FROM table1 t\nORDER BY t._col1_ ASC, t._col2_ DESC"
228+ ) ;
229+ assert . deepStrictEqual ( params , [ ] ) ;
230+ } ) ;
231+
167232 it ( "makeQueryTemplate slice" , ( ) => {
168233 const source = { name : "db" , dialect : "mysql" } ;
169234 const operations = { ...baseOperations } ;
@@ -215,6 +280,87 @@ describe("makeQueryTemplate", () => {
215280 assert . deepStrictEqual ( parts . join ( "?" ) , "SELECT t.col1,t.col2,t.col3 FROM table1 t\nWHERE t.col1 >= ?\nAND t.col2 = ?\nORDER BY t.col1 ASC\nLIMIT 90 OFFSET 10" ) ;
216281 assert . deepStrictEqual ( params , [ "val1" , "val2" ] ) ;
217282 } ) ;
283+
284+ it ( "makeQueryTemplate select, slice and escape column name with mssql syntax" , ( ) => {
285+ const source = { name : "db" , dialect : "mssql" , escape : ( i ) => `_${ i } _` } ;
286+ const operations = {
287+ ...baseOperations ,
288+ select : {
289+ columns : [ "col1" , "col2" , "col3" ]
290+ } ,
291+ slice : { to : 100 }
292+ } ;
293+
294+ const [ parts ] = makeQueryTemplate ( operations , source ) ;
295+ assert . deepStrictEqual (
296+ parts . join ( "?" ) ,
297+ "SELECT t._col1_,t._col2_,t._col3_ FROM table1 t\nORDER BY t._col1_ ASC\nOFFSET 0 ROWS\nFETCH NEXT 100 ROWS ONLY"
298+ ) ;
299+ } ) ;
300+
301+ it ( "makeQueryTemplate select, sort, slice, filter indexed with mssql syntax" , ( ) => {
302+ const source = { name : "db" , dialect : "mssql" } ;
303+ const operations = {
304+ ...baseOperations ,
305+ select : {
306+ columns : [ "col1" , "col2" , "col3" ]
307+ } ,
308+ sort : [ { column : "col2" , direction : "desc" } ] ,
309+ slice : { from : 10 , to : 100 } ,
310+ filter : [
311+ {
312+ type : "gte" ,
313+ operands : [
314+ { type : "column" , value : "col1" } ,
315+ { type : "resolved" , value : "val1" }
316+ ]
317+ } ,
318+ {
319+ type : "eq" ,
320+ operands : [
321+ { type : "column" , value : "col2" } ,
322+ { type : "resolved" , value : "val2" }
323+ ]
324+ }
325+ ]
326+ } ;
327+
328+ const [ parts , ...params ] = makeQueryTemplate ( operations , source ) ;
329+ assert . deepStrictEqual ( parts . join ( "?" ) , "SELECT t.col1,t.col2,t.col3 FROM table1 t\nWHERE t.col1 >= ?\nAND t.col2 = ?\nORDER BY t.col2 DESC\nOFFSET 10 ROWS\nFETCH NEXT 90 ROWS ONLY" ) ;
330+ assert . deepStrictEqual ( params , [ "val1" , "val2" ] ) ;
331+ } ) ;
332+
333+ it ( "makeQueryTemplate throw if no columns are explicitly specified for mssql dialect" , ( ) => {
334+ const source = { name : "db" , dialect : "mssql" } ;
335+ const operations = {
336+ ...baseOperations ,
337+ select : {
338+ columns : null
339+ } ,
340+ sort : [ ] ,
341+ slice : { from : 10 , to : 100 }
342+ } ;
343+
344+ assert . throws ( ( ) => {
345+ makeQueryTemplate ( operations , source ) ;
346+ } , Error ) ;
347+ } ) ;
348+
349+ it ( "makeQueryTemplate the sort and slice if no columns are explicitly BUT sort has value for mssql dialect" , ( ) => {
350+ const source = { name : "db" , dialect : "mssql" } ;
351+ const operations = {
352+ ...baseOperations ,
353+ select : {
354+ columns : null
355+ } ,
356+ sort : [ { column : "col2" , direction : "desc" } ] ,
357+ slice : { from : 10 , to : 100 }
358+ } ;
359+
360+ const [ parts , ...params ] = makeQueryTemplate ( operations , source ) ;
361+ assert . deepStrictEqual ( parts . join ( "?" ) , "SELECT * FROM table1 t\nORDER BY t.col2 DESC\nOFFSET 10 ROWS\nFETCH NEXT 90 ROWS ONLY" ) ;
362+ assert . deepStrictEqual ( params , [ ] ) ;
363+ } ) ;
218364} ) ;
219365
220366describe ( "__table" , ( ) => {
0 commit comments