UNPKG

@egi/smart-db

Version:

Unified Smart DB Access

1 lines 3.55 kB
import fs from"fs";import process from"node:process";import oracleDb from"oracledb";import{take}from"rxjs";import{OracleCatModel}from"../models/oracle-cat-model";import{OracleUserTabColumnsViewModel}from"../models/oracle-user-tab-columns-view-model";import{SmartDb}from"../smart-db";import{SmartErrorLocation}from"../smart-error";export class SmartDbOracle extends SmartDb{constructor(t,e){if(!SmartDbOracle.libInit){let t;"darwin"===process.platform&&(t=process.env.ORACLE_HOME),t&&fs.existsSync(t)&&oracleDb.initOracleClient({libDir:t}),SmartDbOracle.libInit=!0}"string"==typeof t?super(t,e?.noDbLogging):super(null),this.reconnect(t,e)}close(){return new Promise(((t,e)=>{try{this.smartDbLog.setDb(null),this.db.close((e=>{this.isReady=!1,e?(this.lastError=e,t(!1)):(this._onReady.next(!1),t(!0))}))}catch(t){this.lastError=t,e(t)}}))}commit(){return this.db.commit()}exec(t){return new Promise(((e,r)=>{try{this.db.execute(t,(t=>{t?(this.lastError=t,r(t)):e()}))}catch(t){this.lastError=t,r(t)}}))}exists(t,e,r){return new Promise(((r,s)=>{try{const o="string"==typeof t?t:t.getTableName();this.getFirst(OracleCatModel,{name:o.toUpperCase(),type:e?e.toUpperCase():void 0}).then((t=>{r(!!t)})).catch((t=>{s(t)}))}catch(t){this.lastError=t,s(t)}}))}getDatabaseType(){return"oracle"}getTableInfo(t){return new Promise((async(e,r)=>{await this.getAll(OracleUserTabColumnsViewModel,{tableName:t.toUpperCase()}).then((s=>{if(!1!==s){let r=[];r=s.map((t=>({cid:t.columnId,name:t.columnName,type:t.dataType,notNull:"Y"!=t.nullable,defaultValue:t.defaultValue,isPk:"P"==t.constraintType}))),e({name:t,fields:r})}else r(this.getLastError())})).catch((t=>{this.lastError=t,r(t)}))}))}hasConcurrentTransactions(){return!0}reconnect(t,e){t?this.connectorOrDb=t:t=this.connectorOrDb,e?this.options=e:e=this.options,"string"==typeof t?oracleDb.getConnection(Object.assign({connectString:t},e??this.options)).then((t=>{this.db=t,e&&e.onReady&&this.onReady.pipe(take(1)).subscribe((r=>{r&&e.onReady(t)})),this.exec("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'").then((t=>{this.exec("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY:MM:DD HH24:MI:SS.FF'").then((t=>{this.isReady=!0})).catch((t=>{throw t}))})).catch((t=>{throw t}))})).catch((t=>{e&&e.onReady&&e.onReady(null,t),this.options.silent||this.smartDbLog.logError(SmartErrorLocation.Database,"reconnect","unable to prepare database connection",t),this.isReady=null})):this.db=t}rollback(){return this.db.rollback()}statementRun(t){return new Promise(((e,r)=>{try{this.db.execute(this.replacePlaceholders(t.sql),t.values,((t,s)=>{t?(this.lastError=t,r(t)):this.commit().then((()=>{e({changes:s.rowsAffected,affected:s.rowsAffected,lastId:null})})).catch((t=>{r(t)}))}))}catch(t){this.lastError=t,r(t)}}))}statementGet(t){return new Promise(((e,r)=>{try{this.db.execute(this.replacePlaceholders(this.replacePlaceholders(t.sql)),t.values,{maxRows:1,outFormat:oracleDb.OUT_FORMAT_OBJECT},((t,s)=>{if(t)this.lastError=t,r(t);else{const t=s.rows[0];let r=null;if(t){r={};Object.keys(t).forEach((e=>{r[e.toLowerCase()]=t[e]}))}e(r)}}))}catch(t){this.lastError=t,r(t)}}))}statementGetAll(t){return new Promise(((e,r)=>{try{this.db.execute(this.replacePlaceholders(t.sql),t.values,{maxRows:0,outFormat:oracleDb.OUT_FORMAT_OBJECT}).then((t=>{e(t.rows)})).catch((t=>{this.lastError=t,r(t)}))}catch(t){this.lastError=t,r(t)}}))}replacePlaceholders(t){let e=1,r=!1;for(let s=0;s<t.length;s++)"'"==t[s]?r=!r:r||"?"!=t[s]||(t=t.substring(0,s)+":"+e+++t.substring(s+1));return t}}SmartDbOracle.libInit=!1;