reduce ordernumber queries

deepsource-autofix-76c6eb20
Dan Gowans 2023-01-04 15:36:49 -05:00
parent 44ffca489a
commit 1d434264f6
4 changed files with 50 additions and 44 deletions

View File

@ -1,11 +1,11 @@
import sqlite from "better-sqlite3"; import sqlite from "better-sqlite3";
import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js";
import { clearLotOccupantTypesCache } from "../functions.cache.js"; import { getLotOccupantTypeById, clearLotOccupantTypesCache } from "../functions.cache.js";
export function moveLotOccupantTypeDown(lotOccupantTypeId) { export function moveLotOccupantTypeDown(lotOccupantTypeId) {
const currentOrderNumber = getLotOccupantTypeById(typeof lotOccupantTypeId === "string"
? Number.parseInt(lotOccupantTypeId)
: lotOccupantTypeId).orderNumber;
const database = sqlite(databasePath); const database = sqlite(databasePath);
const currentOrderNumber = database
.prepare(`select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?`)
.get(lotOccupantTypeId).orderNumber;
database database
.prepare(`update LotOccupantTypes .prepare(`update LotOccupantTypes
set orderNumber = orderNumber - 1 set orderNumber = orderNumber - 1
@ -20,10 +20,10 @@ export function moveLotOccupantTypeDown(lotOccupantTypeId) {
return result.changes > 0; return result.changes > 0;
} }
export function moveLotOccupantTypeDownToBottom(lotOccupantTypeId) { export function moveLotOccupantTypeDownToBottom(lotOccupantTypeId) {
const currentOrderNumber = getLotOccupantTypeById(typeof lotOccupantTypeId === "string"
? Number.parseInt(lotOccupantTypeId)
: lotOccupantTypeId).orderNumber;
const database = sqlite(databasePath); const database = sqlite(databasePath);
const currentOrderNumber = database
.prepare("select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?")
.get(lotOccupantTypeId).orderNumber;
const maxOrderNumber = database const maxOrderNumber = database
.prepare(`select max(orderNumber) as maxOrderNumber .prepare(`select max(orderNumber) as maxOrderNumber
from LotOccupantTypes from LotOccupantTypes

View File

@ -2,14 +2,16 @@ import sqlite from "better-sqlite3";
import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js";
import { clearLotOccupantTypesCache } from "../functions.cache.js"; import { getLotOccupantTypeById, clearLotOccupantTypesCache } from "../functions.cache.js";
export function moveLotOccupantTypeDown(lotOccupantTypeId: number | string): boolean { export function moveLotOccupantTypeDown(lotOccupantTypeId: number | string): boolean {
const database = sqlite(databasePath); const currentOrderNumber: number = getLotOccupantTypeById(
typeof lotOccupantTypeId === "string"
? Number.parseInt(lotOccupantTypeId)
: lotOccupantTypeId
).orderNumber;
const currentOrderNumber: number = database const database = sqlite(databasePath);
.prepare(`select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?`)
.get(lotOccupantTypeId).orderNumber;
database database
.prepare( .prepare(
@ -32,11 +34,13 @@ export function moveLotOccupantTypeDown(lotOccupantTypeId: number | string): boo
} }
export function moveLotOccupantTypeDownToBottom(lotOccupantTypeId: number | string): boolean { export function moveLotOccupantTypeDownToBottom(lotOccupantTypeId: number | string): boolean {
const database = sqlite(databasePath); const currentOrderNumber: number = getLotOccupantTypeById(
typeof lotOccupantTypeId === "string"
? Number.parseInt(lotOccupantTypeId)
: lotOccupantTypeId
).orderNumber;
const currentOrderNumber: number = database const database = sqlite(databasePath);
.prepare("select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?")
.get(lotOccupantTypeId).orderNumber;
const maxOrderNumber: number = database const maxOrderNumber: number = database
.prepare( .prepare(

View File

@ -1,15 +1,14 @@
import sqlite from "better-sqlite3"; import sqlite from "better-sqlite3";
import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js";
import { clearLotOccupantTypesCache } from "../functions.cache.js"; import { getLotOccupantTypeById, clearLotOccupantTypesCache } from "../functions.cache.js";
export function moveLotOccupantTypeUp(lotOccupantTypeId) { export function moveLotOccupantTypeUp(lotOccupantTypeId) {
const database = sqlite(databasePath); const currentOrderNumber = getLotOccupantTypeById(typeof lotOccupantTypeId === "string"
const currentOrderNumber = database ? Number.parseInt(lotOccupantTypeId)
.prepare(`select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?`) : lotOccupantTypeId).orderNumber;
.get(lotOccupantTypeId).orderNumber;
if (currentOrderNumber <= 0) { if (currentOrderNumber <= 0) {
database.close();
return true; return true;
} }
const database = sqlite(databasePath);
database database
.prepare(`update LotOccupantTypes .prepare(`update LotOccupantTypes
set orderNumber = orderNumber + 1 set orderNumber = orderNumber + 1
@ -26,11 +25,11 @@ export function moveLotOccupantTypeUp(lotOccupantTypeId) {
return result.changes > 0; return result.changes > 0;
} }
export function moveLotOccupantTypeUpToTop(lotOccupantTypeId) { export function moveLotOccupantTypeUpToTop(lotOccupantTypeId) {
const database = sqlite(databasePath); const currentOrderNumber = getLotOccupantTypeById(typeof lotOccupantTypeId === "string"
const currentOrderNumber = database ? Number.parseInt(lotOccupantTypeId)
.prepare("select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?") : lotOccupantTypeId).orderNumber;
.get(lotOccupantTypeId).orderNumber;
if (currentOrderNumber > 0) { if (currentOrderNumber > 0) {
const database = sqlite(databasePath);
database database
.prepare("update LotOccupantTypes set orderNumber = -1 where lotOccupantTypeId = ?") .prepare("update LotOccupantTypes set orderNumber = -1 where lotOccupantTypeId = ?")
.run(lotOccupantTypeId); .run(lotOccupantTypeId);
@ -40,9 +39,9 @@ export function moveLotOccupantTypeUpToTop(lotOccupantTypeId) {
where recordDelete_timeMillis is null where recordDelete_timeMillis is null
and orderNumber < ?`) and orderNumber < ?`)
.run(currentOrderNumber); .run(currentOrderNumber);
}
database.close(); database.close();
clearLotOccupantTypesCache(); clearLotOccupantTypesCache();
}
return true; return true;
} }
export default moveLotOccupantTypeUp; export default moveLotOccupantTypeUp;

View File

@ -2,20 +2,21 @@ import sqlite from "better-sqlite3";
import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js";
import { clearLotOccupantTypesCache } from "../functions.cache.js"; import { getLotOccupantTypeById, clearLotOccupantTypesCache } from "../functions.cache.js";
export function moveLotOccupantTypeUp(lotOccupantTypeId: number | string): boolean { export function moveLotOccupantTypeUp(lotOccupantTypeId: number | string): boolean {
const database = sqlite(databasePath); const currentOrderNumber: number = getLotOccupantTypeById(
typeof lotOccupantTypeId === "string"
const currentOrderNumber: number = database ? Number.parseInt(lotOccupantTypeId)
.prepare(`select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?`) : lotOccupantTypeId
.get(lotOccupantTypeId).orderNumber; ).orderNumber;
if (currentOrderNumber <= 0) { if (currentOrderNumber <= 0) {
database.close();
return true; return true;
} }
const database = sqlite(databasePath);
database database
.prepare( .prepare(
`update LotOccupantTypes `update LotOccupantTypes
@ -41,13 +42,15 @@ export function moveLotOccupantTypeUp(lotOccupantTypeId: number | string): boole
} }
export function moveLotOccupantTypeUpToTop(lotOccupantTypeId: number | string): boolean { export function moveLotOccupantTypeUpToTop(lotOccupantTypeId: number | string): boolean {
const database = sqlite(databasePath); const currentOrderNumber: number = getLotOccupantTypeById(
typeof lotOccupantTypeId === "string"
const currentOrderNumber: number = database ? Number.parseInt(lotOccupantTypeId)
.prepare("select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?") : lotOccupantTypeId
.get(lotOccupantTypeId).orderNumber; ).orderNumber;
if (currentOrderNumber > 0) { if (currentOrderNumber > 0) {
const database = sqlite(databasePath);
database database
.prepare("update LotOccupantTypes set orderNumber = -1 where lotOccupantTypeId = ?") .prepare("update LotOccupantTypes set orderNumber = -1 where lotOccupantTypeId = ?")
.run(lotOccupantTypeId); .run(lotOccupantTypeId);
@ -60,11 +63,11 @@ export function moveLotOccupantTypeUpToTop(lotOccupantTypeId: number | string):
and orderNumber < ?` and orderNumber < ?`
) )
.run(currentOrderNumber); .run(currentOrderNumber);
}
database.close(); database.close();
clearLotOccupantTypesCache(); clearLotOccupantTypesCache();
}
return true; return true;
} }