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
if ("A".equalsIgnoreCase(execRights))
{
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;
}
......@@ -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
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);
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 = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, enterpriseCode);
......@@ -1337,7 +1339,9 @@ public class ManageSqlConf extends ActionHandlerEJB
rs.close();
ps.close();
// Step 2: Schema execution rules
/* -------------------------------
* Schema execution rules
* ------------------------------- */
Map<String, String> schemaRules = new LinkedHashMap<>();
schemaRules.put(dbMain, "ALL");
schemaRules.put(dbDev, "ALL");
......@@ -1345,92 +1349,207 @@ public class ManageSqlConf extends ActionHandlerEJB
schemaRules.put(dbPilot, "SYN");
String createObjectType = getCreateObjectType(inputSql);
boolean isCreateObject = createObjectType != null;
boolean isCreateObject = createObjectType != null;
String objectName = isCreateObject ? extractObjectName(inputSql) : null;
String objectName = null;
if (isCreateObject) {
objectName = extractObjectName(inputSql);
}
// Step 3: Execute based on rule
/* -------------------------------
* Execute per schema
* ------------------------------- */
for (Map.Entry<String, String> entry : schemaRules.entrySet()) {
String schemaName = entry.getKey();
String rule = entry.getValue();
String rule = entry.getValue();
if (schemaName == null || schemaName.trim().isEmpty())
continue;
/* =====================================
* CREATE / DDL FLOW
* ===================================== */
if (isCreateObject) {
if (rule.equals("ALL")) {
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);
}
}
executeAndLog(
conn,
schemaName,
inputSql,
tranId,
enterpriseCode,
linNo,
requestID,
userId,
chgTerm,
dbMain,
true
);
}
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")) {
// Only allow CREATE SYNONYM
boolean isSynonymSql = inputSql.trim().toUpperCase().startsWith("CREATE SYNONYM");
if (!isSynonymSql) {
System.out.println("Skipping SQL for schema (only SYN allowed): " + schemaName);
continue;
}
System.out.println("Skipping SQL for schema (only SYN allowed): " + schemaName);
continue;
}
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);
}
executeAndLog(
conn,
schemaName,
inputSql,
tranId,
enterpriseCode,
linNo,
requestID,
userId,
chgTerm,
dbMain,
true
);
}
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
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
private String extractObjectName(String sql) {
......@@ -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
public static String injectSchemaPlaceholder(String sql) {
String trimmed = sql.trim().toUpperCase();
// For CREATE SYNONYM → It already contains schema explicitly
if (trimmed.startsWith("CREATE SYNONYM")) {
return sql; // Do NOT modify
String s = sql.trim();
// Skip CREATE SYNONYM
if (s.toUpperCase().startsWith("CREATE SYNONYM")) {
return sql;
}
// Patterns: UPDATE table, DELETE FROM table, INSERT INTO table
String regex = "(UPDATE|INTO|FROM)\\s+([A-Za-z0-9_]+)";
return sql.replaceAll(regex, "$1 {SCHEMA}.$2");
// INSERT INTO table
s = s.replaceAll(
"(?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
......@@ -1533,33 +1676,79 @@ public class ManageSqlConf extends ActionHandlerEJB
String s = sql.trim();
// Replace placeholder first (DML)
if (s.contains("{SCHEMA}")) {
s = s.replace("{SCHEMA}", schema);
}
// CREATE TABLE
if (s.toUpperCase().startsWith("CREATE TABLE")) {
if (s.matches("(?is)^CREATE\\s+TABLE\\s+.*")) {
return s.replaceFirst(
"(?i)CREATE\\s+TABLE\\s+",
"(?is)CREATE\\s+TABLE\\s+",
"CREATE TABLE " + schema + "."
);
}
// CREATE VIEW
if (s.toUpperCase().startsWith("CREATE VIEW")) {
if (s.matches("(?is)^CREATE\\s+(OR\\s+REPLACE\\s+)?VIEW\\s+.*")) {
return s.replaceFirst(
"(?i)CREATE\\s+VIEW\\s+",
"CREATE VIEW " + schema + "."
"(?is)CREATE\\s+(OR\\s+REPLACE\\s+)?VIEW\\s+",
"CREATE OR REPLACE VIEW " + schema + "."
);
}
// CREATE SEQUENCE
if (s.toUpperCase().startsWith("CREATE SEQUENCE")) {
if (s.matches("(?is)^CREATE\\s+SEQUENCE\\s+.*")) {
return s.replaceFirst(
"(?i)CREATE\\s+SEQUENCE\\s+",
"(?is)CREATE\\s+SEQUENCE\\s+",
"CREATE SEQUENCE " + schema + "."
);
}
// fallback
return sql;
// CREATE FUNCTION
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