Skip to content

Commit

Permalink
IDEMPIERE-6122 Query class to accept a list of columns to select (#2337)
Browse files Browse the repository at this point in the history
* IDEMPIERE-6122 Query class to accept a list of columns to select

* IDEMPIERE-6122 Query class to accept a list of columns to select

- improve efficiency

* IDEMPIERE-6122 Query class to accept a list of columns to select

- fix id, uuid and standard columns not loaded for partial PO
  • Loading branch information
hengsin committed Apr 29, 2024
1 parent 0676356 commit 5134623
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
10 changes: 6 additions & 4 deletions org.adempiere.base/src/org/compiere/model/PO.java
Expand Up @@ -1655,10 +1655,12 @@ private boolean loadColumn(ResultSet rs, int index) {
String columnName = p_info.getColumnName(index);
String[] selectColumns = MTable.getPartialPOResultSetColumns();
if (selectColumns != null && selectColumns.length > 0) {
Optional<String> optional = Arrays.stream(selectColumns).filter(e -> e.equalsIgnoreCase(columnName)).findFirst();
if (!optional.isPresent()) {
if (log.isLoggable(Level.FINER))log.log(Level.FINER, "Partial PO, Column not loaded: " + columnName);
return true;
if (!p_info.isColumnAlwaysLoadedForPartialPO(index)) {
Optional<String> optional = Arrays.stream(selectColumns).filter(e -> e.equalsIgnoreCase(columnName)).findFirst();
if (!optional.isPresent()) {
if (log.isLoggable(Level.FINER))log.log(Level.FINER, "Partial PO, Column not loaded: " + columnName);
return true;
}
}
}
Class<?> clazz = p_info.getColumnClass(index);
Expand Down
29 changes: 21 additions & 8 deletions org.adempiere.base/src/org/compiere/model/POInfo.java
Expand Up @@ -882,6 +882,25 @@ public StringBuilder buildSelect(boolean fullyQualified, String ... virtualColum
return sql;
}

/**
* Is column should always be loaded for partial loading of PO
* @param columnIndex
* @return true if column should always be loaded for partial loading of PO
*/
protected boolean isColumnAlwaysLoadedForPartialPO(int columnIndex)
{
String columnName = getColumnName(columnIndex);
boolean isKey = isKey(columnIndex);
boolean isUUID = columnName.equals(PO.getUUIDColumnName(m_TableName));
// Always load key, uuid and standard columns
if (isKey || isUUID || columnName.equalsIgnoreCase("ad_client_id") || columnName.equalsIgnoreCase("ad_org_id")
|| columnName.equalsIgnoreCase("isactive") || columnName.equalsIgnoreCase("created") || columnName.equalsIgnoreCase("createdby")
|| columnName.equalsIgnoreCase("updated") || columnName.equalsIgnoreCase("updatedby"))
return true;
else
return false;
}

/**
* Build SQL SELECT statement for columns.
* @param fullyQualified prefix column names with the table name
Expand All @@ -892,17 +911,11 @@ public StringBuilder buildSelectForColumns(boolean fullyQualified, String[] colu
StringBuilder sql = new StringBuilder("SELECT ");
int size = getColumnCount();
int count = 0;
String uuid = PO.getUUIDColumnName(m_TableName);
for (int i = 0; i < size; i++)
{
String columnName = getColumnName(i);
boolean virtual = isVirtualColumn(i);
boolean isKey = isKey(i);
boolean isUUID = columnName.equals(uuid);
//always include key, uuid and standard columns
if (!isKey && !isUUID && !columnName.equalsIgnoreCase("ad_client_id") && !columnName.equalsIgnoreCase("ad_org_id")
&& !columnName.equalsIgnoreCase("isactive") && !columnName.equalsIgnoreCase("created") && !columnName.equalsIgnoreCase("createdby")
&& !columnName.equalsIgnoreCase("updated") && !columnName.equalsIgnoreCase("updatedby"))
boolean virtual = isVirtualColumn(i);
if (!isColumnAlwaysLoadedForPartialPO(i))
{
Optional<String> optional = Arrays.stream(columns).filter(e -> e.equalsIgnoreCase(columnName)).findFirst();
if (!optional.isPresent())
Expand Down
14 changes: 14 additions & 0 deletions org.idempiere.test/src/org/idempiere/test/base/QueryTest.java
Expand Up @@ -442,20 +442,26 @@ public void testTableDirectJoin() {
public void testPartialPO() {
Query query = new Query(Env.getCtx(), MProduct.Table_Name, MProduct.COLUMNNAME_M_Product_ID + "=?", getTrxName());
MProduct product = query.setParameters(DictionaryIDs.M_Product.AZALEA_BUSH.id).first();
assertTrue(product.getM_Product_ID() > 0);
assertTrue(product.getAD_Client_ID() > 0);
assertNotNull(product.getName());
assertNotNull(product.getValue());
assertNotNull(product.getProductType());
assertTrue(product.getM_Product_Category_ID() > 0);
assertFalse(product.is_Immutable());

product = query.selectColumns(MProduct.COLUMNNAME_Name, MProduct.COLUMNNAME_Value).setParameters(DictionaryIDs.M_Product.AZALEA_BUSH.id).first();
assertTrue(product.getM_Product_ID() > 0);
assertTrue(product.getAD_Client_ID() > 0);
assertNotNull(product.getName());
assertNotNull(product.getValue());
assertNull(product.getProductType());
assertTrue(product.getM_Product_Category_ID() == 0);
assertTrue(product.is_Immutable());

product = query.selectColumns().setParameters(DictionaryIDs.M_Product.AZALEA_BUSH.id).first();
assertTrue(product.getM_Product_ID() > 0);
assertTrue(product.getAD_Client_ID() > 0);
assertNotNull(product.getName());
assertNotNull(product.getValue());
assertNotNull(product.getProductType());
Expand All @@ -464,20 +470,26 @@ public void testPartialPO() {

List<MProduct> list = query.selectColumns(MProduct.COLUMNNAME_Name, MProduct.COLUMNNAME_Value).setParameters(DictionaryIDs.M_Product.AZALEA_BUSH.id).list();
product = list.get(0);
assertTrue(product.getM_Product_ID() > 0);
assertTrue(product.getAD_Client_ID() > 0);
assertNotNull(product.getName());
assertNotNull(product.getValue());
assertNull(product.getProductType());
assertTrue(product.getM_Product_Category_ID() == 0);
assertTrue(product.is_Immutable());

product = query.selectColumns(MProduct.COLUMNNAME_Name, MProduct.COLUMNNAME_Value).setParameters(DictionaryIDs.M_Product.AZALEA_BUSH.id).firstOnly();
assertTrue(product.getM_Product_ID() > 0);
assertTrue(product.getAD_Client_ID() > 0);
assertNotNull(product.getName());
assertNotNull(product.getValue());
assertNull(product.getProductType());
assertTrue(product.getM_Product_Category_ID() == 0);
assertTrue(product.is_Immutable());

product = (MProduct) query.selectColumns(MProduct.COLUMNNAME_Name, MProduct.COLUMNNAME_Value).setParameters(DictionaryIDs.M_Product.AZALEA_BUSH.id).scroll().next();
assertTrue(product.getM_Product_ID() > 0);
assertTrue(product.getAD_Client_ID() > 0);
assertNotNull(product.getName());
assertNotNull(product.getValue());
assertNull(product.getProductType());
Expand All @@ -486,6 +498,8 @@ public void testPartialPO() {

Stream<MProduct> stream = query.selectColumns(MProduct.COLUMNNAME_Name, MProduct.COLUMNNAME_Value).setParameters(DictionaryIDs.M_Product.AZALEA_BUSH.id).stream();
product = stream.findFirst().get();
assertTrue(product.getM_Product_ID() > 0);
assertTrue(product.getAD_Client_ID() > 0);
assertNotNull(product.getName());
assertNotNull(product.getValue());
assertNull(product.getProductType());
Expand Down

0 comments on commit 5134623

Please sign in to comment.