trilium/src/services/build_search_query.js

124 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-03-16 23:52:58 +08:00
const VIRTUAL_ATTRIBUTES = ["dateCreated", "dateCreated", "dateModified", "utcDateCreated", "utcDateModified", "isProtected", "title", "content", "type", "mime", "text"];
2019-03-17 02:57:39 +08:00
module.exports = function(filters) {
// alias => join
const joins = {
"notes": null
};
function getAccessor(property) {
let accessor;
if (!VIRTUAL_ATTRIBUTES.includes(property)) {
const alias = "attr_" + property;
if (!(alias in joins)) {
joins[alias] = `LEFT JOIN attributes AS ${alias} `
+ `ON ${alias}.noteId = notes.noteId `
+ `AND ${alias}.name = '${property}' AND ${alias}.isDeleted = 0`;
}
2019-03-17 02:57:39 +08:00
accessor = `${alias}.value`;
}
else if (property === 'content') {
const alias = "note_content";
2019-03-17 02:57:39 +08:00
if (!(alias in joins)) {
// FIXME: this will fail if there's more instances of content
joins[alias] = `JOIN note_contents ON note_contents.noteId = notes.noteId`;
}
2019-03-17 02:57:39 +08:00
accessor = `${alias}.${property}`;
}
2019-03-17 02:57:39 +08:00
else if (property === 'text') {
const alias = "note_fulltext";
if (!(alias in joins)) {
// FIXME: this will fail if there's more instances of content
joins[alias] = `JOIN note_fulltext ON note_fulltext.noteId = notes.noteId`;
}
2019-03-16 23:52:58 +08:00
2019-03-17 02:57:39 +08:00
accessor = alias;
2019-03-16 23:52:58 +08:00
}
2019-03-17 02:57:39 +08:00
else {
accessor = "notes." + property;
2019-03-16 23:52:58 +08:00
}
2019-03-17 02:57:39 +08:00
return accessor;
}
let where = '1';
const params = [];
for (const filter of filters) {
where += " " + filter.relation + " ";
2019-03-17 02:57:39 +08:00
const accessor = getAccessor(filter.name);
if (filter.operator === 'exists') {
2019-03-17 02:57:39 +08:00
where += `${accessor} IS NOT NULL`;
}
else if (filter.operator === 'not-exists') {
2019-03-17 02:57:39 +08:00
where += `${accessor} IS NULL`;
}
else if (filter.operator === '=' || filter.operator === '!=') {
2019-03-16 23:52:58 +08:00
if (filter.name === 'text') {
const safeSearchText = utils.sanitizeSql(filter.value);
2019-03-17 02:57:39 +08:00
where += accessor + ' ' + (filter.operator === '!=' ? 'NOT ' : '') + `MATCH '${safeSearchText}'`;
2019-03-16 23:52:58 +08:00
}
else {
2019-03-17 02:57:39 +08:00
where += `${accessor} ${filter.operator} ?`;
params.push(filter.value);
2019-03-16 23:52:58 +08:00
}
}
else if (filter.operator === '*=' || filter.operator === '!*=') {
2019-03-17 02:57:39 +08:00
where += `${accessor}`
2019-03-16 23:52:58 +08:00
+ (filter.operator.includes('!') ? ' NOT' : '')
+ ` LIKE '%` + filter.value + "'"; // FIXME: escaping
}
else if (filter.operator === '=*' || filter.operator === '!=*') {
2019-03-17 02:57:39 +08:00
where += `${accessor}`
2019-03-16 23:52:58 +08:00
+ (filter.operator.includes('!') ? ' NOT' : '')
+ ` LIKE '` + filter.value + "%'"; // FIXME: escaping
}
else if (filter.operator === '*=*' || filter.operator === '!*=*') {
2019-03-17 02:57:39 +08:00
where += `${accessor}`
2019-03-16 23:52:58 +08:00
+ (filter.operator.includes('!') ? ' NOT' : '')
+ ` LIKE '%` + filter.value + "%'"; // FIXME: escaping
}
else if ([">", ">=", "<", "<="].includes(filter.operator)) {
let floatParam;
// from https://stackoverflow.com/questions/12643009/regular-expression-for-floating-point-numbers
if (/^[+-]?([0-9]*[.])?[0-9]+$/.test(filter.value)) {
floatParam = parseFloat(filter.value);
}
if (floatParam === undefined || isNaN(floatParam)) {
// if the value can't be parsed as float then we assume that string comparison should be used instead of numeric
2019-03-17 02:57:39 +08:00
where += `${accessor} ${filter.operator} ?`;
params.push(filter.value);
}
else {
2019-03-17 02:57:39 +08:00
where += `CAST(${accessor} AS DECIMAL) ${filter.operator} ?`;
params.push(floatParam);
}
}
else {
throw new Error("Unknown operator " + filter.operator);
}
}
const query = `SELECT DISTINCT notes.noteId FROM notes
2019-03-17 02:57:39 +08:00
${Object.values(joins).join('\r\n')}
WHERE
notes.isDeleted = 0
2019-03-17 02:57:39 +08:00
AND (${where})`;
2019-03-16 23:52:58 +08:00
console.log(query);
console.log(params);
return { query, params };
};