UNPKG

prg-class

Version:

Clases genéricas utilizadas por microservicios Programamos SPA.

186 lines (185 loc) 8.09 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DbExecuteMySql = void 0; const mysql = require("mysql2"); const F = require('../function/function'); class DbExecuteMySql { constructor(aoParamsConn) { this.moParamsConn = {}; this.moErr = { k: 0, i: 0, msg: '', imsg: '' }; this.moMetadata = {}; this.moErrInternal = { code: '', errno: 0, sql: 0, sqlMessage: '', sqlState: '' }; this.moParamsConn = aoParamsConn; } /** * Ejecuta procedimiento almacenado MYSQL. * @param asStoreName Nombre de procedimiento almacenado. * @param aoParam Arreglo con parámetros del procedimiento almacenado. */ StoreProc(asStoreName_1, aoParam_1) { return __awaiter(this, arguments, void 0, function* (asStoreName, aoParam, abAudit = false) { const lsErrC = 'No se pudo establecer conexión con la base de datos.'; const lsErrQ = 'No se pudo ejecutar comando SQL.'; this.showAudit(abAudit, 'StoreProc...aoParam', aoParam); const loConn = mysql.createConnection({ host: this.moParamsConn.host, database: this.moParamsConn.database, user: this.moParamsConn.user, password: this.moParamsConn.password, }); return new Promise((rs, rj) => { return loConn.connect((aoErrConn) => { if (aoErrConn) { this.showAudit(abAudit, '.......StoreProc...aoErrConn', aoErrConn); return rj(F.fnRetErrInt(lsErrC)); } ; let lsPrmName = ''; const loFld = []; const loPrm = []; if (aoParam && aoParam.length > 0) { aoParam.forEach((r) => { loPrm.push(r); loFld.push('?'); }); } if (loFld.length > 0) { lsPrmName = loFld.join(','); } const lsSql = `CALL ${asStoreName}(${lsPrmName})`; this.showAudit(abAudit, 'StoreProc...lsSql', lsSql); return loConn.query(lsSql, loPrm, (aoErrQuery, aoResult) => { this.showAudit(abAudit, 'StoreProc...loPrm', loPrm); this.showAudit(abAudit, 'StoreProc...aoErrQuery', aoErrQuery); this.showAudit(abAudit, 'StoreProc...aoResult', aoResult); if (aoErrQuery) { this.moErrInternal.code = aoErrQuery.code; this.moErrInternal.errno = aoErrQuery.errno; this.moErrInternal.sqlMessage = aoErrQuery.sqlMessage; this.moErrInternal.sqlState = aoErrQuery.sqlState; this.moErrInternal.sql = aoErrQuery.sql; // F.fnLogError(aoErrQuery); loConn.end(); return rj(F.fnRetErrInt(lsErrQ)); } ; loConn.end(); if (this.spHasError(aoResult)) { this.showAudit(abAudit, 'StoreProc...hasError', this.moErr); const lsErr = this.moErr.msg; if (this.moErr.ik === -1000003 || this.moErr.ik === -1000004) { this.showAudit(abAudit, 'StoreProc...error...1000003...o...1000004', this.moErr); return rj(F.fnRetErrBP(lsErr)); } else { return rj(F.fnRetErrInt(lsErr)); } } ; const loData = this.getResult(aoResult); this.showAudit(abAudit, 'StoreProc...getResult', loData); return rs(F.fnRetOk(loData)); }); }); }).catch((error) => { // F.fnLogError('Error al ejecutar procedimiento almacenado.'); // F.fnLogError(this.moErr.imsg); return error; }); }); } showAudit(abAudit, asTitle, aoValue) { if (abAudit) { this.developerMsg(asTitle, aoValue); } } developerMsg(asTitle, aoValue) { console.log(asTitle, aoValue); } getMetadata() { return this.moMetadata; } getError() { return this.moErr; } getErrorInternal() { return this.moErrInternal; } getResult(aoRet) { const liLen = aoRet.length; const loRet = []; if (liLen >= 2) { for (let i = 0; i < liLen - 1; i++) { loRet.push(aoRet[i]); } } return loRet; } spHasError(aoRet) { const lsErr = 'Error interno'; if (!aoRet) { this.moErr = { k: -1000001, i: 0, msg: lsErr, ik: -1000001, imsg: 'Error de ejecución.' }; return true; } const liLen = F.intval(aoRet.length); if (liLen <= 0) { this.moErr = { k: -1000002, i: 0, msg: lsErr, ik: -1000002, imsg: 'Objeto vacío.' }; return true; } if (aoRet[0].length <= 0) { this.moErr = { k: -1000003, i: 0, msg: lsErr, ik: -1000003, imsg: 'No existen coincidencias para los criterios solicitados.' }; return true; } if (liLen >= 2) { this.moMetadata = aoRet[liLen - 2][0]; } let liK = this.moMetadata ? F.intval(this.moMetadata.k) : -2000000; let liI = this.moMetadata ? F.intval(this.moMetadata.i) : 0; let lsMsg = this.moMetadata ? F.trim(this.moMetadata.msg) : lsErr; if (liK <= 0) { const R = aoRet; if (R && R.length > 0 && R[0].length > 0 && R[0][0].msg) { liK = R[0][0].msg; liI = R[0][0].i; lsMsg = R[0][0].msg; } if (F.trim(lsMsg) === '') { this.moErr = { k: liK, i: liI, msg: lsMsg, ik: 1000004, imsg: 'SP retorna error.' }; return true; } else { this.moErr = { k: -1000003, i: 0, msg: lsMsg, ik: -1000003, imsg: lsMsg }; return true; } } this.moErr = { k: liK, i: liI, msg: lsMsg, ik: 0, imsg: '' }; return false; } /** * 'Escapa' caracteres especiales. Por ejemplo: TEXTO, se convierte en 'TEXTO', etc. * @param asValue */ // eslint-disable-next-line class-methods-use-this fnEscape(asValue, abLike = false, abHalfLike = false) { if (abLike) { asValue = `%${F.trim(asValue)}%`; } // Prepara like... if (abHalfLike) { asValue = `${F.trim(asValue)}%`; } // Prepara semi-like... return mysql.escape(asValue); } getObjAud(asModule, asFunction, aoError, asSql = '') { return { class: asModule, function: asFunction, sql: asSql, error: aoError }; } } exports.DbExecuteMySql = DbExecuteMySql;