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

View file

@ -420,6 +420,18 @@ describe("Zotero.Search", function() {
var matches = await s.search();
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 () {