FR [ 2781053 ] Introduce DB.getValueNamePairs

https://sourceforge.net/tracker/?func=detail&aid=2781053&group_id=176962&atid=879335
This commit is contained in:
teo_sarca 2009-04-29 19:31:22 +00:00
parent 49cd77b1a6
commit 1927fd553c
4 changed files with 151 additions and 2 deletions

View File

@ -77,6 +77,8 @@ import org.compiere.process.SequenceCheck;
* <li>BF [ 2030233 ] Remove duplicate code from DB class
* <li>FR [ 2107062 ] Add more DB.getKeyNamePairs methods
* <li>FR [ 2448461 ] Introduce DB.getSQLValue*Ex methods
* <li>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.
* <pre> Example:
* String sql = "SELECT Name, Description FROM AD_Ref_List WHERE AD_Reference_ID=?";
* ValueNamePair[] list = DB.getValueNamePairs(sql, false, params);
* </pre>
* @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<Object> params)
{
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<ValueNamePair> list = new ArrayList<ValueNamePair>();
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.
* <pre> Example:
* String sql = "SELECT C_City_ID, Name FROM C_City WHERE C_City_ID=?";
* KeyNamePair[] list = DB.getKeyNamePairs(sql, false, params);
* </pre>
* @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<Object> params)
{
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
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

View File

@ -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 -

View File

@ -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

View File

@ -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<Object> params = new ArrayList<Object>();
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<Object> params = new ArrayList<Object>();
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");
}
}