-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryHelper.js
More file actions
119 lines (100 loc) · 3.51 KB
/
queryHelper.js
File metadata and controls
119 lines (100 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const { TABLE_TYPE, PERSENT } = require('../../constants/constants');
/**
* @param {{ query: string }}
* @returns {string}
*/
const cleanUpQuery = ({ query = '' }) => query.replaceAll(/\s+/g, ' ');
/**
* @param {{ query: string, schemaNameKeyword: string }}
* @returns {string}
*/
const getNonSystemSchemaWhereClause = ({ query, schemaNameKeyword }) => {
// On Windows (cmd.exe), environment variables can be referenced using syntax like %PATH%.
// When a command contains such patterns, cmd.exe automatically replaces them with the corresponding environment variable values.
// To prevent this automatic substitution, a placeholder string (PERSENT) is used here instead,
// which will later be replaced with the % symbol inside the Db2Client Java client.
const whereClause = `
WHERE ${schemaNameKeyword} NOT LIKE 'SYS${PERSENT}'
AND ${schemaNameKeyword} NOT LIKE '${PERSENT}SYSCAT${PERSENT}'
AND ${schemaNameKeyword} NOT LIKE '${PERSENT}SYSIBM${PERSENT}'
AND ${schemaNameKeyword} NOT LIKE '${PERSENT}SYSSTAT${PERSENT}'
AND ${schemaNameKeyword} NOT LIKE '${PERSENT}SYSTOOLS${PERSENT}'
AND ${schemaNameKeyword} NOT LIKE '${PERSENT}NULLID${PERSENT}'
AND ${schemaNameKeyword} NOT LIKE '${PERSENT}SQLJ${PERSENT}';`;
const clause = query.includes('WHERE') ? whereClause.replace('WHERE', 'AND') : whereClause;
return query + clause;
};
/**
* @returns {string}
*/
const getDbVersionQuery = () => {
return 'SELECT SERVICE_LEVEL FROM SYSIBMADM.ENV_INST_INFO';
};
/**
* @returns {string}
*/
const getSchemasQuery = () => {
const baseQuery = 'SELECT SCHEMANAME FROM SYSCAT.SCHEMATA';
const query = getNonSystemSchemaWhereClause({ query: baseQuery, schemaNameKeyword: 'SCHEMANAME' });
return cleanUpQuery({ query });
};
/**
* @param {{ schemaName: string }}
* @returns {string}
*/
const getSchemaQuery = ({ schemaName }) => {
return `SELECT * FROM SYSCAT.SCHEMATA WHERE SCHEMANAME = '${schemaName}'`;
};
/**
* @param {{ tableType: string, includeSystemCollection: boolean }}
* @returns {string}
*/
const getTableNamesQuery = ({ tableType, includeSystemCollection }) => {
const baseQuery = `SELECT TABLE_SCHEM AS SCHEMANAME, TABLE_NAME AS TABLENAME FROM SYSIBM.SQLTABLES WHERE TABLE_TYPE = '${tableType}'`;
if (includeSystemCollection) {
return baseQuery;
}
const query = getNonSystemSchemaWhereClause({ query: baseQuery, schemaNameKeyword: 'TABLE_SCHEM' });
return cleanUpQuery({ query });
};
/**
* @param {{ schemaName: string, tableName: string, tableType: string }}
* @returns {string};
*/
const getGenerateTableDdlQuery = ({ schemaName, tableName, tableType }) => {
const tableArgument = tableType === TABLE_TYPE.table ? '-t' : '-v';
return `CALL SYSPROC.DB2LK_GENERATE_DDL('-a -e -z "${schemaName}" ${tableArgument} "${tableName}"', ?);`;
};
/**
* @param {{ opToken: number, tableType: string }}
* @returns {string}
*/
const getSelectTableDdlQuery = ({ opToken, tableType }) => {
const objectTypeOperator = tableType === TABLE_TYPE.table ? '!=' : '=';
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;`;
return cleanUpQuery({ query });
};
/**
* @returns {string}
*/
const getClearTableDdlQuery = () => {
return 'CALL SYSPROC.DB2LK_CLEAN_TABLE(?);';
};
const queryHelper = {
cleanUpQuery,
getDbVersionQuery,
getSchemaQuery,
getSchemasQuery,
getTableNamesQuery,
getGenerateTableDdlQuery,
getSelectTableDdlQuery,
getClearTableDdlQuery,
};
module.exports = {
queryHelper,
};