From 4927cdf6cebaf57faa78d81cb52178a405994add Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Wed, 7 Dec 2022 16:43:46 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=20=D1=80=D0=B0=D1=81=D1=88=D0=B8=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D0=BC=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/DbSearcher.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/server/core/DbSearcher.js b/server/core/DbSearcher.js index dc72b13..8ab6a43 100644 --- a/server/core/DbSearcher.js +++ b/server/core/DbSearcher.js @@ -539,7 +539,6 @@ class DbSearcher { } async bookSearchIds(query) { - const ids = await this.selectBookIds(query); const queryKey = this.queryKey(query); const bookKey = `book-search-ids-${queryKey}`; let bookIds = await this.getCached(bookKey); @@ -550,17 +549,19 @@ class DbSearcher { searchValue = searchValue.toLowerCase(); //особая обработка префиксов if (searchValue[0] == '=') { + searchValue = searchValue.substring(1); return `(row.${bookField}.toLowerCase().localeCompare(${db.esc(searchValue)}) === 0)`; } else if (searchValue[0] == '*') { + searchValue = searchValue.substring(1); return `(row.${bookField} && row.${bookField}.toLowerCase().indexOf(${db.esc(searchValue)}) >= 0)`; } else if (searchValue[0] == '#') { - //searchValue = searchValue.substring(1); - //return !bookValue || (bookValue !== emptyFieldValue && !enru.has(bookValue[0]) && bookValue.indexOf(searchValue) >= 0); - return 'true'; + searchValue = searchValue.substring(1); + return `(row.${bookField} === '' || (!enru.has(row.${bookField}.toLowerCase()[0]) && row.${bookField}.toLowerCase().indexOf(${db.esc(searchValue)}) >= 0))`; } else { + return `(row.${bookField}.toLowerCase().localeCompare(${db.esc(searchValue)}) >= 0 ` + `&& row.${bookField}.toLowerCase().localeCompare(${db.esc(searchValue)} + ${db.esc(maxUtf8Char)}) <= 0)`; } @@ -573,10 +574,22 @@ class DbSearcher { if (f.type === 'S') { checks.push(filterBySearch(f.field, searchValue)); } if (f.type === 'N') { - searchValue = parseInt(searchValue, 10); - if (isNaN(searchValue)) - throw new Error(`Wrong query param, ${f.field}=${searchValue}`); - checks.push(`row.${f.field} === ${searchValue}`); + const v = searchValue.split('..'); + + if (v.length == 1) { + searchValue = parseInt(searchValue, 10); + if (isNaN(searchValue)) + throw new Error(`Wrong query param, ${f.field}=${searchValue}`); + + checks.push(`row.${f.field} === ${searchValue}`); + } else { + const from = parseInt(v[0] || '0', 10); + const to = parseInt(v[1] || '0', 10); + if (isNaN(from) || isNaN(to)) + throw new Error(`Wrong query param, ${f.field}=${searchValue}`); + + checks.push(`row.${f.field} >= ${from} && row.${f.field} <= ${to}`); + } } } } @@ -585,17 +598,17 @@ class DbSearcher { table: 'book', rawResult: true, where: ` - const ids = ${(ids ? db.esc(Array.from(ids)) : '@all()')}; + const enru = new Set(${db.esc(enruArr)}); const checkBook = (row) => { return ${checks.join(' && ')}; }; - const result = new Set(); - for (const id of ids) { + const result = []; + for (const id of @all()) { const row = @unsafeRow(id); if (checkBook(row)) - result.add(row.id); + result.push(row.id); } return new Uint32Array(result);