我有数百个数据库查询,如下面的格式
select VERSION, INSTALL_TIME from M_DATABASE_HISTORY
我使用下面的正则表达式来获取列名,(它基于(,)逗号标记SELECT和FROM之间的字符串)
String[] columnNames= query.split(" [fF][rR][oO][mM] ")[0]
.split("[ ]*[sS][eE][lL][eE][cC][tT] ")[1]
.split("[ ]*,[ ]*");
此代码适用于上述查询。
然而,我在使用列名中的函数(如下面的MAP)时获取列名时遇到问题,当有4列时它返回8列名称
select A.SYSTEM_ID, DATABASE_NAME, HOST, MAP(VALID, 'TRUE', 'Active' , 'FALSE' , 'Inactive') AS Current_Status from M_LICENSE A, M_DATABASE B where A.SYSTEM_ID = B.SYSTEM_ID
输出我正在
0.]A.SYSTEM_ID
1.]DATABASE_NAME
2.]HOST
3.]MAP(VALID
4.]'TRUE'
5.]'Active'
6.]'FALSE'
7.]'Inactive') AS Current_Status
预期输出
0.]A.SYSTEM_ID
1.]DATABASE_NAME
2.]HOST
3.]MAP(VALID,'TRUE','Active','FALSE','Inactive') AS Current_Status
OR
3.] Current_Status
有人可以帮我为此建立一个正则表达式吗? 或者欢迎任何更好的方法。
提前致谢。
答案 0 :(得分:0)
我最终编写了一个函数来验证我的Columnnames函数名称。如果我的columnName包含'('括号,我会在下一列中显示,直到找到')'支架
代码段:
public static void main(String[] args) throws IOException {
String[] queries = {
"select A.SYSTEM_ID, DATABASE_NAME, HOST, START_TIME, VERSION, HARDWARE_KEY, "
+ "INSTALL_NO, SYSTEM_NO, PRODUCT_NAME, PRODUCT_LIMIT, PRODUCT_USAGE, START_DATE, EXPIRATION_DATE, "
+ "LAST_SUCCESSFUL_CHECK, MAP(PERMANENT, 'TRUE', 'Yes' , 'FALSE' , 'No') PERMANENT, "
+ "MAP(VALID, 'TRUE', 'Active' , 'FALSE' , 'Inactive') AS Current_Status from M_LICENSE A, "
+ "M_DATABASE B where A.SYSTEM_ID = B.SYSTEM_ID",
"SELECT 'USER' TYPE, TABLE_TYPE, count(1) VAL FROM SYS.M_TABLES WHERE SCHEMA_NAME IN (SELECT SCHEMA_NAME from SYS.SCHEMAS WHERE SCHEMA_OWNER NOT IN ('SYS','_SYS_REPO','SYSTEM','_SYS_AFL','SAPHSA','SAPHEA','_SYS_EPM','_SYS_STATISTICS')) GROUP BY TABLE_TYPE UNION SELECT 'USER' TYPE, 'TOTAL', count(1) VAL FROM SYS.M_TABLES WHERE SCHEMA_NAME IN (SELECT SCHEMA_NAME from SYS.SCHEMAS WHERE SCHEMA_OWNER NOT IN ('SYS','_SYS_REPO','SYSTEM','_SYS_AFL','SAPHSA','SAPHEA','_SYS_EPM','_SYS_STATISTICS')) UNION SELECT 'SYSTEM' TYPE, TABLE_TYPE, count(1) VAL FROM SYS.M_TABLES WHERE SCHEMA_NAME IN (SELECT SCHEMA_NAME from SYS.SCHEMAS WHERE SCHEMA_OWNER IN ('SYS','_SYS_REPO','SYSTEM','_SYS_AFL','SAPHSA','SAPHEA','_SYS_EPM','_SYS_STATISTICS')) GROUP BY TABLE_TYPE UNION SELECT 'SYSTEM' TYPE, 'TOTAL', count(1) VAL FROM SYS.M_TABLES WHERE SCHEMA_NAME IN (SELECT SCHEMA_NAME from SYS.SCHEMAS WHERE SCHEMA_OWNER IN ('SYS','_SYS_REPO','SYSTEM','_SYS_AFL','SAPHSA','SAPHEA','_SYS_EPM','_SYS_STATISTICS'))" };
for (int x = 0; x < queries.length; x++) {
String query = queries[x];
String[] fields = query.split(" [fF][rR][oO][mM] ")[0].split("[ ]*[sS][eE][lL][eE][cC][tT] ")[1]
.split("[ ]*,[ ]*");
fields = validateColumnNames(fields);
for (int my = 0; my < fields.length; my++) {
System.out.println(my + ".]" + fields[my]);
}
}
}
//validator for column Names to check function. If my columnName contain a
// '(' bracket I appeneded the next columns until i found a ')' bracket.
private static String[] validateColumnNames(String[] fields) {
ArrayList<String> result = new ArrayList<>();
boolean enter = false;
String appendField = "";
for (int i = 0; i < fields.length; i++) {
String onefield = fields[i];
if (!enter) {
if (onefield.contains("(")) {
appendField = onefield;
enter = true;
} else {
result.add(onefield);
}
} else {
if (onefield.contains(")")) {
appendField = appendField + "," + onefield;
result.add(appendField);
enter = false;
appendField = "";
} else {
appendField = appendField + "," + onefield;
}
}
}
String[] validColNames = new String[result.size()];
validColNames = result.toArray(validColNames);
return validColNames;
}