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,6 +1587,7 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () {
condSQL += ')'; condSQL += ')';
} }
if (!forceNoResults) {
if (includeParentsAndChildren || includeParents) { if (includeParentsAndChildren || includeParents) {
var parentSQL = "SELECT itemID FROM items WHERE " var parentSQL = "SELECT itemID FROM items WHERE "
+ "itemID IN (SELECT parentItemID FROM itemAttachments " + "itemID IN (SELECT parentItemID FROM itemAttachments "
@ -1605,22 +1606,15 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () {
} }
if (includeParentsAndChildren || includeParents) { if (includeParentsAndChildren || includeParents) {
// Tweak forceNoResults expression to work with UNION
if (condSQL == '0=1') {
condSQL = 'SELECT 0=1';
}
condSQL += " UNION " + parentSQL; condSQL += " UNION " + parentSQL;
condSQLParams = condSQLParams.concat(parentSQLParams); condSQLParams = condSQLParams.concat(parentSQLParams);
} }
if (includeParentsAndChildren || includeChildren) { if (includeParentsAndChildren || includeChildren) {
// Tweak forceNoResults expression to work with UNION
if (condSQL == '0=1') {
condSQL = 'SELECT 0=1';
}
condSQL += " UNION " + childrenSQL; condSQL += " UNION " + childrenSQL;
condSQLParams = condSQLParams.concat(childSQLParams); 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 () {