Add indexable smart query types
This commit is contained in:
1 parent
c7022a2735
commit
32dda9f194
1 file changed
+72
-7
+72
-7
@@ -76,7 +76,7 @@ export function getLabels(resource: string): string[] {
|
|||||||
return Object.keys(resources[resource]);
|
return Object.keys(resources[resource]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function queryNumber(param: string, value: string): Expression {
|
function queryNumber(param: Expression, value: string): Expression {
|
||||||
let op = "=";
|
let op = "=";
|
||||||
for (const o of ["<>", "=", "<=", "<", ">=", ">"]) {
|
for (const o of ["<>", "=", "<=", "<", ">=", ">"]) {
|
||||||
if (value.startsWith(o)) {
|
if (value.startsWith(o)) {
|
||||||
@@ -92,7 +92,7 @@ function queryNumber(param: string, value: string): Expression {
|
|||||||
return [op, param, v];
|
return [op, param, v];
|
||||||
}
|
}
|
||||||
|
|
||||||
function queryTimestamp(param, value): Expression {
|
function queryTimestamp(param: Expression, value: string): Expression {
|
||||||
let op = "=";
|
let op = "=";
|
||||||
for (const o of ["<>", "=", "<=", "<", ">=", ">"]) {
|
for (const o of ["<>", "=", "<=", "<", ">=", ">"]) {
|
||||||
if (value.startsWith(o)) {
|
if (value.startsWith(o)) {
|
||||||
@@ -108,18 +108,33 @@ function queryTimestamp(param, value): Expression {
|
|||||||
return [op, param, v];
|
return [op, param, v];
|
||||||
}
|
}
|
||||||
|
|
||||||
function queryString(param, value): Expression {
|
function queryString(param: Expression, value: string): Expression {
|
||||||
return ["LIKE", ["FUNC", "LOWER", param], value.toLowerCase()];
|
return ["LIKE", ["FUNC", "LOWER", param], value.toLowerCase()];
|
||||||
}
|
}
|
||||||
|
|
||||||
function queryMac(param, value): Expression {
|
function queryStringCaseSensitive(
|
||||||
|
param: Expression,
|
||||||
|
value: string
|
||||||
|
): Expression {
|
||||||
|
return ["LIKE", param, value];
|
||||||
|
}
|
||||||
|
|
||||||
|
function queryStringMonoCase(param: Expression, value: string): Expression {
|
||||||
|
return [
|
||||||
|
"OR",
|
||||||
|
["LIKE", param, value.toLowerCase()],
|
||||||
|
["LIKE", param, value.toUpperCase()],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
function queryMac(param: Expression, value: string): Expression {
|
||||||
value = value.replace(/[^a-f0-9]/gi, "").toLowerCase();
|
value = value.replace(/[^a-f0-9]/gi, "").toLowerCase();
|
||||||
if (!value) return null;
|
if (!value) return null;
|
||||||
if (value.length === 12) {
|
if (value.length === 12) {
|
||||||
return [
|
return [
|
||||||
"LIKE",
|
"OR",
|
||||||
["FUNC", "LOWER", param],
|
["=", param, value.replace(/(..)(?!$)/g, "$1:").toLowerCase()],
|
||||||
value.replace(/(..)(?!$)/g, "$1:"),
|
["=", param, value.replace(/(..)(?!$)/g, "$1:").toUpperCase()],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +153,29 @@ function queryMac(param, value): Expression {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function queryMacWildcard(param: Expression, value: string): Expression {
|
||||||
|
if (!/^[a-f0-9%]+$/i.test(value)) return queryStringMonoCase(param, value);
|
||||||
|
const parts = value.split("%");
|
||||||
|
|
||||||
|
const groups = parts.map((p) => [
|
||||||
|
p.replace(/..(?=.)/gi, "$&:"),
|
||||||
|
p.replace(/(.)(.)/gi, "$1:$2"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const res = new Set();
|
||||||
|
for (let i = 0; i < 2 ** groups.length; ++i) {
|
||||||
|
const r = groups.map((g, j) => g[(i >> j) & 1]).join("%");
|
||||||
|
if (/^[a-f0-9]:/i.test(r) || /:[a-f0-9]$/i.test(r)) continue;
|
||||||
|
res.add(r.toLocaleLowerCase());
|
||||||
|
res.add(r.toUpperCase());
|
||||||
|
}
|
||||||
|
if (!res.size) return queryStringMonoCase(param, value);
|
||||||
|
|
||||||
|
const clauses = [...res].map((r) => ["LIKE", param, r]);
|
||||||
|
if (clauses.length === 1) return clauses[0];
|
||||||
|
return ["OR", ...clauses];
|
||||||
|
}
|
||||||
|
|
||||||
function queryTag(tag: string): Expression {
|
function queryTag(tag: string): Expression {
|
||||||
const t = encodeTag(tag);
|
const t = encodeTag(tag);
|
||||||
return ["IS NOT NULL", ["PARAM", `Tags.${t}`]];
|
return ["IS NOT NULL", ["PARAM", `Tags.${t}`]];
|
||||||
@@ -156,6 +194,12 @@ export function getTip(resource: string, label: string): string {
|
|||||||
case "string":
|
case "string":
|
||||||
tips.push("case insensitive string pattern");
|
tips.push("case insensitive string pattern");
|
||||||
break;
|
break;
|
||||||
|
case "string-casesensitive":
|
||||||
|
tips.push("case sensitive string pattern");
|
||||||
|
break;
|
||||||
|
case "string-monocase":
|
||||||
|
tips.push("case insensitive string pattern");
|
||||||
|
break;
|
||||||
case "number":
|
case "number":
|
||||||
tips.push("numeric value");
|
tips.push("numeric value");
|
||||||
break;
|
break;
|
||||||
@@ -167,6 +211,9 @@ export function getTip(resource: string, label: string): string {
|
|||||||
case "mac":
|
case "mac":
|
||||||
tips.push("partial case insensitive MAC address");
|
tips.push("partial case insensitive MAC address");
|
||||||
break;
|
break;
|
||||||
|
case "mac-wildcard":
|
||||||
|
tips.push("case insensitive MAC address");
|
||||||
|
break;
|
||||||
case "tag":
|
case "tag":
|
||||||
tips.push("case sensitive string");
|
tips.push("case sensitive string");
|
||||||
break;
|
break;
|
||||||
@@ -203,11 +250,29 @@ export function unpack(
|
|||||||
if (q) res.push(q);
|
if (q) res.push(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type.includes("string-casesensitive")) {
|
||||||
|
const q = queryStringCaseSensitive(
|
||||||
|
resources[resource][label].parameter,
|
||||||
|
value
|
||||||
|
);
|
||||||
|
if (q) res.push(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type.includes("string-monocase")) {
|
||||||
|
const q = queryStringMonoCase(resources[resource][label].parameter, value);
|
||||||
|
if (q) res.push(q);
|
||||||
|
}
|
||||||
|
|
||||||
if (type.includes("mac")) {
|
if (type.includes("mac")) {
|
||||||
const q = queryMac(resources[resource][label].parameter, value);
|
const q = queryMac(resources[resource][label].parameter, value);
|
||||||
if (q) res.push(q);
|
if (q) res.push(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type.includes("mac-wildcard")) {
|
||||||
|
const q = queryMacWildcard(resources[resource][label].parameter, value);
|
||||||
|
if (q) res.push(q);
|
||||||
|
}
|
||||||
|
|
||||||
if (type.includes("tag")) {
|
if (type.includes("tag")) {
|
||||||
const q = queryTag(value);
|
const q = queryTag(value);
|
||||||
if (q) res.push(q);
|
if (q) res.push(q);
|
||||||
|
|||||||
Reference in new issue
Block a user