2022-08-03 19:20:07 +02:00
const Sql = require ( "../base/sql" ) . default
2022-08-11 14:50:05 +02:00
const { SqlClient } = require ( "../utils" )
2021-06-03 17:31:24 +02:00
const TABLE_NAME = "test"
2022-08-31 20:21:45 +02:00
function endpoint ( table : any , operation : any ) {
2021-06-03 17:31:24 +02:00
return {
datasourceId : "Postgres" ,
operation : operation ,
entityId : table || TABLE_NAME ,
}
}
2022-08-31 20:21:45 +02:00
function generateReadJson ( {
table ,
fields ,
filters ,
sort ,
paginate ,
} : any = { } ) {
2021-06-03 17:31:24 +02:00
return {
endpoint : endpoint ( table || TABLE_NAME , "READ" ) ,
resource : {
fields : fields || [ ] ,
} ,
filters : filters || { } ,
sort : sort || { } ,
paginate : paginate || { } ,
}
}
function generateCreateJson ( table = TABLE_NAME , body = { } ) {
return {
endpoint : endpoint ( table , "CREATE" ) ,
body ,
}
}
function generateUpdateJson ( table = TABLE_NAME , body = { } , filters = { } ) {
return {
endpoint : endpoint ( table , "UPDATE" ) ,
filters ,
body ,
}
}
function generateDeleteJson ( table = TABLE_NAME , filters = { } ) {
return {
endpoint : endpoint ( table , "DELETE" ) ,
filters ,
}
}
2022-12-19 19:12:05 +01:00
function generateRelationshipJson ( config : { schema? : string } = { } ) {
return {
endpoint : {
datasourceId : "Postgres" ,
entityId : "brands" ,
operation : "READ" ,
schema : config.schema ,
} ,
resource : {
fields : [
"brands.brand_id" ,
"brands.brand_name" ,
"products.product_id" ,
"products.product_name" ,
"products.brand_id" ,
] ,
} ,
filters : { } ,
sort : { } ,
paginate : { } ,
relationships : [
{
from : "brand_id" ,
to : "brand_id" ,
tableName : "products" ,
column : "products" ,
} ,
] ,
extra : { idFilter : { } } ,
}
}
function generateManyRelationshipJson ( config : { schema? : string } = { } ) {
return {
endpoint : {
datasourceId : "Postgres" ,
entityId : "stores" ,
operation : "READ" ,
schema : config.schema ,
} ,
resource : {
fields : [
"stores.store_id" ,
"stores.store_name" ,
"products.product_id" ,
"products.product_name" ,
] ,
} ,
filters : { } ,
sort : { } ,
paginate : { } ,
relationships : [
{
from : "store_id" ,
to : "product_id" ,
tableName : "products" ,
column : "products" ,
through : "stocks" ,
fromPrimary : "store_id" ,
toPrimary : "product_id" ,
} ,
] ,
extra : { idFilter : { } } ,
}
}
2021-06-03 17:31:24 +02:00
describe ( "SQL query builder" , ( ) = > {
const limit = 500
2022-08-11 14:50:05 +02:00
const client = SqlClient . POSTGRES
2022-08-31 20:21:45 +02:00
let sql : any
2021-06-03 17:31:24 +02:00
beforeEach ( ( ) = > {
sql = new Sql ( client , limit )
} )
it ( "should test a basic read" , ( ) = > {
2021-06-03 18:45:19 +02:00
const query = sql . _query ( generateReadJson ( ) )
expect ( query ) . toEqual ( {
bindings : [ limit ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from " ${ TABLE_NAME } " limit $ 1) as " ${ TABLE_NAME } " ` ,
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test a read with specific columns" , ( ) = > {
2022-08-31 20:21:45 +02:00
const nameProp = ` ${ TABLE_NAME } .name ` ,
ageProp = ` ${ TABLE_NAME } .age `
const query = sql . _query (
generateReadJson ( {
fields : [ nameProp , ageProp ] ,
} )
)
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ limit ] ,
2022-08-31 20:21:45 +02:00
sql : ` select " ${ TABLE_NAME } "."name" as " ${ nameProp } ", " ${ TABLE_NAME } "."age" as " ${ ageProp } " from (select * from " ${ TABLE_NAME } " limit $ 1) as " ${ TABLE_NAME } " ` ,
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test a where string starts with read" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateReadJson ( {
filters : {
string : {
name : "John" ,
} ,
} ,
} )
)
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ "John%" , limit ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from " ${ TABLE_NAME } " where " ${ TABLE_NAME } "."name" ilike $ 1 limit $ 2) as " ${ TABLE_NAME } " ` ,
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test a where range read" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateReadJson ( {
filters : {
range : {
age : {
low : 2 ,
high : 10 ,
} ,
} ,
} ,
} )
)
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ 2 , 10 , limit ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from " ${ TABLE_NAME } " where " ${ TABLE_NAME } "."age" between $ 1 and $ 2 limit $ 3) as " ${ TABLE_NAME } " ` ,
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
2021-06-15 14:03:55 +02:00
it ( "should test for multiple IDs with OR" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateReadJson ( {
filters : {
equal : {
age : 10 ,
name : "John" ,
} ,
allOr : true ,
2021-06-15 14:03:55 +02:00
} ,
2022-08-31 20:21:45 +02:00
} )
)
2021-06-15 14:03:55 +02:00
expect ( query ) . toEqual ( {
bindings : [ 10 , "John" , limit ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from " ${ TABLE_NAME } " where (" ${ TABLE_NAME } "."age" = $ 1) or (" ${ TABLE_NAME } "."name" = $ 2) limit $ 3) as " ${ TABLE_NAME } " ` ,
2021-06-15 14:03:55 +02:00
} )
} )
2022-01-17 19:20:37 +01:00
it ( "should allow filtering on a related field" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateReadJson ( {
filters : {
equal : {
age : 10 ,
"task.name" : "task 1" ,
} ,
2022-01-17 19:20:37 +01:00
} ,
2022-08-31 20:21:45 +02:00
} )
)
2022-01-17 19:20:37 +01:00
// order of bindings changes because relationship filters occur outside inner query
expect ( query ) . toEqual ( {
bindings : [ 10 , limit , "task 1" ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from " ${ TABLE_NAME } " where " ${ TABLE_NAME } "."age" = $ 1 limit $ 2) as " ${ TABLE_NAME } " where "task"."name" = $ 3 ` ,
2022-01-17 19:20:37 +01:00
} )
} )
2021-06-03 17:31:24 +02:00
it ( "should test an create statement" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateCreateJson ( TABLE_NAME , {
name : "Michael" ,
age : 45 ,
} )
)
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ 45 , "Michael" ] ,
2022-08-31 20:21:45 +02:00
sql : ` insert into " ${ TABLE_NAME } " ("age", "name") values ( $ 1, $ 2) returning * ` ,
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test an update statement" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateUpdateJson (
TABLE_NAME ,
{
name : "John" ,
} ,
{
equal : {
id : 1001 ,
} ,
}
)
)
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ "John" , 1001 ] ,
2022-08-31 20:21:45 +02:00
sql : ` update " ${ TABLE_NAME } " set "name" = $ 1 where " ${ TABLE_NAME } "."id" = $ 2 returning * ` ,
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test a delete statement" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateDeleteJson ( TABLE_NAME , {
equal : {
id : 1001 ,
} ,
} )
)
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ 1001 ] ,
2022-08-31 20:21:45 +02:00
sql : ` delete from " ${ TABLE_NAME } " where " ${ TABLE_NAME } "."id" = $ 1 returning * ` ,
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
2021-06-03 17:45:43 +02:00
it ( "should work with MS-SQL" , ( ) = > {
2022-08-11 14:50:05 +02:00
const query = new Sql ( SqlClient . MS_SQL , 10 ) . _query ( generateReadJson ( ) )
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select top (@p0) * from [ ${ TABLE_NAME } ]) as [ ${ TABLE_NAME } ] ` ,
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:45:43 +02:00
} )
2022-07-21 11:28:54 +02:00
it ( "should work with MySQL" , ( ) = > {
2022-08-11 14:50:05 +02:00
const query = new Sql ( SqlClient . MY_SQL , 10 ) . _query ( generateReadJson ( ) )
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from \` ${ TABLE_NAME } \` limit ?) as \` ${ TABLE_NAME } \` ` ,
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:45:43 +02:00
} )
2022-03-15 12:52:54 +01:00
it ( "should use greater than when only low range specified" , ( ) = > {
const date = new Date ( )
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateReadJson ( {
filters : {
range : {
property : {
low : date ,
} ,
} ,
} ,
} )
)
2022-03-15 12:52:54 +01:00
expect ( query ) . toEqual ( {
bindings : [ date , limit ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from " ${ TABLE_NAME } " where " ${ TABLE_NAME } "."property" > $ 1 limit $ 2) as " ${ TABLE_NAME } " ` ,
2022-03-15 12:52:54 +01:00
} )
} )
it ( "should use less than when only high range specified" , ( ) = > {
const date = new Date ( )
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateReadJson ( {
filters : {
range : {
property : {
high : date ,
} ,
} ,
} ,
} )
)
2022-03-15 12:52:54 +01:00
expect ( query ) . toEqual ( {
bindings : [ date , limit ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from " ${ TABLE_NAME } " where " ${ TABLE_NAME } "."property" < $ 1 limit $ 2) as " ${ TABLE_NAME } " ` ,
2022-03-15 12:52:54 +01:00
} )
} )
it ( "should use greater than when only low range specified" , ( ) = > {
const date = new Date ( )
2022-08-31 20:21:45 +02:00
const query = sql . _query (
generateReadJson ( {
filters : {
range : {
property : {
low : date ,
} ,
} ,
} ,
} )
)
2022-03-15 12:52:54 +01:00
expect ( query ) . toEqual ( {
bindings : [ date , limit ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from " ${ TABLE_NAME } " where " ${ TABLE_NAME } "."property" > $ 1 limit $ 2) as " ${ TABLE_NAME } " ` ,
2022-03-15 12:52:54 +01:00
} )
} )
2022-07-21 11:28:54 +02:00
2022-07-28 10:20:00 +02:00
it ( "should use AND like expression for MS-SQL when filter is contains" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = new Sql ( SqlClient . MS_SQL , 10 ) . _query (
generateReadJson ( {
filters : {
contains : {
age : [ 20 , 25 ] ,
name : [ "John" , "Mary" ] ,
} ,
} ,
} )
)
2022-07-21 11:28:54 +02:00
expect ( query ) . toEqual ( {
2023-03-28 12:46:29 +02:00
bindings : [ 10 , "%20%" , "%25%" , ` %"john"% ` , ` %"mary"% ` ] ,
2023-01-20 15:03:14 +01:00
sql : ` select * from (select top (@p0) * from [ ${ TABLE_NAME } ] where (LOWER([ ${ TABLE_NAME } ].[age]) LIKE @p1 AND LOWER([ ${ TABLE_NAME } ].[age]) LIKE @p2) and (LOWER([ ${ TABLE_NAME } ].[name]) LIKE @p3 AND LOWER([ ${ TABLE_NAME } ].[name]) LIKE @p4)) as [ ${ TABLE_NAME } ] ` ,
2022-07-21 11:28:54 +02:00
} )
} )
it ( "should use JSON_CONTAINS expression for MySQL when filter is contains" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = new Sql ( SqlClient . MY_SQL , 10 ) . _query (
generateReadJson ( {
filters : {
contains : {
age : [ 20 ] ,
name : [ "John" ] ,
} ,
} ,
} )
)
2022-07-21 11:28:54 +02:00
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from \` ${ TABLE_NAME } \` where JSON_CONTAINS( ${ TABLE_NAME } .age, '[20]') and JSON_CONTAINS( ${ TABLE_NAME } .name, '["John"]') limit ?) as \` ${ TABLE_NAME } \` ` ,
2022-07-21 11:28:54 +02:00
} )
} )
it ( "should use jsonb operator expression for PostgreSQL when filter is contains" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = new Sql ( SqlClient . POSTGRES , 10 ) . _query (
generateReadJson ( {
filters : {
contains : {
age : [ 20 ] ,
name : [ "John" ] ,
} ,
} ,
} )
)
2022-07-21 11:28:54 +02:00
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from \ " ${ TABLE_NAME } \ " where \ " ${ TABLE_NAME } \ ". \ "age \ "::jsonb @> '[20]' and \ " ${ TABLE_NAME } \ ". \ "name \ "::jsonb @> '["John"]' limit $ 1) as \ " ${ TABLE_NAME } \ " ` ,
2022-07-21 11:28:54 +02:00
} )
} )
2022-07-27 12:40:46 +02:00
2022-07-27 12:56:57 +02:00
it ( "should use NOT like expression for MS-SQL when filter is notContains" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = new Sql ( SqlClient . MS_SQL , 10 ) . _query (
generateReadJson ( {
filters : {
notContains : {
age : [ 20 ] ,
name : [ "John" ] ,
} ,
} ,
} )
)
2022-07-27 12:40:46 +02:00
expect ( query ) . toEqual ( {
2023-03-28 12:46:29 +02:00
bindings : [ 10 , "%20%" , ` %"john"% ` ] ,
2023-01-20 15:03:14 +01:00
sql : ` select * from (select top (@p0) * from [ ${ TABLE_NAME } ] where NOT (LOWER([ ${ TABLE_NAME } ].[age]) LIKE @p1) and NOT (LOWER([ ${ TABLE_NAME } ].[name]) LIKE @p2)) as [ ${ TABLE_NAME } ] ` ,
2022-07-27 12:40:46 +02:00
} )
} )
it ( "should use NOT JSON_CONTAINS expression for MySQL when filter is notContains" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = new Sql ( SqlClient . MY_SQL , 10 ) . _query (
generateReadJson ( {
filters : {
notContains : {
age : [ 20 ] ,
name : [ "John" ] ,
} ,
} ,
} )
)
2022-07-27 12:40:46 +02:00
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from \` ${ TABLE_NAME } \` where NOT JSON_CONTAINS( ${ TABLE_NAME } .age, '[20]') and NOT JSON_CONTAINS( ${ TABLE_NAME } .name, '["John"]') limit ?) as \` ${ TABLE_NAME } \` ` ,
2022-07-27 12:40:46 +02:00
} )
} )
2022-07-27 12:49:45 +02:00
it ( "should use jsonb operator NOT expression for PostgreSQL when filter is notContains" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = new Sql ( SqlClient . POSTGRES , 10 ) . _query (
generateReadJson ( {
filters : {
notContains : {
age : [ 20 ] ,
name : [ "John" ] ,
} ,
} ,
} )
)
2022-07-27 12:40:46 +02:00
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from \ " ${ TABLE_NAME } \ " where NOT \ " ${ TABLE_NAME } \ ". \ "age \ "::jsonb @> '[20]' and NOT \ " ${ TABLE_NAME } \ ". \ "name \ "::jsonb @> '["John"]' limit $ 1) as \ " ${ TABLE_NAME } \ " ` ,
2022-07-27 12:40:46 +02:00
} )
} )
2022-07-27 14:19:47 +02:00
2022-07-28 10:20:00 +02:00
it ( "should use OR like expression for MS-SQL when filter is containsAny" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = new Sql ( SqlClient . MS_SQL , 10 ) . _query (
generateReadJson ( {
filters : {
containsAny : {
age : [ 20 , 25 ] ,
name : [ "John" , "Mary" ] ,
} ,
} ,
} )
)
2022-07-27 14:19:47 +02:00
expect ( query ) . toEqual ( {
2023-03-28 12:46:29 +02:00
bindings : [ 10 , "%20%" , "%25%" , ` %"john"% ` , ` %"mary"% ` ] ,
2023-01-20 15:03:14 +01:00
sql : ` select * from (select top (@p0) * from [ ${ TABLE_NAME } ] where (LOWER([ ${ TABLE_NAME } ].[age]) LIKE @p1 OR LOWER([ ${ TABLE_NAME } ].[age]) LIKE @p2) and (LOWER([ ${ TABLE_NAME } ].[name]) LIKE @p3 OR LOWER([ ${ TABLE_NAME } ].[name]) LIKE @p4)) as [ ${ TABLE_NAME } ] ` ,
2022-07-27 14:19:47 +02:00
} )
} )
it ( "should use JSON_OVERLAPS expression for MySQL when filter is containsAny" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = new Sql ( SqlClient . MY_SQL , 10 ) . _query (
generateReadJson ( {
filters : {
containsAny : {
age : [ 20 , 25 ] ,
name : [ "John" , "Mary" ] ,
} ,
} ,
} )
)
2022-07-27 14:19:47 +02:00
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from \` ${ TABLE_NAME } \` where JSON_OVERLAPS( ${ TABLE_NAME } .age, '[20,25]') and JSON_OVERLAPS( ${ TABLE_NAME } .name, '["John","Mary"]') limit ?) as \` ${ TABLE_NAME } \` ` ,
2022-07-27 14:19:47 +02:00
} )
} )
2022-07-27 17:37:29 +02:00
it ( "should use ?| operator expression for PostgreSQL when filter is containsAny" , ( ) = > {
2022-08-31 20:21:45 +02:00
const query = new Sql ( SqlClient . POSTGRES , 10 ) . _query (
generateReadJson ( {
filters : {
containsAny : {
age : [ 20 , 25 ] ,
name : [ "John" , "Mary" ] ,
} ,
} ,
} )
)
2022-07-27 14:19:47 +02:00
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2022-08-31 20:21:45 +02:00
sql : ` select * from (select * from \ " ${ TABLE_NAME } \ " where \ " ${ TABLE_NAME } \ ". \ "age \ "::jsonb ?| array [20,25] and \ " ${ TABLE_NAME } \ ". \ "name \ "::jsonb ?| array ['John','Mary'] limit $ 1) as \ " ${ TABLE_NAME } \ " ` ,
2022-07-27 14:19:47 +02:00
} )
} )
2022-12-19 19:12:05 +01:00
it ( "should add the schema to the LEFT JOIN" , ( ) = > {
const query = sql . _query ( generateRelationshipJson ( { schema : "production" } ) )
expect ( query ) . toEqual ( {
bindings : [ 500 , 5000 ] ,
sql : ` select "brands"."brand_id" as "brands.brand_id", "brands"."brand_name" as "brands.brand_name", "products"."product_id" as "products.product_id", "products"."product_name" as "products.product_name", "products"."brand_id" as "products.brand_id" from (select * from "production"."brands" limit $ 1) as "brands" left join "production"."products" on "brands"."brand_id" = "products"."brand_id" limit $ 2 ` ,
} )
} )
it ( "should handle if the schema is not present when doing a LEFT JOIN" , ( ) = > {
const query = sql . _query ( generateRelationshipJson ( ) )
expect ( query ) . toEqual ( {
bindings : [ 500 , 5000 ] ,
sql : ` select "brands"."brand_id" as "brands.brand_id", "brands"."brand_name" as "brands.brand_name", "products"."product_id" as "products.product_id", "products"."product_name" as "products.product_name", "products"."brand_id" as "products.brand_id" from (select * from "brands" limit $ 1) as "brands" left join "products" on "brands"."brand_id" = "products"."brand_id" limit $ 2 ` ,
} )
} )
it ( "should add the schema to both the toTable and throughTable in many-to-many join" , ( ) = > {
const query = sql . _query (
generateManyRelationshipJson ( { schema : "production" } )
)
expect ( query ) . toEqual ( {
bindings : [ 500 , 5000 ] ,
sql : ` select "stores"."store_id" as "stores.store_id", "stores"."store_name" as "stores.store_name", "products"."product_id" as "products.product_id", "products"."product_name" as "products.product_name" from (select * from "production"."stores" limit $ 1) as "stores" left join "production"."stocks" on "stores"."store_id" = "stocks"."store_id" left join "production"."products" on "products"."product_id" = "stocks"."product_id" limit $ 2 ` ,
} )
} )
2023-01-20 15:03:14 +01:00
it ( "should handle table names with dashes when performing a LIKE in MySQL" , ( ) = > {
const tableName = "Table-Name-With-Dashes"
const query = new Sql ( SqlClient . MY_SQL , limit ) . _query (
generateReadJson ( {
table : tableName ,
filters : {
string : {
name : "John" ,
} ,
} ,
} )
)
expect ( query ) . toEqual ( {
2023-03-28 12:46:29 +02:00
bindings : [ "john%" , limit ] ,
2023-01-20 15:03:14 +01:00
sql : ` select * from (select * from \` ${ tableName } \` where LOWER( \` ${ tableName } \` . \` name \` ) LIKE ? limit ?) as \` ${ tableName } \` ` ,
} )
} )
it ( "should handle table names with dashes when performing a LIKE in SQL Server" , ( ) = > {
const tableName = "Table-Name-With-Dashes"
const query = new Sql ( SqlClient . MS_SQL , limit ) . _query (
generateReadJson ( {
table : tableName ,
filters : {
string : {
name : "John" ,
} ,
} ,
} )
)
expect ( query ) . toEqual ( {
2023-03-28 12:46:29 +02:00
bindings : [ limit , "john%" ] ,
2023-01-20 15:03:14 +01:00
sql : ` select * from (select top (@p0) * from [ ${ tableName } ] where LOWER([ ${ tableName } ].[name]) LIKE @p1) as [ ${ tableName } ] ` ,
} )
} )
2023-02-15 10:27:44 +01:00
it ( "should ignore high range value if it is an empty object" , ( ) = > {
const query = sql . _query (
generateReadJson ( {
filters : {
range : {
dob : {
low : "2000-01-01 00:00:00" ,
high : { } ,
} ,
} ,
} ,
} )
)
expect ( query ) . toEqual ( {
bindings : [ "2000-01-01 00:00:00" , 500 ] ,
sql : ` select * from (select * from \ " ${ TABLE_NAME } \ " where \ " ${ TABLE_NAME } \ ". \ "dob \ " > $ 1 limit $ 2) as \ " ${ TABLE_NAME } \ " ` ,
} )
} )
it ( "should ignore low range value if it is an empty object" , ( ) = > {
const query = sql . _query (
generateReadJson ( {
filters : {
range : {
dob : {
low : { } ,
high : "2010-01-01 00:00:00" ,
} ,
} ,
} ,
} )
)
expect ( query ) . toEqual ( {
bindings : [ "2010-01-01 00:00:00" , 500 ] ,
sql : ` select * from (select * from \ " ${ TABLE_NAME } \ " where \ " ${ TABLE_NAME } \ ". \ "dob \ " < $ 1 limit $ 2) as \ " ${ TABLE_NAME } \ " ` ,
} )
} )
2023-03-28 12:46:29 +02:00
it ( "should lowercase the values for Oracle LIKE statements" , ( ) = > {
let query = new Sql ( SqlClient . ORACLE , limit ) . _query (
generateReadJson ( {
filters : {
string : {
name : "John" ,
} ,
} ,
} )
)
expect ( query ) . toEqual ( {
bindings : [ "john%" , limit ] ,
sql : ` select * from (select * from (select * from \ "test \ " where LOWER( \ "test \ ". \ "name \ ") LIKE :1) where rownum <= :2) \ "test \ " ` ,
} )
query = new Sql ( SqlClient . ORACLE , limit ) . _query (
generateReadJson ( {
filters : {
contains : {
age : [ 20 , 25 ] ,
name : [ "John" , "Mary" ] ,
} ,
} ,
} )
)
expect ( query ) . toEqual ( {
bindings : [ "%20%" , "%25%" , ` %"john"% ` , ` %"mary"% ` , limit ] ,
sql : ` select * from (select * from (select * from \ "test \ " where (LOWER( \ "test \ ". \ "age \ ") LIKE :1 AND LOWER( \ "test \ ". \ "age \ ") LIKE :2) and (LOWER( \ "test \ ". \ "name \ ") LIKE :3 AND LOWER( \ "test \ ". \ "name \ ") LIKE :4)) where rownum <= :5) \ "test \ " ` ,
} )
query = new Sql ( SqlClient . ORACLE , limit ) . _query (
generateReadJson ( {
filters : {
fuzzy : {
name : "Jo" ,
} ,
} ,
} )
)
expect ( query ) . toEqual ( {
bindings : [ ` %jo% ` , limit ] ,
sql : ` select * from (select * from (select * from \ "test \ " where LOWER( \ "test \ ". \ "name \ ") LIKE :1) where rownum <= :2) \ "test \ " ` ,
} )
} )
2021-06-03 17:45:43 +02:00
} )