2019-03-16 16:52:58 +01:00
|
|
|
const VIRTUAL_ATTRIBUTES = ["dateCreated", "dateCreated", "dateModified", "utcDateCreated", "utcDateModified", "isProtected", "title", "content", "type", "mime", "text"];
|
2019-02-24 01:34:23 +01:00
|
|
|
|
|
|
|
function getValueForFilter(filter, i) {
|
2019-03-16 16:52:58 +01:00
|
|
|
return VIRTUAL_ATTRIBUTES.includes(filter.name) ? `notes.${filter.name}` :`attribute${i}.value`;
|
2019-02-24 01:34:23 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 20:55:16 +02:00
|
|
|
module.exports = function(attributeFilters) {
|
2018-03-23 23:08:29 -04:00
|
|
|
const joins = [];
|
|
|
|
const joinParams = [];
|
|
|
|
let where = '1';
|
|
|
|
const whereParams = [];
|
|
|
|
|
|
|
|
let i = 1;
|
|
|
|
|
2018-08-09 20:55:16 +02:00
|
|
|
for (const filter of attributeFilters) {
|
2019-03-16 16:52:58 +01:00
|
|
|
const virtual = VIRTUAL_ATTRIBUTES.includes(filter.name);
|
2019-02-24 01:34:23 +01:00
|
|
|
|
|
|
|
if (!virtual) {
|
|
|
|
joins.push(`LEFT JOIN attributes AS attribute${i} `
|
|
|
|
+ `ON attribute${i}.noteId = notes.noteId `
|
|
|
|
+ `AND attribute${i}.name = ? AND attribute${i}.isDeleted = 0`
|
|
|
|
);
|
|
|
|
joinParams.push(filter.name);
|
|
|
|
}
|
2019-03-16 16:52:58 +01:00
|
|
|
else if (filter.name === 'content') {
|
|
|
|
// FIXME: this will fail if there's more instances of content
|
|
|
|
joins.push(`JOIN note_contents ON note_contents.noteId = notes.noteId`);
|
|
|
|
|
|
|
|
filter.name = 'note_contents.content';
|
|
|
|
}
|
|
|
|
else if (filter.name === 'text') {
|
|
|
|
// FIXME: this will fail if there's more instances of content
|
|
|
|
joins.push(`JOIN note_fulltext ON note_fulltext.noteId = notes.noteId`);
|
|
|
|
}
|
2018-03-23 23:08:29 -04:00
|
|
|
|
|
|
|
where += " " + filter.relation + " ";
|
|
|
|
|
2019-02-24 01:34:23 +01:00
|
|
|
// the value we need to test
|
|
|
|
const test = virtual ? filter.name : `attribute${i}.attributeId`;
|
|
|
|
|
2018-03-23 23:08:29 -04:00
|
|
|
if (filter.operator === 'exists') {
|
2019-02-24 01:34:23 +01:00
|
|
|
where += `${test} IS NOT NULL`;
|
2018-03-23 23:08:29 -04:00
|
|
|
}
|
|
|
|
else if (filter.operator === 'not-exists') {
|
2019-02-24 01:34:23 +01:00
|
|
|
where += `${test} IS NULL`;
|
2018-03-23 23:08:29 -04:00
|
|
|
}
|
|
|
|
else if (filter.operator === '=' || filter.operator === '!=') {
|
2019-03-16 16:52:58 +01:00
|
|
|
if (filter.name === 'text') {
|
|
|
|
const safeSearchText = utils.sanitizeSql(filter.value);
|
|
|
|
|
|
|
|
where += (filter.operator === '!=' ? 'NOT ' : '') + `MATCH '${safeSearchText}'`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
where += `${getValueForFilter(filter, i)} ${filter.operator} ?`;
|
|
|
|
whereParams.push(filter.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (filter.operator === '*=' || filter.operator === '!*=') {
|
|
|
|
where += `${getValueForFilter(filter, i)}`
|
|
|
|
+ (filter.operator.includes('!') ? ' NOT' : '')
|
|
|
|
+ ` LIKE '%` + filter.value + "'"; // FIXME: escaping
|
|
|
|
}
|
|
|
|
else if (filter.operator === '=*' || filter.operator === '!=*') {
|
|
|
|
where += `${getValueForFilter(filter, i)}`
|
|
|
|
+ (filter.operator.includes('!') ? ' NOT' : '')
|
|
|
|
+ ` LIKE '` + filter.value + "%'"; // FIXME: escaping
|
|
|
|
}
|
|
|
|
else if (filter.operator === '*=*' || filter.operator === '!*=*') {
|
|
|
|
where += `${getValueForFilter(filter, i)}`
|
|
|
|
+ (filter.operator.includes('!') ? ' NOT' : '')
|
|
|
|
+ ` LIKE '%` + filter.value + "%'"; // FIXME: escaping
|
2018-03-23 23:08:29 -04:00
|
|
|
}
|
|
|
|
else if ([">", ">=", "<", "<="].includes(filter.operator)) {
|
2018-12-30 22:09:14 +01:00
|
|
|
let floatParam;
|
2019-02-24 01:34:23 +01:00
|
|
|
const value = getValueForFilter(filter, i);
|
2018-03-23 23:08:29 -04:00
|
|
|
|
2018-12-30 22:09:14 +01: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-02-24 01:34:23 +01:00
|
|
|
where += `${value} ${filter.operator} ?`;
|
2018-03-23 23:08:29 -04:00
|
|
|
whereParams.push(filter.value);
|
|
|
|
}
|
|
|
|
else {
|
2019-02-24 01:34:23 +01:00
|
|
|
where += `CAST(${value} AS DECIMAL) ${filter.operator} ?`;
|
2018-03-23 23:08:29 -04:00
|
|
|
whereParams.push(floatParam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error("Unknown operator " + filter.operator);
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
let searchCondition = '';
|
|
|
|
const searchParams = [];
|
|
|
|
|
|
|
|
const query = `SELECT DISTINCT notes.noteId FROM notes
|
|
|
|
${joins.join('\r\n')}
|
2019-02-24 01:34:23 +01:00
|
|
|
WHERE
|
2018-03-23 23:08:29 -04:00
|
|
|
notes.isDeleted = 0
|
2019-02-24 01:34:23 +01:00
|
|
|
AND (${where})
|
2018-03-23 23:08:29 -04:00
|
|
|
${searchCondition}`;
|
|
|
|
|
|
|
|
const params = joinParams.concat(whereParams).concat(searchParams);
|
|
|
|
|
2019-03-16 16:52:58 +01:00
|
|
|
console.log(query);
|
|
|
|
console.log(params);
|
|
|
|
|
2018-03-23 23:08:29 -04:00
|
|
|
return { query, params };
|
2019-02-24 01:34:23 +01:00
|
|
|
};
|