2023-02-16 12:55:24 +02:00
|
|
|
/* eslint-disable id-denylist */
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
import time from '../../time';
|
2020-08-08 01:13:21 +02:00
|
|
|
|
|
|
|
interface Term {
|
2020-11-12 21:29:22 +02:00
|
|
|
name: string;
|
|
|
|
value: string;
|
|
|
|
negated: boolean;
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Relation {
|
2023-07-06 20:17:41 +02:00
|
|
|
OR = 'OR',
|
|
|
|
AND = 'AND',
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Operation {
|
|
|
|
UNION = 'UNION',
|
2020-11-12 21:23:54 +02:00
|
|
|
INTERSECT = 'INTERSECT',
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Requirement {
|
|
|
|
EXCLUSION = 'EXCLUSION',
|
2020-11-12 21:23:54 +02:00
|
|
|
INCLUSION = 'INCLUSION',
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const _notebookFilter = (notebooks: string[], requirement: Requirement, conditions: string[], params: string[], withs: string[], useFts: boolean) => {
|
2020-08-08 01:13:21 +02:00
|
|
|
if (notebooks.length === 0) return;
|
|
|
|
|
|
|
|
const likes = [];
|
|
|
|
for (let i = 0; i < notebooks.length; i++) {
|
|
|
|
likes.push('folders.title LIKE ?');
|
|
|
|
}
|
2021-03-11 16:42:39 +02:00
|
|
|
|
2021-01-23 17:51:19 +02:00
|
|
|
const relevantFolders = likes.join(' OR ');
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-03-11 16:42:39 +02:00
|
|
|
const viewName = requirement === Requirement.EXCLUSION ? 'notebooks_not_in_scope' : 'notebooks_in_scope';
|
2020-08-08 01:13:21 +02:00
|
|
|
const withInNotebook = `
|
2021-03-11 16:42:39 +02:00
|
|
|
${viewName}(id)
|
2020-08-08 01:13:21 +02:00
|
|
|
AS (
|
|
|
|
SELECT folders.id
|
|
|
|
FROM folders
|
|
|
|
WHERE id
|
|
|
|
IN (
|
|
|
|
SELECT id
|
|
|
|
FROM folders
|
|
|
|
WHERE ${relevantFolders}
|
|
|
|
)
|
|
|
|
UNION ALL
|
|
|
|
SELECT folders.id
|
|
|
|
FROM folders
|
2021-03-11 16:42:39 +02:00
|
|
|
JOIN ${viewName}
|
|
|
|
ON folders.parent_id=${viewName}.id
|
2020-08-08 01:13:21 +02:00
|
|
|
)`;
|
2021-03-11 16:42:39 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const tableName = useFts ? 'notes_normalized' : 'notes';
|
2020-08-08 01:13:21 +02:00
|
|
|
const where = `
|
2021-03-11 16:42:39 +02:00
|
|
|
AND ROWID ${requirement === Requirement.EXCLUSION ? 'NOT' : ''} IN (
|
2021-06-07 16:15:04 +02:00
|
|
|
SELECT ${tableName}.ROWID
|
2021-03-11 16:42:39 +02:00
|
|
|
FROM ${viewName}
|
2021-06-07 16:15:04 +02:00
|
|
|
JOIN ${tableName}
|
|
|
|
ON ${viewName}.id=${tableName}.parent_id
|
2020-08-08 01:13:21 +02:00
|
|
|
)`;
|
|
|
|
|
2021-03-11 16:42:39 +02:00
|
|
|
|
2020-08-08 01:13:21 +02:00
|
|
|
withs.push(withInNotebook);
|
|
|
|
params.push(...notebooks);
|
|
|
|
conditions.push(where);
|
2021-03-11 16:42:39 +02:00
|
|
|
|
2020-08-08 01:13:21 +02:00
|
|
|
};
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const notebookFilter = (terms: Term[], conditions: string[], params: string[], withs: string[], useFts: boolean) => {
|
2021-03-11 16:42:39 +02:00
|
|
|
const notebooksToInclude = terms.filter(x => x.name === 'notebook' && !x.negated).map(x => x.value);
|
2021-06-07 16:15:04 +02:00
|
|
|
_notebookFilter(notebooksToInclude, Requirement.INCLUSION, conditions, params, withs, useFts);
|
2021-03-11 16:42:39 +02:00
|
|
|
|
|
|
|
const notebooksToExclude = terms.filter(x => x.name === 'notebook' && x.negated).map(x => x.value);
|
2021-06-07 16:15:04 +02:00
|
|
|
_notebookFilter(notebooksToExclude, Requirement.EXCLUSION, conditions, params, withs, useFts);
|
2021-03-11 16:42:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-08 01:13:21 +02:00
|
|
|
const getOperator = (requirement: Requirement, relation: Relation): Operation => {
|
|
|
|
if (relation === 'AND' && requirement === 'INCLUSION') { return Operation.INTERSECT; } else { return Operation.UNION; }
|
|
|
|
};
|
|
|
|
|
|
|
|
const filterByTableName = (
|
|
|
|
terms: Term[],
|
|
|
|
conditions: string[],
|
|
|
|
params: string[],
|
|
|
|
relation: Relation,
|
|
|
|
noteIDs: string,
|
|
|
|
requirement: Requirement,
|
|
|
|
withs: string[],
|
2021-06-07 16:15:04 +02:00
|
|
|
tableName: string,
|
2023-08-22 12:58:53 +02:00
|
|
|
useFts: boolean,
|
2020-08-08 01:13:21 +02:00
|
|
|
) => {
|
|
|
|
const operator: Operation = getOperator(requirement, relation);
|
|
|
|
|
|
|
|
const values = terms.map(x => x.value);
|
|
|
|
|
|
|
|
let withCondition = null;
|
|
|
|
|
|
|
|
if (relation === Relation.OR && requirement === Requirement.EXCLUSION) {
|
|
|
|
// with_${requirement}_${tableName} is added to the names to make them unique
|
|
|
|
withs.push(`
|
|
|
|
all_notes_with_${requirement}_${tableName}
|
|
|
|
AS (
|
|
|
|
SELECT DISTINCT note_${tableName}.note_id AS id FROM note_${tableName}
|
|
|
|
)`);
|
|
|
|
|
|
|
|
const notesWithoutExcludedField = `
|
|
|
|
SELECT * FROM (
|
|
|
|
SELECT *
|
|
|
|
FROM all_notes_with_${requirement}_${tableName}
|
|
|
|
EXCEPT ${noteIDs}
|
|
|
|
)`;
|
|
|
|
|
|
|
|
const requiredNotes = [];
|
|
|
|
for (let i = 0; i < values.length; i++) {
|
|
|
|
requiredNotes.push(notesWithoutExcludedField);
|
|
|
|
}
|
|
|
|
const requiredNotesQuery = requiredNotes.join(' UNION ');
|
|
|
|
|
2024-03-11 13:51:02 +02:00
|
|
|
// We need notes without at least one excluded (tag/resource)
|
2020-08-08 01:13:21 +02:00
|
|
|
withCondition = `
|
|
|
|
notes_with_${requirement}_${tableName}
|
|
|
|
AS (
|
|
|
|
${requiredNotesQuery}
|
|
|
|
)`;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
const requiredNotes = [];
|
|
|
|
for (let i = 0; i < values.length; i++) {
|
|
|
|
requiredNotes.push(noteIDs);
|
|
|
|
}
|
|
|
|
const requiredNotesQuery = requiredNotes.join(` ${operator} `);
|
|
|
|
|
|
|
|
|
|
|
|
// Notes with any/all values depending upon relation and requirement
|
|
|
|
withCondition = `
|
|
|
|
notes_with_${requirement}_${tableName}
|
|
|
|
AS (
|
|
|
|
SELECT note_${tableName}.note_id as id
|
|
|
|
FROM note_${tableName}
|
|
|
|
WHERE
|
|
|
|
${operator === 'INTERSECT' ? 1 : 0} ${operator}
|
|
|
|
${requiredNotesQuery}
|
|
|
|
)`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ROWIDs that satisfy the condition so we can filter the result
|
2021-06-07 16:15:04 +02:00
|
|
|
const targetTableName = useFts ? 'notes_normalized' : 'notes';
|
2020-08-08 01:13:21 +02:00
|
|
|
const whereCondition = `
|
|
|
|
${relation} ROWID ${(relation === 'AND' && requirement === 'EXCLUSION') ? 'NOT' : ''}
|
|
|
|
IN (
|
2021-06-07 16:15:04 +02:00
|
|
|
SELECT ${targetTableName}.ROWID
|
2020-08-08 01:13:21 +02:00
|
|
|
FROM notes_with_${requirement}_${tableName}
|
2021-06-07 16:15:04 +02:00
|
|
|
JOIN ${targetTableName}
|
|
|
|
ON notes_with_${requirement}_${tableName}.id=${targetTableName}.id
|
2020-08-08 01:13:21 +02:00
|
|
|
)`;
|
|
|
|
|
|
|
|
withs.push(withCondition);
|
|
|
|
params.push(...values);
|
|
|
|
conditions.push(whereCondition);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const resourceFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, withs: string[], useFts: boolean) => {
|
2020-08-08 01:13:21 +02:00
|
|
|
const tableName = 'resources';
|
|
|
|
|
|
|
|
const resourceIDs = `
|
|
|
|
SELECT resources.id
|
|
|
|
FROM resources
|
|
|
|
WHERE resources.mime LIKE ?`;
|
|
|
|
|
|
|
|
const noteIDsWithResource = `
|
|
|
|
SELECT note_resources.note_id AS id
|
|
|
|
FROM note_resources
|
|
|
|
WHERE note_resources.is_associated=1
|
|
|
|
AND note_resources.resource_id IN (${resourceIDs})`;
|
|
|
|
|
|
|
|
const requiredResources = terms.filter(x => x.name === 'resource' && !x.negated);
|
|
|
|
const excludedResources = terms.filter(x => x.name === 'resource' && x.negated);
|
|
|
|
|
|
|
|
if (requiredResources.length > 0) {
|
2021-06-07 16:15:04 +02:00
|
|
|
filterByTableName(requiredResources, conditions, params, relation, noteIDsWithResource, Requirement.INCLUSION, withs, tableName, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (excludedResources.length > 0) {
|
2021-06-07 16:15:04 +02:00
|
|
|
filterByTableName(excludedResources, conditions, params, relation, noteIDsWithResource, Requirement.EXCLUSION, withs, tableName, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const tagFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, withs: string[], useFts: boolean) => {
|
2020-08-08 01:13:21 +02:00
|
|
|
const tableName = 'tags';
|
|
|
|
|
|
|
|
const tagIDs = `
|
|
|
|
SELECT tags.id
|
|
|
|
FROM tags
|
|
|
|
WHERE tags.title
|
|
|
|
LIKE ?`;
|
|
|
|
|
|
|
|
const noteIDsWithTag = `
|
|
|
|
SELECT note_tags.note_id AS id
|
|
|
|
FROM note_tags
|
|
|
|
WHERE note_tags.tag_id IN (${tagIDs})`;
|
|
|
|
|
|
|
|
const requiredTags = terms.filter(x => x.name === 'tag' && !x.negated);
|
|
|
|
const excludedTags = terms.filter(x => x.name === 'tag' && x.negated);
|
|
|
|
|
|
|
|
if (requiredTags.length > 0) {
|
2021-06-07 16:15:04 +02:00
|
|
|
filterByTableName(requiredTags, conditions, params, relation, noteIDsWithTag, Requirement.INCLUSION, withs, tableName, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (excludedTags.length > 0) {
|
2021-06-07 16:15:04 +02:00
|
|
|
filterByTableName(excludedTags, conditions, params, relation, noteIDsWithTag, Requirement.EXCLUSION, withs, tableName, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const genericFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, fieldName: string, useFts: boolean) => {
|
2020-08-08 01:13:21 +02:00
|
|
|
if (fieldName === 'iscompleted' || fieldName === 'type') {
|
|
|
|
// Faster query when values can only take two distinct values
|
2021-06-07 16:15:04 +02:00
|
|
|
biConditionalFilter(terms, conditions, relation, fieldName, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const tableName = useFts ? 'notes_normalized' : 'notes';
|
|
|
|
|
2020-08-08 01:13:21 +02:00
|
|
|
const getCondition = (term: Term) => {
|
2021-04-29 16:27:38 +02:00
|
|
|
if (fieldName === 'sourceurl') {
|
2021-06-07 16:15:04 +02:00
|
|
|
return `${tableName}.source_url ${term.negated ? 'NOT' : ''} LIKE ?`;
|
2021-04-29 16:27:38 +02:00
|
|
|
} else if (fieldName === 'date' && term.name === 'due') {
|
|
|
|
return `todo_due ${term.negated ? '<' : '>='} ?`;
|
|
|
|
} else if (fieldName === 'id') {
|
|
|
|
return `id ${term.negated ? 'NOT' : ''} LIKE ?`;
|
|
|
|
} else {
|
2021-06-07 16:15:04 +02:00
|
|
|
return `${tableName}.${fieldName === 'date' ? `user_${term.name}_time` : `${term.name}`} ${term.negated ? '<' : '>='} ?`;
|
2021-04-29 16:27:38 +02:00
|
|
|
}
|
2020-08-08 01:13:21 +02:00
|
|
|
};
|
|
|
|
|
2023-06-30 10:39:21 +02:00
|
|
|
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
|
2020-08-08 01:13:21 +02:00
|
|
|
terms.forEach(term => {
|
|
|
|
conditions.push(`
|
2021-04-29 16:27:38 +02:00
|
|
|
${relation} ( ${term.name === 'due' ? 'is_todo IS 1 AND ' : ''} ROWID IN (
|
2020-08-08 01:13:21 +02:00
|
|
|
SELECT ROWID
|
2021-06-07 16:15:04 +02:00
|
|
|
FROM ${tableName}
|
2020-08-08 01:13:21 +02:00
|
|
|
WHERE ${getCondition(term)}
|
2021-04-29 16:27:38 +02:00
|
|
|
))`);
|
2020-08-08 01:13:21 +02:00
|
|
|
params.push(term.value);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const biConditionalFilter = (terms: Term[], conditions: string[], relation: Relation, filterName: string, useFts: boolean) => {
|
2023-01-11 20:37:22 +02:00
|
|
|
const getCondition = (filterName: string, value: string, relation: Relation) => {
|
2021-06-07 16:15:04 +02:00
|
|
|
const tableName = useFts ? (relation === 'AND' ? 'notes_fts' : 'notes_normalized') : 'notes';
|
2020-08-08 01:13:21 +02:00
|
|
|
if (filterName === 'type') {
|
|
|
|
return `${tableName}.is_todo IS ${value === 'todo' ? 1 : 0}`;
|
|
|
|
} else if (filterName === 'iscompleted') {
|
|
|
|
return `${tableName}.is_todo IS 1 AND ${tableName}.todo_completed IS ${value === '1' ? 'NOT 0' : '0'}`;
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid filter name.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const values = terms.map(x => x.value);
|
|
|
|
|
|
|
|
// AND and OR are handled differently because FTS restricts how OR can be used.
|
2023-06-30 10:39:21 +02:00
|
|
|
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
|
2020-08-08 01:13:21 +02:00
|
|
|
values.forEach(value => {
|
|
|
|
if (relation === 'AND') {
|
|
|
|
conditions.push(`
|
|
|
|
AND ${getCondition(filterName, value, relation)}`);
|
|
|
|
}
|
|
|
|
if (relation === 'OR') {
|
2021-06-07 16:15:04 +02:00
|
|
|
if (useFts) {
|
|
|
|
conditions.push(`
|
|
|
|
OR ROWID IN (
|
|
|
|
SELECT ROWID
|
|
|
|
FROM notes_normalized
|
|
|
|
WHERE ${getCondition(filterName, value, relation)}
|
|
|
|
)`);
|
|
|
|
} else {
|
|
|
|
conditions.push(`
|
|
|
|
OR ${getCondition(filterName, value, relation)}`);
|
|
|
|
}
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const noteIdFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, useFts: boolean) => {
|
2021-04-29 16:27:38 +02:00
|
|
|
const noteIdTerms = terms.filter(x => x.name === 'id');
|
2021-06-07 16:15:04 +02:00
|
|
|
genericFilter(noteIdTerms, conditions, params, relation, 'id', useFts);
|
2021-04-29 16:27:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const typeFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, useFts: boolean) => {
|
2020-08-08 01:13:21 +02:00
|
|
|
const typeTerms = terms.filter(x => x.name === 'type');
|
2021-06-07 16:15:04 +02:00
|
|
|
genericFilter(typeTerms, conditions, params, relation, 'type', useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
};
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const completedFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, useFts: boolean) => {
|
2020-08-08 01:13:21 +02:00
|
|
|
const completedTerms = terms.filter(x => x.name === 'iscompleted');
|
2021-06-07 16:15:04 +02:00
|
|
|
genericFilter(completedTerms, conditions, params, relation, 'iscompleted', useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-02-26 12:16:23 +02:00
|
|
|
const locationFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, useFts: boolean) => {
|
2020-08-08 01:13:21 +02:00
|
|
|
const locationTerms = terms.filter(x => x.name === 'latitude' || x.name === 'longitude' || x.name === 'altitude');
|
2024-02-26 12:16:23 +02:00
|
|
|
genericFilter(locationTerms, conditions, params, relation, 'location', useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
};
|
|
|
|
|
2024-02-26 12:16:23 +02:00
|
|
|
const dateFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, useFts: boolean) => {
|
2020-11-12 21:13:28 +02:00
|
|
|
const getUnixMs = (date: string): string => {
|
2020-08-08 01:13:21 +02:00
|
|
|
const yyyymmdd = /^[0-9]{8}$/;
|
|
|
|
const yyyymm = /^[0-9]{6}$/;
|
|
|
|
const yyyy = /^[0-9]{4}$/;
|
2021-04-29 16:27:38 +02:00
|
|
|
const smartValue = /^(day|week|month|year)(\+|-)([0-9]+)$/i;
|
2020-08-08 01:13:21 +02:00
|
|
|
|
|
|
|
if (yyyymmdd.test(date)) {
|
|
|
|
return time.formatLocalToMs(date, 'YYYYMMDD').toString();
|
|
|
|
} else if (yyyymm.test(date)) {
|
|
|
|
return time.formatLocalToMs(date, 'YYYYMM').toString();
|
|
|
|
} else if (yyyy.test(date)) {
|
|
|
|
return time.formatLocalToMs(date, 'YYYY').toString();
|
|
|
|
} else if (smartValue.test(date)) {
|
|
|
|
const match = smartValue.exec(date);
|
|
|
|
const timeUnit = match[1]; // eg. day, week, month, year
|
2021-04-29 16:27:38 +02:00
|
|
|
const timeDirection = match[2]; // + or -
|
|
|
|
const num = Number(match[3]); // eg. 1, 12, 15
|
|
|
|
|
|
|
|
if (timeDirection === '+') { return time.goForwardInTime(Date.now(), num, timeUnit); } else { return time.goBackInTime(Date.now(), num, timeUnit); }
|
2020-08-08 01:13:21 +02:00
|
|
|
} else {
|
|
|
|
throw new Error('Invalid date format!');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-29 16:27:38 +02:00
|
|
|
const dateTerms = terms.filter(x => x.name === 'created' || x.name === 'updated' || x.name === 'due');
|
2020-08-08 01:13:21 +02:00
|
|
|
const unixDateTerms = dateTerms.map(term => { return { ...term, value: getUnixMs(term.value) }; });
|
2024-02-26 12:16:23 +02:00
|
|
|
genericFilter(unixDateTerms, conditions, params, relation, 'date', useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
};
|
|
|
|
|
2024-02-26 12:16:23 +02:00
|
|
|
const sourceUrlFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, useFts: boolean) => {
|
2020-08-08 01:13:21 +02:00
|
|
|
const urlTerms = terms.filter(x => x.name === 'sourceurl');
|
2024-02-26 12:16:23 +02:00
|
|
|
genericFilter(urlTerms, conditions, params, relation, 'sourceurl', useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
};
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const trimQuotes = (str: string) => str.startsWith('"') && str.endsWith('"') ? str.substr(1, str.length - 2) : str;
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const textFilter = (terms: Term[], conditions: string[], params: string[], relation: Relation, useFts: boolean) => {
|
|
|
|
const createLikeMatch = (term: Term, negate: boolean) => {
|
|
|
|
const query = `${relation} ${negate ? 'NOT' : ''} (
|
|
|
|
${(term.name === 'text' || term.name === 'body') ? 'notes.body LIKE ? ' : ''}
|
|
|
|
${term.name === 'text' ? 'OR' : ''}
|
|
|
|
${(term.name === 'text' || term.name === 'title') ? 'notes.title LIKE ? ' : ''})`;
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
conditions.push(query);
|
|
|
|
const param = `%${trimQuotes(term.value).replace(/\*/, '%')}%`;
|
|
|
|
params.push(param);
|
|
|
|
if (term.name === 'text') params.push(param);
|
|
|
|
};
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const addExcludeTextConditions = (excludedTerms: Term[], conditions: string[], params: string[], relation: Relation) => {
|
|
|
|
if (useFts) {
|
|
|
|
const type = excludedTerms[0].name === 'text' ? '' : `.${excludedTerms[0].name}`;
|
|
|
|
if (relation === 'AND') {
|
2020-08-08 01:13:21 +02:00
|
|
|
conditions.push(`
|
2021-06-07 16:15:04 +02:00
|
|
|
AND ROWID NOT IN (
|
|
|
|
SELECT ROWID
|
|
|
|
FROM notes_fts
|
|
|
|
WHERE notes_fts${type} MATCH ?
|
2020-08-08 01:13:21 +02:00
|
|
|
)`);
|
2021-06-07 16:15:04 +02:00
|
|
|
params.push(excludedTerms.map(x => x.value).join(' OR '));
|
|
|
|
}
|
|
|
|
if (relation === 'OR') {
|
2023-06-30 10:39:21 +02:00
|
|
|
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
|
2021-06-07 16:15:04 +02:00
|
|
|
excludedTerms.forEach(term => {
|
|
|
|
conditions.push(`
|
|
|
|
OR ROWID IN (
|
|
|
|
SELECT *
|
|
|
|
FROM (
|
|
|
|
SELECT ROWID
|
|
|
|
FROM notes_fts
|
|
|
|
EXCEPT
|
|
|
|
SELECT ROWID
|
|
|
|
FROM notes_fts
|
|
|
|
WHERE notes_fts${type} MATCH ?
|
|
|
|
)
|
|
|
|
)`);
|
|
|
|
params.push(term.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-30 10:39:21 +02:00
|
|
|
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
|
2021-06-07 16:15:04 +02:00
|
|
|
excludedTerms.forEach(term => {
|
|
|
|
createLikeMatch(term, true);
|
2020-08-08 01:13:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const allTerms = terms.filter(x => x.name === 'title' || x.name === 'body' || x.name === 'text');
|
|
|
|
|
|
|
|
const includedTerms = allTerms.filter(x => !x.negated);
|
|
|
|
if (includedTerms.length > 0) {
|
2021-06-07 16:15:04 +02:00
|
|
|
if (useFts) {
|
|
|
|
conditions.push(`${relation} notes_fts MATCH ?`);
|
|
|
|
const termsToMatch = includedTerms.map(term => {
|
|
|
|
if (term.name === 'text') return term.value;
|
|
|
|
else return `${term.name}:${term.value}`;
|
|
|
|
});
|
|
|
|
const matchQuery = (relation === 'OR') ? termsToMatch.join(' OR ') : termsToMatch.join(' ');
|
|
|
|
params.push(matchQuery);
|
|
|
|
} else {
|
2023-06-30 10:39:21 +02:00
|
|
|
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
|
2021-06-07 16:15:04 +02:00
|
|
|
includedTerms.forEach(term => {
|
|
|
|
createLikeMatch(term, false);
|
|
|
|
});
|
|
|
|
}
|
2020-08-08 01:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const excludedTextTerms = allTerms.filter(x => x.name === 'text' && x.negated);
|
|
|
|
const excludedTitleTerms = allTerms.filter(x => x.name === 'title' && x.negated);
|
|
|
|
const excludedBodyTerms = allTerms.filter(x => x.name === 'body' && x.negated);
|
|
|
|
|
|
|
|
if ((excludedTextTerms.length > 0)) {
|
|
|
|
addExcludeTextConditions(excludedTextTerms, conditions, params, relation);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (excludedTitleTerms.length > 0) {
|
|
|
|
addExcludeTextConditions(excludedTitleTerms, conditions, params, relation);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (excludedBodyTerms.length > 0) {
|
|
|
|
addExcludeTextConditions(excludedBodyTerms, conditions, params, relation);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDefaultRelation = (terms: Term[]): Relation => {
|
|
|
|
const anyTerm = terms.find(term => term.name === 'any');
|
|
|
|
if (anyTerm) { return (anyTerm.value === '1') ? Relation.OR : Relation.AND; }
|
|
|
|
return Relation.AND;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getConnective = (terms: Term[], relation: Relation): string => {
|
|
|
|
const notebookTerm = terms.find(x => x.name === 'notebook');
|
|
|
|
return (!notebookTerm && (relation === 'OR')) ? 'ROWID=-1' : '1'; // ROWID=-1 acts as 0 (something always false)
|
|
|
|
};
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
export default function queryBuilder(terms: Term[], useFts: boolean) {
|
2020-08-08 01:13:21 +02:00
|
|
|
const queryParts: string[] = [];
|
|
|
|
const params: string[] = [];
|
|
|
|
const withs: string[] = [];
|
|
|
|
|
|
|
|
const relation: Relation = getDefaultRelation(terms);
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
const tableName = useFts ? 'notes_fts' : 'notes';
|
|
|
|
|
2020-08-08 01:13:21 +02:00
|
|
|
queryParts.push(`
|
|
|
|
SELECT
|
2021-06-07 16:15:04 +02:00
|
|
|
${tableName}.id,
|
|
|
|
${tableName}.title,
|
|
|
|
${useFts ? 'offsets(notes_fts) AS offsets, matchinfo(notes_fts, \'pcnalx\') AS matchinfo,' : ''}
|
|
|
|
${tableName}.user_created_time,
|
|
|
|
${tableName}.user_updated_time,
|
|
|
|
${tableName}.is_todo,
|
|
|
|
${tableName}.todo_completed,
|
|
|
|
${tableName}.parent_id
|
|
|
|
FROM ${tableName}
|
2020-08-08 01:13:21 +02:00
|
|
|
WHERE ${getConnective(terms, relation)}`);
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
noteIdFilter(terms, queryParts, params, relation, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
notebookFilter(terms, queryParts, params, withs, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
tagFilter(terms, queryParts, params, relation, withs, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
resourceFilter(terms, queryParts, params, relation, withs, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
textFilter(terms, queryParts, params, relation, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
typeFilter(terms, queryParts, params, relation, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
completedFilter(terms, queryParts, params, relation, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
dateFilter(terms, queryParts, params, relation, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
locationFilter(terms, queryParts, params, relation, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
2021-06-07 16:15:04 +02:00
|
|
|
sourceUrlFilter(terms, queryParts, params, relation, useFts);
|
2020-08-08 01:13:21 +02:00
|
|
|
|
|
|
|
let query;
|
|
|
|
if (withs.length > 0) {
|
2020-10-20 12:54:46 +02:00
|
|
|
if (terms.find(x => x.name === 'notebook') && terms.find(x => x.name === 'any')) {
|
|
|
|
// The notebook filter should be independent of the any filter.
|
|
|
|
// So we're first finding the OR of other filters and then combining them with the notebook filter using AND
|
|
|
|
// in-required-notebook AND (condition #1 OR condition #2 OR ... )
|
|
|
|
const queryString = `${queryParts.slice(0, 2).join(' ')} AND ROWID IN ( SELECT ROWID FROM notes_fts WHERE 1=0 ${queryParts.slice(2).join(' ')})`;
|
2023-01-11 20:37:22 +02:00
|
|
|
query = ['WITH RECURSIVE', withs.join(','), queryString].join(' ');
|
2020-10-20 12:54:46 +02:00
|
|
|
} else {
|
2023-01-11 20:37:22 +02:00
|
|
|
query = ['WITH RECURSIVE', withs.join(','), queryParts.join(' ')].join(' ');
|
2020-10-20 12:54:46 +02:00
|
|
|
}
|
2020-08-08 01:13:21 +02:00
|
|
|
} else {
|
|
|
|
query = queryParts.join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
return { query, params };
|
|
|
|
}
|