IDEMPIERE-4179 Add quote column name support to AdempiereDatabase

interface
This commit is contained in:
hengsin 2020-03-02 23:02:00 +08:00
parent 41711e3437
commit 8445b0cb3d
5 changed files with 42 additions and 5 deletions

View File

@ -353,6 +353,15 @@ public interface AdempiereDatabase
*/
public String intersectClauseForCSV(String columnName, String csv);
/**
* Quote column name if necessary (usually to avoid conflict with reserved keywords)
* @param columnName
* @return columnName or quoted columnName
*/
public default String quoteColumnName(String columnName) {
return columnName;
}
/**
*
* @return true if using native dialect, false if using oracle dialect

View File

@ -21,6 +21,7 @@ import java.util.List;
import java.util.Properties;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.DB;
public class MViewComponent extends X_AD_ViewComponent {
@ -143,9 +144,9 @@ public class MViewComponent extends X_AD_ViewComponent {
colSQL = "NULL";
}
sb.append(colSQL);
sb.append(DB.getDatabase().quoteColumnName(colSQL));
if (!colName.equals("*"))
sb.append(" AS ").append(colName);
sb.append(" AS ").append(DB.getDatabase().quoteColumnName(colName));
}
sb.append(" ").append(getFromClause());

View File

@ -1323,7 +1323,12 @@ public abstract class PO
{
if (i != 0)
sql.append(",");
sql.append(p_info.getColumnSQL(i)); // Normal and Virtual Column
String columnSQL = p_info.getColumnSQL(i);
if (!p_info.isVirtualColumn(i))
{
columnSQL = DB.getDatabase().quoteColumnName(columnSQL);
}
sql.append(columnSQL); // Normal and Virtual Column
}
sql.append(" FROM ").append(p_info.getTableName())
.append(" WHERE ")
@ -2545,7 +2550,7 @@ public abstract class PO
if (changes)
sql.append(", ");
changes = true;
sql.append(columnName).append("=");
sql.append(DB.getDatabase().quoteColumnName(columnName)).append("=");
if (withValues)
{
@ -2888,7 +2893,7 @@ public abstract class PO
}
else
doComma = true;
sqlInsert.append(p_info.getColumnName(i));
sqlInsert.append(DB.getDatabase().quoteColumnName(p_info.getColumnName(i)));
//
// Based on class of definition, not class of value
Class<?> c = p_info.getColumnClass(i);

View File

@ -814,6 +814,8 @@ public class POInfo implements Serializable
if (count > 1)
sql.append(",");
String columnSQL = getColumnSQL(i);
if (!virtual)
columnSQL = DB.getDatabase().quoteColumnName(columnSQL);
if (fullyQualified && !virtual)
sql.append(getTableName()).append(".");
sql.append(columnSQL); // Normal and Virtual Column

View File

@ -35,6 +35,8 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.logging.Level;
@ -135,6 +137,8 @@ public class DB_PostgreSQL implements AdempiereDatabase
private Random rand = new Random();
private static final List<String> reservedKeywords = Arrays.asList("limit","action","old","new");
/**
* Get Database Name
* @return database short name
@ -1175,6 +1179,22 @@ public class DB_PostgreSQL implements AdempiereDatabase
return builder.toString();
}
@Override
public String quoteColumnName(String columnName) {
if (!isNativeMode()) {
return columnName;
}
String lowerCase = columnName.toLowerCase();
if (reservedKeywords.contains(lowerCase)) {
StringBuilder sql = new StringBuilder("\"");
sql.append(lowerCase).append("\"");
return sql.toString();
} else {
return columnName;
}
}
@Override
public String intersectClauseForCSV(String columnName, String csv) {
StringBuilder builder = new StringBuilder();