import DBAccess from './DBAccess' ; import DBAccessQueryResult from './DBAccessQueryResult' ; import DBVendors from './DBVendors' ; import { log } from './FsOps' ; import Conversion from './Conversion' ; import IDBAccessQueryParams from './IDBAccessQueryParams' ; /**
 * Returns the data pool table name.
 */ export const getDataPoolTableName = ( conversion ) : string => { return `"${ conversion . _schema }"."data_pool_${ conversion . _schema }${ conversion . _mySqlDbName }"` ; } ; /**
 * Creates the "{schema}"."data_pool_{self._schema + self._mySqlDbName}" temporary table.
 */ export const createDataPoolTable = async ( conversion ) : Promise < Conversion > => { const logTitle = 'DataPoolManager::createDataPoolTable' ; const table = getDataPoolTableName ( conversion ) ; const sql = `CREATE TABLE IF NOT EXISTS ${ table }("id" BIGSERIAL, "metadata" TEXT);` ; const params = { conversion : conversion , caller : logTitle , sql : sql , vendor : DBVendors . PG , processExitOnError : true , shouldReturnClient : false } ; await DBAccess . query ( params ) ; log ( conversion , `\t--[${ logTitle }] table ${ table } is created...` ) ; return conversion ; } ; /**
 * Drops the "{schema}"."data_pool_{self._schema + self._mySqlDbName}" temporary table.
 */ export const dropDataPoolTable = async ( conversion ) : Promise < Conversion > => { const logTitle = 'DataPoolManager::dropDataPoolTable' ; const table = getDataPoolTableName ( conversion ) ; const params = { conversion : conversion , caller : logTitle , sql : `DROP TABLE ${ table };` , vendor : DBVendors . PG , processExitOnError : false , shouldReturnClient : false } ; await DBAccess . query ( params ) ; log ( conversion , `\t--[${ logTitle }] table ${ table } is dropped...` ) ; return conversion ; } ; /**
 * Reads temporary table, and generates Data-pool.
 */ export const readDataPool = async ( conversion ) : Promise < Conversion > => { const logTitle = 'DataPoolManager::readDataPool' ; const table = getDataPoolTableName ( conversion ) ; const params = { conversion : conversion , caller : logTitle , sql : `SELECT id AS id, metadata AS metadata FROM ${ table };` , vendor : DBVendors . PG , processExitOnError : true , shouldReturnClient : false } ; const result = await DBAccess . query ( params ) ; result . data . rows . forEach ( ( row ) => { const obj = JSON . parse ( row . metadata ) ; obj . _id = row . id ; conversion . _dataPool . push ( obj ) ; } ) ; log ( conversion , `\t--[${ logTitle }] Data-Pool is loaded...` ) ; return conversion ; } ;