renamed outstanding attribute references to labels

This commit is contained in:
azivner
2018-04-01 09:59:44 -04:00
parent ab2f28ceef
commit c9d73c6115
12 changed files with 71 additions and 73 deletions

View File

@@ -34,9 +34,7 @@ async function backupNow() {
log.info("Created backup at " + backupFile);
await sql.doInTransaction(async () => {
await options.setOption('last_backup_date', now);
});
await options.setOption('last_backup_date', now);
});
}

View File

@@ -1,4 +1,4 @@
module.exports = function(attrFilters, searchText) {
module.exports = function(labelFilters, searchText) {
const joins = [];
const joinParams = [];
let where = '1';
@@ -6,31 +6,31 @@ module.exports = function(attrFilters, searchText) {
let i = 1;
for (const filter of attrFilters) {
joins.push(`LEFT JOIN labels AS attr${i} ON attr${i}.noteId = notes.noteId AND attr${i}.name = ?`);
for (const filter of labelFilters) {
joins.push(`LEFT JOIN labels AS label${i} ON label${i}.noteId = notes.noteId AND label${i}.name = ?`);
joinParams.push(filter.name);
where += " " + filter.relation + " ";
if (filter.operator === 'exists') {
where += `attr${i}.labelId IS NOT NULL`;
where += `label${i}.labelId IS NOT NULL`;
}
else if (filter.operator === 'not-exists') {
where += `attr${i}.labelId IS NULL`;
where += `label${i}.labelId IS NULL`;
}
else if (filter.operator === '=' || filter.operator === '!=') {
where += `attr${i}.value ${filter.operator} ?`;
where += `label${i}.value ${filter.operator} ?`;
whereParams.push(filter.value);
}
else if ([">", ">=", "<", "<="].includes(filter.operator)) {
const floatParam = parseFloat(filter.value);
if (isNaN(floatParam)) {
where += `attr${i}.value ${filter.operator} ?`;
where += `label${i}.value ${filter.operator} ?`;
whereParams.push(filter.value);
}
else {
where += `CAST(attr${i}.value AS DECIMAL) ${filter.operator} ?`;
where += `CAST(label${i}.value AS DECIMAL) ${filter.operator} ?`;
whereParams.push(floatParam);
}
}

View File

@@ -104,8 +104,8 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
const {noteId} = await createNewNote(parentNoteId, note);
if (extraOptions.labels) {
for (const attrName in extraOptions.labels) {
await labels.createLabel(noteId, attrName, extraOptions.labels[attrName]);
for (const labelName in extraOptions.labels) {
await labels.createLabel(noteId, labelName, extraOptions.labels[labelName]);
}
}

View File

@@ -1,9 +1,9 @@
module.exports = function(searchText) {
const attrFilters = [];
const labelFilters = [];
const attrRegex = /(\b(and|or)\s+)?@(!?)([\w_-]+|"[^"]+")((=|!=|<|<=|>|>=)([\w_-]+|"[^"]+"))?/i;
const labelRegex = /(\b(and|or)\s+)?@(!?)([\w_-]+|"[^"]+")((=|!=|<|<=|>|>=)([\w_-]+|"[^"]+"))?/i;
let match = attrRegex.exec(searchText);
let match = labelRegex.exec(searchText);
function trimQuotes(str) { return str.startsWith('"') ? str.substr(1, str.length - 2) : str; }
@@ -11,7 +11,7 @@ module.exports = function(searchText) {
const relation = match[2] !== undefined ? match[2].toLowerCase() : 'and';
const operator = match[3] === '!' ? 'not-exists' : 'exists';
attrFilters.push({
labelFilters.push({
relation: relation,
name: trimQuotes(match[4]),
operator: match[6] !== undefined ? match[6] : operator,
@@ -21,8 +21,8 @@ module.exports = function(searchText) {
// remove labels from further fulltext search
searchText = searchText.split(match[0]).join('');
match = attrRegex.exec(searchText);
match = labelRegex.exec(searchText);
}
return {attrFilters, searchText};
return {labelFilters: labelFilters, searchText};
};

View File

@@ -44,12 +44,12 @@ function ScriptApi(startNote, currentNote) {
return await repository.getNote(noteId);
};
this.getNotesWithLabel = async function (attrName, attrValue) {
return await labels.getNotesWithLabel(attrName, attrValue);
this.getNotesWithLabel = async function (labelName, labelValue) {
return await labels.getNotesWithLabel(labelName, labelValue);
};
this.getNoteWithLabel = async function (attrName, attrValue) {
const notes = await this.getNotesWithLabel(attrName, attrValue);
this.getNoteWithLabel = async function (labelName, labelValue) {
const notes = await this.getNotesWithLabel(labelName, labelValue);
return notes.length > 0 ? notes[0] : null;
};