Commit 30b90110 authored by Ajit Deshmukh's avatar Ajit Deshmukh

Updated the code for SQL Log entry for Admin-level transaction approval.

Updated the case for create function query.
parent f5e149d0
...@@ -329,7 +329,7 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -329,7 +329,7 @@ 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:: "+selectedEnterprise+ "]");//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, selectedEnterprise, oracleStatment); processSqlForEnterprise(con, selectedEnterprise, oracleStatment, tranId, linNo, requestID, userId, chgTerm);
continue; continue;
} }
...@@ -1314,12 +1314,14 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1314,12 +1314,14 @@ 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 -- START //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 -- START
public void processSqlForEnterprise(Connection conn, String enterpriseCode, String inputSql) throws Exception { public void processSqlForEnterprise(Connection conn, String enterpriseCode, String inputSql, String tranId, String linNo, String requestID, String userId, String chgTerm) throws Exception {
inputSql = injectSchemaPlaceholder(inputSql); inputSql = injectSchemaPlaceholder(inputSql);
System.out.println("inputSql ::::::::" + inputSql); System.out.println("inputSqllklklk ::::::::" + inputSql);
// Step 1: Fetch schemas /* -------------------------------
* 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 = ?";
PreparedStatement ps = conn.prepareStatement(query); PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, enterpriseCode); ps.setString(1, enterpriseCode);
...@@ -1337,7 +1339,9 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1337,7 +1339,9 @@ public class ManageSqlConf extends ActionHandlerEJB
rs.close(); rs.close();
ps.close(); ps.close();
// Step 2: Schema execution rules /* -------------------------------
* Schema execution rules
* ------------------------------- */
Map<String, String> schemaRules = new LinkedHashMap<>(); Map<String, String> schemaRules = new LinkedHashMap<>();
schemaRules.put(dbMain, "ALL"); schemaRules.put(dbMain, "ALL");
schemaRules.put(dbDev, "ALL"); schemaRules.put(dbDev, "ALL");
...@@ -1345,92 +1349,207 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1345,92 +1349,207 @@ public class ManageSqlConf extends ActionHandlerEJB
schemaRules.put(dbPilot, "SYN"); schemaRules.put(dbPilot, "SYN");
String createObjectType = getCreateObjectType(inputSql); String createObjectType = getCreateObjectType(inputSql);
boolean isCreateObject = createObjectType != null; boolean isCreateObject = createObjectType != null;
String objectName = isCreateObject ? extractObjectName(inputSql) : null;
String objectName = null; /* -------------------------------
if (isCreateObject) { * Execute per schema
objectName = extractObjectName(inputSql); * ------------------------------- */
}
// Step 3: Execute based on rule
for (Map.Entry<String, String> entry : schemaRules.entrySet()) { for (Map.Entry<String, String> entry : schemaRules.entrySet()) {
String schemaName = entry.getKey(); String schemaName = entry.getKey();
String rule = entry.getValue(); String rule = entry.getValue();
if (schemaName == null || schemaName.trim().isEmpty()) if (schemaName == null || schemaName.trim().isEmpty())
continue; continue;
/* =====================================
* CREATE / DDL FLOW
* ===================================== */
if (isCreateObject) { if (isCreateObject) {
if (rule.equals("ALL")) { if (rule.equals("ALL")) {
String finalSql = applySchema(inputSql, schemaName); executeAndLog(
System.out.println("Executing for schema: " + schemaName); conn,
System.out.println("SQL: " + finalSql); schemaName,
try (Statement stmt = conn.createStatement()) { inputSql,
stmt.execute(finalSql); tranId,
} enterpriseCode,
} linNo,
requestID,
userId,
chgTerm,
dbMain,
true
);
}
else if (rule.equals("SYN")) { else if (rule.equals("SYN")) {
System.out.println("Processing CREATE " + createObjectType + " special logic for schema: " + schemaName);
// 1. Create Synonym
String synonymSql =
"CREATE SYNONYM " + schemaName + "." + objectName +
" FOR " + dbMain + "." + objectName;
System.out.println("SQL: " + synonymSql);
try (Statement stmt = conn.createStatement()) {
stmt.execute(synonymSql);
}
// 2. Grant privileges (only where applicable)
String grantSql = null;
if (schemaName.equals(dbRead)) {
grantSql = "GRANT SELECT ON " + dbMain + "." + objectName + " TO " + dbRead;
}
else if (schemaName.equals(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);
}
}
String synonymSql =
"CREATE SYNONYM " + schemaName + "." + objectName +
" FOR " + dbMain + "." + objectName;
executeAndLog(
conn,
schemaName,
synonymSql,
tranId,
enterpriseCode,
linNo,
requestID,
userId,
chgTerm,
dbMain,
false
);
String grantSql = null;
if (schemaName.equals(dbRead)) {
grantSql = "GRANT SELECT ON " + dbMain + "." + objectName + " TO " + dbRead;
} else if (schemaName.equals(dbPilot)) {
grantSql = "GRANT EXECUTE ON " + dbMain + "." + objectName + " TO " + dbPilot;
}
if (grantSql != null) {
executeAndLog(
conn,
schemaName,
grantSql,
tranId,
enterpriseCode,
linNo,
requestID,
userId,
chgTerm,
dbMain,
false
);
}
} }
continue; // skip normal execution continue; // skip DML flow
} }
// =========================== /* =====================================
// Normal Flow (non CREATE TABLE) * INSERT / UPDATE / DELETE FLOW
// =========================== * ===================================== */
if (rule.equals("SYN")) { if (rule.equals("SYN")) {
// Only allow CREATE SYNONYM System.out.println("Skipping SQL for schema (only SYN allowed): " + schemaName);
boolean isSynonymSql = inputSql.trim().toUpperCase().startsWith("CREATE SYNONYM"); continue;
if (!isSynonymSql) {
System.out.println("Skipping SQL for schema (only SYN allowed): " + schemaName);
continue;
}
} }
String finalSql = applySchema(inputSql, schemaName); executeAndLog(
System.out.println("Executing for schema: " + schemaName); conn,
System.out.println("SQL: " + finalSql); schemaName,
inputSql,
try (Statement stmt = conn.createStatement()) { tranId,
stmt.execute(finalSql); enterpriseCode,
} linNo,
requestID,
userId,
chgTerm,
dbMain,
true
);
} }
System.out.println("Execution completed successfully."); System.out.println("Execution completed successfully.");
} }
//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
private void executeAndLog(
Connection conn,
String schemaName,
String inputSql,
String tranId,
String enterpriseCode,
String linNo,
String requestID,
String userId,
String chgTerm,
String dbMainSchema,
boolean rethrow
) {
String finalSql = applySchema(inputSql, schemaName);
int updCount = 0;
String error = null;
try (Statement stmt = conn.createStatement()) {
updCount = stmt.executeUpdate(finalSql);
logSqlExecution(
tranId, enterpriseCode, linNo, requestID,
finalSql, updCount, "C", userId, chgTerm, null,dbMainSchema
);
} catch (SQLException e) {
error = e.getMessage();
logSqlExecution(
tranId, enterpriseCode, linNo, requestID,
finalSql, updCount, "E", userId, chgTerm, error,dbMainSchema
);
if (rethrow) {
throw new RuntimeException(e);
}
}
}
private void logSqlExecution(
String tranId,
String enterpriseCode,
String linNo,
String requestID,
String sql,
int updCount,
String status,
String userId,
String chgTerm,
String error,
String schemaName
) {
Connection logConn = null;
try {
ConnDriver connDriver = new ConnDriver();
// ✅ Always log using MAIN / APP schema
logConn = connDriver.getConnectDB("APPVIS");
logConn.setAutoCommit(true);
insertIntoSqlExectionLog(
logConn,
tranId,
enterpriseCode,
linNo,
requestID,
sql,
updCount,
status,
userId,
chgTerm,
error
);
} catch (Exception ex) {
System.out.println("SQL LOGGING FAILED: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (logConn != null) logConn.close();
} catch (Exception ignore) {}
}
}
//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 extractObjectName(String sql) { private String extractObjectName(String sql) {
...@@ -1500,17 +1619,41 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1500,17 +1619,41 @@ 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 -- 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
public static String injectSchemaPlaceholder(String sql) { public static String injectSchemaPlaceholder(String sql) {
String trimmed = sql.trim().toUpperCase();
// For CREATE SYNONYM → It already contains schema explicitly String s = sql.trim();
if (trimmed.startsWith("CREATE SYNONYM")) {
return sql; // Do NOT modify // Skip CREATE SYNONYM
if (s.toUpperCase().startsWith("CREATE SYNONYM")) {
return sql;
} }
// Patterns: UPDATE table, DELETE FROM table, INSERT INTO table // INSERT INTO table
String regex = "(UPDATE|INTO|FROM)\\s+([A-Za-z0-9_]+)"; s = s.replaceAll(
return sql.replaceAll(regex, "$1 {SCHEMA}.$2"); "(?i)INSERT\\s+INTO\\s+(?!\\{SCHEMA\\}|[A-Z0-9_]+\\.)",
"INSERT INTO {SCHEMA}."
);
// UPDATE table
s = s.replaceAll(
"(?i)UPDATE\\s+(?!\\{SCHEMA\\}|[A-Z0-9_]+\\.)",
"UPDATE {SCHEMA}."
);
// DELETE FROM table
s = s.replaceAll(
"(?i)DELETE\\s+FROM\\s+(?!\\{SCHEMA\\}|[A-Z0-9_]+\\.)",
"DELETE FROM {SCHEMA}."
);
// SELECT ... FROM table
s = s.replaceAll(
"(?i)FROM\\s+(?!\\{SCHEMA\\}|[A-Z0-9_]+\\.)",
"FROM {SCHEMA}."
);
return s;
} }
//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
...@@ -1533,33 +1676,79 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1533,33 +1676,79 @@ public class ManageSqlConf extends ActionHandlerEJB
String s = sql.trim(); String s = sql.trim();
// Replace placeholder first (DML)
if (s.contains("{SCHEMA}")) {
s = s.replace("{SCHEMA}", schema);
}
// CREATE TABLE // CREATE TABLE
if (s.toUpperCase().startsWith("CREATE TABLE")) { if (s.matches("(?is)^CREATE\\s+TABLE\\s+.*")) {
return s.replaceFirst( return s.replaceFirst(
"(?i)CREATE\\s+TABLE\\s+", "(?is)CREATE\\s+TABLE\\s+",
"CREATE TABLE " + schema + "." "CREATE TABLE " + schema + "."
); );
} }
// CREATE VIEW // CREATE VIEW
if (s.toUpperCase().startsWith("CREATE VIEW")) { if (s.matches("(?is)^CREATE\\s+(OR\\s+REPLACE\\s+)?VIEW\\s+.*")) {
return s.replaceFirst( return s.replaceFirst(
"(?i)CREATE\\s+VIEW\\s+", "(?is)CREATE\\s+(OR\\s+REPLACE\\s+)?VIEW\\s+",
"CREATE VIEW " + schema + "." "CREATE OR REPLACE VIEW " + schema + "."
); );
} }
// CREATE SEQUENCE // CREATE SEQUENCE
if (s.toUpperCase().startsWith("CREATE SEQUENCE")) { if (s.matches("(?is)^CREATE\\s+SEQUENCE\\s+.*")) {
return s.replaceFirst( return s.replaceFirst(
"(?i)CREATE\\s+SEQUENCE\\s+", "(?is)CREATE\\s+SEQUENCE\\s+",
"CREATE SEQUENCE " + schema + "." "CREATE SEQUENCE " + schema + "."
); );
} }
// fallback // CREATE FUNCTION
return sql; if (s.matches("(?is)^CREATE\\s+(OR\\s+REPLACE\\s+)?FUNCTION\\s+.*")) {
return s.replaceFirst(
"(?is)CREATE\\s+(OR\\s+REPLACE\\s+)?FUNCTION\\s+",
"CREATE OR REPLACE FUNCTION " + schema + "."
);
}
// CREATE PROCEDURE
if (s.matches("(?is)^CREATE\\s+(OR\\s+REPLACE\\s+)?PROCEDURE\\s+.*")) {
return s.replaceFirst(
"(?is)CREATE\\s+(OR\\s+REPLACE\\s+)?PROCEDURE\\s+",
"CREATE OR REPLACE PROCEDURE " + schema + "."
);
}
// CREATE PACKAGE
if (s.matches("(?is)^CREATE\\s+(OR\\s+REPLACE\\s+)?PACKAGE\\s+.*")) {
return s.replaceFirst(
"(?is)CREATE\\s+(OR\\s+REPLACE\\s+)?PACKAGE\\s+",
"CREATE OR REPLACE PACKAGE " + schema + "."
);
}
// CREATE PACKAGE BODY
if (s.matches("(?is)^CREATE\\s+(OR\\s+REPLACE\\s+)?PACKAGE\\s+BODY\\s+.*")) {
return s.replaceFirst(
"(?is)CREATE\\s+(OR\\s+REPLACE\\s+)?PACKAGE\\s+BODY\\s+",
"CREATE OR REPLACE PACKAGE BODY " + schema + "."
);
}
// CREATE TYPE
if (s.matches("(?is)^CREATE\\s+(OR\\s+REPLACE\\s+)?TYPE\\s+.*")) {
return s.replaceFirst(
"(?is)CREATE\\s+(OR\\s+REPLACE\\s+)?TYPE\\s+",
"CREATE OR REPLACE TYPE " + schema + "."
);
}
return s;
} }
}
}
\ No newline at end of file
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