2024-02-28 14:44:52 +01:00
import {
2024-04-17 18:12:26 +02:00
FieldType ,
2024-02-28 14:44:52 +01:00
Operation ,
QueryJson ,
Table ,
2024-04-17 18:12:26 +02:00
TableSourceType ,
2024-05-20 19:22:46 +02:00
SqlClient ,
2024-02-28 14:44:52 +01:00
} from "@budibase/types"
2024-05-20 19:22:46 +02:00
import { sql } from "@budibase/backend-core"
const Sql = sql . Sql
2021-06-03 17:31:24 +02:00
const TABLE_NAME = "test"
2024-04-16 17:41:39 +02:00
const TABLE : Table = {
type : "table" ,
sourceType : TableSourceType.EXTERNAL ,
sourceId : "SOURCE_ID" ,
2024-04-17 18:12:26 +02:00
schema : {
id : {
name : "id" ,
type : FieldType . NUMBER ,
} ,
} ,
2024-04-16 17:41:39 +02:00
name : TABLE_NAME ,
primary : [ "id" ] ,
}
2021-06-03 17:31:24 +02:00
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 ,
2024-01-30 18:57:10 +01:00
} : any = { } ) : QueryJson {
2024-04-16 17:41:39 +02:00
const tableObj = { . . . TABLE }
if ( table ) {
tableObj . name = table
}
2021-06-03 17:31:24 +02:00
return {
endpoint : endpoint ( table || TABLE_NAME , "READ" ) ,
resource : {
fields : fields || [ ] ,
} ,
filters : filters || { } ,
sort : sort || { } ,
paginate : paginate || { } ,
2023-08-04 14:53:30 +02:00
meta : {
2024-04-16 17:41:39 +02:00
table : tableObj ,
2023-08-04 14:53:30 +02:00
} ,
2021-06-03 17:31:24 +02:00
}
}
2024-02-28 14:44:52 +01:00
function generateRelationshipJson ( config : { schema? : string } = { } ) : QueryJson {
2022-12-19 19:12:05 +01:00
return {
endpoint : {
datasourceId : "Postgres" ,
entityId : "brands" ,
2024-02-28 14:44:52 +01:00
operation : Operation.READ ,
2022-12-19 19:12:05 +01:00
schema : config.schema ,
} ,
resource : {
fields : [
"brands.brand_id" ,
"brands.brand_name" ,
"products.product_id" ,
"products.product_name" ,
"products.brand_id" ,
] ,
} ,
filters : { } ,
sort : { } ,
relationships : [
{
from : "brand_id" ,
to : "brand_id" ,
tableName : "products" ,
column : "products" ,
} ,
] ,
extra : { idFilter : { } } ,
2024-04-16 17:41:39 +02:00
meta : {
table : TABLE ,
} ,
2022-12-19 19:12:05 +01:00
}
}
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 : { } } ,
2024-04-17 18:12:26 +02:00
meta : {
table : TABLE ,
} ,
2022-12-19 19:12:05 +01:00
}
}
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 )
} )
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 ] ,
2024-02-29 13:33:03 +01:00
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" as "products" on "brands"."brand_id" = "products"."brand_id" limit $ 2 ` ,
2022-12-19 19:12:05 +01:00
} )
} )
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 ] ,
2024-02-29 13:33:03 +01:00
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" as "products" on "brands"."brand_id" = "products"."brand_id" limit $ 2 ` ,
2022-12-19 19:12:05 +01:00
} )
} )
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 ] ,
2024-02-29 13:33:03 +01:00
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" as "stocks" on "stores"."store_id" = "stocks"."store_id" left join "production"."products" as "products" on "products"."product_id" = "stocks"."product_id" limit $ 2 ` ,
2022-12-19 19:12:05 +01:00
} )
} )
2023-01-20 15:03:14 +01:00
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 ] ,
2024-03-19 12:14:16 +01:00
sql : ` select * from (select * from (select * from "test" where LOWER("test"."name") LIKE :1) where rownum <= :2) "test" ` ,
2023-03-28 12:46:29 +02:00
} )
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 ] ,
2024-05-23 18:02:08 +02:00
sql : ` select * from (select * from (select * from "test" where (COALESCE(LOWER("test"."age"), '') LIKE :1 AND COALESCE(LOWER("test"."age"), '') LIKE :2) and (COALESCE(LOWER("test"."name"), '') LIKE :3 AND COALESCE(LOWER("test"."name"), '') LIKE :4)) where rownum <= :5) "test" ` ,
2023-03-28 12:46:29 +02:00
} )
query = new Sql ( SqlClient . ORACLE , limit ) . _query (
generateReadJson ( {
filters : {
fuzzy : {
name : "Jo" ,
} ,
} ,
} )
)
expect ( query ) . toEqual ( {
bindings : [ ` %jo% ` , limit ] ,
2024-03-19 12:14:16 +01:00
sql : ` select * from (select * from (select * from "test" where LOWER("test"."name") LIKE :1) where rownum <= :2) "test" ` ,
2023-03-28 12:46:29 +02:00
} )
} )
2021-06-03 17:45:43 +02:00
} )