All files index.ts

57.71% Statements 86/149
51.11% Branches 23/45
54.16% Functions 26/48
55.35% Lines 62/112

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198  1x         1x     1x 1x 1x     1x 1x 1x 1x 1x 1x 1x       1x 1x     36x 36x 36x 36x 36x   36x 36x     36x 36x 36x 36x 36x 36x   36x 9x 1x   4x       36x 8x     4x       1x   1x     1x 1x       36x 1x 1x 1x             36x                         36x 5x 5x 4x 7x   6x 3x 2x 3x 2x   1x 1x                 36x                                 36x                               1x                                 1x                         36x     1x                           1x  
// Import interface
import { QueryType } from "./IDatabase";
import IConnectionConfig from "./IConfig";
import IDrivers from "./IDrivers";
 
// Import all drivers
import Drivers from './drivers'
 
// Import dependecies
const jdbc = require('jdbc');
const jinst = require('jdbc/lib/jinst');
const path = require('path');
 
// Connection Type
export enum ConnectionType {
    hive = 'hive', // using hive driver
    firebirdsql = 'firebirdsql', // using hive driver
    postgreSql = 'postgresql', // using postgre sql driver
    sqlite = 'sqlite', // using sqlite driver
    tibero = 'tibero', // using tibero driver
    custom = 'custom', // Connect any jdbc connection using custom driver
}
 
 
export default class JdbcDriver<T extends ConnectionType> extends Drivers<T> implements IDrivers {
    protected static connection: any = new Map();
    protected type: T;
    constructor(type: T, config: IConnectionConfig<T>) {
        super(type, config);
        this.type = type;
        if (!jinst.isJvmCreated()) {
            jinst.addOption('-Xrs');
            jinst.setupClasspath([path.join(__dirname, this.driverPath)]);
        }
        const connection = new jdbc(this.get_config())
        JdbcDriver.connection.set(this.type, connection)
    }
 
    public get_version = () => this.driver.version;
    public findAll = async (tableName: string) => await this.sql(`SELECT * FROM ${tableName}`)
    public count = async (tableName: any) => await this.sql(`SELECT COUNT(*) as total from ${tableName}`)
    public find = async (tableName: string, Iwhere: number | string = 1) => await this.sql(`SELECT * FROM ${tableName} WHERE ${where}`)
    public connection_count = () => JdbcDriver.connection.size;
    public connection_details = () => JdbcDriver.connection.entries();
 
    public get_columns = async (tableName: string) => {
        if (this.type === 'custom') {
            return 'This method is not supported for the custom drivers'
        } else {
            return await this.sql(this.get_query(tableName, QueryType.columns))
        }
    }
 
    public get_table_properties = async (tableName: string) => {
        if (this.type === 'custom') {
            return 'This method is not supported for the custom drivers'
        } else{
            return await this.sql(this.get_query(tableName, QueryType.describe))
        }
    }
 
    public async sql(sql: string): Promise<any> {
        try {
            const res = await this.executeQuery(sql);
            return res;
        } catch (err) {
            console.error('Error in sql method:', err);
            throw err;
        }
    }
 
    public ddl = async (sql: string) => {
        try {
            const res = this.executeUpdate(sql)
            return res
        } catch (err) {
            console.error('Error in ddl method:', err);
            throw err;
        }
    }
 
    protected close = async (connObj: any) => {
        try {
            const coon = JdbcDriver.connection.get(this.type)
            coon.release(connObj, (err: any) => {
                if (err) console.log('Connection relase issues::::')
                else console.log('Connection relase')
            })
        } catch (err) {
            console.error('Connection close error:::::', err)
            throw err;
        }
    }
 
    protected executeQuery = async (sql: any) => {
        return new Promise(async (resolve, reject) => {
            const stat: any = await this.createStatement()
            stat[0].executeQuery(sql, async (err: any, resultset: any) => {
                if (err) reject(err)
                else {
                    await resultset.toObjArray((err: any, rows: any) => {
                        if (err) reject(err)
                        else resolve(rows)
                        stat[0].close((err: any) => {
                            if (err) console.log('Statement closing issues...', err)
                            else {
                                console.log('Statement closed.')
                                this.close(stat[1])
                            }
                        })
                    })
                }
            })
        })
    }
 
    protected executeUpdate = async (sql: any) => {
        return new Promise(async (resolve, reject) => {
            const stat: any = await this.createStatement()
            stat[0].executeUpdate(sql, async (err: any, count: any) => {
                if (err) reject(err)
                else resolve(count)
                stat[0].close((err: any) => {
                    if (err) console.log('Statement closing issues::::')
                    else {
                        console.log('Statement closed')
                        this.close(stat[1])
                    }
                })
            })
        })
    }
 
    protected createStatement = async () => {
        return new Promise(async (resolve, reject) => {
            const connObj: any = await this.open()
            if (connObj) {
                console.log("Using connection: " + connObj.uuid);
                const conn = connObj.conn;
                conn.createStatement((err: any, statement: any) => {
                    if (err) reject(err)
                    else resolve([statement, connObj])
                })
            } else {
                reject('Connection object not found')
            }
        })
    }
 
    protected async open() {
        try {
            const connection = JdbcDriver.connection.get(this.type);
            if (Ithis.is_init(connection)) {
                return connection._reserved[0];
            } else {
                if (I!connection._pool.length) {
                    await this.init(connection);
                }
                return await this.reserveConnection(connection);
            }
        } catch (err) {
            console.error('Error opening connection:', err);
            throw err;
        }
    }
    
    private async reserveConnection(connection: any) {
        return new Promise((resolve, reject) => {
            connection.reserve((err: any, connObj: any) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(connObj);
                }
            });
        });
    }
    
 
    protected is_init = (conn: any) => {
        return conn._reserved.length
    }
protected async init(connection: any): Promise<void> {
    return new Promise((resolve, reject) => {
        connection.initialize((err: any) => {
            if (err) {
                console.error('Error initializing connection:', err);
                reject(err);
            } else {
                JdbcDriver.connection.set(this.type, connection);
                resolve();
            }
        });
    });
}
 
}