2019-03-16 23:52:58 +08:00
|
|
|
const VIRTUAL_ATTRIBUTES = ["dateCreated", "dateCreated", "dateModified", "utcDateCreated", "utcDateModified", "isProtected", "title", "content", "type", "mime", "text"];
|
2019-02-24 08:34:23 +08:00
|
|
|
|
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`;
|
|
|
|
}
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2019-03-17 02:57:39 +08:00
|
|
|
accessor = `${alias}.value`;
|
|
|
|
}
|
|
|
|
else if (property === 'content') {
|
|
|
|
const alias = "note_content";
|
2018-03-24 11:08:29 +08:00
|
|
|
|
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-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-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
|
|
|
}
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2019-03-17 02:57:39 +08:00
|
|
|
return accessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
let where = '1';
|
|
|
|
const params = [];
|
|
|
|
|
|
|
|
for (const filter of filters) {
|
2018-03-24 11:08:29 +08:00
|
|
|
where += " " + filter.relation + " ";
|
|
|
|
|
2019-03-17 02:57:39 +08:00
|
|
|
const accessor = getAccessor(filter.name);
|
2019-02-24 08:34:23 +08:00
|
|
|
|
2018-03-24 11:08:29 +08:00
|
|
|
if (filter.operator === 'exists') {
|
2019-03-17 02:57:39 +08:00
|
|
|
where += `${accessor} IS NOT NULL`;
|
2018-03-24 11:08:29 +08:00
|
|
|
}
|
|
|
|
else if (filter.operator === 'not-exists') {
|
2019-03-17 02:57:39 +08:00
|
|
|
where += `${accessor} IS NULL`;
|
2018-03-24 11:08:29 +08:00
|
|
|
}
|
|
|
|
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
|
2018-03-24 11:08:29 +08:00
|
|
|
}
|
|
|
|
else if ([">", ">=", "<", "<="].includes(filter.operator)) {
|
2018-12-31 05:09:14 +08:00
|
|
|
let floatParam;
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2018-12-31 05:09:14 +08:00
|
|
|
// 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);
|
2018-03-24 11:08:29 +08:00
|
|
|
}
|
|
|
|
else {
|
2019-03-17 02:57:39 +08:00
|
|
|
where += `CAST(${accessor} AS DECIMAL) ${filter.operator} ?`;
|
|
|
|
params.push(floatParam);
|
2018-03-24 11:08:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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')}
|
2019-02-24 08:34:23 +08:00
|
|
|
WHERE
|
2018-03-24 11:08:29 +08:00
|
|
|
notes.isDeleted = 0
|
2019-03-17 02:57:39 +08:00
|
|
|
AND (${where})`;
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2019-03-16 23:52:58 +08:00
|
|
|
console.log(query);
|
|
|
|
console.log(params);
|
|
|
|
|
2018-03-24 11:08:29 +08:00
|
|
|
return { query, params };
|
2019-02-24 08:34:23 +08:00
|
|
|
};
|