From dba87541bf87e6567620ac22a213794e241cd44d Mon Sep 17 00:00:00 2001 From: sol Date: Sun, 29 Dec 2024 17:30:46 +0200 Subject: [PATCH] bulk insertion/removal of db rows --- src/sqlFunctions.ts | 66 ++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/src/sqlFunctions.ts b/src/sqlFunctions.ts index e72480c..1aa5269 100644 --- a/src/sqlFunctions.ts +++ b/src/sqlFunctions.ts @@ -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(); - await conn.query(` - INSERT INTO auctions (auctionid, LBin) - VALUES (?,?) - ON DUPLICATE KEY UPDATE - LBin = VALUES(LBin); + 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(); + } + 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) { @@ -144,16 +159,17 @@ class SqlSystem { ); INSERT INTO TEMP (auctionID, lbin) VALUES ? SELECT tmp.* - FROM TEMP AS tmp - WHERE NOT EXISTS ( - SELECT 1 - FROM auctions AS auc - WHERE auc.auctionID = tmp.auctionID - AND auc.lbin = tmp.lbin); - `,[await this.UnifiedArray(auctionid,lbin)]) + FROM TEMP AS tmp + WHERE NOT EXISTS ( + SELECT 1 + FROM auctions AS auc + WHERE auc.auctionID = tmp.auctionID + AND auc.lbin = tmp.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)