From a3030f845b87f9e9437ec88b5f0956c147ec88c1 Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 20 Nov 2017 23:51:28 -0500 Subject: [PATCH] unified SQL syntax to uppercase --- public/javascripts/dialogs/add_link.js | 2 +- public/javascripts/note_editor.js | 2 +- routes/api/note_history.js | 2 +- routes/api/notes.js | 6 ++--- routes/api/notes_move.js | 18 ++++++------- routes/api/recent_changes.js | 2 +- routes/api/tree.js | 14 +++++----- services/audit_category.js | 2 +- services/notes.js | 37 +++++++++++++------------- services/sql.js | 12 +++++++-- services/sync.js | 2 +- services/sync_update.js | 10 +++---- 12 files changed, 58 insertions(+), 51 deletions(-) diff --git a/public/javascripts/dialogs/add_link.js b/public/javascripts/dialogs/add_link.js index ff1086d6a..649cb14d5 100644 --- a/public/javascripts/dialogs/add_link.js +++ b/public/javascripts/dialogs/add_link.js @@ -38,7 +38,7 @@ const addLink = (function() { } }, // this is called when user goes through autocomplete list with keyboard - // at this point the item isn't selected yet so we use supplied ui.item to see where the cursor is + // at this point the item isn't selected yet so we use supplied ui.item to see WHERE the cursor is focus: (event, ui) => { const noteId = link.getNodePathFromLabel(ui.item.value); diff --git a/public/javascripts/note_editor.js b/public/javascripts/note_editor.js index 021cbc981..016cda841 100644 --- a/public/javascripts/note_editor.js +++ b/public/javascripts/note_editor.js @@ -123,7 +123,7 @@ const noteEditor = (function() { async function createNote(node, parentTreeId, target, isProtected) { // if isProtected isn't available (user didn't enter password yet), then note is created as unencrypted - // but this is quite weird since user doesn't see where the note is being created so it shouldn't occur often + // but this is quite weird since user doesn't see WHERE the note is being created so it shouldn't occur often if (!isProtected || !protected_session.isProtectedSessionAvailable()) { isProtected = false; } diff --git a/routes/api/note_history.js b/routes/api/note_history.js index 07db8a8d6..f1a5201cd 100644 --- a/routes/api/note_history.js +++ b/routes/api/note_history.js @@ -10,7 +10,7 @@ const sync_table = require('../../services/sync_table'); router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => { const noteId = req.params.noteId; - const history = await sql.getResults("select * from notes_history where note_id = ? order by date_modified_to desc", [noteId]); + const history = await sql.getResults("SELECT * FROM notes_history WHERE note_id = ? order by date_modified_to desc", [noteId]); const dataKey = protected_session.getDataKey(req); diff --git a/routes/api/notes.js b/routes/api/notes.js index 5f28c5a3f..cbc437463 100644 --- a/routes/api/notes.js +++ b/routes/api/notes.js @@ -13,7 +13,7 @@ const RequestContext = require('../../services/request_context'); router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => { const noteId = req.params.noteId; - const detail = await sql.getSingleResult("select * from notes where note_id = ?", [noteId]); + const detail = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); if (detail.is_protected) { const dataKey = protected_session.getDataKey(req); @@ -24,7 +24,7 @@ router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => { res.send({ detail: detail, - images: await sql.getResults("select * from images where note_id = ? order by note_offset", [detail.note_id]), + images: await sql.getResults("SELECT * FROM images WHERE note_id = ? order by note_offset", [detail.note_id]), loadTime: utils.nowTimestamp() }); }); @@ -65,7 +65,7 @@ router.delete('/:noteTreeId', async (req, res, next) => { router.get('/', async (req, res, next) => { const search = '%' + req.query.search + '%'; - const result = await sql.getResults("select note_id from notes where note_title like ? or note_text like ?", [search, search]); + const result = await sql.getResults("SELECT note_id FROM notes WHERE note_title liKE ? OR note_text LIKE ?", [search, search]); const noteIdList = []; diff --git a/routes/api/notes_move.js b/routes/api/notes_move.js index 3a8044f6d..7496d1adb 100644 --- a/routes/api/notes_move.js +++ b/routes/api/notes_move.js @@ -12,13 +12,13 @@ router.put('/:noteTreeId/moveTo/:parentNoteId', auth.checkApiAuth, async (req, r const noteTreeId = req.params.noteTreeId; const parentNoteId = req.params.parentNoteId; - const maxNotePos = await sql.getSingleValue('select max(note_pos) from notes_tree where note_pid = ? and is_deleted = 0', [parentNoteId]); + const maxNotePos = await sql.getSingleValue('SELECT MAX(note_pos) FROM notes_tree WHERE note_pid = ? AND is_deleted = 0', [parentNoteId]); const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; const now = utils.nowTimestamp(); await sql.doInTransaction(async () => { - await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_tree_id = ?", + await sql.execute("UPDATE notes_tree SET note_pid = ?, note_pos = ?, date_modified = ? WHERE note_tree_id = ?", [parentNoteId, newNotePos, now, noteTreeId]); await sync_table.addNoteTreeSync(noteTreeId); @@ -32,17 +32,17 @@ router.put('/:noteTreeId/moveBefore/:beforeNoteTreeId', async (req, res, next) = const noteTreeId = req.params.noteTreeId; const beforeNoteTreeId = req.params.beforeNoteTreeId; - const beforeNote = await sql.getSingleResult("select * from notes_tree where note_tree_id = ?", [beforeNoteTreeId]); + const beforeNote = await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [beforeNoteTreeId]); if (beforeNote) { await sql.doInTransaction(async () => { // we don't change date_modified so other changes are prioritized in case of conflict - await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos >= ? and is_deleted = 0", + await sql.execute("UPDATE notes_tree SET note_pos = note_pos + 1 WHERE note_pid = ? AND note_pos >= ? AND is_deleted = 0", [beforeNote.note_pid, beforeNote.note_pos]); const now = utils.nowTimestamp(); - await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_tree_id = ?", + await sql.execute("UPDATE notes_tree SET note_pid = ?, note_pos = ?, date_modified = ? WHERE note_tree_id = ?", [beforeNote.note_pid, beforeNote.note_pos, now, noteTreeId]); await sync_table.addNoteTreeSync(noteTreeId); @@ -61,17 +61,17 @@ router.put('/:noteTreeId/moveAfter/:afterNoteTreeId', async (req, res, next) => const noteTreeId = req.params.noteTreeId; const afterNoteTreeId = req.params.afterNoteTreeId; - const afterNote = await sql.getSingleResult("select * from notes_tree where note_tree_id = ?", [afterNoteTreeId]); + const afterNote = await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]); if (afterNote) { await sql.doInTransaction(async () => { // we don't change date_modified so other changes are prioritized in case of conflict - await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ? and is_deleted = 0", + await sql.execute("UPDATE notes_tree SET note_pos = note_pos + 1 WHERE note_pid = ? AND note_pos > ? AND is_deleted = 0", [afterNote.note_pid, afterNote.note_pos]); const now = utils.nowTimestamp(); - await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_tree_id = ?", + await sql.execute("UPDATE notes_tree SET note_pid = ?, note_pos = ?, date_modified = ? WHERE note_tree_id = ?", [afterNote.note_pid, afterNote.note_pos + 1, now, noteTreeId]); await sync_table.addNoteTreeSync(noteTreeId); @@ -91,7 +91,7 @@ router.put('/:noteTreeId/expanded/:expanded', async (req, res, next) => { const expanded = req.params.expanded; await sql.doInTransaction(async () => { - await sql.execute("update notes_tree set is_expanded = ? where note_tree_id = ?", [expanded, noteTreeId]); + await sql.execute("UPDATE notes_tree SET is_expanded = ? WHERE note_tree_id = ?", [expanded, noteTreeId]); }); res.send({}); diff --git a/routes/api/recent_changes.js b/routes/api/recent_changes.js index 06c970100..018e163da 100644 --- a/routes/api/recent_changes.js +++ b/routes/api/recent_changes.js @@ -6,7 +6,7 @@ const sql = require('../../services/sql'); const auth = require('../../services/auth'); router.get('/', auth.checkApiAuth, async (req, res, next) => { - const recentChanges = await sql.getResults("select * from notes_history order by date_modified_to desc limit 1000"); + const recentChanges = await sql.getResults("SELECT * FROM notes_history order by date_modified_to desc limit 1000"); res.send(recentChanges); }); diff --git a/routes/api/tree.js b/routes/api/tree.js index f69109e54..bf67962eb 100644 --- a/routes/api/tree.js +++ b/routes/api/tree.js @@ -12,14 +12,14 @@ const notes = require('../../services/notes'); const sync_table = require('../../services/sync_table'); router.get('/', auth.checkApiAuth, async (req, res, next) => { - const notes = await sql.getResults("select " + const notes = await sql.getResults("SELECT " + "notes_tree.*, " + "notes.note_title, " + "notes.is_protected " - + "from notes_tree " - + "join notes on notes.note_id = notes_tree.note_id " - + "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 " - + "order by note_pos"); + + "FROM notes_tree " + + "JOIN notes ON notes.note_id = notes_tree.note_id " + + "WHERE notes.is_deleted = 0 AND notes_tree.is_deleted = 0 " + + "ORDER BY note_pos"); const dataKey = protected_session.getDataKey(req); @@ -52,10 +52,10 @@ router.put('/:parentNoteId/addChild/:childNoteId', auth.checkApiAuth, async (req const parentNoteId = req.params.parentNoteId; const childNoteId = req.params.childNoteId; - const existing = await sql.getSingleValue('select * from notes_tree where note_id = ? and note_pid = ?', [childNoteId, parentNoteId]); + const existing = await sql.getSingleValue('SELECT * FROM notes_tree WHERE note_id = ? AND note_pid = ?', [childNoteId, parentNoteId]); if (!existing) { - const maxNotePos = await sql.getSingleValue('select max(note_pos) from notes_tree where note_pid = ? and is_deleted = 0', [parentNoteId]); + const maxNotePos = await sql.getSingleValue('SELECT MAX(note_pos) FROM notes_tree WHERE note_pid = ? AND is_deleted = 0', [parentNoteId]); const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; const noteTreeId = utils.newNoteTreeId(); diff --git a/services/audit_category.js b/services/audit_category.js index ac6e25b4f..77f831e2d 100644 --- a/services/audit_category.js +++ b/services/audit_category.js @@ -3,7 +3,7 @@ module.exports = { UPDATE_CONTENT: 'CONTENT', UPDATE_TITLE: 'TITLE', - // associated noteId is parent of notes where position changes happened + // associated noteId is parent of notes WHERE position changes happened CHANGE_POSITION: 'POSITION', CHANGE_EXPANDED: 'EXPANDED', CREATE_NOTE: 'CREATE', diff --git a/services/notes.js b/services/notes.js index 00ffc6dc4..d5e9a5085 100644 --- a/services/notes.js +++ b/services/notes.js @@ -13,16 +13,16 @@ async function createNewNote(parentNoteId, note, browserId) { let newNotePos = 0; if (note.target === 'into') { - const maxNotePos = await sql.getSingleValue('select max(note_pos) from notes_tree where note_pid = ? and is_deleted = 0', [parentNoteId]); + const maxNotePos = await sql.getSingleValue('SELECT MAX(note_pos) FROM notes_tree WHERE note_pid = ? AND is_deleted = 0', [parentNoteId]); newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; } else if (note.target === 'after') { - const afterNote = await sql.getSingleResult('select note_pos from notes_tree where note_id = ?', [note.target_note_id]); + const afterNote = await sql.getSingleResult('SELECT note_pos FROM notes_tree WHERE note_id = ?', [note.target_note_id]); newNotePos = afterNote.note_pos + 1; - await sql.execute('update notes_tree set note_pos = note_pos + 1, date_modified = ? where note_pid = ? and note_pos > ? and is_deleted = 0', + await sql.execute('UPDATE notes_tree SET note_pos = note_pos + 1, date_modified = ? WHERE note_pid = ? AND note_pos > ? AND is_deleted = 0', [utils.nowTimestamp(), parentNoteId, afterNote.note_pos]); } else { @@ -139,7 +139,7 @@ async function updateNote(noteId, newNote, ctx) { await encryptNote(newNote, ctx); } - const origNoteDetail = await sql.getSingleResult("select * from notes where note_id = ?", [noteId]); + const origNoteDetail = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); const now = utils.nowTimestamp(); @@ -147,22 +147,21 @@ async function updateNote(noteId, newNote, ctx) { const historyCutoff = now - historySnapshotTimeInterval; - const existingNoteHistoryId = await sql.getSingleValue("select note_history_id from notes_history where note_id = ? and date_modified_from >= ?", [noteId, historyCutoff]); + const existingNoteHistoryId = await sql.getSingleValue("SELECT note_history_id FROM notes_history WHERE note_id = ? AND date_modified_from >= ?", [noteId, historyCutoff]); await sql.doInTransaction(async () => { if (!existingNoteHistoryId) { const newNoteHistoryId = utils.newNoteHistoryId(); - await sql.execute("insert into notes_history (note_history_id, note_id, note_title, note_text, is_protected, date_modified_from, date_modified_to) " + - "values (?, ?, ?, ?, ?, ?, ?)", [ - newNoteHistoryId, - noteId, - noteTitleForHistory, - noteTextForHistory, - false, // we don't care about encryption - this will be handled in protectNoteHistory() - now, - now - ]); + await sql.insert('notes_history', { + note_history_id: newNoteHistoryId, + note_id: noteId, + note_title: noteTitleForHistory, + note_text: noteTextForHistory, + is_protected: false, // we don't care about encryption - this will be handled in protectNoteHistory() + date_modified_from: now, + date_modified_to: now + }); await sync_table.addNoteHistorySync(newNoteHistoryId); } @@ -171,7 +170,7 @@ async function updateNote(noteId, newNote, ctx) { await addNoteAudits(origNoteDetail, newNote.detail, ctx.browserId); - await sql.execute("update notes set note_title = ?, note_text = ?, is_protected = ?, date_modified = ? where note_id = ?", [ + await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ?, date_modified = ? WHERE note_id = ?", [ newNote.detail.note_title, newNote.detail.note_text, newNote.detail.is_protected, @@ -220,7 +219,7 @@ async function addNoteAudits(origNote, newNote, browserId) { async function deleteNote(noteTreeId, browserId) { const now = utils.nowTimestamp(); - await sql.execute("update notes_tree set is_deleted = 1, date_modified = ? where note_tree_id = ?", [now, noteTreeId]); + await sql.execute("UPDATE notes_tree SET is_deleted = 1, date_modified = ? WHERE note_tree_id = ?", [now, noteTreeId]); await sync_table.addNoteTreeSync(noteTreeId); const noteId = await sql.getSingleValue("SELECT note_id FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]); @@ -228,10 +227,10 @@ async function deleteNote(noteTreeId, browserId) { const notDeletedNoteTreesCount = await sql.getSingleValue("SELECT COUNT(*) FROM notes_tree WHERE note_id = ?", [noteId]); if (!notDeletedNoteTreesCount) { - await sql.execute("update notes set is_deleted = 1, date_modified = ? where note_id = ?", [now, noteTreeId]); + await sql.execute("UPDATE notes SET is_deleted = 1, date_modified = ? WHERE note_id = ?", [now, noteTreeId]); await sync_table.addNoteSync(noteTreeId); - const children = await sql.getResults("select note_tree_id from notes_tree where note_pid = ? and is_deleted = 0", [noteTreeId]); + const children = await sql.getResults("SELECT note_tree_id FROM notes_tree WHERE note_pid = ? AND is_deleted = 0", [noteTreeId]); for (const child of children) { await deleteNote(child.note_tree_id, browserId); diff --git a/services/sql.js b/services/sql.js index 5c882d196..4fdd2751f 100644 --- a/services/sql.js +++ b/services/sql.js @@ -110,8 +110,16 @@ async function addAudit(category, browserId=null, noteId=null, changeFrom=null, const id = utils.randomString(14); - await execute("INSERT INTO audit_log (id, date_modified, category, browser_id, note_id, change_from, change_to, comment)" - + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [id, now, category, browserId, noteId, changeFrom, changeTo, comment]); + await insert("audit_log", { + id: id, + date_modified: now, + category: category, + browser_id: browserId, + note_id: noteId, + change_from: changeFrom, + change_to: changeTo, + comment: comment + }); } async function deleteRecentAudits(category, browserId, noteId) { diff --git a/services/sync.js b/services/sync.js index 5a8e0ca65..666adc612 100644 --- a/services/sync.js +++ b/services/sync.js @@ -217,7 +217,7 @@ async function sendEntity(syncContext, entity, entityName) { }; if (entityName === 'notes') { - payload.links = await sql.getResults('select * from links where note_id = ?', [entity.note_id]); + payload.links = await sql.getResults('SELECT * FROM links WHERE note_id = ?', [entity.note_id]); } await syncRequest(syncContext, 'PUT', '/api/sync/' + entityName, payload); diff --git a/services/sync_update.js b/services/sync_update.js index 58564ae16..c0e57d162 100644 --- a/services/sync_update.js +++ b/services/sync_update.js @@ -8,7 +8,7 @@ const notes = require('./notes'); const sync_table = require('./sync_table'); async function updateNote(entity, links, sourceId) { - const origNote = await sql.getSingleResult("select * from notes where note_id = ?", [entity.note_id]); + const origNote = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [entity.note_id]); if (!origNote || origNote.date_modified <= entity.date_modified) { await sql.doInTransaction(async () => { @@ -35,7 +35,7 @@ async function updateNote(entity, links, sourceId) { } async function updateNoteTree(entity, sourceId) { - const orig = await sql.getSingleResultOrNull("select * from notes_tree where note_tree_id = ?", [entity.note_tree_id]); + const orig = await sql.getSingleResultOrNull("SELECT * FROM notes_tree WHERE note_tree_id = ?", [entity.note_tree_id]); if (orig === null || orig.date_modified < entity.date_modified) { await sql.doInTransaction(async () => { @@ -57,7 +57,7 @@ async function updateNoteTree(entity, sourceId) { } async function updateNoteHistory(entity, sourceId) { - const orig = await sql.getSingleResultOrNull("select * from notes_history where note_history_id = ?", [entity.note_history_id]); + const orig = await sql.getSingleResultOrNull("SELECT * FROM notes_history WHERE note_history_id = ?", [entity.note_history_id]); if (orig === null || orig.date_modified_to < entity.date_modified_to) { await sql.doInTransaction(async () => { @@ -89,7 +89,7 @@ async function updateOptions(entity, sourceId) { return; } - const orig = await sql.getSingleResultOrNull("select * from options where opt_name = ?", [entity.opt_name]); + const orig = await sql.getSingleResultOrNull("SELECT * FROM options WHERE opt_name = ?", [entity.opt_name]); if (orig === null || orig.date_modified < entity.date_modified) { await sql.doInTransaction(async () => { @@ -106,7 +106,7 @@ async function updateOptions(entity, sourceId) { } async function updateRecentNotes(entity, sourceId) { - const orig = await sql.getSingleResultOrNull("select * from recent_notes where note_path = ?", [entity.note_path]); + const orig = await sql.getSingleResultOrNull("SELECT * FROM recent_notes WHERE note_path = ?", [entity.note_path]); if (orig === null || orig.date_accessed < entity.date_accessed) { await sql.doInTransaction(async () => {