diff --git a/constants/constants.js b/constants/constants.js index c1d0a82..280731f 100644 --- a/constants/constants.js +++ b/constants/constants.js @@ -6,7 +6,7 @@ const ERROR_MESSAGE = { /** * @enum {string} */ -const TABLE_TYPE = { +const OBJECT_TYPE = { table: 'TABLE', view: 'VIEW', }; @@ -24,7 +24,7 @@ const CONSTRAINT_POSTFIX = { module.exports = { ERROR_MESSAGE, - TABLE_TYPE, + OBJECT_TYPE, INLINE_COMMENT, CONSTRAINT_POSTFIX, }; diff --git a/reverse_engineering/api.js b/reverse_engineering/api.js index 003f62d..5d53799 100644 --- a/reverse_engineering/api.js +++ b/reverse_engineering/api.js @@ -11,7 +11,7 @@ const { mapSeries } = require('async'); const { connectionHelper } = require('../shared/helpers/connectionHelper'); const { instanceHelper } = require('../shared/helpers/instanceHelper'); const { logHelper } = require('../shared/helpers/logHelper'); -const { TABLE_TYPE } = require('../constants/constants'); +const { OBJECT_TYPE } = require('../constants/constants'); const { nameHelper } = require('../shared/helpers/nameHelper'); const { testConnection } = require('../shared/api/testConnection'); @@ -83,7 +83,7 @@ const getDbCollectionsNames = async (connectionInfo, appLogger, callback, app) = const tableNames = await instanceHelper.getDatabasesWithTableNames({ connection, - tableType: TABLE_TYPE.table, + objectType: OBJECT_TYPE.table, includeSystemCollection: connectionInfo.includeSystemCollection, tableNameModifier: identity, }); @@ -92,7 +92,7 @@ const getDbCollectionsNames = async (connectionInfo, appLogger, callback, app) = const viewNames = await instanceHelper.getDatabasesWithTableNames({ connection, - tableType: TABLE_TYPE.view, + objectType: OBJECT_TYPE.view, includeSystemCollection: connectionInfo.includeSystemCollection, tableNameModifier: nameHelper.setViewSign, }); @@ -153,7 +153,7 @@ const getDbCollectionsData = async (connectionInfo, appLogger, callback, app) => connection, schemaName, tableName, - tableType: TABLE_TYPE.table, + objectType: OBJECT_TYPE.table, logger, }); @@ -185,7 +185,7 @@ const getDbCollectionsData = async (connectionInfo, appLogger, callback, app) => connection, schemaName, tableName: viewName, - tableType: TABLE_TYPE.view, + objectType: OBJECT_TYPE.view, logger, }); diff --git a/shared/helpers/instanceHelper.js b/shared/helpers/instanceHelper.js index 995b8db..65cf34c 100644 --- a/shared/helpers/instanceHelper.js +++ b/shared/helpers/instanceHelper.js @@ -4,7 +4,6 @@ * @typedef {import("../types").Logger} Logger */ -const { TABLE_TYPE } = require('../../constants/constants'); const { queryHelper } = require('./queryHelper'); /** @@ -32,11 +31,11 @@ const getSchemaNames = async ({ connection }) => { }; /** - * @param {{ connection: Connection, tableType: string, includeSystemCollection: boolean, tableNameModifier: (name: string) => string }} + * @param {{ connection: Connection, objectType: string, includeSystemCollection: boolean, tableNameModifier: (name: string) => string }} * @returns {Promise} */ -const getDatabasesWithTableNames = async ({ connection, tableType, includeSystemCollection, tableNameModifier }) => { - const query = queryHelper.getTableNamesQuery({ tableType, includeSystemCollection }); +const getDatabasesWithTableNames = async ({ connection, objectType, includeSystemCollection, tableNameModifier }) => { + const query = queryHelper.getTableNamesQuery({ objectType, includeSystemCollection }); const result = await connection.execute({ query }); return result.reduce((result, { SCHEMANAME, TABLENAME }) => { @@ -73,12 +72,21 @@ const getSchemaProperties = async ({ connection, schemaName, logger }) => { * @param {{ connection: Connection, schemaName: string, tableName: string, tableName: string, logger: Logger}} * @returns {Promise} */ -const getTableDdl = async ({ connection, schemaName, tableName, tableType, logger }) => { +const getTableDdl = async ({ connection, schemaName, tableName, objectType, logger }) => { try { - const generateQuery = queryHelper.getGenerateTableDdlQuery({ schemaName, tableName, tableType }); + const generateQuery = queryHelper.getGenerateTableDdlQuery({ schemaName, tableName, objectType }); + const opToken = await connection.execute({ query: generateQuery, callable: true }); - const selectQuery = queryHelper.getSelectTableDdlQuery({ opToken, tableType }); + + const selectQuery = queryHelper.getSelectTableDdlQuery({ + opToken, + schemaName, + objectName: tableName, + objectType, + }); + const ddlResult = await connection.execute({ query: selectQuery }); + const clearQuery = queryHelper.getClearTableDdlQuery(); await connection.execute({ query: clearQuery, callable: true, inparam: opToken }); diff --git a/shared/helpers/queryHelper.js b/shared/helpers/queryHelper.js index a65b788..b7de0a3 100644 --- a/shared/helpers/queryHelper.js +++ b/shared/helpers/queryHelper.js @@ -1,13 +1,13 @@ -const { TABLE_TYPE } = require('../../constants/constants'); +const { OBJECT_TYPE } = require('../../constants/constants'); /** - * @param {{ query: string }} + * @param {{ query: string }} params * @returns {string} */ const cleanUpQuery = ({ query = '' }) => query.replaceAll(/\s+/g, ' '); /** - * @param {{ query: string, schemaNameKeyword: string }} + * @param {{ query: string, schemaNameKeyword: string }} params * @returns {string} */ const getNonSystemSchemaWhereClause = ({ query, schemaNameKeyword }) => { @@ -43,7 +43,7 @@ const getSchemasQuery = () => { }; /** - * @param {{ schemaName: string }} + * @param {{ schemaName: string }} params * @returns {string} */ const getSchemaQuery = ({ schemaName }) => { @@ -51,11 +51,11 @@ const getSchemaQuery = ({ schemaName }) => { }; /** - * @param {{ tableType: string, includeSystemCollection: boolean }} + * @param {{ objectType: string, includeSystemCollection: boolean }} params * @returns {string} */ -const getTableNamesQuery = ({ tableType, includeSystemCollection }) => { - const baseQuery = `SELECT TABLE_SCHEM AS SCHEMANAME, TABLE_NAME AS TABLENAME FROM SYSIBM.SQLTABLES WHERE TABLE_TYPE = '${tableType}'`; +const getTableNamesQuery = ({ objectType, includeSystemCollection }) => { + const baseQuery = `SELECT TABLE_SCHEM AS SCHEMANAME, TABLE_NAME AS TABLENAME FROM SYSIBM.SQLTABLES WHERE TABLE_TYPE = '${objectType}'`; if (includeSystemCollection) { return baseQuery; @@ -67,27 +67,33 @@ const getTableNamesQuery = ({ tableType, includeSystemCollection }) => { }; /** - * @param {{ schemaName: string, tableName: string, tableType: string }} + * @param {{ schemaName: string, tableName: string, objectType: string }} params * @returns {string}; */ -const getGenerateTableDdlQuery = ({ schemaName, tableName, tableType }) => { - const tableArgument = tableType === TABLE_TYPE.table ? '-t' : '-v'; +const getGenerateTableDdlQuery = ({ schemaName, tableName, objectType }) => { + const objectArgument = objectType === OBJECT_TYPE.table ? '-t' : '-v'; - return `CALL SYSPROC.DB2LK_GENERATE_DDL('-a -e -z "${schemaName}" ${tableArgument} "${tableName}"', ?);`; + return `CALL SYSPROC.DB2LK_GENERATE_DDL('-a -e -z "${schemaName}" ${objectArgument} "${tableName}"', ?);`; }; /** - * @param {{ opToken: number, tableType: string }} + * @param {{ opToken: number, schemaName: string, objectName: string, objectType: string }} params * @returns {string} */ -const getSelectTableDdlQuery = ({ opToken, tableType }) => { - const objectTypeOperator = tableType === TABLE_TYPE.table ? '!=' : '='; +const getSelectTableDdlQuery = ({ opToken, schemaName, objectName, objectType }) => { + const predicate = + objectType === OBJECT_TYPE.view + ? `SQL_STMT LIKE 'CREATE%VIEW ${objectName}%' + OR SQL_STMT LIKE 'COMMENT ON TABLE ${objectName}%'` + : `SQL_STMT LIKE '%"${schemaName}"."${objectName}"%'`; + const query = ` - SELECT SQL_STMT - FROM SYSTOOLS.DB2LOOK_INFO - WHERE OP_TOKEN= ${opToken} - AND OBJ_TYPE ${objectTypeOperator} '${TABLE_TYPE.view}' - ORDER BY CREATION_TIME, OP_SEQUENCE;`; + SELECT SQL_STMT + FROM SYSTOOLS.DB2LOOK_INFO + WHERE OP_TOKEN = ${opToken} + AND ( ${predicate} ) + ORDER BY CREATION_TIME, OP_SEQUENCE + `; return cleanUpQuery({ query }); };