Skip to content

Commit

Permalink
unescapeLike
Browse files Browse the repository at this point in the history
  • Loading branch information
ukrbublik committed Jan 14, 2025
1 parent 93810f6 commit 970e5bf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/core/modules/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,21 @@ const operators = {
reversedOp: "not_like",
sqlOp: "LIKE",
// tip: this function covers import of 3 operators
sqlImport: (sqlObj) => {
sqlImport: function (sqlObj) {
if (sqlObj?.operator == "LIKE" || sqlObj?.operator == "NOT LIKE") {
const not = sqlObj?.operator == "NOT LIKE";
const [_left, right] = sqlObj.children || [];
if (right?.valueType?.endsWith("_quote_string")) {
if (right?.value.startsWith("%") && right?.value.endsWith("%")) {
right.value = right.value.substring(1, right.value.length - 1);
right.value = this.utils.SqlString.unescapeLike(right.value.substring(1, right.value.length - 1));
sqlObj.operator = not ? "not_like" : "like";
return sqlObj;
} else if (right?.value.startsWith("%")) {
right.value = right.value.substring(1);
right.value = this.utils.SqlString.unescapeLike(right.value.substring(1));

Check warning on line 182 in packages/core/modules/config/index.js

View check run for this annotation

Codecov / codecov/patch

packages/core/modules/config/index.js#L182

Added line #L182 was not covered by tests
sqlObj.operator = "ends_with";
return sqlObj;
} else if (right?.value.endsWith("%")) {
right.value = right.value.substring(0, right.value.length - 1);
right.value = this.utils.SqlString.unescapeLike(right.value.substring(0, right.value.length - 1));
sqlObj.operator = "starts_with";
return sqlObj;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/modules/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ interface ExportUtils {
trim(val?: string): string;
escape(val?: string): string;
escapeLike(val?: string, any_start?: boolean, any_end?: boolean): string;
unescapeLike(val?: string): string;
},
stringifyForDisplay(val: any): string;
/**
Expand Down
10 changes: 10 additions & 0 deletions packages/core/modules/utils/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ SqlString.trim = (val) => {
return val;
};

SqlString.unescapeLike = (val) => {
if (typeof val !== "string") {
return val;

Check warning on line 14 in packages/core/modules/utils/export.js

View check run for this annotation

Codecov / codecov/patch

packages/core/modules/utils/export.js#L14

Added line #L14 was not covered by tests
}
let res = val;
// unescape % and _
res = res.replace(/\\([%_])/g, "$1");
return res;
};

SqlString.escapeLike = (val, any_start = true, any_end = true) => {
if (typeof val !== "string") {
return val;
Expand Down

0 comments on commit 970e5bf

Please sign in to comment.