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