details: https://code.openbravo.com/erp/devel/pi/rev/84ddadc11e41 changeset: 31649:84ddadc11e41 user: Carlos Aristu <carlos.aristu <at> openbravo.com> date: Tue Mar 14 16:55:41 2017 +0100 summary: fixes bug 35509: OBQuery without an alias and a select clause is not working
diffstat: src-test/src/org/openbravo/test/dal/DalTest.java | 43 ++++++++++++++++++++++++ src/org/openbravo/dal/service/OBQuery.java | 4 +- 2 files changed, 45 insertions(+), 2 deletions(-) diffs (79 lines): diff -r e625fac3bb60 -r 84ddadc11e41 src-test/src/org/openbravo/test/dal/DalTest.java --- a/src-test/src/org/openbravo/test/dal/DalTest.java Mon Mar 13 14:22:51 2017 +0100 +++ b/src-test/src/org/openbravo/test/dal/DalTest.java Tue Mar 14 16:55:41 2017 +0100 @@ -56,6 +56,7 @@ import org.openbravo.dal.core.SessionHandler; import org.openbravo.dal.service.OBCriteria; import org.openbravo.dal.service.OBDal; +import org.openbravo.dal.service.OBQuery; import org.openbravo.database.ExternalConnectionPool; import org.openbravo.model.ad.system.SystemInformation; import org.openbravo.model.common.businesspartner.BusinessPartner; @@ -616,4 +617,46 @@ return OBPropertiesProvider.getInstance().getOpenbravoProperties() .containsKey("bbdd.readonly.url"); } + + /** + * Test to check that an OBQuery with a select clause can be executed properly without setting an + * alias for the main entity of the query. + */ + @Test + public void testOBQueryWithoutAlias() { + setTestUserContext(); + String isoCode = getISOCodeFromCurrencyId(EURO_ID, false); + assertEquals(isoCode, EURO); + } + + /** + * Test to check that an OBQuery with a select clause can be executed properly by defining an + * alias for the main entity of the query. + */ + @Test + public void testOBQueryWithAlias() { + setTestUserContext(); + String isoCode = getISOCodeFromCurrencyId(EURO_ID, true); + assertEquals(isoCode, EURO); + } + + private String getISOCodeFromCurrencyId(String currencyId, boolean includeAlias) { + String isoCode = null; + try { + StringBuilder hql = new StringBuilder(); + if (includeAlias) { + hql.append(" as c"); + hql.append(" where c." + Currency.PROPERTY_ID); + } else { + hql.append(" where " + Currency.PROPERTY_ID); + } + hql.append(" = :currencyId"); + OBQuery<Currency> query = OBDal.getInstance().createQuery(Currency.class, hql.toString()); + query.setNamedParameter("currencyId", currencyId); + query.setSelectClause(Currency.PROPERTY_ISOCODE); + isoCode = (String) query.uniqueResultObject(); + } catch (Exception ignored) { + } + return isoCode; + } } diff -r e625fac3bb60 -r 84ddadc11e41 src/org/openbravo/dal/service/OBQuery.java --- a/src/org/openbravo/dal/service/OBQuery.java Mon Mar 13 14:22:51 2017 +0100 +++ b/src/org/openbravo/dal/service/OBQuery.java Tue Mar 14 16:55:41 2017 +0100 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2008-2016 Openbravo SLU + * All portions are Copyright (C) 2008-2017 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -378,7 +378,7 @@ result = "select " + (selectClause == null ? alias : selectClause) + " from " + getEntity().getName() + " " + aliasJoinClause + " " + whereClause + orderByClause; } else { - result = (selectClause == null ? "" : "select " + selectClause) + "from " + result = (selectClause == null ? "" : "select " + selectClause + " ") + "from " + getEntity().getName() + " " + aliasJoinClause + " " + whereClause + orderByClause; } log.debug("Created query string " + result); ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits