trilium/src/services/build_search_query.js

157 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-03-17 03:52:21 +08:00
const utils = require('./utils');
2019-03-16 23:52:58 +08:00
const VIRTUAL_ATTRIBUTES = ["dateCreated", "dateCreated", "dateModified", "utcDateCreated", "utcDateModified", "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] = `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)) {
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;
}
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) {
2019-03-17 03:52:21 +08:00
if (filter.name.toLowerCase() === 'orderby') {
continue; // orderby is not real filter
}
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 03:52:21 +08:00
let condition = accessor + ' ' + `MATCH '${safeSearchText}'`;
if (filter.operator.includes("!")) {
// not supported!
}
else if (orderBy.length === 0) {
// if there's a positive full text search and there's no defined order then order by rank
orderBy.push("rank");
}
2019-03-16 23:52:58 +08:00
2019-03-17 03:52:21 +08:00
where += condition;
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 ` + 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 === '!*=*') {
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, '%');
}
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(", ")}`;
2019-03-16 23:52:58 +08:00
console.log(query);
console.log(params);
return { query, params };
};