Commit f5e149d0 authored by Ajit Deshmukh's avatar Ajit Deshmukh

Replace ManageSqlConf.java

parent d166049d
...@@ -328,8 +328,8 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -328,8 +328,8 @@ public class ManageSqlConf extends ActionHandlerEJB
if ("A".equalsIgnoreCase(execRights)) if ("A".equalsIgnoreCase(execRights))
{ {
BaseLogger.log("3", null, null, "multiple schema execution for provided enterprise:: oracleStatment::" + oracleStatment + " enterprise:: "+enterprise+ "]");//Added by Ashish.J BaseLogger.log("3", null, null, "multiple schema execution for provided enterprise:: oracleStatment::" + oracleStatment + " enterprise:: "+selectedEnterprise+ "]");//Added by Ashish.J
processSqlForEnterprise(con, enterprise, oracleStatment); processSqlForEnterprise(con, selectedEnterprise, oracleStatment);
continue; continue;
} }
...@@ -1317,7 +1317,7 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1317,7 +1317,7 @@ public class ManageSqlConf extends ActionHandlerEJB
public void processSqlForEnterprise(Connection conn, String enterpriseCode, String inputSql) throws Exception { public void processSqlForEnterprise(Connection conn, String enterpriseCode, String inputSql) throws Exception {
inputSql = injectSchemaPlaceholder(inputSql); inputSql = injectSchemaPlaceholder(inputSql);
System.out.println("inputSql :" + inputSql); System.out.println("inputSql ::::::::" + inputSql);
// Step 1: Fetch schemas // Step 1: Fetch schemas
String query = "SELECT DB_DEV, DB_MAIN, DB_READ, DB_PILOT FROM enterprise WHERE enterprise = ?"; String query = "SELECT DB_DEV, DB_MAIN, DB_READ, DB_PILOT FROM enterprise WHERE enterprise = ?";
...@@ -1344,12 +1344,12 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1344,12 +1344,12 @@ public class ManageSqlConf extends ActionHandlerEJB
schemaRules.put(dbRead, "SYN"); schemaRules.put(dbRead, "SYN");
schemaRules.put(dbPilot, "SYN"); schemaRules.put(dbPilot, "SYN");
boolean isCreateTable = inputSql.trim().toUpperCase().startsWith("CREATE TABLE"); String createObjectType = getCreateObjectType(inputSql);
boolean isCreateObject = createObjectType != null;
// Extract table name from CREATE TABLE SCHEMA.TABLE pattern String objectName = null;
String tableName = null; if (isCreateObject) {
if (isCreateTable) { objectName = extractObjectName(inputSql);
tableName = extractTableName(inputSql);
} }
// Step 3: Execute based on rule // Step 3: Execute based on rule
...@@ -1361,51 +1361,48 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1361,51 +1361,48 @@ public class ManageSqlConf extends ActionHandlerEJB
if (schemaName == null || schemaName.trim().isEmpty()) if (schemaName == null || schemaName.trim().isEmpty())
continue; continue;
if (isCreateTable) { if (isCreateObject) {
// ===========================
// SPECIAL LOGIC FOR CREATE TABLE
// ===========================
if (rule.equals("ALL")) { if (rule.equals("ALL")) {
// Execute CREATE TABLE normally for MAIN + DEV String finalSql = applySchema(inputSql, schemaName);
String finalSql = inputSql.replace("{SCHEMA}", schemaName); System.out.println("Executing for schema: " + schemaName);
System.out.println("Executing CREATE TABLE for schema: " + schemaName);
System.out.println("SQL: " + finalSql); System.out.println("SQL: " + finalSql);
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
stmt.execute(finalSql); stmt.execute(finalSql);
} }
} }
else if (rule.equals("SYN")) { else if (rule.equals("SYN")) {
// READ / PILOT special handling: System.out.println("Processing CREATE " + createObjectType + " special logic for schema: " + schemaName);
System.out.println("Processing CREATE TABLE special logic for schema: " + schemaName);
// 1. Create Synonym // 1. Create Synonym
String synonymSql = String synonymSql =
"CREATE SYNONYM " + schemaName + "." + tableName + "CREATE SYNONYM " + schemaName + "." + objectName +
" FOR " + dbMain + "." + tableName; " FOR " + dbMain + "." + objectName;
System.out.println("SQL: " + synonymSql); System.out.println("SQL: " + synonymSql);
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
stmt.execute(synonymSql); stmt.execute(synonymSql);
} }
// 2. Grant Privileges // 2. Grant privileges (only where applicable)
String grantSql = ""; String grantSql = null;
if (schemaName.equals(dbRead)) { if (schemaName.equals(dbRead)) {
grantSql = grantSql = "GRANT SELECT ON " + dbMain + "." + objectName + " TO " + dbRead;
"GRANT SELECT ON " + dbMain + "." + tableName + " TO " + dbRead;
} }
else if (schemaName.equals(dbPilot)) { else if (schemaName.equals(dbPilot)) {
grantSql = grantSql = "GRANT EXECUTE ON " + dbMain + "." + objectName + " TO " + dbPilot;
"GRANT INSERT, UPDATE, DELETE ON " + dbMain + "." + tableName + " TO " + dbPilot;
} }
if (grantSql != null) {
System.out.println("SQL: " + grantSql); System.out.println("SQL: " + grantSql);
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
stmt.execute(grantSql); stmt.execute(grantSql);
} }
} }
}
continue; // skip normal execution continue; // skip normal execution
} }
...@@ -1421,7 +1418,7 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1421,7 +1418,7 @@ public class ManageSqlConf extends ActionHandlerEJB
} }
} }
String finalSql = inputSql.replace("{SCHEMA}", schemaName); String finalSql = applySchema(inputSql, schemaName);
System.out.println("Executing for schema: " + schemaName); System.out.println("Executing for schema: " + schemaName);
System.out.println("SQL: " + finalSql); System.out.println("SQL: " + finalSql);
...@@ -1435,28 +1432,70 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1435,28 +1432,70 @@ public class ManageSqlConf extends ActionHandlerEJB
//Created a method to execute the SQL in the schemas stored in the Enterprise table for the provided enterprise value by Ajit on 03-DEC-25 -- END //Created a method to execute the SQL in the schemas stored in the Enterprise table for the provided enterprise value by Ajit on 03-DEC-25 -- END
//Created a method to fetch the table name from provided SQL string by Ajit on 03-DEC-25 -- START //Created a method to fetch the table name from provided SQL string by Ajit on 03-DEC-25 -- START
private String extractTableName(String sql) { private String extractObjectName(String sql) {
sql = sql.replace("\n", " ").replace("\t", " ");
sql = sql.toUpperCase(); if (sql == null) return null;
// Expected: CREATE TABLE {SCHEMA}.TABLE_NAME // Normalize
int idx = sql.indexOf("CREATE TABLE"); String s = sql.replace("\n", " ")
.replace("\t", " ")
.trim()
.toUpperCase();
// Remove multiple spaces
s = s.replaceAll("\\s+", " ");
String objectType = getCreateObjectType(s);
if (objectType == null) return null;
String keyword;
switch (objectType) {
case "TABLE":
keyword = "CREATE TABLE";
break;
case "SEQUENCE":
keyword = "CREATE SEQUENCE";
break;
case "TYPE":
keyword = "CREATE TYPE";
break;
case "FUNCTION":
keyword = "CREATE FUNCTION";
break;
case "PROCEDURE":
keyword = "CREATE PROCEDURE";
break;
case "PACKAGE BODY":
keyword = "CREATE PACKAGE BODY";
break;
case "PACKAGE":
keyword = "CREATE PACKAGE";
break;
case "VIEW":
keyword = "CREATE VIEW";
break;
default:
return null;
}
int idx = s.indexOf(keyword);
if (idx == -1) return null; if (idx == -1) return null;
String after = sql.substring(idx + "CREATE TABLE".length()).trim(); String after = s.substring(idx + keyword.length()).trim();
// Remove schema // Remove schema if present (SCHEMA.OBJECT)
if (after.contains(".")) { if (after.contains(".")) {
after = after.substring(after.indexOf(".") + 1).trim(); after = after.substring(after.indexOf(".") + 1);
} }
// Remove ( column definitions // Stop at first delimiter
if (after.contains("(")) { // (, AS, IS
after = after.substring(0, after.indexOf("(")).trim(); after = after.split("\\s|\\(|AS|IS", 2)[0].trim();
}
return after; return after;
} }
//Created a method to fetch the table name from provided SQL string by Ajit on 03-DEC-25 -- END //Created a method to fetch the table name from provided SQL string by Ajit on 03-DEC-25 -- END
//Created a method to add a placeholder before the table name which will be replaced by the schema name by Ajit on 03-DEC-25 -- START //Created a method to add a placeholder before the table name which will be replaced by the schema name by Ajit on 03-DEC-25 -- START
...@@ -1474,4 +1513,53 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1474,4 +1513,53 @@ public class ManageSqlConf extends ActionHandlerEJB
} }
//Created a method to add a placeholder before the table name which will be replaced by the schema name by Ajit on 03-DEC-25 -- END //Created a method to add a placeholder before the table name which will be replaced by the schema name by Ajit on 03-DEC-25 -- END
private String getCreateObjectType(String sql) {
String s = sql.trim().toUpperCase();
if (s.startsWith("CREATE TABLE")) return "TABLE";
if (s.startsWith("CREATE SEQUENCE")) return "SEQUENCE";
if (s.startsWith("CREATE TYPE")) return "TYPE";
if (s.startsWith("CREATE FUNCTION")) return "FUNCTION";
if (s.startsWith("CREATE PROCEDURE")) return "PROCEDURE";
if (s.startsWith("CREATE PACKAGE BODY")) return "PACKAGE BODY";
if (s.startsWith("CREATE PACKAGE")) return "PACKAGE";
if (s.startsWith("CREATE VIEW")) return "VIEW";
return null;
}
private String applySchema(String sql, String schema) {
String s = sql.trim();
// CREATE TABLE
if (s.toUpperCase().startsWith("CREATE TABLE")) {
return s.replaceFirst(
"(?i)CREATE\\s+TABLE\\s+",
"CREATE TABLE " + schema + "."
);
}
// CREATE VIEW
if (s.toUpperCase().startsWith("CREATE VIEW")) {
return s.replaceFirst(
"(?i)CREATE\\s+VIEW\\s+",
"CREATE VIEW " + schema + "."
);
}
// CREATE SEQUENCE
if (s.toUpperCase().startsWith("CREATE SEQUENCE")) {
return s.replaceFirst(
"(?i)CREATE\\s+SEQUENCE\\s+",
"CREATE SEQUENCE " + schema + "."
);
}
// fallback
return sql;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment