Additional fix for search crash with includeParentsAndChildren

Follow-up to 76081ab05
This commit is contained in:
Dan Stillman 2020-02-11 13:08:20 -05:00
parent 1db3fc0b49
commit a53f363b8d
2 changed files with 39 additions and 33 deletions

View file

@ -1086,6 +1086,7 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () {
var selectOpenParens = 0; var selectOpenParens = 0;
var condSelectSQL = ''; var condSelectSQL = '';
var condSQLParams = []; var condSQLParams = [];
let forceNoResults = false;
// //
// Special table handling // Special table handling
@ -1170,7 +1171,6 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () {
let objKey = condition.value; let objKey = condition.value;
let objectType = condition.name == 'collection' ? 'collection' : 'search'; let objectType = condition.name == 'collection' ? 'collection' : 'search';
let objectTypeClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(objectType); let objectTypeClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(objectType);
let forceNoResults = false;
// libraryID assigned on search // libraryID assigned on search
if (this.libraryID !== null) { if (this.libraryID !== null) {
@ -1217,7 +1217,7 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () {
} }
if (forceNoResults) { if (forceNoResults) {
condSQL += '0=1'; condSQL += 'itemID IN (0)';
} }
else if (objectType == 'collection') { else if (objectType == 'collection') {
let ids = [obj.id]; let ids = [obj.id];
@ -1587,39 +1587,33 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () {
condSQL += ')'; condSQL += ')';
} }
if (includeParentsAndChildren || includeParents) { if (!forceNoResults) {
var parentSQL = "SELECT itemID FROM items WHERE " if (includeParentsAndChildren || includeParents) {
+ "itemID IN (SELECT parentItemID FROM itemAttachments " var parentSQL = "SELECT itemID FROM items WHERE "
+ "WHERE itemID IN (" + condSQL + ")) " + "itemID IN (SELECT parentItemID FROM itemAttachments "
+ "OR itemID IN (SELECT parentItemID FROM itemNotes " + "WHERE itemID IN (" + condSQL + ")) "
+ "WHERE itemID IN (" + condSQL + ")) "; + "OR itemID IN (SELECT parentItemID FROM itemNotes "
var parentSQLParams = condSQLParams.concat(condSQLParams); + "WHERE itemID IN (" + condSQL + ")) ";
} var parentSQLParams = condSQLParams.concat(condSQLParams);
if (includeParentsAndChildren || includeChildren) {
var childrenSQL = "SELECT itemID FROM itemAttachments WHERE "
+ "parentItemID IN (" + condSQL + ") UNION "
+ "SELECT itemID FROM itemNotes "
+ "WHERE parentItemID IN (" + condSQL + ")";
var childSQLParams = condSQLParams.concat(condSQLParams);
}
if (includeParentsAndChildren || includeParents) {
// Tweak forceNoResults expression to work with UNION
if (condSQL == '0=1') {
condSQL = 'SELECT 0=1';
} }
condSQL += " UNION " + parentSQL;
condSQLParams = condSQLParams.concat(parentSQLParams); if (includeParentsAndChildren || includeChildren) {
} var childrenSQL = "SELECT itemID FROM itemAttachments WHERE "
+ "parentItemID IN (" + condSQL + ") UNION "
if (includeParentsAndChildren || includeChildren) { + "SELECT itemID FROM itemNotes "
// Tweak forceNoResults expression to work with UNION + "WHERE parentItemID IN (" + condSQL + ")";
if (condSQL == '0=1') { var childSQLParams = condSQLParams.concat(condSQLParams);
condSQL = 'SELECT 0=1'; }
if (includeParentsAndChildren || includeParents) {
condSQL += " UNION " + parentSQL;
condSQLParams = condSQLParams.concat(parentSQLParams);
}
if (includeParentsAndChildren || includeChildren) {
condSQL += " UNION " + childrenSQL;
condSQLParams = condSQLParams.concat(childSQLParams);
} }
condSQL += " UNION " + childrenSQL;
condSQLParams = condSQLParams.concat(childSQLParams);
} }
condSQL = condSelectSQL + condSQL; condSQL = condSelectSQL + condSQL;

View file

@ -420,6 +420,18 @@ describe("Zotero.Search", function() {
var matches = await s.search(); var matches = await s.search();
assert.lengthOf(matches, 0); assert.lengthOf(matches, 0);
}); });
it("should handle ANY search with two no-op conditions", async function () {
var s = new Zotero.Search();
s.libraryID = userLibraryID;
s.name = "Test";
s.addCondition('joinMode', 'any');
s.addCondition('savedSearch', 'is', Zotero.Utilities.randomString());
s.addCondition('savedSearch', 'is', Zotero.Utilities.randomString());
s.addCondition('includeParentsAndChildren', 'true');
var matches = await s.search();
assert.lengthOf(matches, 0);
});
}); });
describe("key", function () { describe("key", function () {