import { log } from './FsOps' ; import Conversion from './Conversion' ; import DBAccess from './DBAccess' ; import DBVendors from './DBVendors' ; import DBAccessQueryResult from './DBAccessQueryResult' ; import * as extraConfigProcessor from './ExtraConfigProcessor' ; import IDBAccessQueryParams from './IDBAccessQueryParams' ; /**
 * Escapes quotes inside given string.
 */ const escapeQuotes = ( str ) : string => { const regexp = new RegExp ( `'` , 'g' ) ; return str . replace ( regexp , `''` ) ; } ; /**
 * Creates table comments.
 */ const processTableComments = async ( conversion , tableName ) : Promise < void > => { const logTitle = 'CommentsProcessor::processTableComments' ; const sqlSelectComment = `SELECT table_comment AS table_comment FROM information_schema.tables 
        WHERE table_schema = '${ conversion . _mySqlDbName }' 
        AND table_name = '${ extraConfigProcessor . getTableName ( conversion , tableName , true ) }';` ; const params = { conversion : conversion , caller : logTitle , sql : sqlSelectComment , vendor : DBVendors . MYSQL , processExitOnError : false , shouldReturnClient : false } ; const resultSelectComment = await DBAccess . query ( params ) ; if ( resultSelectComment . error ) { return ; } const comment = escapeQuotes ( resultSelectComment . data [ 0 ] . table_comment ) ; params . sql = `COMMENT ON TABLE "${ conversion . _schema }"."${ tableName }" IS '${ comment }';` ; params . vendor = DBVendors . PG ; const createCommentResult = await DBAccess . query ( params ) ; if ( createCommentResult . error ) { return ; } const successMsg = `\t--[${ logTitle }] Successfully set comment for table "${ conversion . _schema }"."${ tableName }"` ; log ( conversion , successMsg , conversion . _dicTables [ tableName ] . tableLogPath ) ; } ; /**
 * Creates columns comments.
 */ const processColumnsComments = async ( conversion , tableName ) : Promise < void > => { const logTitle = 'CommentsProcessor::processColumnsComments' ; const originalTableName = extraConfigProcessor . getTableName ( conversion , tableName , true ) ; const commentPromises = conversion . _dicTables [ tableName ] . arrTableColumns . map ( async ( column ) => { if ( column . Comment === '' ) { return ; } const columnName = extraConfigProcessor . getColumnName ( conversion , originalTableName , column . Field , false ) ; const comment = escapeQuotes ( column . Comment ) ; const params = { conversion : conversion , caller : logTitle , sql : `COMMENT ON COLUMN "${ conversion . _schema }"."${ tableName }"."${ columnName }" IS '${ comment }';` , vendor : DBVendors . PG , processExitOnError : false , shouldReturnClient : false } ; const createCommentResult = await DBAccess . query ( params ) ; if ( createCommentResult . error ) { return ; } const successMsg = `\t--[${ logTitle }] Set comment for "${ conversion . _schema }"."${ tableName }" column: "${ columnName }"...` ; log ( conversion , successMsg , conversion . _dicTables [ tableName ] . tableLogPath ) ; } ) ; await Promise . all ( commentPromises ) ; } ; /**
 * Migrates comments.
 */ export default async ( conversion , tableName ) : Promise < void > => { const logTitle = 'CommentsProcessor::default' ; const msg = `\t--[${ logTitle }] Creates comments for table "${ conversion . _schema }"."${ tableName }"...` ; log ( conversion , msg , conversion . _dicTables [ tableName ] . tableLogPath ) ; await Promise . all ( [ processTableComments ( conversion , tableName ) , processColumnsComments ( conversion , tableName ) ] ) ; } ;