From 95ac39b0543d1fb468336c077d857f71e40dc09d Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Fri, 30 Dec 2022 13:15:16 -0500 Subject: [PATCH] attempt to reduce complexity --- helpers/lotOccupancyDB/getLotOccupancies.js | 37 +++++++++---------- helpers/lotOccupancyDB/getLotOccupancies.ts | 41 +++++++++++---------- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/helpers/lotOccupancyDB/getLotOccupancies.js b/helpers/lotOccupancyDB/getLotOccupancies.js index b79207b4..5dceda09 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.js +++ b/helpers/lotOccupancyDB/getLotOccupancies.js @@ -70,7 +70,8 @@ export function getLotOccupancies(filters, options, connectedDatabase) { database.function("userFn_dateIntegerToString", dateIntegerToString); const { sqlWhereClause, sqlParameters } = buildWhereClause(filters); let count = 0; - if (options.limit !== -1) { + const isLimited = options.limit !== -1; + if (isLimited) { count = database .prepare("select count(*) as recordCount" + " from LotOccupancies o" + @@ -79,26 +80,24 @@ export function getLotOccupancies(filters, options, connectedDatabase) { .get(sqlParameters).recordCount; } let lotOccupancies = []; - if (options.limit === -1 || count > 0) { + if (!isLimited || count > 0) { lotOccupancies = database - .prepare("select o.lotOccupancyId," + - " o.occupancyTypeId, t.occupancyType," + - " o.lotId, lt.lotType, l.lotName," + - " l.mapId, m.mapName," + - " o.occupancyStartDate, userFn_dateIntegerToString(o.occupancyStartDate) as occupancyStartDateString," + - " o.occupancyEndDate, userFn_dateIntegerToString(o.occupancyEndDate) as occupancyEndDateString" + - " from LotOccupancies o" + - " left join OccupancyTypes t on o.occupancyTypeId = t.occupancyTypeId" + - " left join Lots l on o.lotId = l.lotId" + - " left join LotTypes lt on l.lotTypeId = lt.lotTypeId" + - " left join Maps m on l.mapId = m.mapId" + - sqlWhereClause + - " order by o.occupancyStartDate desc, ifnull(o.occupancyEndDate, 99999999) desc, l.lotName, o.lotId, o.lotOccupancyId desc" + - (options.limit === -1 - ? "" - : " limit " + options.limit + " offset " + options.offset)) + .prepare(`select o.lotOccupancyId, + o.occupancyTypeId, t.occupancyType, + o.lotId, lt.lotType, l.lotName, + l.mapId, m.mapName, + o.occupancyStartDate, userFn_dateIntegerToString(o.occupancyStartDate) as occupancyStartDateString, + o.occupancyEndDate, userFn_dateIntegerToString(o.occupancyEndDate) as occupancyEndDateString + from LotOccupancies o + left join OccupancyTypes t on o.occupancyTypeId = t.occupancyTypeId + left join Lots l on o.lotId = l.lotId + left join LotTypes lt on l.lotTypeId = lt.lotTypeId + left join Maps m on l.mapId = m.mapId + ${sqlWhereClause} + order by o.occupancyStartDate desc, ifnull(o.occupancyEndDate, 99999999) desc, l.lotName, o.lotId, o.lotOccupancyId desc` + + (isLimited ? " limit " + options.limit + " offset " + options.offset : "")) .all(sqlParameters); - if (options.limit === -1) { + if (!isLimited) { count = lotOccupancies.length; } for (const lotOccupancy of lotOccupancies) { diff --git a/helpers/lotOccupancyDB/getLotOccupancies.ts b/helpers/lotOccupancyDB/getLotOccupancies.ts index 84323ecd..eb05a6bb 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.ts +++ b/helpers/lotOccupancyDB/getLotOccupancies.ts @@ -136,7 +136,12 @@ export function getLotOccupancies( let count = 0; - if (options.limit !== -1) { + /** + * options.limit !== -1 + */ + const isLimited = options.limit !== -1; + + if (isLimited) { count = database .prepare( "select count(*) as recordCount" + @@ -149,29 +154,27 @@ export function getLotOccupancies( let lotOccupancies: recordTypes.LotOccupancy[] = []; - if (options.limit === -1 || count > 0) { + if (!isLimited || count > 0) { lotOccupancies = database .prepare( - "select o.lotOccupancyId," + - " o.occupancyTypeId, t.occupancyType," + - " o.lotId, lt.lotType, l.lotName," + - " l.mapId, m.mapName," + - " o.occupancyStartDate, userFn_dateIntegerToString(o.occupancyStartDate) as occupancyStartDateString," + - " o.occupancyEndDate, userFn_dateIntegerToString(o.occupancyEndDate) as occupancyEndDateString" + - " from LotOccupancies o" + - " left join OccupancyTypes t on o.occupancyTypeId = t.occupancyTypeId" + - " left join Lots l on o.lotId = l.lotId" + - " left join LotTypes lt on l.lotTypeId = lt.lotTypeId" + - " left join Maps m on l.mapId = m.mapId" + - sqlWhereClause + - " order by o.occupancyStartDate desc, ifnull(o.occupancyEndDate, 99999999) desc, l.lotName, o.lotId, o.lotOccupancyId desc" + - (options.limit === -1 - ? "" - : " limit " + options.limit + " offset " + options.offset) + `select o.lotOccupancyId, + o.occupancyTypeId, t.occupancyType, + o.lotId, lt.lotType, l.lotName, + l.mapId, m.mapName, + o.occupancyStartDate, userFn_dateIntegerToString(o.occupancyStartDate) as occupancyStartDateString, + o.occupancyEndDate, userFn_dateIntegerToString(o.occupancyEndDate) as occupancyEndDateString + from LotOccupancies o + left join OccupancyTypes t on o.occupancyTypeId = t.occupancyTypeId + left join Lots l on o.lotId = l.lotId + left join LotTypes lt on l.lotTypeId = lt.lotTypeId + left join Maps m on l.mapId = m.mapId + ${sqlWhereClause} + order by o.occupancyStartDate desc, ifnull(o.occupancyEndDate, 99999999) desc, l.lotName, o.lotId, o.lotOccupancyId desc` + + (isLimited ? " limit " + options.limit + " offset " + options.offset : "") ) .all(sqlParameters); - if (options.limit === -1) { + if (!isLimited) { count = lotOccupancies.length; }