IDEMPIERE-5691 Add Table Direct type joint method to Query (#1802)

This commit is contained in:
hengsin 2023-04-28 18:16:13 +08:00 committed by GitHub
parent 72a546bcad
commit f18bb88f4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -290,6 +290,19 @@ public class Query
return this;
}
/**
* Convenient method to add table direct type of joint.<br/>
* For e.g, if foreignTableName is C_BPartner and TableName for Query is AD_User,<br/>
* this will add join clause of <br/>
* "INNER JOIN C_BPartner ON (AD_User.C_BPartner_ID=C_BPartner.C_BParner_ID)".
* @param foreignTableName
*/
public void addTableDirectJoin(String foreignTableName) {
String foreignId = foreignTableName + "_ID";
addJoinClause("INNER JOIN " + foreignTableName + " ON (" + table.getTableName() + "." + foreignId
+ "=" + foreignTableName + "." + foreignId + ")");
}
/**
* Return a list of all po that match the query criteria.
* @return List

View File

@ -48,6 +48,7 @@ import org.compiere.model.MPInstance;
import org.compiere.model.MProcess;
import org.compiere.model.MTable;
import org.compiere.model.MTest;
import org.compiere.model.MUser;
import org.compiere.model.PO;
import org.compiere.model.POResultSet;
import org.compiere.model.Query;
@ -56,6 +57,7 @@ import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
import org.idempiere.test.AbstractTestCase;
import org.idempiere.test.DictionaryIDs;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -421,4 +423,15 @@ public class QueryTest extends AbstractTestCase {
assertEquals(expected, testRecord.getTestVirtualQty().setScale(2, RoundingMode.HALF_UP), "Wrong value returned");
}
@Test
public void testTableDirectJoin() {
Query query = new Query(Env.getCtx(), MUser.Table_Name, MUser.COLUMNNAME_AD_User_ID + "=?", getTrxName());
query.addTableDirectJoin("C_BPartner");
query.setParameters(DictionaryIDs.AD_User.GARDEN_USER.id);
MUser user = query.first();
assertNotNull(user, "Failed to retrieve garden user record");
String sql = query.getSQL();
assertTrue(sql.toLowerCase().contains("inner join c_bpartner on (ad_user.c_bpartner_id=c_bpartner.c_bpartner_id)"), "Unexpected SQL clause generated from query");
}
}