Skip to content

Commit cc3d619

Browse files
committed
!tmp
1 parent 62b0c82 commit cc3d619

File tree

8 files changed

+116
-20
lines changed

8 files changed

+116
-20
lines changed

packages/core/modules/utils/export.js

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,67 @@ SqlString.unescapeLike = (val, sqlDialect = undefined) => {
1414
return val;
1515
}
1616
let res = val;
17-
// unescape % and _
18-
if (sqlDialect === "BigQuery") {
19-
// https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#like_operator
17+
if (!sqlDialect) {
2018
res = res.replace(/\\\\([%_])/g, "$1");
21-
} else {
22-
res = res.replace(/\\([%_])/g, "$1");
19+
res = res.replace(/\\(['"])/g, "$1"); // todo: not automatically ?
20+
res = res.replace(/\\\\\\\\/g, "\\");
2321
}
22+
// if (sqlDialect === "BigQuery") {
23+
// // https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#like_operator
24+
// res = res.replace(/\\\\([%_])/g, "$1");
25+
// } else {
26+
// res = res.replace(/\\\\([%_])/g, "$1");
27+
// }
2428
return res;
2529
};
2630

2731
SqlString.escapeLike = (val, any_start = true, any_end = true, sqlDialect = undefined) => {
2832
if (typeof val !== "string") {
2933
return val;
3034
}
31-
// normal escape
32-
let res = SqlString.escape(val);
33-
// unwrap ''
34-
res = SqlString.trim(res);
35-
// escape % and _
36-
if (sqlDialect === "BigQuery") {
37-
// https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#like_operator
38-
res = res.replace(/[%_\\]/g, "\\\\$&");
35+
36+
let res = val;
37+
38+
if (!sqlDialect) {
39+
// escape % and _ and \ with \
40+
res = res.replace(/[%_\\]/g, "\\$&");
41+
// wrap with % for LIKE
42+
res = (any_start ? "%" : "") + res + (any_end ? "%" : "");
43+
// escape (will escape all \ with \)
44+
res = SqlString.escape(res);
3945
} else {
40-
res = res.replace(/[%_\\]/g, "\\\\$&");
46+
// normal escape
47+
res = SqlString.escape(res);
48+
// unwrap ''
49+
res = SqlString.trim(res);
50+
if (sqlDialect === "PostgreSQL") {
51+
// TODO: for PostgreSQL it should just replace ' -> '' (without \), for like it should escape % ) and \
52+
//!!!!!!
53+
} else if (sqlDialect === "BigQuery") {
54+
// https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#like_operator
55+
// escape \ with \\
56+
res = res.replace(/(\\\\)/g, "\\$&");
57+
// escape % and _ with \\
58+
res = res.replace(/[%_]/g, "\\\\$&");
59+
} else if (sqlDialect === "MySQL") {
60+
// https://dev.mysql.com/doc/refman/8.4/en/string-comparison-functions.html
61+
// tip: for mysql it can be \_ or \\_ for escaping _ BUT it's ALWAYS \\\\ for one \
62+
// escape \ -> \\\\
63+
res = res.replace(/(\\\\)/g, "\\\\$&");
64+
// escape % and _ with \
65+
res = res.replace(/[%_]/g, "\\$&");
66+
} else {
67+
// escape \ -> \\\\
68+
res = res.replace(/(\\\\)/g, "\\\\$&");
69+
// escape % and _ with \\
70+
res = res.replace(/[%_]/g, "\\\\$&");
71+
}
72+
// wrap with % for LIKE
73+
res = (any_start ? "%" : "") + res + (any_end ? "%" : "");
74+
// wrap with ''
75+
res = "'" + res + "'";
4176
}
42-
// wrap with % for LIKE
43-
res = (any_start ? "%" : "") + res + (any_end ? "%" : "");
44-
// wrap ''
45-
res = "'" + res + "'";
77+
4678
return res;
4779
};
4880

packages/examples/src/demo/config/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ export default (skin: string) => {
234234
...InitialConfig.settings,
235235
...localeSettings,
236236

237+
sqlDialect: "BigQuery",
238+
237239
defaultSliderWidth: "200px",
238240
defaultSelectWidth: "200px",
239241
defaultSearchWidth: "100px",

packages/sql/modules/import/convert.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ const convertArg = (logic: OutLogic | undefined, conv: Conv, config: Config, met
250250
}
251251
const value = logic.value; // todo: convert ?
252252
if (valueType === "text") {
253+
// todo: unescape properly
253254
// fix issues with date/time values
254255
valueType = undefined;
255256
}

packages/sql/modules/import/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const loadFromSql = (
3434
if (!options?.database) {
3535
// todo
3636
options.database = "Postgresql";
37+
options.database = "MariaDB";
3738
}
3839
if (!sqlStr.startsWith("SELECT ")) {
3940
sqlStr = "SELECT * FROM t WHERE " + sqlStr;

packages/tests/karma.tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Enzyme.configure({adapter: new Adapter()});
77
// FILTER YOUR TESTS HERE
88
const testsFilter = [
99
// "QueryWithOperators",
10-
// "OtherUtils",
10+
"SqlUtils",
1111
];
1212
const specFilter = [
13-
// "@sql",
13+
// "@like",
1414
// "OtherUtils"
1515
];
1616

packages/tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"antd": "^5.24.5",
5353
"bootstrap": "^5.3.3",
5454
"lodash": "^4.17.21",
55+
"node-sql-parser": "^5.3.5",
5556
"moment": "^2.30.1",
5657
"prop-types": "^15.8.1",
5758
"react": "^17.0.2",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Utils } from "@react-awesome-query-builder/ui";
2+
import { expect } from "chai";
3+
import {
4+
Parser as NodeSqlParser, Option as SqlParseOption, AST,
5+
} from "node-sql-parser";
6+
7+
8+
describe("ExportUtils.SqlString", () => {
9+
const sqlParser = new NodeSqlParser();
10+
const parseString = (escapedString) => {
11+
return sqlParser.parse("select * from a where b like " + escapedString).ast.where.right.value;
12+
};
13+
const extractFromLike = (escapedString) => {
14+
const likeSrting = parseString(escapedString);
15+
if (likeSrting.startsWith("%") && likeSrting.endsWith("%")) {
16+
return likeSrting.substring(1, likeSrting.length - 1);
17+
} else if (likeSrting.startsWith("%")) {
18+
return likeSrting.substring(1);
19+
} else if (likeSrting.endsWith("%")) {
20+
return likeSrting.substring(0, likeSrting.length - 1);
21+
}
22+
};
23+
24+
describe("escapeLike + unescapeLike", () => {
25+
const testString = " _ % \\ ' \" "; // _ % \\ ' \"
26+
it("no sqlDialect", () => {
27+
const a = " ' \" ";
28+
console.log(1, a)
29+
console.log(2, Utils.ExportUtils.SqlString.escape(a))
30+
console.log(3, parseString(Utils.ExportUtils.SqlString.escape(a)))
31+
const escapedString = "'% \\\\_ \\\\% \\\\\\\\ \\' \\\" %'"; // '% \\_ \\% \\\\ \' \" %'
32+
expect(Utils.ExportUtils.SqlString.escapeLike(testString), "escapeLike").to.eql(escapedString);
33+
expect(Utils.ExportUtils.SqlString.unescapeLike(extractFromLike(escapedString)), "unescapeLike").to.eql(testString);
34+
});
35+
// it("sqlDialect == MySQL", () => {
36+
// const escapedString = "'% \\_ \\% \\\\\\\\ \\' \\\" %'"; // '% \_ \% \\\\ \' \" %'
37+
// expect(Utils.ExportUtils.SqlString.escapeLike(testString, true, true, "MySQL"), "escapeLike").to.eql(escapedString);
38+
// expect(Utils.ExportUtils.SqlString.unescapeLike(eval(escapedString), "MySQL"), "unescapeLike").to.eql(testString);
39+
// });
40+
// it("sqlDialect == BigQuery", () => {
41+
// const escapedString = "'% \\\\_ \\\\% \\\\\\ \\' \\\" %'"; // '% \\_ \\% \\\ \' \" %'
42+
// expect(Utils.ExportUtils.SqlString.escapeLike(testString, true, true, "BigQuery"), "escapeLike").to.eql(escapedString);
43+
// expect(Utils.ExportUtils.SqlString.unescapeLike(escapedString, "BigQuery"), "unescapeLike").to.eql(testString);
44+
// });
45+
// it("sqlDialect == PostgreSQL", () => {
46+
// const escapedString = "?"; // todo
47+
// expect(Utils.ExportUtils.SqlString.escapeLike(testString, true, true, "PostgreSQL"), "escapeLike").to.eql(escapedString);
48+
// expect(Utils.ExportUtils.SqlString.unescapeLike(escapedString, "PostgreSQL"), "unescapeLike").to.eql(testString);
49+
// });
50+
// it("sqlDialect == XX", () => {
51+
// const escapedString = "'% \\\\_ \\\\% \\\\\\\\ \\' \\\" %'"; // '% \\_ \\% \\\\ \' \" %'
52+
// expect(Utils.ExportUtils.SqlString.escapeLike(testString, true, true, "XX"), "escapeLike").to.eql(escapedString);
53+
// expect(Utils.ExportUtils.SqlString.unescapeLike(escapedString, "XX"), "unescapeLike").to.eql(testString);
54+
// });
55+
});
56+
});

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)