bulk insertion/removal of db rows

This commit is contained in:
SolPuffy 2024-12-29 17:30:46 +02:00
parent e2c28f8735
commit dba87541bf

View file

@ -19,7 +19,7 @@ class SqlSystem {
UNIQUE INDEX 'auctionid' ('auctionid')
);
CREATE TABLE if NOT EXISTS lifetimes (id INT PRIMARY KEY, insertedon DATETIME(2));
`)
`);
//Setup lifetime Trigger
await conn.query(`
DELIMITER $$
@ -47,7 +47,7 @@ class SqlSystem {
DELETE FROM lifetimes WHERE id=OLD.ID;
END$$
DELIMITER ;
`)
`);
//Setup Table sanitation
await conn.query(`
DELIMITER $$
@ -76,7 +76,7 @@ class SqlSystem {
ON SCHEDULE EVERY 2 MINUTE
DO
CALL CleanupOldEntries();
`)
`);
}
catch (error) {
console.error("InitDB Error: ", error);
@ -87,37 +87,52 @@ class SqlSystem {
}
}
//INSERT ELEMENT IN AUCTIONS TABLE OR UPDATE IF IT ALREADY EXISTS
public static async Upsert(auctionid: string, lbin: number) {
public static async Upsert(auctionid: string|string[], lbin: number|number[]) {
let conn: mariadb.PoolConnection | undefined;
try {
conn = await this.pool.getConnection();
await conn.beginTransaction();
if(Array.isArray(auctionid) && Array.isArray(lbin))
if(auctionid.length === lbin.length) {
await conn.query(`
INSERT INTO auctions (auctionid, LBin)
VALUES (?,?)
ON DUPLICATE KEY UPDATE
LBin = VALUES(LBin);
`,[await this.UnifiedArray(auctionid,lbin)]);
}
else if(Array.isArray(auctionid) || Array.isArray(lbin))
throw Error(`Upsert SQL Function error - cannot unify collection ${Array.isArray(auctionid) ? auctionid : lbin} to singlet ${!Array.isArray(auctionid) ? auctionid : lbin}`);
else {
await conn.query(`
INSERT INTO auctions (auctionid, LBin)
VALUES (?,?)
ON DUPLICATE KEY UPDATE
LBin = VALUES(LBin);
`, [auctionid, lbin]);
}
await conn.commit();
}
catch (error) {
console.error("InitDB Error: ", error);
catch(error)
{
console.error("message_before_error: ", error);
if(conn) await conn.rollback();
throw error;
}
finally {
finally
{
if (conn) conn.release();
}
}
//REMOVE ELEMENT IN AUCTIONS TABLE
public static async Remove(auctionId: string) {
public static async Remove(auctionid: string|string[]) {
let conn: mariadb.PoolConnection | undefined;
try {
conn = await this.pool.getConnection();
await conn.beginTransaction();
await conn.query(`
DELETE FROM auctions WHERE auctionid = ?
`,[auctionId]);
`,[auctionid]);
await conn.commit();
}
catch (error) {
@ -150,10 +165,11 @@ class SqlSystem {
FROM auctions AS auc
WHERE auc.auctionID = tmp.auctionID
AND auc.lbin = tmp.lbin);
`,[await this.UnifiedArray(auctionid,lbin)])
`,[await this.UnifiedArray(auctionid,lbin)]);
return result;
}
else if(Array.isArray(auctionid) || Array.isArray(lbin)) throw Error('Match SQL Function error - cannot match collection to singlet');
else if(Array.isArray(auctionid) || Array.isArray(lbin))
throw Error(`Match SQL Function error - cannot unify collection ${Array.isArray(auctionid) ? auctionid : lbin} to singlet ${!Array.isArray(auctionid) ? auctionid : lbin}`);
else {
result = await conn.query(`
SELECT CASE
@ -162,8 +178,8 @@ class SqlSystem {
ELSE false
END AS user_exists
FROM auctions WHERE auctionID = ?;
`,[auctionid])
return Boolean(result[0].result)
`,[auctionid]);
return Boolean(result[0].result);
}
}
catch(error)