2019-03-17 03:52:21 +08:00
|
|
|
const utils = require('./utils');
|
|
|
|
|
2019-03-30 06:24:41 +08:00
|
|
|
const VIRTUAL_ATTRIBUTES = [
|
|
|
|
"dateCreated",
|
|
|
|
"dateModified",
|
|
|
|
"utcDateCreated",
|
|
|
|
"utcDateModified",
|
2019-04-01 04:23:50 +08:00
|
|
|
"noteId",
|
2019-03-30 06:24:41 +08:00
|
|
|
"isProtected",
|
|
|
|
"title",
|
|
|
|
"content",
|
|
|
|
"type",
|
|
|
|
"mime",
|
2019-12-04 03:31:34 +08:00
|
|
|
"text",
|
2020-04-26 04:10:56 +08:00
|
|
|
"parentCount",
|
|
|
|
"attributeName",
|
|
|
|
"attributeValue"
|
2019-03-30 06:24:41 +08:00
|
|
|
];
|
2019-02-24 08:34:23 +08:00
|
|
|
|
2019-03-17 05:19:01 +08:00
|
|
|
module.exports = function(filters, selectedColumns = 'notes.*') {
|
2019-03-17 02:57:39 +08:00
|
|
|
// alias => join
|
|
|
|
const joins = {
|
|
|
|
"notes": null
|
|
|
|
};
|
|
|
|
|
2020-01-02 03:49:26 +08:00
|
|
|
let attrFilterId = 1;
|
|
|
|
|
2019-03-17 02:57:39 +08:00
|
|
|
function getAccessor(property) {
|
|
|
|
let accessor;
|
|
|
|
|
|
|
|
if (!VIRTUAL_ATTRIBUTES.includes(property)) {
|
2020-01-02 03:49:26 +08:00
|
|
|
// not reusing existing filters to support multi-valued filters e.g. "@tag=christmas @tag=shopping"
|
|
|
|
// can match notes because @tag can be both "shopping" and "christmas"
|
|
|
|
const alias = "attr_" + property + "_" + attrFilterId++;
|
2019-03-17 02:57:39 +08:00
|
|
|
|
2020-01-05 02:22:16 +08:00
|
|
|
// forcing to use particular index since SQLite query planner would often choose something pretty bad
|
|
|
|
joins[alias] = `LEFT JOIN attributes AS ${alias} INDEXED BY IDX_attributes_noteId_index `
|
2020-04-26 04:10:56 +08:00
|
|
|
+ `ON ${alias}.noteId = notes.noteId AND ${alias}.isDeleted = 0`
|
|
|
|
+ `AND ${alias}.name = '${property}' `;
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2019-03-17 02:57:39 +08:00
|
|
|
accessor = `${alias}.value`;
|
|
|
|
}
|
2020-04-26 04:10:56 +08:00
|
|
|
else if (['attributeType', 'attributeName', 'attributeValue'].includes(property)) {
|
|
|
|
const alias = "attr_filter";
|
|
|
|
|
|
|
|
if (!(alias in joins)) {
|
|
|
|
joins[alias] = `LEFT JOIN attributes AS ${alias} INDEXED BY IDX_attributes_noteId_index `
|
|
|
|
+ `ON ${alias}.noteId = notes.noteId AND ${alias}.isDeleted = 0`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (property === 'attributeType') {
|
|
|
|
accessor = `${alias}.type`
|
|
|
|
} else if (property === 'attributeName') {
|
|
|
|
accessor = `${alias}.name`
|
|
|
|
} else if (property === 'attributeValue') {
|
|
|
|
accessor = `${alias}.value`
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unrecognized property ${property}`);
|
|
|
|
}
|
|
|
|
}
|
2019-03-17 02:57:39 +08:00
|
|
|
else if (property === 'content') {
|
2019-03-17 03:52:21 +08:00
|
|
|
const alias = "note_contents";
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2019-03-17 02:57:39 +08:00
|
|
|
if (!(alias in joins)) {
|
2019-04-01 04:23:50 +08:00
|
|
|
joins[alias] = `LEFT JOIN note_contents ON note_contents.noteId = notes.noteId`;
|
2019-03-17 02:57:39 +08:00
|
|
|
}
|
2019-02-24 08:34:23 +08:00
|
|
|
|
2019-03-17 02:57:39 +08:00
|
|
|
accessor = `${alias}.${property}`;
|
2019-02-24 08:34:23 +08:00
|
|
|
}
|
2019-12-04 03:31:34 +08:00
|
|
|
else if (property === 'parentCount') {
|
|
|
|
// need to cast as string for the equality operator to work
|
|
|
|
// for >= etc. it is cast again to DECIMAL
|
2019-12-04 04:31:46 +08:00
|
|
|
// also cannot use COUNT() in WHERE so using subquery ...
|
2019-12-04 03:31:34 +08:00
|
|
|
accessor = `CAST((SELECT COUNT(1) FROM branches WHERE branches.noteId = notes.noteId AND isDeleted = 0) AS STRING)`;
|
|
|
|
}
|
2019-03-17 02:57:39 +08:00
|
|
|
else {
|
|
|
|
accessor = "notes." + property;
|
2019-03-16 23:52:58 +08:00
|
|
|
}
|
2018-03-24 11:08:29 +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";
|
2019-03-17 05:19:01 +08:00
|
|
|
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
|
|
|
const params = [];
|
|
|
|
|
2020-04-26 04:10:56 +08:00
|
|
|
function parseWhereFilters(filters) {
|
|
|
|
let whereStmt = '';
|
2019-04-21 17:54:13 +08:00
|
|
|
|
2020-04-26 04:10:56 +08:00
|
|
|
for (const filter of filters) {
|
|
|
|
if (['isarchived', 'in', 'orderby', 'limit'].includes(filter.name.toLowerCase())) {
|
|
|
|
continue; // these are not real filters
|
2019-04-21 17:54:13 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 04:10:56 +08:00
|
|
|
if (whereStmt) {
|
|
|
|
whereStmt += " " + filter.relation + " ";
|
2019-12-11 04:40:53 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 04:10:56 +08:00
|
|
|
if (filter.children) {
|
|
|
|
whereStmt += "(" + parseWhereFilters(filter.children) + ")";
|
|
|
|
continue;
|
2018-12-31 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 04:10:56 +08:00
|
|
|
const accessor = getAccessor(filter.name);
|
|
|
|
|
|
|
|
if (filter.operator === 'exists') {
|
|
|
|
whereStmt += `${accessor} IS NOT NULL`;
|
|
|
|
} else if (filter.operator === 'not-exists') {
|
|
|
|
whereStmt += `${accessor} IS NULL`;
|
|
|
|
} else if (filter.operator === '=' || filter.operator === '!=') {
|
|
|
|
whereStmt += `${accessor} ${filter.operator} ?`;
|
2019-03-17 02:57:39 +08:00
|
|
|
params.push(filter.value);
|
2020-04-26 04:10:56 +08:00
|
|
|
} else if (filter.operator === '*=' || filter.operator === '!*=') {
|
|
|
|
whereStmt += `${accessor}`
|
|
|
|
+ (filter.operator.includes('!') ? ' NOT' : '')
|
|
|
|
+ ` LIKE ` + utils.prepareSqlForLike('%', filter.value, '');
|
|
|
|
} else if (filter.operator === '=*' || filter.operator === '!=*') {
|
|
|
|
whereStmt += `${accessor}`
|
|
|
|
+ (filter.operator.includes('!') ? ' NOT' : '')
|
|
|
|
+ ` LIKE ` + utils.prepareSqlForLike('', filter.value, '%');
|
|
|
|
} 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 + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['text', 'title', 'content'].includes(filter.name)) {
|
|
|
|
// for title/content search does not make sense to search for protected notes
|
|
|
|
condition = `(${condition} AND notes.isProtected = 0)`;
|
|
|
|
}
|
|
|
|
|
|
|
|
whereStmt += 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
|
|
|
|
whereStmt += `${accessor} ${filter.operator} ?`;
|
|
|
|
params.push(filter.value);
|
|
|
|
} else {
|
|
|
|
whereStmt += `CAST(${accessor} AS DECIMAL) ${filter.operator} ?`;
|
|
|
|
params.push(floatParam);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error("Unknown operator " + filter.operator);
|
2018-03-24 11:08:29 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-26 04:10:56 +08:00
|
|
|
|
|
|
|
return whereStmt;
|
2018-03-24 11:08:29 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 04:10:56 +08:00
|
|
|
const where = parseWhereFilters(filters);
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2019-03-17 05:19:01 +08:00
|
|
|
const query = `SELECT ${selectedColumns} FROM notes
|
2019-03-17 02:57:39 +08:00
|
|
|
${Object.values(joins).join('\r\n')}
|
2019-02-24 08:34:23 +08:00
|
|
|
WHERE
|
2018-03-24 11:08:29 +08:00
|
|
|
notes.isDeleted = 0
|
2019-03-17 03:52:21 +08:00
|
|
|
AND (${where})
|
2019-03-17 05:19:01 +08:00
|
|
|
GROUP BY notes.noteId
|
|
|
|
ORDER BY ${orderBy.join(", ")}`;
|
2018-03-24 11:08:29 +08:00
|
|
|
|
|
|
|
return { query, params };
|
2019-02-24 08:34:23 +08:00
|
|
|
};
|