|
@@ -298,77 +298,60 @@ class DbSearcher {
|
|
let filter = '';
|
|
let filter = '';
|
|
let closures = '';
|
|
let closures = '';
|
|
|
|
|
|
- //авторы
|
|
|
|
- if (exclude !== 'author' && query.author && query.author !== '*') {
|
|
|
|
|
|
+ //порядок важен, более простые проверки вперед
|
|
|
|
+
|
|
|
|
+ //серии
|
|
|
|
+ if (exclude !== 'series' && query.series && query.series !== '*') {
|
|
closures += `
|
|
closures += `
|
|
- const splitAuthor = (author) => {
|
|
|
|
- if (!author)
|
|
|
|
- author = ${db.esc(emptyFieldValue)};
|
|
|
|
-
|
|
|
|
- const result = author.split(',');
|
|
|
|
- if (result.length > 1)
|
|
|
|
- result.push(author);
|
|
|
|
-
|
|
|
|
- return result;
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- const checkAuthor = (bookValue) => {
|
|
|
|
|
|
+ const checkSeries = (bookValue) => {
|
|
if (!bookValue)
|
|
if (!bookValue)
|
|
bookValue = ${db.esc(emptyFieldValue)};
|
|
bookValue = ${db.esc(emptyFieldValue)};
|
|
|
|
|
|
bookValue = bookValue.toLowerCase();
|
|
bookValue = bookValue.toLowerCase();
|
|
|
|
|
|
- return ${filterBySearch(query.author)};
|
|
|
|
|
|
+ return ${filterBySearch(query.series)};
|
|
};
|
|
};
|
|
`;
|
|
`;
|
|
-
|
|
|
|
filter += `
|
|
filter += `
|
|
- const author = splitAuthor(book.author);
|
|
|
|
- let found = false;
|
|
|
|
- for (const a of author) {
|
|
|
|
- if (checkAuthor(a)) {
|
|
|
|
- found = true;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (!found)
|
|
|
|
|
|
+ if (!checkSeries(book.series))
|
|
return false;
|
|
return false;
|
|
`;
|
|
`;
|
|
}
|
|
}
|
|
|
|
|
|
- //серии
|
|
|
|
- if (exclude !== 'series' && query.series && query.series !== '*') {
|
|
|
|
|
|
+ //названия
|
|
|
|
+ if (exclude !== 'title' && query.title && query.title !== '*') {
|
|
closures += `
|
|
closures += `
|
|
- const checkSeries = (bookValue) => {
|
|
|
|
|
|
+ const checkTitle = (bookValue) => {
|
|
if (!bookValue)
|
|
if (!bookValue)
|
|
bookValue = ${db.esc(emptyFieldValue)};
|
|
bookValue = ${db.esc(emptyFieldValue)};
|
|
|
|
|
|
bookValue = bookValue.toLowerCase();
|
|
bookValue = bookValue.toLowerCase();
|
|
|
|
|
|
- return ${filterBySearch(query.series)};
|
|
|
|
|
|
+ return ${filterBySearch(query.title)};
|
|
};
|
|
};
|
|
`;
|
|
`;
|
|
filter += `
|
|
filter += `
|
|
- if (!checkSeries(book.series))
|
|
|
|
|
|
+ if (!checkTitle(book.title))
|
|
return false;
|
|
return false;
|
|
`;
|
|
`;
|
|
}
|
|
}
|
|
|
|
|
|
- //названия
|
|
|
|
- if (exclude !== 'title' && query.title && query.title !== '*') {
|
|
|
|
|
|
+ //языки
|
|
|
|
+ if (exclude !== 'lang' && query.lang) {
|
|
|
|
+ const queryLangs = query.lang.split(',');
|
|
|
|
+
|
|
closures += `
|
|
closures += `
|
|
- const checkTitle = (bookValue) => {
|
|
|
|
|
|
+ const queryLangs = new Set(${db.esc(queryLangs)});
|
|
|
|
+
|
|
|
|
+ const checkLang = (bookValue) => {
|
|
if (!bookValue)
|
|
if (!bookValue)
|
|
bookValue = ${db.esc(emptyFieldValue)};
|
|
bookValue = ${db.esc(emptyFieldValue)};
|
|
|
|
|
|
- bookValue = bookValue.toLowerCase();
|
|
|
|
-
|
|
|
|
- return ${filterBySearch(query.title)};
|
|
|
|
|
|
+ return queryLangs.has(bookValue);
|
|
};
|
|
};
|
|
`;
|
|
`;
|
|
filter += `
|
|
filter += `
|
|
- if (!checkTitle(book.title))
|
|
|
|
|
|
+ if (!checkLang(book.lang))
|
|
return false;
|
|
return false;
|
|
`;
|
|
`;
|
|
}
|
|
}
|
|
@@ -402,22 +385,41 @@ class DbSearcher {
|
|
`;
|
|
`;
|
|
}
|
|
}
|
|
|
|
|
|
- //языки
|
|
|
|
- if (exclude !== 'lang' && query.lang) {
|
|
|
|
- const queryLangs = query.lang.split(',');
|
|
|
|
-
|
|
|
|
|
|
+ //авторы
|
|
|
|
+ if (exclude !== 'author' && query.author && query.author !== '*') {
|
|
closures += `
|
|
closures += `
|
|
- const queryLangs = new Set(${db.esc(queryLangs)});
|
|
|
|
|
|
+ const splitAuthor = (author) => {
|
|
|
|
+ if (!author)
|
|
|
|
+ author = ${db.esc(emptyFieldValue)};
|
|
|
|
|
|
- const checkLang = (bookValue) => {
|
|
|
|
|
|
+ const result = author.split(',');
|
|
|
|
+ if (result.length > 1)
|
|
|
|
+ result.push(author);
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const checkAuthor = (bookValue) => {
|
|
if (!bookValue)
|
|
if (!bookValue)
|
|
bookValue = ${db.esc(emptyFieldValue)};
|
|
bookValue = ${db.esc(emptyFieldValue)};
|
|
|
|
|
|
- return queryLangs.has(bookValue);
|
|
|
|
|
|
+ bookValue = bookValue.toLowerCase();
|
|
|
|
+
|
|
|
|
+ return ${filterBySearch(query.author)};
|
|
};
|
|
};
|
|
`;
|
|
`;
|
|
|
|
+
|
|
filter += `
|
|
filter += `
|
|
- if (!checkLang(book.lang))
|
|
|
|
|
|
+ const author = splitAuthor(book.author);
|
|
|
|
+ let found = false;
|
|
|
|
+ for (const a of author) {
|
|
|
|
+ if (checkAuthor(a)) {
|
|
|
|
+ found = true;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!found)
|
|
return false;
|
|
return false;
|
|
`;
|
|
`;
|
|
}
|
|
}
|
|
@@ -527,21 +529,21 @@ class DbSearcher {
|
|
if (query.title && query.title !== '*') {
|
|
if (query.title && query.title !== '*') {
|
|
const where = this.getWhere(query.title);
|
|
const where = this.getWhere(query.title);
|
|
|
|
|
|
- const seriesRows = await db.select({
|
|
|
|
|
|
+ const titleRows = await db.select({
|
|
table: 'title',
|
|
table: 'title',
|
|
rawResult: true,
|
|
rawResult: true,
|
|
where: `return Array.from(${where})`,
|
|
where: `return Array.from(${where})`,
|
|
});
|
|
});
|
|
|
|
|
|
- titleIds = seriesRows[0].rawResult;
|
|
|
|
|
|
+ titleIds = titleRows[0].rawResult;
|
|
} else {
|
|
} else {
|
|
- const seriesRows = await db.select({
|
|
|
|
|
|
+ const titleRows = await db.select({
|
|
table: 'title',
|
|
table: 'title',
|
|
rawResult: true,
|
|
rawResult: true,
|
|
where: `return Array.from(@all())`,
|
|
where: `return Array.from(@all())`,
|
|
});
|
|
});
|
|
|
|
|
|
- titleIds = seriesRows[0].rawResult;
|
|
|
|
|
|
+ titleIds = titleRows[0].rawResult;
|
|
}
|
|
}
|
|
|
|
|
|
titleIds.sort((a, b) => a - b);
|
|
titleIds.sort((a, b) => a - b);
|
|
@@ -552,7 +554,7 @@ class DbSearcher {
|
|
const where = this.getWhere2(query, (isAll ? false : titleIds), 'title');
|
|
const where = this.getWhere2(query, (isAll ? false : titleIds), 'title');
|
|
|
|
|
|
if (where) {
|
|
if (where) {
|
|
- //тяжелый запрос перебором в series_book
|
|
|
|
|
|
+ //тяжелый запрос перебором в title_book
|
|
const rows = await db.select({
|
|
const rows = await db.select({
|
|
table: 'title_book',
|
|
table: 'title_book',
|
|
rawResult: true,
|
|
rawResult: true,
|
|
@@ -727,14 +729,14 @@ class DbSearcher {
|
|
const key = `title-ids-${this.queryKey(query)}`;
|
|
const key = `title-ids-${this.queryKey(query)}`;
|
|
|
|
|
|
//сначала попробуем найти в кеше
|
|
//сначала попробуем найти в кеше
|
|
- let seriesIds = await this.getCached(key);
|
|
|
|
- if (seriesIds === null) {//не нашли в кеше, ищем в поисковых таблицах
|
|
|
|
- seriesIds = await this.selectTitleIds(query);
|
|
|
|
|
|
+ let titleIds = await this.getCached(key);
|
|
|
|
+ if (titleIds === null) {//не нашли в кеше, ищем в поисковых таблицах
|
|
|
|
+ titleIds = await this.selectTitleIds(query);
|
|
|
|
|
|
- await this.putCached(key, seriesIds);
|
|
|
|
|
|
+ await this.putCached(key, titleIds);
|
|
}
|
|
}
|
|
|
|
|
|
- const totalFound = seriesIds.length;
|
|
|
|
|
|
+ const totalFound = titleIds.length;
|
|
let limit = (query.limit ? query.limit : 100);
|
|
let limit = (query.limit ? query.limit : 100);
|
|
limit = (limit > maxLimit ? maxLimit : limit);
|
|
limit = (limit > maxLimit ? maxLimit : limit);
|
|
const offset = (query.offset ? query.offset : 0);
|
|
const offset = (query.offset ? query.offset : 0);
|
|
@@ -742,8 +744,8 @@ class DbSearcher {
|
|
//выборка найденных авторов
|
|
//выборка найденных авторов
|
|
const result = await db.select({
|
|
const result = await db.select({
|
|
table: 'title_book',
|
|
table: 'title_book',
|
|
- map: `(r) => ({id: r.id, series: r.series, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`,
|
|
|
|
- where: `@@id(${db.esc(seriesIds.slice(offset, offset + limit))})`
|
|
|
|
|
|
+ map: `(r) => ({id: r.id, title: r.title, books: r.books, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`,
|
|
|
|
+ where: `@@id(${db.esc(titleIds.slice(offset, offset + limit))})`
|
|
});
|
|
});
|
|
|
|
|
|
return {result, totalFound};
|
|
return {result, totalFound};
|