IDEMPIERE-4182 Add Ini property to turn off Oracle to PostgreSQL

translation
This commit is contained in:
hengsin 2020-03-02 21:20:50 +08:00
parent a2ade6340a
commit 41711e3437
5 changed files with 116 additions and 26 deletions

View File

@ -352,5 +352,13 @@ public interface AdempiereDatabase
* @return subset sql clause * @return subset sql clause
*/ */
public String intersectClauseForCSV(String columnName, String csv); public String intersectClauseForCSV(String columnName, String csv);
/**
*
* @return true if using native dialect, false if using oracle dialect
*/
public default boolean isNativeMode() {
return true;
}
} // AdempiereDatabase } // AdempiereDatabase

View File

@ -438,13 +438,7 @@ public abstract class Convert
public synchronized static void logMigrationScript(String oraStatement, String pgStatement) { public synchronized static void logMigrationScript(String oraStatement, String pgStatement) {
// Check AdempiereSys // Check AdempiereSys
// check property Log migration script // check property Log migration script
boolean logMigrationScript = false; boolean logMigrationScript = isLogMigrationScript();
if (Ini.isClient()) {
logMigrationScript = Ini.isPropertyBool(Ini.P_LOGMIGRATIONSCRIPT);
} else {
String sysProperty = Env.getCtx().getProperty("LogMigrationScript", "N");
logMigrationScript = "y".equalsIgnoreCase(sysProperty) || "true".equalsIgnoreCase(sysProperty);
}
if (logMigrationScript) { if (logMigrationScript) {
if (dontLog(oraStatement)) if (dontLog(oraStatement))
return; return;
@ -480,6 +474,20 @@ public abstract class Convert
} }
} }
/**
* @return true if it is in log migration script mode
*/
public static boolean isLogMigrationScript() {
boolean logMigrationScript = false;
if (Ini.isClient()) {
logMigrationScript = Ini.isPropertyBool(Ini.P_LOGMIGRATIONSCRIPT);
} else {
String sysProperty = Env.getCtx().getProperty("LogMigrationScript", "N");
logMigrationScript = "y".equalsIgnoreCase(sysProperty) || "true".equalsIgnoreCase(sysProperty);
}
return logMigrationScript;
}
private static String [] dontLogTables = new String[] { private static String [] dontLogTables = new String[] {
"AD_ACCESSLOG", "AD_ACCESSLOG",

View File

@ -37,6 +37,7 @@ import java.util.logging.Logger;
import org.adempiere.base.Service; import org.adempiere.base.Service;
import org.compiere.Adempiere; import org.compiere.Adempiere;
import org.compiere.db.AdempiereDatabase;
import org.compiere.db.CConnection; import org.compiere.db.CConnection;
import org.compiere.model.MClient; import org.compiere.model.MClient;
import org.idempiere.distributed.IClusterMember; import org.idempiere.distributed.IClusterMember;
@ -680,9 +681,17 @@ public class CLogMgt
private static String getDatabaseInfo() private static String getDatabaseInfo()
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(CConnection.get().getDbHost()).append(" : ") sb.append(CConnection.get().getDbHost()).append(":")
.append(CConnection.get().getDbPort()).append(" / ") .append(CConnection.get().getDbPort()).append("/")
.append(CConnection.get().getDbName()); .append(CConnection.get().getDbName());
AdempiereDatabase db = DB.getDatabase();
sb.append(" (").append(db.getName());
if (!DB.isOracle())
{
sb.append(", ").append(db.isNativeMode() ? "Native Dialect" : "Oracle Dialect");
}
sb.append(")");
// Connection Manager // Connection Manager
if (CConnection.get().isViaFirewall()) if (CConnection.get().isViaFirewall())
sb.append(getMsg("via")).append(" ") sb.append(getMsg("via")).append(" ")

View File

@ -73,8 +73,21 @@ import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DB_PostgreSQL implements AdempiereDatabase public class DB_PostgreSQL implements AdempiereDatabase
{ {
private static final String POOL_PROPERTIES = "pool.properties"; private static final String P_POSTGRE_SQL_NATIVE = "PostgreSQLNative";
private static final String POOL_PROPERTIES = "pool.properties";
private static Boolean sysNative = null;
static
{
String property = System.getProperty(P_POSTGRE_SQL_NATIVE);
if (!Util.isEmpty(property, true) )
{
sysNative = "Y".equalsIgnoreCase(property);
}
}
public Convert getConvert() { public Convert getConvert() {
return m_convert; return m_convert;
} }
@ -116,7 +129,7 @@ public class DB_PostgreSQL implements AdempiereDatabase
private static int m_maxbusyconnections = 0; private static int m_maxbusyconnections = 0;
public static final String NATIVE_MARKER = "NATIVE_"+Database.DB_POSTGRESQL+"_KEYWORK"; private static final String NATIVE_MARKER = "NATIVE_"+Database.DB_POSTGRESQL+"_KEYWORK";
private CCache<String, String> convertCache = new CCache<String, String>(null, "DB_PostgreSQL_Convert_Cache", 1000, 60, false); private CCache<String, String> convertCache = new CCache<String, String>(null, "DB_PostgreSQL_Convert_Cache", 1000, 60, false);
@ -331,14 +344,17 @@ public class DB_PostgreSQL implements AdempiereDatabase
*/ */
public String convertStatement (String oraStatement) public String convertStatement (String oraStatement)
{ {
String cache = convertCache.get(oraStatement); if (!isNativeMode())
if (cache != null) { {
Convert.logMigrationScript(oraStatement, cache); String cache = convertCache.get(oraStatement);
if ("true".equals(System.getProperty("org.idempiere.db.postgresql.debug"))) { if (cache != null) {
// log.warning("Oracle -> " + oraStatement); Convert.logMigrationScript(oraStatement, cache);
log.warning("Pgsql -> " + cache); if ("true".equals(System.getProperty("org.idempiere.db.postgresql.debug"))) {
// log.warning("Oracle -> " + oraStatement);
log.warning("Pgsql -> " + cache);
}
return cache;
} }
return cache;
} }
String retValue[] = m_convert.convert(oraStatement); String retValue[] = m_convert.convert(oraStatement);
@ -359,7 +375,8 @@ public class DB_PostgreSQL implements AdempiereDatabase
} }
//end vpj-cd 24/06/2005 e-evolution //end vpj-cd 24/06/2005 e-evolution
convertCache.put(oraStatement, retValue[0]); if (!isNativeMode())
convertCache.put(oraStatement, retValue[0]);
// Diagnostics (show changed, but not if AD_Error // Diagnostics (show changed, but not if AD_Error
if (log.isLoggable(Level.FINE)) if (log.isLoggable(Level.FINE))
@ -372,7 +389,7 @@ public class DB_PostgreSQL implements AdempiereDatabase
} }
//end vpj-cd 24/06/2005 e-evolution //end vpj-cd 24/06/2005 e-evolution
// //
Convert.logMigrationScript(oraStatement, retValue[0]); Convert.logMigrationScript(oraStatement, retValue[0]);
return retValue[0]; return retValue[0];
} // convertStatement } // convertStatement
@ -1171,4 +1188,43 @@ public class DB_PostgreSQL implements AdempiereDatabase
return builder.toString(); return builder.toString();
} }
@Override
public boolean isNativeMode() {
return isUseNativeDialect();
}
/**
* @return true if it is using native dialect
*/
public final static boolean isUseNativeDialect() {
if (Convert.isLogMigrationScript())
return false;
else if (sysNative != null)
return sysNative;
else
return Ini.isPropertyBool(P_POSTGRE_SQL_NATIVE);
}
/**
*
* @param keyword
* @return if not using native dialect, return native_marker + keyword
*/
public final static String markNativeKeyword(String keyword) {
if (isUseNativeDialect())
return keyword;
else
return NATIVE_MARKER + keyword;
}
/**
*
* @param statement
* @return statement after the removal of native keyword marker
*/
public final static String removeNativeKeyworkMarker(String statement) {
return statement.replace(DB_PostgreSQL.NATIVE_MARKER, "");
}
} // DB_PostgreSQL } // DB_PostgreSQL

