bulk insertion/removal of db rows
This commit is contained in:
parent
e2c28f8735
commit
dba87541bf
1 changed files with 41 additions and 25 deletions
|
@ -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();
|
||||||
|
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(`
|
await conn.query(`
|
||||||
INSERT INTO auctions (auctionid, LBin)
|
INSERT INTO auctions (auctionid, LBin)
|
||||||
VALUES (?,?)
|
VALUES (?,?)
|
||||||
ON DUPLICATE KEY UPDATE
|
ON DUPLICATE KEY UPDATE
|
||||||
LBin = VALUES(LBin);
|
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) {
|
||||||
|
@ -150,10 +165,11 @@ class SqlSystem {
|
||||||
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)
|
||||||
|
|
Loading…
Reference in a new issue