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

View File

@ -2,14 +2,16 @@ import sqlite from "better-sqlite3";
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 {
const database = sqlite(databasePath);
const currentOrderNumber: number = getLotOccupantTypeById(
typeof lotOccupantTypeId === "string"
? Number.parseInt(lotOccupantTypeId)
: lotOccupantTypeId
).orderNumber;
const currentOrderNumber: number = database
.prepare(`select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?`)
.get(lotOccupantTypeId).orderNumber;
const database = sqlite(databasePath);
database
.prepare(
@ -32,11 +34,13 @@ export function moveLotOccupantTypeDown(lotOccupantTypeId: number | string): boo
}
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
.prepare("select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?")
.get(lotOccupantTypeId).orderNumber;
const database = sqlite(databasePath);
const maxOrderNumber: number = database
.prepare(

View File

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

View File

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