trilium/src/services/build_search_query.js

149 lines
4.7 KiB
JavaScript
Raw Normal View History

2019-03-17 03:52:21 +08:00
const utils = require('./utils');
const VIRTUAL_ATTRIBUTES = [
"dateCreated",
"dateModified",
"utcDateCreated",
"utcDateModified",
"noteId",
"isProtected",
"title",
"content",
"type",
"mime",
"text"
];
module.exports = function(filters, selectedColumns = 'notes.*') {
2019-03-17 02:57:39 +08:00
// 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') {
2019-03-17 03:52:21 +08:00
const alias = "note_contents";
2019-03-17 02:57:39 +08:00
if (!(alias in joins)) {
joins[alias] = `LEFT JOIN note_contents ON note_contents.noteId = notes.noteId`;
2019-03-17 02:57:39 +08:00
}
2019-03-17 02:57:39 +08:00
accessor = `${alias}.${property}`;
}
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;
}
2019-03-17 03:52:21 +08:00
let orderBy = [];
const orderByFilter = filters.find(filter => filter.name.toLowerCase() === 'orderby');
if (orderByFilter) {
orderBy = orderByFilter.value.split(",").map(prop => {
const direction = prop.includes("-") ? "DESC" : "ASC";
const cleanedProp = prop.trim().replace(/-/g, "");
2019-03-17 03:52:21 +08:00
return getAccessor(cleanedProp) + " " + direction;
});
}
2019-03-17 02:57:39 +08:00
let where = '1';
const params = [];
for (const filter of filters) {
if (['isarchived', 'in', 'orderby', 'limit'].includes(filter.name.toLowerCase())) {
continue; // these are not real filters
2019-03-17 03:52:21 +08:00
}
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 === '!=') {
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 ` + utils.prepareSqlForLike('%', 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 ` + utils.prepareSqlForLike('', filter.value, '%');
2019-03-16 23:52:58 +08:00
}
else if (filter.operator === '*=*' || filter.operator === '!*=*') {
const columns = filter.name === 'text' ? [getAccessor("title"), getAccessor("content")] : [accessor];
let condition = "(" + columns.map(column =>
`${column}` + ` LIKE ` + utils.prepareSqlForLike('%', filter.value, '%'))
.join(" OR ") + ")";
if (filter.operator.includes('!')) {
condition = "NOT(" + condition + ")";
}
where += condition;
}
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);
}
}
2019-03-17 03:52:21 +08:00
if (orderBy.length === 0) {
// if no ordering is given then order at least by note title
orderBy.push("notes.title");
}
const query = `SELECT ${selectedColumns} FROM notes
2019-03-17 02:57:39 +08:00
${Object.values(joins).join('\r\n')}
WHERE
notes.isDeleted = 0
2019-03-17 03:52:21 +08:00
AND (${where})
GROUP BY notes.noteId
ORDER BY ${orderBy.join(", ")}`;
return { query, params };
};