IDEMPIERE-4462 (#263)
Integrate patch from Paul Bowden (Adaxa) Co-authored-by: Paul Bowden <pbowden@adaxa.com>
This commit is contained in:
parent
703ce7bda8
commit
599e13dda3
|
@ -293,13 +293,13 @@ public abstract class Convert
|
||||||
* @param retVars
|
* @param retVars
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected String replaceQuotedStrings(String inputValue, Vector<String>retVars) {
|
protected String replaceQuotedStrings(String inputValue, Vector<String>retVars, String nonce) {
|
||||||
// save every value
|
// save every value
|
||||||
// Carlos Ruiz - globalqss - better matching regexp
|
// Carlos Ruiz - globalqss - better matching regexp
|
||||||
retVars.clear();
|
retVars.clear();
|
||||||
|
|
||||||
// First we need to replace double quotes to not be matched by regexp - Teo Sarca BF [3137355 ]
|
// First we need to replace double quotes to not be matched by regexp - Teo Sarca BF [3137355 ]
|
||||||
final String quoteMarker = "<--QUOTE"+System.currentTimeMillis()+"-->";
|
final String quoteMarker = "<--QUOTE"+nonce+"-->";
|
||||||
inputValue = inputValue.replace("''", quoteMarker);
|
inputValue = inputValue.replace("''", quoteMarker);
|
||||||
|
|
||||||
Pattern p = Pattern.compile("'[[^']*]*'");
|
Pattern p = Pattern.compile("'[[^']*]*'");
|
||||||
|
@ -309,7 +309,7 @@ public abstract class Convert
|
||||||
while (m.find()) {
|
while (m.find()) {
|
||||||
String var = inputValue.substring(m.start(), m.end()).replace(quoteMarker, "''"); // Put back quotes, if any
|
String var = inputValue.substring(m.start(), m.end()).replace(quoteMarker, "''"); // Put back quotes, if any
|
||||||
retVars.addElement(var);
|
retVars.addElement(var);
|
||||||
m.appendReplacement(retValue, "<--" + i + "-->");
|
m.appendReplacement(retValue, "<--QS" + i + "QS" + nonce + "-->");
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
m.appendTail(retValue);
|
m.appendTail(retValue);
|
||||||
|
@ -324,12 +324,12 @@ public abstract class Convert
|
||||||
* @param retVars
|
* @param retVars
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected String recoverQuotedStrings(String retValue, Vector<String>retVars) {
|
protected String recoverQuotedStrings(String retValue, Vector<String>retVars, String nonce) {
|
||||||
for (int i = 0; i < retVars.size(); i++) {
|
for (int i = 0; i < retVars.size(); i++) {
|
||||||
//hengsin, special character in replacement can cause exception
|
//hengsin, special character in replacement can cause exception
|
||||||
String replacement = (String) retVars.get(i);
|
String replacement = (String) retVars.get(i);
|
||||||
replacement = escapeQuotedString(replacement);
|
replacement = escapeQuotedString(replacement);
|
||||||
retValue = retValue.replace("<--" + i + "-->", replacement);
|
retValue = retValue.replace("<--QS" + i + "QS" + nonce + "-->", replacement);
|
||||||
}
|
}
|
||||||
return retValue;
|
return retValue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import java.util.Map;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@ -47,6 +48,8 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
|
||||||
|
|
||||||
private TreeMap<String,String> m_map;
|
private TreeMap<String,String> m_map;
|
||||||
|
|
||||||
|
private String sharedNonce = generateNonce();
|
||||||
|
|
||||||
/** Logger */
|
/** Logger */
|
||||||
private static final CLogger log = CLogger.getCLogger(Convert_PostgreSQL.class);
|
private static final CLogger log = CLogger.getCLogger(Convert_PostgreSQL.class);
|
||||||
|
|
||||||
|
@ -88,7 +91,15 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
|
||||||
/** 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 nonce = sharedNonce;
|
||||||
|
|
||||||
|
// check for collision with nonce
|
||||||
|
while ( sqlStatement.contains(nonce))
|
||||||
|
{
|
||||||
|
nonce = generateNonce();
|
||||||
|
}
|
||||||
|
|
||||||
|
String statement = replaceQuotedStrings(sqlStatement, retVars, nonce);
|
||||||
statement = convertWithConvertMap(statement);
|
statement = convertWithConvertMap(statement);
|
||||||
statement = convertSimilarTo(statement);
|
statement = convertSimilarTo(statement);
|
||||||
statement = DB_PostgreSQL.removeNativeKeyworkMarker(statement);
|
statement = DB_PostgreSQL.removeNativeKeyworkMarker(statement);
|
||||||
|
@ -106,7 +117,7 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
|
||||||
else if (isCreate && cmpString.indexOf(" VIEW ") != -1)
|
else if (isCreate && cmpString.indexOf(" VIEW ") != -1)
|
||||||
;
|
;
|
||||||
else if (cmpString.indexOf("ALTER TABLE") != -1) {
|
else if (cmpString.indexOf("ALTER TABLE") != -1) {
|
||||||
statement = recoverQuotedStrings(statement, retVars);
|
statement = recoverQuotedStrings(statement, retVars, nonce);
|
||||||
retVars.clear();
|
retVars.clear();
|
||||||
statement = convertDDL(convertComplexStatement(statement));
|
statement = convertDDL(convertComplexStatement(statement));
|
||||||
/*
|
/*
|
||||||
|
@ -124,7 +135,7 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
|
||||||
statement = convertComplexStatement(convertAlias(statement));
|
statement = convertComplexStatement(convertAlias(statement));
|
||||||
}
|
}
|
||||||
if (retVars.size() > 0)
|
if (retVars.size() > 0)
|
||||||
statement = recoverQuotedStrings(statement, retVars);
|
statement = recoverQuotedStrings(statement, retVars, nonce);
|
||||||
result.add(statement);
|
result.add(statement);
|
||||||
|
|
||||||
if ("true".equals(System.getProperty("org.idempiere.db.debug"))) {
|
if ("true".equals(System.getProperty("org.idempiere.db.debug"))) {
|
||||||
|
@ -176,6 +187,20 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
|
||||||
return "Y".equals(Env.getContext(Env.getCtx(), "P|IsUseSimilarTo"));
|
return "Y".equals(Env.getContext(Env.getCtx(), "P|IsUseSimilarTo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate fairly hard to guess numeric string
|
||||||
|
*/
|
||||||
|
private String generateNonce() {
|
||||||
|
|
||||||
|
String newNonce = Long.toString(ThreadLocalRandom.current()
|
||||||
|
.nextLong(100000000000000000L,
|
||||||
|
999999999999999999L)).intern();
|
||||||
|
|
||||||
|
sharedNonce = newNonce;
|
||||||
|
|
||||||
|
return newNonce;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String escapeQuotedString(String in)
|
protected String escapeQuotedString(String in)
|
||||||
{
|
{
|
||||||
|
@ -921,6 +946,9 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
|
||||||
}
|
}
|
||||||
if (token.startsWith("'") && token.endsWith("'"))
|
if (token.startsWith("'") && token.endsWith("'"))
|
||||||
return false;
|
return false;
|
||||||
|
// quoted string substitution marker
|
||||||
|
else if ( token.matches("QS\\d+QS\\d{18}") )
|
||||||
|
return false;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue