Commit f5e149d0 authored by Ajit Deshmukh's avatar Ajit Deshmukh

Replace ManageSqlConf.java

parent d166049d
......@@ -328,8 +328,8 @@ public class ManageSqlConf extends ActionHandlerEJB
if ("A".equalsIgnoreCase(execRights))
{
BaseLogger.log("3", null, null, "multiple schema execution for provided enterprise:: oracleStatment::" + oracleStatment + " enterprise:: "+enterprise+ "]");//Added by Ashish.J
processSqlForEnterprise(con, enterprise, oracleStatment);
BaseLogger.log("3", null, null, "multiple schema execution for provided enterprise:: oracleStatment::" + oracleStatment + " enterprise:: "+selectedEnterprise+ "]");//Added by Ashish.J
processSqlForEnterprise(con, selectedEnterprise, oracleStatment);
continue;
}
......@@ -1317,7 +1317,7 @@ public class ManageSqlConf extends ActionHandlerEJB
public void processSqlForEnterprise(Connection conn, String enterpriseCode, String inputSql) throws Exception {
inputSql = injectSchemaPlaceholder(inputSql);
System.out.println("inputSql :" + inputSql);
System.out.println("inputSql ::::::::" + inputSql);
// Step 1: Fetch schemas
String query = "SELECT DB_DEV, DB_MAIN, DB_READ, DB_PILOT FROM enterprise WHERE enterprise = ?";
......@@ -1344,12 +1344,12 @@ public class ManageSqlConf extends ActionHandlerEJB
schemaRules.put(dbRead, "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 tableName = null;
if (isCreateTable) {
tableName = extractTableName(inputSql);
String objectName = null;
if (isCreateObject) {
objectName = extractObjectName(inputSql);
}
// Step 3: Execute based on rule
......@@ -1361,51 +1361,48 @@ public class ManageSqlConf extends ActionHandlerEJB
if (schemaName == null || schemaName.trim().isEmpty())
continue;
if (isCreateTable) {
// ===========================
// SPECIAL LOGIC FOR CREATE TABLE
// ===========================
if (isCreateObject) {
if (rule.equals("ALL")) {
// Execute CREATE TABLE normally for MAIN + DEV
String finalSql = inputSql.replace("{SCHEMA}", schemaName);
System.out.println("Executing CREATE TABLE for schema: " + schemaName);
String finalSql = applySchema(inputSql, schemaName);
System.out.println("Executing for schema: " + schemaName);
System.out.println("SQL: " + finalSql);
try (Statement stmt = conn.createStatement()) {
stmt.execute(finalSql);
}
}
else if (rule.equals("SYN")) {
// READ / PILOT special handling:
System.out.println("Processing CREATE TABLE special logic for schema: " + schemaName);
System.out.println("Processing CREATE " + createObjectType + " special logic for schema: " + schemaName);
// 1. Create Synonym
String synonymSql =
"CREATE SYNONYM " + schemaName + "." + tableName +
" FOR " + dbMain + "." + tableName;
"CREATE SYNONYM " + schemaName + "." + objectName +
" FOR " + dbMain + "." + objectName;
System.out.println("SQL: " + synonymSql);
try (Statement stmt = conn.createStatement()) {
stmt.execute(synonymSql);
}
// 2. Grant Privileges
String grantSql = "";
// 2. Grant privileges (only where applicable)
String grantSql = null;
if (schemaName.equals(dbRead)) {
grantSql =
"GRANT SELECT ON " + dbMain + "." + tableName + " TO " + dbRead;
grantSql = "GRANT SELECT ON " + dbMain + "." + objectName + " TO " + dbRead;
}
else if (schemaName.equals(dbPilot)) {
grantSql =
"GRANT INSERT, UPDATE, DELETE ON " + dbMain + "." + tableName + " TO " + dbPilot;
grantSql = "GRANT EXECUTE ON " + dbMain + "." + objectName + " TO " + dbPilot;
}
if (grantSql != null) {
System.out.println("SQL: " + grantSql);
try (Statement stmt = conn.createStatement()) {
stmt.execute(grantSql);
}
}
}
continue; // skip normal execution
}
......@@ -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("SQL: " + finalSql);
......@@ -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 fetch the table name from provided SQL string by Ajit on 03-DEC-25 -- START
private String extractTableName(String sql) {
sql = sql.replace("\n", " ").replace("\t", " ");
sql = sql.toUpperCase();
// Expected: CREATE TABLE {SCHEMA}.TABLE_NAME
int idx = sql.indexOf("CREATE TABLE");
private String extractObjectName(String sql) {
if (sql == null) return null;
// Normalize
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;
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(".")) {
after = after.substring(after.indexOf(".") + 1).trim();
after = after.substring(after.indexOf(".") + 1);
}
// Remove ( column definitions
if (after.contains("(")) {
after = after.substring(0, after.indexOf("(")).trim();
}
// Stop at first delimiter
// (, AS, IS
after = after.split("\\s|\\(|AS|IS", 2)[0].trim();
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 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
}
//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