renaming of sql methods to fit getRows/getEntities model

This commit is contained in:
azivner 2018-01-29 17:41:59 -05:00
parent 9b53a17168
commit b44412bc32
32 changed files with 134 additions and 123 deletions

View File

@ -17,7 +17,7 @@ module.exports = async () => {
const password = await question("Enter password: ");
const dataKey = await password_encryption.getDecryptedDataKey(password);
const protectedNotes = await sql.getAll("SELECT * FROM notes WHERE is_protected = 1");
const protectedNotes = await sql.getRows("SELECT * FROM notes WHERE is_protected = 1");
for (const note of protectedNotes) {
const decryptedTitle = data_encryption.decrypt(dataKey, note.note_title);
@ -30,7 +30,7 @@ module.exports = async () => {
await sql.execute("UPDATE notes SET note_title = ?, note_text = ? WHERE note_id = ?", [note.note_title, note.note_text, note.note_id]);
}
const protectedNotesHistory = await sql.getAll("SELECT * FROM notes_history WHERE is_protected = 1");
const protectedNotesHistory = await sql.getRows("SELECT * FROM notes_history WHERE is_protected = 1");
for (const noteHistory of protectedNotesHistory) {
const decryptedTitle = data_encryption.decrypt(dataKey, noteHistory.note_title);

View File

@ -10,7 +10,11 @@ class Note {
}
async attributes() {
return this.sql.getAll("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]);
return this.sql.getRows("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]);
}
async revisions() {
return this.sql.getRows("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]);
}
}

View File

@ -11,7 +11,7 @@ const wrap = require('express-promise-wrap').wrap;
router.get('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteId = req.params.noteId;
res.send(await sql.getAll("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId]));
res.send(await sql.getRows("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId]));
}));
router.put('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => {
@ -42,7 +42,7 @@ router.put('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next)
}
});
res.send(await sql.getAll("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId]));
res.send(await sql.getRows("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId]));
}));
module.exports = router;

View File

@ -11,7 +11,7 @@ const wrap = require('express-promise-wrap').wrap;
router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, res, next) => {
await sql.doInTransaction(async () => {
const noteIdsToDelete = await sql.getFirstColumn("SELECT noteId FROM notes WHERE isDeleted = 1");
const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1");
const noteIdsSql = noteIdsToDelete
.map(noteId => "'" + utils.sanitizeSql(noteId) + "'")
.join(', ');
@ -49,7 +49,7 @@ router.post('/cleanup-unused-images', auth.checkApiAuth, wrap(async (req, res, n
const sourceId = req.headers.source_id;
await sql.doInTransaction(async () => {
const unusedImageIds = await sql.getFirstColumn(`
const unusedImageIds = await sql.getColumn(`
SELECT images.imageId
FROM images
LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0

View File

@ -19,7 +19,7 @@ router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, wrap(async
return;
}
const maxNotePos = await sql.getFirstValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
await sql.doInTransaction(async () => {

View File

@ -9,13 +9,13 @@ const wrap = require('express-promise-wrap').wrap;
router.get('', auth.checkApiAuth, wrap(async (req, res, next) => {
await deleteOld();
const result = await sql.getAll("SELECT * FROM event_log ORDER BY dateAdded DESC");
const result = await sql.getRows("SELECT * FROM event_log ORDER BY dateAdded DESC");
res.send(result);
}));
async function deleteOld() {
const cutoffId = await sql.getFirstValue("SELECT id FROM event_log ORDER BY id DESC LIMIT 1000, 1");
const cutoffId = await sql.getValue("SELECT id FROM event_log ORDER BY id DESC LIMIT 1000, 1");
if (cutoffId) {
await sql.doInTransaction(async () => {

View File

@ -26,7 +26,7 @@ router.get('/:noteId/to/:directory', auth.checkApiAuth, wrap(async (req, res, ne
fs.mkdirSync(completeExportDir);
const noteTreeId = await sql.getFirstValue('SELECT noteTreeId FROM note_tree WHERE noteId = ?', [noteId]);
const noteTreeId = await sql.getValue('SELECT noteTreeId FROM note_tree WHERE noteId = ?', [noteId]);
await exportNote(noteTreeId, completeExportDir);
@ -34,14 +34,14 @@ router.get('/:noteId/to/:directory', auth.checkApiAuth, wrap(async (req, res, ne
}));
async function exportNote(noteTreeId, dir) {
const noteTree = await sql.getFirst("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteTree.noteId]);
const noteTree = await sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteTree.noteId]);
const pos = (noteTree.notePosition + '').padStart(4, '0');
fs.writeFileSync(dir + '/' + pos + '-' + note.title + '.html', html.prettyPrint(note.content, {indent_size: 2}));
const children = await sql.getAll("SELECT * FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]);
const children = await sql.getRows("SELECT * FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]);
if (children.length > 0) {
const childrenDir = dir + '/' + pos + '-' + note.title;

View File

@ -19,7 +19,7 @@ const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
const fs = require('fs');
router.get('/:imageId/:filename', auth.checkApiAuthOrElectron, wrap(async (req, res, next) => {
const image = await sql.getFirst("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]);
const image = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]);
if (!image) {
return res.status(404).send({});
@ -39,7 +39,7 @@ router.post('', auth.checkApiAuthOrElectron, multer.single('upload'), wrap(async
const noteId = req.query.noteId;
const file = req.file;
const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
if (!note) {
return res.status(404).send(`Note ${noteId} doesn't exist.`);

View File

@ -22,7 +22,7 @@ router.get('/:directory/to/:parentNoteId', auth.checkApiAuth, wrap(async (req, r
}));
async function importNotes(dir, parentNoteId) {
const parent = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
const parent = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
if (!parent) {
return;
@ -52,7 +52,7 @@ async function importNotes(dir, parentNoteId) {
noteTitle = match[2];
}
else {
let maxPos = await sql.getFirstValue("SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]);
let maxPos = await sql.getValue("SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]);
if (maxPos) {
notePos = maxPos + 1;
}

View File

@ -10,7 +10,7 @@ const wrap = require('express-promise-wrap').wrap;
router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteId = req.params.noteId;
const history = await sql.getAll("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]);
const history = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]);
protected_session.decryptNoteHistoryRows(req, history);
res.send(history);

View File

@ -15,7 +15,7 @@ const wrap = require('express-promise-wrap').wrap;
router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteId = req.params.noteId;
const detail = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const detail = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
if (!detail) {
log.info("Note " + noteId + " has not been found.");
@ -61,7 +61,7 @@ router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {
const search = '%' + utils.sanitizeSql(req.query.search) + '%';
// searching in protected notes is pointless because of encryption
const noteIds = await sql.getFirstColumn(`SELECT noteId FROM notes
const noteIds = await sql.getColumn(`SELECT noteId FROM notes
WHERE isDeleted = 0 AND isProtected = 0 AND (title LIKE ? OR content LIKE ?)`, [search, search]);
res.send(noteIds);

View File

@ -7,7 +7,7 @@ const auth = require('../../services/auth');
const wrap = require('express-promise-wrap').wrap;
router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {
const recentChanges = await sql.getAll(
const recentChanges = await sql.getRows(
`SELECT
notes.isDeleted AS current_isDeleted,
notes.title AS current_title,

View File

@ -35,7 +35,7 @@ router.put('/:noteTreeId/:notePath', auth.checkApiAuth, wrap(async (req, res, ne
}));
async function getRecentNotes() {
return await sql.getAll(`
return await sql.getRows(`
SELECT
recent_notes.*
FROM

View File

@ -49,7 +49,7 @@ async function getNoteWithSubtreeScript(noteId, req) {
}
async function getSubTreeScripts(parentId, includedNoteIds, dataKey) {
const children = await sql.getAll(`SELECT notes.noteId, notes.title, notes.content, notes.isProtected, notes.mime
const children = await sql.getRows(`SELECT notes.noteId, notes.title, notes.content, notes.isProtected, notes.mime
FROM notes JOIN note_tree USING(noteId)
WHERE note_tree.isDeleted = 0 AND notes.isDeleted = 0
AND note_tree.parentNoteId = ? AND notes.type = 'code'

View File

@ -12,7 +12,7 @@ router.post('/execute', auth.checkApiAuth, wrap(async (req, res, next) => {
try {
res.send({
success: true,
rows: await sql.getAll(query)
rows: await sql.getRows(query)
});
}
catch (e) {

View File

@ -15,7 +15,7 @@ const wrap = require('express-promise-wrap').wrap;
router.get('/check', auth.checkApiAuth, wrap(async (req, res, next) => {
res.send({
'hashes': await content_hash.getHashes(),
'max_sync_id': await sql.getFirstValue('SELECT MAX(id) FROM sync')
'max_sync_id': await sql.getValue('SELECT MAX(id) FROM sync')
});
}));
@ -53,12 +53,12 @@ router.post('/force-note-sync/:noteId', auth.checkApiAuth, wrap(async (req, res,
await sql.doInTransaction(async () => {
await sync_table.addNoteSync(noteId);
for (const noteTreeId of await sql.getFirstColumn("SELECT noteTreeId FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [noteId])) {
for (const noteTreeId of await sql.getColumn("SELECT noteTreeId FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [noteId])) {
await sync_table.addNoteTreeSync(noteTreeId);
await sync_table.addRecentNoteSync(noteTreeId);
}
for (const noteRevisionId of await sql.getFirstColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) {
for (const noteRevisionId of await sql.getColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) {
await sync_table.addNoteHistorySync(noteRevisionId);
}
});
@ -74,32 +74,32 @@ router.post('/force-note-sync/:noteId', auth.checkApiAuth, wrap(async (req, res,
router.get('/changed', auth.checkApiAuth, wrap(async (req, res, next) => {
const lastSyncId = parseInt(req.query.lastSyncId);
res.send(await sql.getAll("SELECT * FROM sync WHERE id > ?", [lastSyncId]));
res.send(await sql.getRows("SELECT * FROM sync WHERE id > ?", [lastSyncId]));
}));
router.get('/notes/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteId = req.params.noteId;
res.send({
entity: await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId])
entity: await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId])
});
}));
router.get('/note_tree/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteTreeId = req.params.noteTreeId;
res.send(await sql.getFirst("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]));
res.send(await sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]));
}));
router.get('/note_revisions/:noteRevisionId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteRevisionId = req.params.noteRevisionId;
res.send(await sql.getFirst("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]));
res.send(await sql.getRow("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]));
}));
router.get('/options/:name', auth.checkApiAuth, wrap(async (req, res, next) => {
const name = req.params.name;
const opt = await sql.getFirst("SELECT * FROM options WHERE name = ?", [name]);
const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]);
if (!opt.isSynced) {
res.send("This option can't be synced.");
@ -121,12 +121,12 @@ router.get('/note_reordering/:parentNoteId', auth.checkApiAuth, wrap(async (req,
router.get('/recent_notes/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteTreeId = req.params.noteTreeId;
res.send(await sql.getFirst("SELECT * FROM recent_notes WHERE noteTreeId = ?", [noteTreeId]));
res.send(await sql.getRow("SELECT * FROM recent_notes WHERE noteTreeId = ?", [noteTreeId]));
}));
router.get('/images/:imageId', auth.checkApiAuth, wrap(async (req, res, next) => {
const imageId = req.params.imageId;
const entity = await sql.getFirst("SELECT * FROM images WHERE imageId = ?", [imageId]);
const entity = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [imageId]);
if (entity && entity.data !== null) {
entity.data = entity.data.toString('base64');
@ -138,13 +138,13 @@ router.get('/images/:imageId', auth.checkApiAuth, wrap(async (req, res, next) =>
router.get('/note_images/:noteImageId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteImageId = req.params.noteImageId;
res.send(await sql.getFirst("SELECT * FROM note_images WHERE noteImageId = ?", [noteImageId]));
res.send(await sql.getRow("SELECT * FROM note_images WHERE noteImageId = ?", [noteImageId]));
}));
router.get('/attributes/:attributeId', auth.checkApiAuth, wrap(async (req, res, next) => {
const attributeId = req.params.attributeId;
res.send(await sql.getFirst("SELECT * FROM attributes WHERE attributeId = ?", [attributeId]));
res.send(await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [attributeId]));
}));
router.put('/notes', auth.checkApiAuth, wrap(async (req, res, next) => {

View File

@ -11,7 +11,7 @@ const sync_table = require('../../services/sync_table');
const wrap = require('express-promise-wrap').wrap;
router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {
const notes = await sql.getAll(`
const notes = await sql.getRows(`
SELECT
note_tree.*,
notes.title,

View File

@ -26,7 +26,7 @@ router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, wrap(async (
return;
}
const maxNotePos = await sql.getFirstValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
const now = utils.nowDate();

View File

@ -10,7 +10,7 @@ const wrap = require('express-promise-wrap').wrap;
router.get('', auth.checkAuth, wrap(async (req, res, next) => {
res.render('index', {
sourceId: await source_id.generateSourceId(),
maxSyncIdAtLoad: await sql.getFirstValue("SELECT MAX(id) FROM sync")
maxSyncIdAtLoad: await sql.getValue("SELECT MAX(id) FROM sync")
});
}));

View File

@ -10,7 +10,7 @@ async function getNoteAttributeMap(noteId) {
}
async function getNoteIdWithAttribute(name, value) {
return await sql.getFirstValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
return await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
}
@ -18,11 +18,11 @@ async function getNotesWithAttribute(dataKey, name, value) {
let notes;
if (value !== undefined) {
notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
notes = await sql.getRows(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
}
else {
notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
notes = await sql.getRows(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]);
}
@ -40,7 +40,7 @@ async function getNoteWithAttribute(dataKey, name, value) {
}
async function getNoteIdsWithAttribute(name) {
return await sql.getFirstColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN attributes USING(noteId)
return await sql.getColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]);
}

View File

@ -9,7 +9,7 @@ const utils = require('./utils');
async function runCheck(query, errorText, errorList) {
utils.assertArguments(query, errorText, errorList);
const result = await sql.getFirstColumn(query);
const result = await sql.getColumn(query);
if (result.length > 0) {
const resultText = result.map(val => "'" + val + "'").join(', ');
@ -23,7 +23,7 @@ async function runCheck(query, errorText, errorList) {
async function checkTreeCycles(errorList) {
const childToParents = {};
const rows = await sql.getAll("SELECT noteId, parentNoteId FROM note_tree WHERE isDeleted = 0");
const rows = await sql.getRows("SELECT noteId, parentNoteId FROM note_tree WHERE isDeleted = 0");
for (const row of rows) {
const childNoteId = row.noteId;

View File

@ -17,7 +17,7 @@ async function getHashes() {
const startTime = new Date();
const hashes = {
notes: getHash(await sql.getAll(`
notes: getHash(await sql.getRows(`
SELECT
noteId,
title,
@ -29,7 +29,7 @@ async function getHashes() {
FROM notes
ORDER BY noteId`)),
note_tree: getHash(await sql.getAll(`
note_tree: getHash(await sql.getRows(`
SELECT
noteTreeId,
noteId,
@ -41,7 +41,7 @@ async function getHashes() {
FROM note_tree
ORDER BY noteTreeId`)),
note_revisions: getHash(await sql.getAll(`
note_revisions: getHash(await sql.getRows(`
SELECT
noteRevisionId,
noteId,
@ -52,7 +52,7 @@ async function getHashes() {
FROM note_revisions
ORDER BY noteRevisionId`)),
recent_notes: getHash(await sql.getAll(`
recent_notes: getHash(await sql.getRows(`
SELECT
noteTreeId,
notePath,
@ -61,7 +61,7 @@ async function getHashes() {
FROM recent_notes
ORDER BY notePath`)),
options: getHash(await sql.getAll(`
options: getHash(await sql.getRows(`
SELECT
name,
value
@ -71,7 +71,7 @@ async function getHashes() {
// we don't include image data on purpose because they are quite large, checksum is good enough
// to represent the data anyway
images: getHash(await sql.getAll(`
images: getHash(await sql.getRows(`
SELECT
imageId,
format,
@ -83,7 +83,7 @@ async function getHashes() {
FROM images
ORDER BY imageId`)),
attributes: getHash(await sql.getAll(`
attributes: getHash(await sql.getRows(`
SELECT
attributeId,
noteId

View File

@ -19,14 +19,14 @@ async function createNote(parentNoteId, noteTitle, noteText) {
}
async function getNoteStartingWith(parentNoteId, startsWith) {
return await sql.getFirstValue(`SELECT noteId FROM notes JOIN note_tree USING(noteId)
return await sql.getValue(`SELECT noteId FROM notes JOIN note_tree USING(noteId)
WHERE parentNoteId = ? AND title LIKE '${startsWith}%'
AND notes.isDeleted = 0 AND isProtected = 0
AND note_tree.isDeleted = 0`, [parentNoteId]);
}
async function getRootNoteId() {
let rootNoteId = await sql.getFirstValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
let rootNoteId = await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.isDeleted = 0`);
if (!rootNoteId) {

View File

@ -64,11 +64,11 @@ async function sendMessageToAllClients(message) {
}
async function sendPing(client, lastSentSyncId) {
const syncData = await sql.getAll("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
const syncData = await sql.getRows("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
const lastSyncedPush = await options.getOption('last_synced_push');
const changesToPushCount = await sql.getFirstValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
const changesToPushCount = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
await sendMessage(client, {
type: 'sync',

View File

@ -6,7 +6,7 @@ const attributes = require('./attributes');
const protected_session = require('./protected_session');
async function getNoteById(noteId, dataKey) {
const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
protected_session.decryptNote(dataKey, note);
@ -53,12 +53,12 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) {
let newNotePos = 0;
if (noteOpts.target === 'into') {
const maxNotePos = await sql.getFirstValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
}
else if (noteOpts.target === 'after') {
const afterNote = await sql.getFirst('SELECT notePosition FROM note_tree WHERE noteTreeId = ?', [noteOpts.target_noteTreeId]);
const afterNote = await sql.getRow('SELECT notePosition FROM note_tree WHERE noteTreeId = ?', [noteOpts.target_noteTreeId]);
newNotePos = afterNote.notePosition + 1;
@ -73,7 +73,7 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) {
}
if (parentNoteId !== 'root') {
const parent = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
const parent = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
if (!noteOpts.type) {
noteOpts.type = parent.type;
@ -125,11 +125,11 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) {
}
async function protectNoteRecursively(noteId, dataKey, protect, sourceId) {
const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
await protectNote(note, dataKey, protect, sourceId);
const children = await sql.getFirstColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
const children = await sql.getColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
for (const childNoteId of children) {
await protectNoteRecursively(childNoteId, dataKey, protect, sourceId);
@ -165,7 +165,7 @@ async function protectNote(note, dataKey, protect, sourceId) {
}
async function protectNoteHistory(noteId, dataKey, protect, sourceId) {
const historyToChange = await sql.getAll("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [noteId, protect]);
const historyToChange = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [noteId, protect]);
for (const history of historyToChange) {
if (protect) {
@ -187,7 +187,7 @@ async function protectNoteHistory(noteId, dataKey, protect, sourceId) {
}
async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) {
const oldNote = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const oldNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
if (oldNote.isProtected) {
protected_session.decryptNote(dataKey, oldNote);
@ -212,7 +212,7 @@ async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) {
}
async function saveNoteImages(noteId, noteText, sourceId) {
const existingNoteImages = await sql.getAll("SELECT * FROM note_images WHERE noteId = ?", [noteId]);
const existingNoteImages = await sql.getRows("SELECT * FROM note_images WHERE noteId = ?", [noteId]);
const foundImageIds = [];
const now = utils.nowDate();
const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g;
@ -272,7 +272,7 @@ async function updateNote(noteId, newNote, dataKey, sourceId) {
const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000));
const existingnoteRevisionId = await sql.getFirstValue(
const existingnoteRevisionId = await sql.getValue(
"SELECT noteRevisionId FROM note_revisions WHERE noteId = ? AND dateModifiedTo >= ?", [noteId, historyCutoff]);
await sql.doInTransaction(async () => {
@ -301,7 +301,7 @@ async function updateNote(noteId, newNote, dataKey, sourceId) {
}
async function deleteNote(noteTreeId, sourceId) {
const noteTree = await sql.getFirstOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
const noteTree = await sql.getRowOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
if (!noteTree || noteTree.isDeleted === 1) {
return;
@ -312,15 +312,15 @@ async function deleteNote(noteTreeId, sourceId) {
await sql.execute("UPDATE note_tree SET isDeleted = 1, dateModified = ? WHERE noteTreeId = ?", [now, noteTreeId]);
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
const noteId = await sql.getFirstValue("SELECT noteId FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
const noteId = await sql.getValue("SELECT noteId FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
const notDeletedNoteTreesCount = await sql.getFirstValue("SELECT COUNT(*) FROM note_tree WHERE noteId = ? AND isDeleted = 0", [noteId]);
const notDeletedNoteTreesCount = await sql.getValue("SELECT COUNT(*) FROM note_tree WHERE noteId = ? AND isDeleted = 0", [noteId]);
if (!notDeletedNoteTreesCount) {
await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]);
await sync_table.addNoteSync(noteId, sourceId);
const children = await sql.getAll("SELECT noteTreeId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
const children = await sql.getRows("SELECT noteTreeId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
for (const child of children) {
await deleteNote(child.noteTreeId, sourceId);

View File

@ -5,10 +5,10 @@ const app_info = require('./app_info');
async function getOptionOrNull(name) {
try {
return await sql.getFirstOrNull("SELECT value FROM options WHERE name = ?", [name]);
return await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
}
catch (e) {
return await sql.getFirstOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [name]);
return await sql.getRowOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [name]);
}
}
@ -26,10 +26,10 @@ async function setOption(name, value, sourceId = null) {
let opt;
try {
opt = await sql.getFirst("SELECT * FROM options WHERE name = ?", [name]);
opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]);
}
catch (e) {
opt = await sql.getFirst("SELECT * FROM options WHERE opt_name = ?", [name]);
opt = await sql.getRow("SELECT * FROM options WHERE opt_name = ?", [name]);
}
if (!opt) {

View File

@ -29,7 +29,7 @@ async function generateSourceId() {
}
async function refreshSourceIds() {
allSourceIds = await sql.getFirstColumn("SELECT sourceId FROM source_ids ORDER BY dateCreated DESC");
allSourceIds = await sql.getColumn("SELECT sourceId FROM source_ids ORDER BY dateCreated DESC");
}
let allSourceIds = [];

View File

@ -25,7 +25,7 @@ const dbReady = new Promise((resolve, reject) => {
resolve(db);
};
const tableResults = await getAll("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'");
const tableResults = await getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'");
if (tableResults.length !== 1) {
log.info("Connected to db, but schema doesn't exist. Initializing schema ...");
@ -42,7 +42,7 @@ const dbReady = new Promise((resolve, reject) => {
await executeScript(imagesSql);
await executeScript(notesImageSql);
const startNoteId = await getFirstValue("SELECT noteId FROM note_tree WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition");
const startNoteId = await getValue("SELECT noteId FROM note_tree WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition");
await require('./options').initOptions(startNoteId);
await require('./sync_table').fillAllSyncRows();
@ -110,18 +110,18 @@ async function rollback() {
return await wrap(async db => db.run("ROLLBACK"));
}
async function getFirst(query, params = []) {
async function getRow(query, params = []) {
return await wrap(async db => db.get(query, ...params));
}
async function getFirstOrNull(query, params = []) {
async function getRowOrNull(query, params = []) {
const all = await wrap(async db => db.all(query, ...params));
return all.length > 0 ? all[0] : null;
}
async function getFirstValue(query, params = []) {
const row = await getFirstOrNull(query, params);
async function getValue(query, params = []) {
const row = await getRowOrNull(query, params);
if (!row) {
return null;
@ -130,19 +130,25 @@ async function getFirstValue(query, params = []) {
return row[Object.keys(row)[0]];
}
async function getAll(query, params = []) {
async function getRows(query, params = []) {
return await wrap(async db => db.all(query, ...params));
}
async function getAllEntities(query, params = []) {
const rows = await getAll(query, params);
async function getEntities(query, params = []) {
const rows = await getRows(query, params);
return rows.map(row => new Note(module.exports, row));
}
async function getEntity(query, params = []) {
const rows = await getRows(query, params);
return rows.map(row => new Note(module.exports, row));
}
async function getMap(query, params = []) {
const map = {};
const results = await getAll(query, params);
const results = await getRows(query, params);
for (const row of results) {
const keys = Object.keys(row);
@ -153,9 +159,9 @@ async function getMap(query, params = []) {
return map;
}
async function getFirstColumn(query, params = []) {
async function getColumn(query, params = []) {
const list = [];
const result = await getAll(query, params);
const result = await getRows(query, params);
if (result.length === 0) {
return list;
@ -236,10 +242,10 @@ async function isDbUpToDate() {
let dbVersion;
try {
dbVersion = parseInt(await getFirstValue("SELECT value FROM options WHERE name = 'db_version'"));
dbVersion = parseInt(await getValue("SELECT value FROM options WHERE name = 'db_version'"));
}
catch (e) {
dbVersion = parseInt(await getFirstValue("SELECT opt_value FROM options WHERE opt_name = 'db_version'"));
dbVersion = parseInt(await getValue("SELECT opt_value FROM options WHERE opt_name = 'db_version'"));
}
const upToDate = dbVersion >= app_info.db_version;
@ -255,10 +261,10 @@ async function isUserInitialized() {
let username;
try {
username = await getFirstValue("SELECT value FROM options WHERE name = 'username'");
username = await getValue("SELECT value FROM options WHERE name = 'username'");
}
catch (e) {
username = await getFirstValue("SELECT opt_value FROM options WHERE opt_name = 'username'");
username = await getValue("SELECT opt_value FROM options WHERE opt_name = 'username'");
}
return !!username;
@ -269,13 +275,14 @@ module.exports = {
isUserInitialized,
insert,
replace,
getFirstValue,
getFirst,
getFirstOrNull,
getAll,
getAllEntities,
getValue,
getRow,
getRowOrNull,
getRows,
getEntities,
getEntity,
getMap,
getFirstColumn,
getColumn,
execute,
executeScript,
doInTransaction,

View File

@ -173,7 +173,7 @@ async function pushSync(syncContext) {
let lastSyncedPush = await getLastSyncedPush();
while (true) {
const sync = await sql.getFirstOrNull('SELECT * FROM sync WHERE id > ? LIMIT 1', [lastSyncedPush]);
const sync = await sql.getRowOrNull('SELECT * FROM sync WHERE id > ? LIMIT 1', [lastSyncedPush]);
if (sync === null) {
// nothing to sync
@ -200,13 +200,13 @@ async function pushEntity(sync, syncContext) {
let entity;
if (sync.entityName === 'notes') {
entity = await sql.getFirst('SELECT * FROM notes WHERE noteId = ?', [sync.entityId]);
entity = await sql.getRow('SELECT * FROM notes WHERE noteId = ?', [sync.entityId]);
}
else if (sync.entityName === 'note_tree') {
entity = await sql.getFirst('SELECT * FROM note_tree WHERE noteTreeId = ?', [sync.entityId]);
entity = await sql.getRow('SELECT * FROM note_tree WHERE noteTreeId = ?', [sync.entityId]);
}
else if (sync.entityName === 'note_revisions') {
entity = await sql.getFirst('SELECT * FROM note_revisions WHERE noteRevisionId = ?', [sync.entityId]);
entity = await sql.getRow('SELECT * FROM note_revisions WHERE noteRevisionId = ?', [sync.entityId]);
}
else if (sync.entityName === 'note_reordering') {
entity = {
@ -215,23 +215,23 @@ async function pushEntity(sync, syncContext) {
};
}
else if (sync.entityName === 'options') {
entity = await sql.getFirst('SELECT * FROM options WHERE name = ?', [sync.entityId]);
entity = await sql.getRow('SELECT * FROM options WHERE name = ?', [sync.entityId]);
}
else if (sync.entityName === 'recent_notes') {
entity = await sql.getFirst('SELECT * FROM recent_notes WHERE noteTreeId = ?', [sync.entityId]);
entity = await sql.getRow('SELECT * FROM recent_notes WHERE noteTreeId = ?', [sync.entityId]);
}
else if (sync.entityName === 'images') {
entity = await sql.getFirst('SELECT * FROM images WHERE imageId = ?', [sync.entityId]);
entity = await sql.getRow('SELECT * FROM images WHERE imageId = ?', [sync.entityId]);
if (entity.data !== null) {
entity.data = entity.data.toString('base64');
}
}
else if (sync.entityName === 'note_images') {
entity = await sql.getFirst('SELECT * FROM note_images WHERE noteImageId = ?', [sync.entityId]);
entity = await sql.getRow('SELECT * FROM note_images WHERE noteImageId = ?', [sync.entityId]);
}
else if (sync.entityName === 'attributes') {
entity = await sql.getFirst('SELECT * FROM attributes WHERE attributeId = ?', [sync.entityId]);
entity = await sql.getRow('SELECT * FROM attributes WHERE attributeId = ?', [sync.entityId]);
}
else {
throw new Error(`Unrecognized entity type ${sync.entityName} in sync #${sync.id}`);
@ -262,7 +262,7 @@ async function checkContentHash(syncContext) {
}
const lastSyncedPush = await getLastSyncedPush();
const notPushedSyncs = await sql.getFirstValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
const notPushedSyncs = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
if (notPushedSyncs > 0) {
log.info("There's " + notPushedSyncs + " outstanding pushes, skipping content check.");

View File

@ -66,10 +66,10 @@ async function cleanupSyncRowsForMissingEntities(entityName, entityKey) {
async function fillSyncRows(entityName, entityKey) {
await cleanupSyncRowsForMissingEntities(entityName, entityKey);
const entityIds = await sql.getFirstColumn(`SELECT ${entityKey} FROM ${entityName}`);
const entityIds = await sql.getColumn(`SELECT ${entityKey} FROM ${entityName}`);
for (const entityId of entityIds) {
const existingRows = await sql.getFirstValue("SELECT COUNT(id) FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
const existingRows = await sql.getValue("SELECT COUNT(id) FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
// we don't want to replace existing entities (which would effectively cause full resync)
if (existingRows === 0) {

View File

@ -5,7 +5,7 @@ const notes = require('./notes');
const sync_table = require('./sync_table');
async function updateNote(entity, sourceId) {
const origNote = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);
const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);
if (!origNote || origNote.dateModified <= entity.dateModified) {
await sql.doInTransaction(async () => {
@ -20,7 +20,7 @@ async function updateNote(entity, sourceId) {
}
async function updateNoteTree(entity, sourceId) {
const orig = await sql.getFirstOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [entity.noteTreeId]);
const orig = await sql.getRowOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [entity.noteTreeId]);
await sql.doInTransaction(async () => {
if (orig === null || orig.dateModified < entity.dateModified) {
@ -36,7 +36,7 @@ async function updateNoteTree(entity, sourceId) {
}
async function updateNoteHistory(entity, sourceId) {
const orig = await sql.getFirstOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]);
const orig = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]);
await sql.doInTransaction(async () => {
// we update note history even if date modified to is the same because the only thing which might have changed
@ -62,7 +62,7 @@ async function updateNoteReordering(entity, sourceId) {
}
async function updateOptions(entity, sourceId) {
const orig = await sql.getFirstOrNull("SELECT * FROM options WHERE name = ?", [entity.name]);
const orig = await sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [entity.name]);
if (!orig.isSynced) {
return;
@ -80,7 +80,7 @@ async function updateOptions(entity, sourceId) {
}
async function updateRecentNotes(entity, sourceId) {
const orig = await sql.getFirstOrNull("SELECT * FROM recent_notes WHERE noteTreeId = ?", [entity.noteTreeId]);
const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteTreeId = ?", [entity.noteTreeId]);
if (orig === null || orig.dateAccessed < entity.dateAccessed) {
await sql.doInTransaction(async () => {
@ -96,7 +96,7 @@ async function updateImage(entity, sourceId) {
entity.data = Buffer.from(entity.data, 'base64');
}
const origImage = await sql.getFirst("SELECT * FROM images WHERE imageId = ?", [entity.imageId]);
const origImage = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [entity.imageId]);
if (!origImage || origImage.dateModified <= entity.dateModified) {
await sql.doInTransaction(async () => {
@ -110,7 +110,7 @@ async function updateImage(entity, sourceId) {
}
async function updateNoteImage(entity, sourceId) {
const origNoteImage = await sql.getFirst("SELECT * FROM note_images WHERE noteImageId = ?", [entity.noteImageId]);
const origNoteImage = await sql.getRow("SELECT * FROM note_images WHERE noteImageId = ?", [entity.noteImageId]);
if (!origNoteImage || origNoteImage.dateModified <= entity.dateModified) {
await sql.doInTransaction(async () => {
@ -124,7 +124,7 @@ async function updateNoteImage(entity, sourceId) {
}
async function updateAttribute(entity, sourceId) {
const origAttribute = await sql.getFirst("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]);
const origAttribute = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]);
if (!origAttribute || origAttribute.dateModified <= entity.dateModified) {
await sql.doInTransaction(async () => {

View File

@ -28,7 +28,7 @@ async function validateParentChild(res, parentNoteId, childNoteId, noteTreeId =
}
async function getExistingNoteTree(parentNoteId, childNoteId) {
return await sql.getFirst('SELECT * FROM note_tree WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0', [childNoteId, parentNoteId]);
return await sql.getRow('SELECT * FROM note_tree WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0', [childNoteId, parentNoteId]);
}
/**
@ -51,7 +51,7 @@ async function checkTreeCycle(parentNoteId, childNoteId) {
return false;
}
const parentNoteIds = await sql.getFirstColumn("SELECT DISTINCT parentNoteId FROM note_tree WHERE noteId = ? AND isDeleted = 0", [parentNoteId]);
const parentNoteIds = await sql.getColumn("SELECT DISTINCT parentNoteId FROM note_tree WHERE noteId = ? AND isDeleted = 0", [parentNoteId]);
for (const pid of parentNoteIds) {
if (!await checkTreeCycleInner(pid)) {
@ -66,13 +66,13 @@ async function checkTreeCycle(parentNoteId, childNoteId) {
}
async function getNoteTree(noteTreeId) {
return sql.getFirst("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
return sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
}
async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) {
subTreeNoteIds.push(parentNoteId);
const children = await sql.getFirstColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]);
const children = await sql.getColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]);
for (const childNoteId of children) {
await loadSubTreeNoteIds(childNoteId, subTreeNoteIds);
@ -81,7 +81,7 @@ async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) {
async function sortNotesAlphabetically(parentNoteId, req, sourceId) {
await sql.doInTransaction(async () => {
const notes = await sql.getAll(`SELECT noteTreeId, noteId, title, isProtected
const notes = await sql.getRows(`SELECT noteTreeId, noteId, title, isProtected
FROM notes JOIN note_tree USING(noteId)
WHERE note_tree.isDeleted = 0 AND parentNoteId = ?`, [parentNoteId]);