From 6b3fa6cf22bf4091794012524d4e9fd0a1351527 Mon Sep 17 00:00:00 2001 From: sol Date: Tue, 7 Jan 2025 11:08:44 +0200 Subject: [PATCH] Fixed and ported functions mariadb > postgre almost. --- src/helperFunctions.ts | 22 +++ src/sqlConsoleCommands.ts | 52 ------- src/sqlFunctions.ts | 303 +++++++++++++++++++++++--------------- 3 files changed, 209 insertions(+), 168 deletions(-) delete mode 100644 src/sqlConsoleCommands.ts diff --git a/src/helperFunctions.ts b/src/helperFunctions.ts index ee0e648..0754ea3 100644 --- a/src/helperFunctions.ts +++ b/src/helperFunctions.ts @@ -228,6 +228,24 @@ function currentIntervals(): Record { return currentAsyncIntervals; } +function threadTimeout(miliseconds: number): Promise { + return new Promise(resolve => setTimeout(resolve,miliseconds)); +} +function unifyArrays(array1: any[], array2: any[]): Promise<{ x: any[]; y: any[] }> { + return Promise.resolve({ x: [...array1], y: [...array2] }); +} +function randomArray(item_count: number,range_scale: number): any { + let random_array: any[] = []; + for(let i: number = 0;i { - const command_args = process.argv.slice(2); - if(arguments.length===0) return; - switch(command_args[0].toString()) { - case "insdummy": { - this.InsertDummyData(); - break; - } - case "reqdummy": { - this.RequestDummyData(); - break; - } - default:break; - } - } - - private static randomArray(item_count: number,range_scale: number): any { - let random_array: any[] = []; - for(let i: number = 0;i { + let conn: PoolClient | undefined; + let result; + try{ + conn = await this.pool.connect(); + if(Array.isArray(auctionid) && Array.isArray(lbin)) + if(auctionid.length === lbin.length) { + result = await conn.query(` + CREATE TEMPORARY TABLE IF NOT EXISTS TEMP ( + auctionID VARCHAR(255) PRIMARY KEY, + lbin DECIMAL(65,5) + ); + 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 unifyArrays(auctionid,lbin)]); + return result.rows; + } + 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 + WHEN COUNT(*) > 0 + THEN true + ELSE false + END AS user_exists + FROM auctions WHERE auctionID = ?; + `,[auctionid]); + return Boolean(result.rows.length); + } + } + catch(error) + { + console.error("message_before_error: ", error); + throw error; + } + finally + { + if (conn) conn.release(); + } + } //INSERT ELEMENT IN AUCTIONS TABLE OR UPDATE IF IT ALREADY EXISTS - public static async Upsert(auctionid: string|string[], lbin: number|number[]) { + public static async deprecated_Upsert(auctionid: string|string[], lbin: number|number[]) { let conn: PoolClient | undefined; try { conn = await this.pool.connect(); @@ -176,120 +361,6 @@ class SqlSystem { if (conn) conn.release(); } } -//REMOVE ELEMENT IN AUCTIONS TABLE - public static async Remove(auctionid: string|string[]) { - let conn: PoolClient | undefined; - try { - conn = await this.pool.connect(); - await conn.query('BEGIN'); - await conn.query(` - DELETE FROM auctions WHERE auctionid = ? - `,[auctionid]); - await conn.query('COMMIT'); - } - catch (error) { - console.error("InitDB Error: ", error); - if (conn) await conn.query('ROLLBACK'); - throw error; - } - finally { - if (conn) conn.release(); - } - } - - public static async Query(query:string,use_transaction:boolean = true) { - let conn: PoolClient | undefined; - try{ - conn = await this.pool.connect() - if (use_transaction) await conn.query('BEGIN'); - await conn.query(query); - if (use_transaction) await conn.query('COMMIT'); - } - catch(error) { - console.error("InitDB Error: ", error); - if (use_transaction && conn) await conn.query('ROLLBACK'); - throw error; - } - finally - { - if(conn) conn.release(); - } - } - //MATCH PROVIDED ELEMENTS IN AUCTIONS TABLE - returns true/false - public static async Match(auctionid:string|string[],lbin:number|number[]): Promise { - let conn: PoolClient | undefined; - let result; - try{ - conn = await this.pool.connect(); - if(Array.isArray(auctionid) && Array.isArray(lbin)) - if(auctionid.length === lbin.length) { - result = await conn.query(` - CREATE TEMPORARY TABLE IF NOT EXISTS TEMP ( - auctionID VARCHAR(255) PRIMARY KEY, - lbin DECIMAL(65,5) - ); - 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)]); - return result; - } - 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 - WHEN COUNT(*) > 0 - THEN true - ELSE false - END AS user_exists - FROM auctions WHERE auctionID = ?; - `,[auctionid]); - return Boolean(result.rows.length); - } - } - catch(error) - { - console.error("message_before_error: ", error); - throw error; - } - finally - { - if (conn) conn.release(); - } - } - -//EXAMPLE BLOCK OF CODE FOR ADDING DATABASE FUNCTIONS - /* - public static async example_name() { - let conn:PoolClient|undefined; - try{ - conn = await this.pool.connect(); - await conn.query('BEGIN'); - - //CODE HERE// - - await conn.query('COMMIT'); - } - catch(error) - { - console.error("message_before_error: ", error); - if(conn) await conn.query('ROLLBACK'); - throw error; - } - finally - { - if(conn) conn.release(); - } - */ - private static async UnifiedArray(auctionid: string[], lbin: number[]): Promise<{ x: string[], y: number[] }> { - return { x: [...auctionid], y: [...lbin] }; - } } export { SqlSystem