2022-04-21 11:57:45 +00:00
|
|
|
import { collectNodesOfType, findNodeOfType, replaceNodesMatching } from "@silverbulletmd/common/tree";
|
|
|
|
import { lezerToParseTree } from "@silverbulletmd/common/parse_tree";
|
2022-04-11 18:34:09 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
import { parser } from "./parse-query";
|
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
export type QueryProviderEvent = {
|
|
|
|
query: ParsedQuery;
|
|
|
|
pageName: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Filter = {
|
2022-04-11 18:34:09 +00:00
|
|
|
op: string;
|
|
|
|
prop: string;
|
|
|
|
value: any;
|
|
|
|
};
|
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
export type ParsedQuery = {
|
2022-04-11 18:34:09 +00:00
|
|
|
table: string;
|
|
|
|
orderBy?: string;
|
|
|
|
orderDesc?: boolean;
|
|
|
|
limit?: number;
|
|
|
|
filter: Filter[];
|
2022-04-21 09:46:33 +00:00
|
|
|
select?: string[];
|
2022-04-11 18:34:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function parseQuery(query: string): ParsedQuery {
|
|
|
|
let n = lezerToParseTree(query, parser.parse(query).topNode);
|
|
|
|
// Clean the tree a bit
|
|
|
|
replaceNodesMatching(n, (n) => {
|
|
|
|
if (!n.type) {
|
|
|
|
let trimmed = n.text!.trim();
|
|
|
|
if (!trimmed) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
n.text = trimmed;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-12 11:33:07 +00:00
|
|
|
// console.log("Parsed", JSON.stringify(n, null, 2));
|
|
|
|
|
2022-04-11 18:34:09 +00:00
|
|
|
let queryNode = n.children![0];
|
|
|
|
let parsedQuery: ParsedQuery = {
|
|
|
|
table: queryNode.children![0].children![0].text!,
|
|
|
|
filter: [],
|
|
|
|
};
|
|
|
|
let orderByNode = findNodeOfType(queryNode, "OrderClause");
|
|
|
|
if (orderByNode) {
|
|
|
|
let nameNode = findNodeOfType(orderByNode, "Name");
|
|
|
|
parsedQuery.orderBy = nameNode!.children![0].text!;
|
|
|
|
let orderNode = findNodeOfType(orderByNode, "Order");
|
|
|
|
parsedQuery.orderDesc = orderNode
|
|
|
|
? orderNode.children![0].text! === "desc"
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
let limitNode = findNodeOfType(queryNode, "LimitClause");
|
|
|
|
if (limitNode) {
|
|
|
|
let nameNode = findNodeOfType(limitNode, "Number");
|
|
|
|
parsedQuery.limit = +nameNode!.children![0].text!;
|
|
|
|
}
|
|
|
|
let filterNodes = collectNodesOfType(queryNode, "FilterExpr");
|
|
|
|
for (let filterNode of filterNodes) {
|
|
|
|
let val: any = undefined;
|
|
|
|
let valNode = filterNode.children![2].children![0];
|
|
|
|
switch (valNode.type) {
|
|
|
|
case "Number":
|
|
|
|
val = valNode.children![0].text!;
|
|
|
|
break;
|
|
|
|
case "Bool":
|
|
|
|
val = valNode.children![0].text! === "true";
|
|
|
|
break;
|
2022-04-13 12:46:52 +00:00
|
|
|
case "Null":
|
|
|
|
val = null;
|
|
|
|
break;
|
2022-04-11 18:34:09 +00:00
|
|
|
case "Name":
|
|
|
|
val = valNode.children![0].text!;
|
|
|
|
break;
|
2022-04-12 11:33:07 +00:00
|
|
|
case "Regex":
|
|
|
|
val = valNode.children![0].text!;
|
2022-04-19 14:54:47 +00:00
|
|
|
val = val.substring(1, val.length - 1);
|
2022-04-12 11:33:07 +00:00
|
|
|
break;
|
2022-04-11 18:34:09 +00:00
|
|
|
case "String":
|
|
|
|
val = valNode.children![0].text!;
|
|
|
|
val = val.substring(1, val.length - 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let f: Filter = {
|
|
|
|
prop: filterNode.children![0].children![0].text!,
|
|
|
|
op: filterNode.children![1].text!,
|
|
|
|
value: val,
|
|
|
|
};
|
|
|
|
parsedQuery.filter.push(f);
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
let selectNode = findNodeOfType(queryNode, "SelectClause");
|
|
|
|
if (selectNode) {
|
|
|
|
console.log("Select node", JSON.stringify(selectNode));
|
|
|
|
parsedQuery.select = [];
|
|
|
|
collectNodesOfType(selectNode, "Name").forEach((t) => {
|
|
|
|
parsedQuery.select!.push(t.children![0].text!);
|
|
|
|
});
|
|
|
|
// let nameNode = findNodeOfType(selectNode, "Number");
|
|
|
|
// parsedQuery.limit = +nameNode!.children![0].text!;
|
|
|
|
}
|
|
|
|
|
2022-04-11 18:34:09 +00:00
|
|
|
// console.log(JSON.stringify(queryNode, null, 2));
|
|
|
|
return parsedQuery;
|
|
|
|
}
|
|
|
|
|
2022-04-12 11:33:07 +00:00
|
|
|
export function applyQuery<T>(parsedQuery: ParsedQuery, records: T[]): T[] {
|
2022-04-11 18:34:09 +00:00
|
|
|
let resultRecords: any[] = [];
|
|
|
|
if (parsedQuery.filter.length === 0) {
|
|
|
|
resultRecords = records.slice();
|
|
|
|
} else {
|
|
|
|
recordLoop: for (let record of records) {
|
2022-04-12 11:33:07 +00:00
|
|
|
const recordAny: any = record;
|
2022-04-11 18:34:09 +00:00
|
|
|
for (let { op, prop, value } of parsedQuery.filter) {
|
|
|
|
switch (op) {
|
|
|
|
case "=":
|
2022-04-13 12:46:52 +00:00
|
|
|
if (!(recordAny[prop] == value)) {
|
2022-04-11 18:34:09 +00:00
|
|
|
continue recordLoop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "!=":
|
2022-04-13 12:46:52 +00:00
|
|
|
if (!(recordAny[prop] != value)) {
|
2022-04-11 18:34:09 +00:00
|
|
|
continue recordLoop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "<":
|
2022-04-12 11:33:07 +00:00
|
|
|
if (!(recordAny[prop] < value)) {
|
2022-04-11 18:34:09 +00:00
|
|
|
continue recordLoop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "<=":
|
2022-04-12 11:33:07 +00:00
|
|
|
if (!(recordAny[prop] <= value)) {
|
2022-04-11 18:34:09 +00:00
|
|
|
continue recordLoop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ">":
|
2022-04-12 11:33:07 +00:00
|
|
|
if (!(recordAny[prop] > value)) {
|
2022-04-11 18:34:09 +00:00
|
|
|
continue recordLoop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ">=":
|
2022-04-12 11:33:07 +00:00
|
|
|
if (!(recordAny[prop] >= value)) {
|
2022-04-11 18:34:09 +00:00
|
|
|
continue recordLoop;
|
|
|
|
}
|
|
|
|
break;
|
2022-04-12 11:33:07 +00:00
|
|
|
case "=~":
|
2022-04-19 14:54:47 +00:00
|
|
|
// TODO: Cache regexps somehow
|
|
|
|
if (!new RegExp(value).exec(recordAny[prop])) {
|
2022-04-11 18:34:09 +00:00
|
|
|
continue recordLoop;
|
|
|
|
}
|
|
|
|
break;
|
2022-04-13 12:46:52 +00:00
|
|
|
case "!=~":
|
2022-04-19 14:54:47 +00:00
|
|
|
if (new RegExp(value).exec(recordAny[prop])) {
|
2022-04-13 12:46:52 +00:00
|
|
|
continue recordLoop;
|
|
|
|
}
|
|
|
|
break;
|
2022-04-11 18:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-12 11:33:07 +00:00
|
|
|
resultRecords.push(recordAny);
|
2022-04-11 18:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now the sorting
|
|
|
|
if (parsedQuery.orderBy) {
|
|
|
|
resultRecords = resultRecords.sort((a: any, b: any) => {
|
|
|
|
const orderBy = parsedQuery.orderBy!;
|
|
|
|
const orderDesc = parsedQuery.orderDesc!;
|
|
|
|
if (a[orderBy] === b[orderBy]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a[orderBy] < b[orderBy]) {
|
|
|
|
return orderDesc ? 1 : -1;
|
|
|
|
} else {
|
|
|
|
return orderDesc ? -1 : 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (parsedQuery.limit) {
|
|
|
|
resultRecords = resultRecords.slice(0, parsedQuery.limit);
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
if (parsedQuery.select) {
|
|
|
|
resultRecords = resultRecords.map((rec) => {
|
|
|
|
let newRec: any = {};
|
|
|
|
for (let k of parsedQuery.select!) {
|
|
|
|
newRec[k] = rec[k];
|
|
|
|
}
|
|
|
|
return newRec;
|
|
|
|
});
|
|
|
|
}
|
2022-04-11 18:34:09 +00:00
|
|
|
return resultRecords;
|
|
|
|
}
|