fix issue with limitation of number of SQLite parameters (999) which caused problems when loading tree which was too expanded

This commit is contained in:
azivner 2018-05-30 20:28:10 -04:00
parent be51e533fc
commit bd66b8a1c8
2 changed files with 30 additions and 12 deletions

View File

@ -5,11 +5,9 @@ const optionService = require('../../services/options');
const protectedSessionService = require('../../services/protected_session'); const protectedSessionService = require('../../services/protected_session');
async function getNotes(noteIds) { async function getNotes(noteIds) {
const questionMarks = noteIds.map(() => "?").join(","); const notes = await sql.getManyRows(`
const notes = await sql.getRows(`
SELECT noteId, title, isProtected, type, mime SELECT noteId, title, isProtected, type, mime
FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds); FROM notes WHERE isDeleted = 0 AND noteId IN (???)`, noteIds);
protectedSessionService.decryptNotes(notes); protectedSessionService.decryptNotes(notes);
@ -18,11 +16,11 @@ async function getNotes(noteIds) {
} }
async function getRelations(noteIds) { async function getRelations(noteIds) {
const questionMarks = noteIds.map(() => "?").join(","); // we need to fetch both parentNoteId and noteId matches because we can have loaded child
const doubledNoteIds = noteIds.concat(noteIds); // of which only some of the parents has been loaded.
return await sql.getRows(`SELECT branchId, noteId AS 'childNoteId', parentNoteId FROM branches WHERE isDeleted = 0 return await sql.getManyRows(`SELECT branchId, noteId AS 'childNoteId', parentNoteId FROM branches WHERE isDeleted = 0
AND (parentNoteId IN (${questionMarks}) OR noteId IN (${questionMarks}))`, doubledNoteIds); AND (parentNoteId IN (???) OR noteId IN (???))`, noteIds);
} }
async function getTree() { async function getTree() {
@ -58,12 +56,11 @@ async function load(req) {
const branchIds = req.body.branchIds; const branchIds = req.body.branchIds;
if (branchIds && branchIds.length > 0) { if (branchIds && branchIds.length > 0) {
noteIds = await sql.getColumn(`SELECT noteId FROM branches WHERE isDeleted = 0 AND branchId IN(${branchIds.map(() => "?").join(",")})`, branchIds); noteIds = (await sql.getColumn(`SELECT noteId FROM branches WHERE isDeleted = 0 AND branchId IN(???)`, branchIds))
.map(note => note.noteId);
} }
const questionMarks = noteIds.map(() => "?").join(","); const branches = await sql.getManyRows(`SELECT * FROM branches WHERE isDeleted = 0 AND noteId IN (???)`, noteIds);
const branches = await sql.getRows(`SELECT * FROM branches WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds);
const notes = await getNotes(noteIds); const notes = await getNotes(noteIds);

View File

@ -62,6 +62,26 @@ async function getValue(query, params = []) {
return row[Object.keys(row)[0]]; return row[Object.keys(row)[0]];
} }
const PARAM_LIMIT = 900; // actual limit is 999
// this is to overcome 999 limit of number of query parameters
async function getManyRows(query, params) {
let results = [];
while (params.length > 0) {
const curParams = params.slice(0, Math.max(params.length, PARAM_LIMIT));
params = params.slice(curParams.length);
let i = 1;
const questionMarks = curParams.map(() => "?" + i++).join(",");
const curQuery = query.replace(/\?\?\?/g, questionMarks);
results = results.concat(await getRows(curQuery, curParams));
}
return results;
}
async function getRows(query, params = []) { async function getRows(query, params = []) {
return await wrap(async db => db.all(query, ...params)); return await wrap(async db => db.all(query, ...params));
} }
@ -179,6 +199,7 @@ module.exports = {
getRow, getRow,
getRowOrNull, getRowOrNull,
getRows, getRows,
getManyRows,
getMap, getMap,
getColumn, getColumn,
execute, execute,