sql test console commands
This commit is contained in:
parent
dba87541bf
commit
f0cad4f19e
4 changed files with 75 additions and 2 deletions
|
@ -6,3 +6,4 @@ node_modules/
|
|||
.*
|
||||
pnpm-lock*
|
||||
dist
|
||||
db_data
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,3 +4,4 @@ pnpm-lock*
|
|||
dist
|
||||
*.db
|
||||
*.sqlite*
|
||||
db_data
|
52
src/sqlConsoleCommands.ts
Normal file
52
src/sqlConsoleCommands.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import { SqlSystem } from './sqlFunctions';
|
||||
|
||||
class SqlCommands {
|
||||
|
||||
private static async InsertDummyData() {
|
||||
console.log("Inserting random numbers into Pool");
|
||||
let dummy_auctionid: string[] = this.randomArray(10,8),dummy_lbin: number[] = this.randomArray(10,5);
|
||||
await SqlSystem.Upsert(dummy_auctionid,dummy_lbin)
|
||||
}
|
||||
|
||||
private static async RequestDummyData() {
|
||||
console.log("Requesting All from table auctions");
|
||||
console.log(await SqlSystem.Query("SELECT * FROM auctions"));
|
||||
}
|
||||
|
||||
public static async main(): Promise<void> {
|
||||
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<item_count;i++)
|
||||
{
|
||||
random_array.push(this.randomNumber(range_scale));
|
||||
}
|
||||
return random_array;
|
||||
}
|
||||
|
||||
private static randomNumber(range_scale: number): number {
|
||||
return Math.floor(Math.random() * (Math.pow(10, range_scale) - Math.pow(10, range_scale - 1))) + Math.pow(10, range_scale - 1);
|
||||
}
|
||||
//example
|
||||
// scale 1 returns a number from 0 to 9
|
||||
// scale 2 returns a number from 10 to 99
|
||||
// scale 3 returns a number from 100 to 999
|
||||
// ...
|
||||
// scale 64 returns a crash
|
||||
}
|
||||
|
||||
SqlCommands.main();
|
|
@ -144,6 +144,25 @@ class SqlSystem {
|
|||
if (conn) conn.release();
|
||||
}
|
||||
}
|
||||
|
||||
public static async Query(query:string,use_transaction:boolean = true) {
|
||||
let conn: mariadb.PoolConnection | undefined;
|
||||
try{
|
||||
conn = await this.pool.getConnection()
|
||||
if(use_transaction) await conn.beginTransaction();
|
||||
await conn.query(query);
|
||||
if(use_transaction) await conn.commit();
|
||||
}
|
||||
catch(error) {
|
||||
console.error("InitDB Error: ", error);
|
||||
if(use_transaction && conn) await conn.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<boolean|any> {
|
||||
let conn: mariadb.PoolConnection | undefined;
|
||||
|
|
Loading…
Reference in a new issue