Skip to content

Commit b17ab4c

Browse files
committed
Merge branch 'main' into dev
2 parents 238460d + 7af15bd commit b17ab4c

7 files changed

Lines changed: 89 additions & 11 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@entity-access/entity-access",
3-
"version": "1.1.16",
3+
"version": "1.1.17",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

src/compiler/postgres/PostgreSqlMethodTransformer.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { joinAny, joinMap, prepareAny } from "../../query/ast/IStringTransformer.js";
1+
import { expandParamArray, joinAny, joinMap, prepareAny } from "../../query/ast/IStringTransformer.js";
22
import { NotSupportedError } from "../../query/parser/NotSupportedError.js";
33
import Sql from "../../sql/Sql.js";
44
import { ISqlHelpers, flattenMethods } from "../ISqlHelpers.js";
@@ -280,8 +280,12 @@ export const PostgreSqlHelper: ISqlHelpers = {
280280
iLike(text, test) {
281281
return prepareAny `(${text} iLike ${test})`;
282282
},
283-
iLikeAny(text, test) {
284-
return ["(" , text , " iLIKE ANY (ARRAY[", (x)=> joinMap(",", x, test, (item) => [() => item] ), "]))"] as any;
283+
iLikeAny(text, input) {
284+
return expandParamArray({
285+
input,
286+
prefix: ["(", text , " iLIKE ANY (ARRAY["],
287+
suffix: ["]))"]
288+
});
285289
},
286290
indexOf(text, test) {
287291
return prepareAny `(strpos(${text}, ${test}) - 1)`;
@@ -295,8 +299,12 @@ export const PostgreSqlHelper: ISqlHelpers = {
295299
like(text, test) {
296300
return prepareAny `(${text} LIKE ${test})`;
297301
},
298-
likeAny(text, test) {
299-
return ["(" , text , " LIKE ANY (ARRAY[", (x)=> joinMap(",", x, test, (item) => [() => item] ), "]))"] as any;
302+
likeAny(text, input) {
303+
return expandParamArray({
304+
input,
305+
prefix: ["(", text , " LIKE ANY (ARRAY["],
306+
suffix: ["]))"]
307+
});
300308
},
301309
lower(text) {
302310
return prepareAny `LOWER(${text})`;

src/compiler/sql-server/SqlServerSqlMethodTransformer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { joinAny, joinMap, prepareAny } from "../../query/ast/IStringTransformer.js";
2+
import { expandParamArray, joinAny, joinMap, prepareAny } from "../../query/ast/IStringTransformer.js";
33
import Sql from "../../sql/Sql.js";
44
import { ISqlHelpers, flattenMethods } from "../ISqlHelpers.js";
55

@@ -281,8 +281,14 @@ export const SqlServerSqlHelper: ISqlHelpers = {
281281
iLike(text, test) {
282282
return prepareAny `(${text} like ${test})`;
283283
},
284-
iLikeAny(text, test) {
285-
return ["(", (x)=> joinMap(" OR ", x, test, (item) => [ "(" , text, " like ", () => item , ")" ]), ")"] as any;
284+
iLikeAny(text, input) {
285+
return expandParamArray({
286+
input,
287+
sep: " OR ",
288+
prefix: ["("],
289+
fx: (item) => ["(", text, " like ", () => item, ")" ] as any,
290+
suffix: [")"]
291+
});
286292
},
287293
indexOf(text, test) {
288294
return prepareAny `(CHARINDEX(${test}, ${text}) - 1)`;

src/query/ast/IStringTransformer.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@ export class QueryParameter {
2020
}
2121
}
2222

23+
export const expandParamArray = ({
24+
sep = ",",
25+
input,
26+
prefix = [],
27+
suffix = [],
28+
emptyResult = [" false "],
29+
fx = (xi) => [() => xi]
30+
}) => [(p) => {
31+
const a = input[0](p);
32+
if (!a.length) {
33+
return emptyResult;
34+
}
35+
const r = prefix;
36+
let s = "";
37+
for (const iterator of a) {
38+
if (s) {
39+
r.push(s);
40+
}
41+
s = sep;
42+
const items = fx(iterator).flat(2);
43+
for (const fi of items) {
44+
r.push(fi);
45+
}
46+
}
47+
for(const sx of suffix) {
48+
r.push(sx);
49+
}
50+
return r.flat(2);
51+
}] as any;
52+
53+
2354
export const joinMap = (sep: string, input, a: any, fx: ((item) => any) = (xi) => [() => xi]) => {
2455
const r = [];
2556
a = (Array.isArray(a) ? a.map((x) => x(input)) : a(input)).flat(2);

src/tests/expressions/simple/parse-array.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ export default function () {
2727

2828
r = compiler.execute({ names }, (x, p) => Sql.text.iLikeAny(x.firstName, p.names));
2929
assert.equal(`(x."firstName" iLIKE ANY (ARRAY[$1,$2]))`, r.text);
30+
31+
r = compiler.execute({ names: [] }, (x, p) => Sql.text.iLikeAny(x.firstName, p.names));
32+
assert.equal(` false `, r.text);
3033
}
3134

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import assert from "assert";
2+
import Sql from "../../../sql/Sql.js";
3+
import QueryCompiler from "../../../compiler/QueryCompiler.js";
4+
import SqlServerQueryCompiler from "../../../drivers/sql-server/SqlServerQueryCompiler.js";
5+
6+
export default function () {
7+
8+
const compiler = new SqlServerQueryCompiler();
9+
10+
const names = ["Akash", "Simmi"];
11+
12+
let r = compiler.execute({ names }, (x, p) => Sql.in(x.firstName, p.names));
13+
assert.equal(`x.[firstName] IN ($1,$2)`, r.text);
14+
15+
r = compiler.execute({ names }, (x, p) => x.firstName in p.names);
16+
assert.equal(`x.[firstName] IN ($1,$2)`, r.text);
17+
18+
r = compiler.execute({ names }, (x, p) => x.firstName in ["a", "b"]);
19+
assert.equal(`x.[firstName] IN (N'a',N'b')`, r.text);
20+
21+
r = compiler.execute({ names }, (x, p) => Sql.text.likeAny(x.firstName, p.names));
22+
assert.equal(`((x.[firstName] LIKE $1) OR (x.[firstName] LIKE $2))`, r.text);
23+
24+
r = compiler.execute({ names }, (x, p) => Sql.text.iLikeAny(x.firstName, p.names));
25+
assert.equal(`((x.[firstName] like $1) OR (x.[firstName] like $2))`, r.text);
26+
27+
r = compiler.execute({ names: [] }, (x, p) => Sql.text.iLikeAny(x.firstName, p.names));
28+
assert.equal(` false `, r.text);
29+
}
30+

0 commit comments

Comments
 (0)