attempt to reduce complexity

deepsource-autofix-76c6eb20
Dan Gowans 2022-12-30 13:15:16 -05:00
parent 5b94b09c7b
commit 95ac39b054
2 changed files with 40 additions and 38 deletions

View File

@ -70,7 +70,8 @@ export function getLotOccupancies(filters, options, connectedDatabase) {
database.function("userFn_dateIntegerToString", dateIntegerToString); database.function("userFn_dateIntegerToString", dateIntegerToString);
const { sqlWhereClause, sqlParameters } = buildWhereClause(filters); const { sqlWhereClause, sqlParameters } = buildWhereClause(filters);
let count = 0; let count = 0;
if (options.limit !== -1) { const isLimited = options.limit !== -1;
if (isLimited) {
count = database count = database
.prepare("select count(*) as recordCount" + .prepare("select count(*) as recordCount" +
" from LotOccupancies o" + " from LotOccupancies o" +
@ -79,26 +80,24 @@ export function getLotOccupancies(filters, options, connectedDatabase) {
.get(sqlParameters).recordCount; .get(sqlParameters).recordCount;
} }
let lotOccupancies = []; let lotOccupancies = [];
if (options.limit === -1 || count > 0) { if (!isLimited || count > 0) {
lotOccupancies = database lotOccupancies = database
.prepare("select o.lotOccupancyId," + .prepare(`select o.lotOccupancyId,
" o.occupancyTypeId, t.occupancyType," + o.occupancyTypeId, t.occupancyType,
" o.lotId, lt.lotType, l.lotName," + o.lotId, lt.lotType, l.lotName,
" l.mapId, m.mapName," + l.mapId, m.mapName,
" o.occupancyStartDate, userFn_dateIntegerToString(o.occupancyStartDate) as occupancyStartDateString," + o.occupancyStartDate, userFn_dateIntegerToString(o.occupancyStartDate) as occupancyStartDateString,
" o.occupancyEndDate, userFn_dateIntegerToString(o.occupancyEndDate) as occupancyEndDateString" + o.occupancyEndDate, userFn_dateIntegerToString(o.occupancyEndDate) as occupancyEndDateString
" from LotOccupancies o" + from LotOccupancies o
" left join OccupancyTypes t on o.occupancyTypeId = t.occupancyTypeId" + left join OccupancyTypes t on o.occupancyTypeId = t.occupancyTypeId
" left join Lots l on o.lotId = l.lotId" + left join Lots l on o.lotId = l.lotId
" left join LotTypes lt on l.lotTypeId = lt.lotTypeId" + left join LotTypes lt on l.lotTypeId = lt.lotTypeId
" left join Maps m on l.mapId = m.mapId" + left join Maps m on l.mapId = m.mapId
sqlWhereClause + ${sqlWhereClause}
" order by o.occupancyStartDate desc, ifnull(o.occupancyEndDate, 99999999) desc, l.lotName, o.lotId, o.lotOccupancyId desc" + order by o.occupancyStartDate desc, ifnull(o.occupancyEndDate, 99999999) desc, l.lotName, o.lotId, o.lotOccupancyId desc` +
(options.limit === -1 (isLimited ? " limit " + options.limit + " offset " + options.offset : ""))
? ""
: " limit " + options.limit + " offset " + options.offset))
.all(sqlParameters); .all(sqlParameters);
if (options.limit === -1) { if (!isLimited) {
count = lotOccupancies.length; count = lotOccupancies.length;
} }
for (const lotOccupancy of lotOccupancies) { for (const lotOccupancy of lotOccupancies) {

View File

@ -136,7 +136,12 @@ export function getLotOccupancies(
let count = 0; let count = 0;
if (options.limit !== -1) { /**
* options.limit !== -1
*/
const isLimited = options.limit !== -1;
if (isLimited) {
count = database count = database
.prepare( .prepare(
"select count(*) as recordCount" + "select count(*) as recordCount" +
@ -149,29 +154,27 @@ export function getLotOccupancies(
let lotOccupancies: recordTypes.LotOccupancy[] = []; let lotOccupancies: recordTypes.LotOccupancy[] = [];
if (options.limit === -1 || count > 0) { if (!isLimited || count > 0) {
lotOccupancies = database lotOccupancies = database
.prepare( .prepare(
"select o.lotOccupancyId," + `select o.lotOccupancyId,
" o.occupancyTypeId, t.occupancyType," + o.occupancyTypeId, t.occupancyType,
" o.lotId, lt.lotType, l.lotName," + o.lotId, lt.lotType, l.lotName,
" l.mapId, m.mapName," + l.mapId, m.mapName,
" o.occupancyStartDate, userFn_dateIntegerToString(o.occupancyStartDate) as occupancyStartDateString," + o.occupancyStartDate, userFn_dateIntegerToString(o.occupancyStartDate) as occupancyStartDateString,
" o.occupancyEndDate, userFn_dateIntegerToString(o.occupancyEndDate) as occupancyEndDateString" + o.occupancyEndDate, userFn_dateIntegerToString(o.occupancyEndDate) as occupancyEndDateString
" from LotOccupancies o" + from LotOccupancies o
" left join OccupancyTypes t on o.occupancyTypeId = t.occupancyTypeId" + left join OccupancyTypes t on o.occupancyTypeId = t.occupancyTypeId
" left join Lots l on o.lotId = l.lotId" + left join Lots l on o.lotId = l.lotId
" left join LotTypes lt on l.lotTypeId = lt.lotTypeId" + left join LotTypes lt on l.lotTypeId = lt.lotTypeId
" left join Maps m on l.mapId = m.mapId" + left join Maps m on l.mapId = m.mapId
sqlWhereClause + ${sqlWhereClause}
" order by o.occupancyStartDate desc, ifnull(o.occupancyEndDate, 99999999) desc, l.lotName, o.lotId, o.lotOccupancyId desc" + order by o.occupancyStartDate desc, ifnull(o.occupancyEndDate, 99999999) desc, l.lotName, o.lotId, o.lotOccupancyId desc` +
(options.limit === -1 (isLimited ? " limit " + options.limit + " offset " + options.offset : "")
? ""
: " limit " + options.limit + " offset " + options.offset)
) )
.all(sqlParameters); .all(sqlParameters);
if (options.limit === -1) { if (!isLimited) {
count = lotOccupancies.length; count = lotOccupancies.length;
} }