Commit dde99e52 authored by Ajit Deshmukh's avatar Ajit Deshmukh

Updated the condition for creating functions, procedures, views, and sequences.

Updated the code to resolve the issue of duplicate SQL log screen entries with the same schema name
parent b4daf2d8
...@@ -326,12 +326,70 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -326,12 +326,70 @@ 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(
processSqlForEnterprise(con, selectedEnterprise, oracleStatment, tranId, linNo, requestID, userId, chgTerm); "3",
continue; null,
null,
"Multiple schema execution :: SQL :: " + oracleStatment +
" :: enterprises :: [" + selectedEnterprise + "]"
);
// VERY IMPORTANT
con.setAutoCommit(false);
String[] enterpriseArr = selectedEnterprise.split(",");
for (String enterpriseForExec : enterpriseArr) {
String ent = enterpriseForExec.trim();
if (ent.isEmpty()) {
continue;
}
Savepoint entSavepoint = null;
try {
// ✅ ENTERPRISE-LEVEL SAVEPOINT
entSavepoint = con.setSavepoint("SP_" + ent);
processSqlForEnterprise(
con,
ent,
oracleStatment,
tranId,
linNo,
requestID,
userId,
chgTerm
);
// ✅ commit enterprise success
con.commit();
} catch (Exception e) {
// ❌ rollback ONLY this enterprise
if (entSavepoint != null) {
con.rollback(entSavepoint);
}
BaseLogger.log(
"1",
null,
null,
"Enterprise execution failed :: " + ent + " :: " + e.getMessage()
);
// continue next enterprise
}
} }
continue;
}
//if( oracleStatment.toUpperCase().startsWith("ALTER") || oracleStatment.toUpperCase().startsWith("INSERT INTO") || oracleStatment.toUpperCase().startsWith("UPDATE") || oracleStatment.toUpperCase().startsWith("DELETE")) //if( oracleStatment.toUpperCase().startsWith("ALTER") || oracleStatment.toUpperCase().startsWith("INSERT INTO") || oracleStatment.toUpperCase().startsWith("UPDATE") || oracleStatment.toUpperCase().startsWith("DELETE"))
...@@ -753,6 +811,8 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -753,6 +811,8 @@ public class ManageSqlConf extends ActionHandlerEJB
try try
{ {
System.out.println("inside try of method"); System.out.println("inside try of method");
System.out.println("Sql_log SQL :: "+Sql);
GenericUtility genericUtility = GenericUtility.getInstance(); GenericUtility genericUtility = GenericUtility.getInstance();
String Aformat= genericUtility.getApplDateFormat(); String Aformat= genericUtility.getApplDateFormat();
java.text.SimpleDateFormat dtf= new java.text.SimpleDateFormat(Aformat); java.text.SimpleDateFormat dtf= new java.text.SimpleDateFormat(Aformat);
...@@ -1317,7 +1377,7 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1317,7 +1377,7 @@ public class ManageSqlConf extends ActionHandlerEJB
public void processSqlForEnterprise(Connection conn, String enterpriseCode, String inputSql, String tranId, String linNo, String requestID, String userId, String chgTerm) 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("inputSqllklklk ::::::::" + inputSql); System.out.println("inputSqllklklk ::::::::>>>>>" + inputSql);
/* ------------------------------- /* -------------------------------
* Fetch schemas * Fetch schemas
...@@ -1370,7 +1430,6 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1370,7 +1430,6 @@ public class ManageSqlConf extends ActionHandlerEJB
if (rule.equals("ALL")) { if (rule.equals("ALL")) {
executeAndLog( executeAndLog(
conn,
schemaName, schemaName,
inputSql, inputSql,
tranId, tranId,
...@@ -1390,7 +1449,6 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1390,7 +1449,6 @@ public class ManageSqlConf extends ActionHandlerEJB
" FOR " + dbMain + "." + objectName; " FOR " + dbMain + "." + objectName;
executeAndLog( executeAndLog(
conn,
schemaName, schemaName,
synonymSql, synonymSql,
tranId, tranId,
...@@ -1413,7 +1471,6 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1413,7 +1471,6 @@ public class ManageSqlConf extends ActionHandlerEJB
if (grantSql != null) { if (grantSql != null) {
executeAndLog( executeAndLog(
conn,
schemaName, schemaName,
grantSql, grantSql,
tranId, tranId,
...@@ -1440,7 +1497,6 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1440,7 +1497,6 @@ public class ManageSqlConf extends ActionHandlerEJB
} }
executeAndLog( executeAndLog(
conn,
schemaName, schemaName,
inputSql, inputSql,
tranId, tranId,
...@@ -1450,7 +1506,7 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1450,7 +1506,7 @@ public class ManageSqlConf extends ActionHandlerEJB
userId, userId,
chgTerm, chgTerm,
dbMain, dbMain,
true false
); );
} }
...@@ -1461,7 +1517,6 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1461,7 +1517,6 @@ public class ManageSqlConf extends ActionHandlerEJB
private void executeAndLog( private void executeAndLog(
Connection conn,
String schemaName, String schemaName,
String inputSql, String inputSql,
String tranId, String tranId,
...@@ -1472,9 +1527,14 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1472,9 +1527,14 @@ public class ManageSqlConf extends ActionHandlerEJB
String chgTerm, String chgTerm,
String dbMainSchema, String dbMainSchema,
boolean rethrow boolean rethrow
) { ) throws Exception {
Connection conn = null;
ConnDriver connDriver = new ConnDriver();
conn = connDriver.getConnectDB(schemaName);
String finalSql = applySchema(inputSql, schemaName); String finalSql = applySchema(inputSql, schemaName);
System.out.println("finalSql:: "+finalSql);
System.out.println("schemaName:: "+schemaName);
int updCount = 0; int updCount = 0;
String error = null; String error = null;
...@@ -1482,6 +1542,7 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1482,6 +1542,7 @@ public class ManageSqlConf extends ActionHandlerEJB
updCount = stmt.executeUpdate(finalSql); updCount = stmt.executeUpdate(finalSql);
conn.commit();
logSqlExecution( logSqlExecution(
tranId, enterpriseCode, linNo, requestID, tranId, enterpriseCode, linNo, requestID,
finalSql, updCount, "C", userId, chgTerm, null,dbMainSchema finalSql, updCount, "C", userId, chgTerm, null,dbMainSchema
...@@ -1489,6 +1550,12 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1489,6 +1550,12 @@ public class ManageSqlConf extends ActionHandlerEJB
} catch (SQLException e) { } catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
error = e.getMessage(); error = e.getMessage();
logSqlExecution( logSqlExecution(
...@@ -1519,10 +1586,8 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1519,10 +1586,8 @@ public class ManageSqlConf extends ActionHandlerEJB
Connection logConn = null; Connection logConn = null;
try { try {
ConnDriver connDriver = new ConnDriver();
logConn = getConnection();
// ✅ Always log using MAIN / APP schema
logConn = connDriver.getConnectDB("APPVIS");
logConn.setAutoCommit(true); logConn.setAutoCommit(true);
insertIntoSqlExectionLog( insertIntoSqlExectionLog(
...@@ -1555,15 +1620,19 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1555,15 +1620,19 @@ public class ManageSqlConf extends ActionHandlerEJB
if (sql == null) return null; if (sql == null) return null;
// Normalize // Normalize whitespace
String s = sql.replace("\n", " ") String s = sql.replace("\n", " ")
.replace("\t", " ") .replace("\t", " ")
.trim() .trim()
.toUpperCase(); .toUpperCase();
// Remove multiple spaces
s = s.replaceAll("\\s+", " "); s = s.replaceAll("\\s+", " ");
// IMPORTANT: normalize CREATE OR REPLACE
if (s.startsWith("CREATE OR REPLACE")) {
s = s.replaceFirst("CREATE\\s+OR\\s+REPLACE\\s+", "CREATE ");
}
String objectType = getCreateObjectType(s); String objectType = getCreateObjectType(s);
if (objectType == null) return null; if (objectType == null) return null;
...@@ -1608,13 +1677,13 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1608,13 +1677,13 @@ public class ManageSqlConf extends ActionHandlerEJB
after = after.substring(after.indexOf(".") + 1); after = after.substring(after.indexOf(".") + 1);
} }
// Stop at first delimiter // Stop at first delimiter: space, (, AS, IS
// (, AS, IS
after = after.split("\\s|\\(|AS|IS", 2)[0].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
...@@ -1658,8 +1727,16 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1658,8 +1727,16 @@ public class ManageSqlConf extends ActionHandlerEJB
private String getCreateObjectType(String sql) { private String getCreateObjectType(String sql) {
if (sql == null) return null;
String s = sql.trim().toUpperCase(); String s = sql.trim().toUpperCase();
// 🔴 Normalize CREATE OR REPLACE
if (s.startsWith("CREATE OR REPLACE")) {
s = s.replaceFirst("CREATE\\s+OR\\s+REPLACE\\s+", "CREATE ");
}
if (s.startsWith("CREATE TABLE")) return "TABLE"; if (s.startsWith("CREATE TABLE")) return "TABLE";
if (s.startsWith("CREATE SEQUENCE")) return "SEQUENCE"; if (s.startsWith("CREATE SEQUENCE")) return "SEQUENCE";
if (s.startsWith("CREATE TYPE")) return "TYPE"; if (s.startsWith("CREATE TYPE")) return "TYPE";
...@@ -1671,6 +1748,8 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1671,6 +1748,8 @@ public class ManageSqlConf extends ActionHandlerEJB
return null; return null;
} }
private String applySchema(String sql, String schema) { private String applySchema(String sql, String schema) {
...@@ -1751,4 +1830,4 @@ public class ManageSqlConf extends ActionHandlerEJB ...@@ -1751,4 +1830,4 @@ public class ManageSqlConf extends ActionHandlerEJB
} }
\ 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