diff --git a/base/src/org/compiere/util/DB.java b/base/src/org/compiere/util/DB.java index 03a5a5473a..8e6ad6b2df 100644 --- a/base/src/org/compiere/util/DB.java +++ b/base/src/org/compiere/util/DB.java @@ -77,6 +77,8 @@ import org.compiere.process.SequenceCheck; *
  • BF [ 2030233 ] Remove duplicate code from DB class *
  • FR [ 2107062 ] Add more DB.getKeyNamePairs methods *
  • FR [ 2448461 ] Introduce DB.getSQLValue*Ex methods + *
  • FR [ 2781053 ] Introduce DB.getValueNamePairs + * */ public final class DB { @@ -2000,5 +2002,91 @@ public final class DB { return getSQLValueBD(trxName, sql, new Object[]{int_param1}); } + + /** + * Get Array of ValueNamePair items. + *
     Example:
    +	 * String sql = "SELECT Name, Description FROM AD_Ref_List WHERE AD_Reference_ID=?";
    +	 * ValueNamePair[] list = DB.getValueNamePairs(sql, false, params);
    +	 * 
    + * @param sql SELECT Value_Column, Name_Column FROM ... + * @param optional if {@link ValueNamePair#EMPTY} is added + * @param params query parameters + * @return array of {@link ValueNamePair} or empty array + * @throws DBException if there is any SQLException + */ + public static ValueNamePair[] getValueNamePairs(String sql, boolean optional, List params) + { + PreparedStatement pstmt = null; + ResultSet rs = null; + ArrayList list = new ArrayList(); + if (optional) + { + list.add (ValueNamePair.EMPTY); + } + try + { + pstmt = DB.prepareStatement(sql, null); + setParameters(pstmt, params); + rs = pstmt.executeQuery(); + while (rs.next()) + { + list.add(new ValueNamePair(rs.getString(1), rs.getString(2))); + } + } + catch (SQLException e) + { + throw new DBException(e, sql); + } + finally + { + close(rs, pstmt); + rs = null; pstmt = null; + } + return list.toArray(new ValueNamePair[list.size()]); + } + + /** + * Get Array of KeyNamePair items. + *
     Example:
    +	 * String sql = "SELECT C_City_ID, Name FROM C_City WHERE C_City_ID=?";
    +	 * KeyNamePair[] list = DB.getKeyNamePairs(sql, false, params);
    +	 * 
    + * @param sql SELECT ID_Column, Name_Column FROM ... + * @param optional if {@link ValueNamePair#EMPTY} is added + * @param params query parameters + * @return array of {@link KeyNamePair} or empty array + * @throws DBException if there is any SQLException + */ + public static KeyNamePair[] getKeyNamePairs(String sql, boolean optional, List params) + { + PreparedStatement pstmt = null; + ResultSet rs = null; + ArrayList list = new ArrayList(); + if (optional) + { + list.add (KeyNamePair.EMPTY); + } + try + { + pstmt = DB.prepareStatement(sql, null); + setParameters(pstmt, params); + rs = pstmt.executeQuery(); + while (rs.next()) + { + list.add(new KeyNamePair(rs.getInt(1), rs.getString(2))); + } + } + catch (SQLException e) + { + throw new DBException(e, sql); + } + finally + { + close(rs, pstmt); + rs = null; pstmt = null; + } + return list.toArray(new KeyNamePair[list.size()]); + } } // DB diff --git a/base/src/org/compiere/util/KeyNamePair.java b/base/src/org/compiere/util/KeyNamePair.java index 0976938419..ca6851d07b 100644 --- a/base/src/org/compiere/util/KeyNamePair.java +++ b/base/src/org/compiere/util/KeyNamePair.java @@ -27,7 +27,9 @@ public final class KeyNamePair extends NamePair /** * */ - private static final long serialVersionUID = 9187402069085556500L; + private static final long serialVersionUID = 6347385376010388473L; + + public static final KeyNamePair EMPTY = new KeyNamePair(-1, ""); /** * Constructor KeyValue Pair - diff --git a/base/src/org/compiere/util/ValueNamePair.java b/base/src/org/compiere/util/ValueNamePair.java index f91dad727d..5fbbc1789d 100644 --- a/base/src/org/compiere/util/ValueNamePair.java +++ b/base/src/org/compiere/util/ValueNamePair.java @@ -27,7 +27,9 @@ public final class ValueNamePair extends NamePair /** * */ - private static final long serialVersionUID = -3020367706232064508L; + private static final long serialVersionUID = -8315081335749462163L; + + public static final ValueNamePair EMPTY = new ValueNamePair("", ""); /** * Construct KeyValue Pair diff --git a/extend/src/test/functional/DBTest.java b/extend/src/test/functional/DBTest.java index ee374f8395..7cb3da1546 100644 --- a/extend/src/test/functional/DBTest.java +++ b/extend/src/test/functional/DBTest.java @@ -15,10 +15,14 @@ package test.functional; import java.math.BigDecimal; import java.sql.Timestamp; +import java.util.ArrayList; import org.adempiere.exceptions.DBException; +import org.compiere.model.MTable; import org.compiere.util.DB; +import org.compiere.util.KeyNamePair; import org.compiere.util.TimeUtil; +import org.compiere.util.ValueNamePair; import test.AdempiereTestCase; @@ -159,4 +163,57 @@ public class DBTest extends AdempiereTestCase result = DB.getSQLValueTS(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM INEXISTENT_TABLE"); assertNull("Error should be signaled", result); } + + public void test_getValueNamePairs() throws Exception + { + ArrayList params = new ArrayList(); + params.add(MTable.ACCESSLEVEL_AD_Reference_ID); + final String sql = "SELECT Value, Name FROM AD_Ref_List WHERE AD_Reference_ID=? ORDER BY Value"; + // Get (with optional item) + ValueNamePair[] arr = DB.getValueNamePairs(sql, true, params); + assertEquals("Invalid size", 6+1, arr.length); + assertSame("First value should be EMPTY", ValueNamePair.EMPTY, arr[0]); + assertEquals(arr[1].getValue(), "1"); + assertEquals(arr[2].getValue(), "2"); + assertEquals(arr[3].getValue(), "3"); + assertEquals(arr[4].getValue(), "4"); + assertEquals(arr[5].getValue(), "6"); + assertEquals(arr[6].getValue(), "7"); + // Get (NO optional item) + arr = DB.getValueNamePairs(sql, false, params); + assertEquals("Invalid size", 6, arr.length); + assertEquals(arr[0].getValue(), "1"); + assertEquals(arr[1].getValue(), "2"); + assertEquals(arr[2].getValue(), "3"); + assertEquals(arr[3].getValue(), "4"); + assertEquals(arr[4].getValue(), "6"); + assertEquals(arr[5].getValue(), "7"); + } + + public void test_getKeyNamePairs() throws Exception + { + ArrayList params = new ArrayList(); + params.add(MTable.ACCESSLEVEL_AD_Reference_ID); + final String sql = "SELECT AD_Ref_List_ID, Value FROM AD_Ref_List WHERE AD_Reference_ID=? ORDER BY Value"; + // Get (with optional item) + KeyNamePair[] arr = DB.getKeyNamePairs(sql, true, params); + assertEquals("Invalid size", 6+1, arr.length); + assertSame("First value should be EMPTY", KeyNamePair.EMPTY, arr[0]); + assertEquals(arr[1].getName(), "1"); + assertEquals(arr[2].getName(), "2"); + assertEquals(arr[3].getName(), "3"); + assertEquals(arr[4].getName(), "4"); + assertEquals(arr[5].getName(), "6"); + assertEquals(arr[6].getName(), "7"); + // Get (NO optional item) + arr = DB.getKeyNamePairs(sql, false, params); + assertEquals("Invalid size", 6, arr.length); + assertEquals(arr[0].getName(), "1"); + assertEquals(arr[1].getName(), "2"); + assertEquals(arr[2].getName(), "3"); + assertEquals(arr[3].getName(), "4"); + assertEquals(arr[4].getName(), "6"); + assertEquals(arr[5].getName(), "7"); + } + }