View File

@ -51,6 +51,7 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
private static final CLogger log = CLogger.getCLogger(Convert_PostgreSQL.class); private static final CLogger log = CLogger.getCLogger(Convert_PostgreSQL.class);
private final static Pattern likePattern = Pattern.compile("\\bLIKE\\b", REGEX_FLAGS);
/** /**
* Is Oracle DB * Is Oracle DB
@ -75,13 +76,19 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
*/ */
protected ArrayList<String> convertStatement(String sqlStatement) { protected ArrayList<String> convertStatement(String sqlStatement) {
ArrayList<String> result = new ArrayList<String>(); ArrayList<String> result = new ArrayList<String>();
if (DB_PostgreSQL.isUseNativeDialect()) {
sqlStatement = convertSimilarTo(sqlStatement);
result.add(sqlStatement);
return result;
}
/** Vector to save previous values of quoted strings **/ /** Vector to save previous values of quoted strings **/
Vector<String> retVars = new Vector<String>(); Vector<String> retVars = new Vector<String>();
String statement = replaceQuotedStrings(sqlStatement, retVars); String statement = replaceQuotedStrings(sqlStatement, retVars);
statement = convertWithConvertMap(statement); statement = convertWithConvertMap(statement);
statement = convertSimilarTo(statement); statement = convertSimilarTo(statement);
statement = statement.replace(DB_PostgreSQL.NATIVE_MARKER, ""); statement = DB_PostgreSQL.removeNativeKeyworkMarker(statement);
String cmpString = statement.toUpperCase(); String cmpString = statement.toUpperCase();
boolean isCreate = cmpString.startsWith("CREATE "); boolean isCreate = cmpString.startsWith("CREATE ");
@ -126,16 +133,14 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
private String convertSimilarTo(String statement) { private String convertSimilarTo(String statement) {
String retValue = statement; String retValue = statement;
boolean useSimilarTo = "Y".equals(Env.getContext(Env.getCtx(), "P|IsUseSimilarTo")); boolean useSimilarTo = isUseSimilarTo();
if (useSimilarTo) { if (useSimilarTo) {
String regex = "\\bLIKE\\b";
String replacement = "SIMILAR TO"; String replacement = "SIMILAR TO";
try { try {
Pattern p = Pattern.compile(regex, REGEX_FLAGS); Matcher m = likePattern.matcher(retValue);
Matcher m = p.matcher(retValue);
retValue = m.replaceAll(replacement); retValue = m.replaceAll(replacement);
} catch (Exception e) { } catch (Exception e) {
String error = "Error expression: " + regex + " - " + e; String error = "Error expression: " + likePattern.pattern() + " - " + e;
log.info(error); log.info(error);
m_conversionError = error; m_conversionError = error;
} }
@ -143,6 +148,10 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
return retValue; return retValue;
} }
private boolean isUseSimilarTo() {
return "Y".equals(Env.getContext(Env.getCtx(), "P|IsUseSimilarTo"));
}
@Override @Override
protected String escapeQuotedString(String in) protected String escapeQuotedString(String in)
{ {