FR [ 2448461 ] Introduce DB.getSQLValue*Ex methods
http://sourceforge.net/tracker/index.php?func=detail&aid=2448461&group_id=176962&atid=879335
This commit is contained in:
parent
5adac0eda8
commit
4b10b24702
|
|
@ -76,6 +76,7 @@ import org.compiere.process.SequenceCheck;
|
||||||
* <li>FR [ 1986583 ] Add DB.executeUpdateEx(String, Object[], String)
|
* <li>FR [ 1986583 ] Add DB.executeUpdateEx(String, Object[], String)
|
||||||
* <li>BF [ 2030233 ] Remove duplicate code from DB class
|
* <li>BF [ 2030233 ] Remove duplicate code from DB class
|
||||||
* <li>FR [ 2107062 ] Add more DB.getKeyNamePairs methods
|
* <li>FR [ 2107062 ] Add more DB.getKeyNamePairs methods
|
||||||
|
* <li>FR [ 2448461 ] Introduce DB.getSQLValue*Ex methods
|
||||||
*/
|
*/
|
||||||
public final class DB
|
public final class DB
|
||||||
{
|
{
|
||||||
|
|
@ -1161,6 +1162,19 @@ public final class DB
|
||||||
}
|
}
|
||||||
return retValue;
|
return retValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get String Value from sql
|
||||||
|
* @param trxName trx
|
||||||
|
* @param sql sql
|
||||||
|
* @param params collection of parameters
|
||||||
|
* @return first value or -1
|
||||||
|
* @throws DBException if there is any SQLException
|
||||||
|
*/
|
||||||
|
public static int getSQLValueEx (String trxName, String sql, Collection<Object> params)
|
||||||
|
{
|
||||||
|
return getSQLValueEx(trxName, sql, params.toArray(new Object[params.size()]));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get int Value from sql
|
* Get int Value from sql
|
||||||
|
|
@ -1192,9 +1206,7 @@ public final class DB
|
||||||
*/
|
*/
|
||||||
public static int getSQLValue (String trxName, String sql, Collection<Object> params)
|
public static int getSQLValue (String trxName, String sql, Collection<Object> params)
|
||||||
{
|
{
|
||||||
Object[] arr = new Object[params.size()];
|
return getSQLValue(trxName, sql, params.toArray(new Object[params.size()]));
|
||||||
params.toArray(arr);
|
|
||||||
return getSQLValue(trxName, sql, arr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1203,8 +1215,9 @@ public final class DB
|
||||||
* @param sql sql
|
* @param sql sql
|
||||||
* @param params array of parameters
|
* @param params array of parameters
|
||||||
* @return first value or null
|
* @return first value or null
|
||||||
|
* @throws DBException if there is any SQLException
|
||||||
*/
|
*/
|
||||||
public static String getSQLValueString (String trxName, String sql, Object... params)
|
public static String getSQLValueStringEx (String trxName, String sql, Object... params)
|
||||||
{
|
{
|
||||||
String retValue = null;
|
String retValue = null;
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
|
|
@ -1219,9 +1232,9 @@ public final class DB
|
||||||
else
|
else
|
||||||
log.info("No Value " + sql);
|
log.info("No Value " + sql);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (SQLException e)
|
||||||
{
|
{
|
||||||
log.log(Level.SEVERE, sql, getSQLException(e));
|
throw new DBException(e, sql);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|
@ -1231,6 +1244,40 @@ public final class DB
|
||||||
return retValue;
|
return retValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get String Value from sql
|
||||||
|
* @param trxName trx
|
||||||
|
* @param sql sql
|
||||||
|
* @param params collection of parameters
|
||||||
|
* @return first value or null
|
||||||
|
* @throws DBException if there is any SQLException
|
||||||
|
*/
|
||||||
|
public static String getSQLValueStringEx (String trxName, String sql, Collection<Object> params)
|
||||||
|
{
|
||||||
|
return getSQLValueStringEx(trxName, sql, params.toArray(new Object[params.size()]));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get String Value from sql
|
||||||
|
* @param trxName trx
|
||||||
|
* @param sql sql
|
||||||
|
* @param params array of parameters
|
||||||
|
* @return first value or null
|
||||||
|
*/
|
||||||
|
public static String getSQLValueString (String trxName, String sql, Object... params)
|
||||||
|
{
|
||||||
|
String retValue = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
retValue = getSQLValueStringEx(trxName, sql, params);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
log.log(Level.SEVERE, sql, getSQLException(e));
|
||||||
|
}
|
||||||
|
return retValue;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get String Value from sql
|
* Get String Value from sql
|
||||||
* @param trxName trx
|
* @param trxName trx
|
||||||
|
|
@ -1240,9 +1287,7 @@ public final class DB
|
||||||
*/
|
*/
|
||||||
public static String getSQLValueString (String trxName, String sql, Collection<Object> params)
|
public static String getSQLValueString (String trxName, String sql, Collection<Object> params)
|
||||||
{
|
{
|
||||||
Object[] arr = new Object[params.size()];
|
return getSQLValueString(trxName, sql, params.toArray(new Object[params.size()]));
|
||||||
params.toArray(arr);
|
|
||||||
return getSQLValueString(trxName, sql, arr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1281,6 +1326,20 @@ public final class DB
|
||||||
return retValue;
|
return retValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get BigDecimal Value from sql
|
||||||
|
* @param trxName trx
|
||||||
|
* @param sql sql
|
||||||
|
* @param params collection of parameters
|
||||||
|
* @return first value or null if not found
|
||||||
|
* @throws DBException if there is any SQLException
|
||||||
|
*/
|
||||||
|
public static BigDecimal getSQLValueBDEx (String trxName, String sql, Collection<Object> params) throws DBException
|
||||||
|
{
|
||||||
|
return getSQLValueBDEx(trxName, sql, params.toArray(new Object[params.size()]));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get BigDecimal Value from sql
|
* Get BigDecimal Value from sql
|
||||||
* @param trxName trx
|
* @param trxName trx
|
||||||
|
|
@ -1311,9 +1370,7 @@ public final class DB
|
||||||
*/
|
*/
|
||||||
public static BigDecimal getSQLValueBD (String trxName, String sql, Collection<Object> params)
|
public static BigDecimal getSQLValueBD (String trxName, String sql, Collection<Object> params)
|
||||||
{
|
{
|
||||||
Object[] arr = new Object[params.size()];
|
return getSQLValueBD(trxName, sql, params.toArray(new Object[params.size()]));
|
||||||
params.toArray(arr);
|
|
||||||
return getSQLValueBD(trxName, sql, arr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1322,8 +1379,9 @@ public final class DB
|
||||||
* @param sql sql
|
* @param sql sql
|
||||||
* @param params array of parameters
|
* @param params array of parameters
|
||||||
* @return first value or null
|
* @return first value or null
|
||||||
|
* @throws DBException if there is any SQLException
|
||||||
*/
|
*/
|
||||||
public static Timestamp getSQLValueTS (String trxName, String sql, Object... params)
|
public static Timestamp getSQLValueTSEx (String trxName, String sql, Object... params)
|
||||||
{
|
{
|
||||||
Timestamp retValue = null;
|
Timestamp retValue = null;
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
|
|
@ -1338,9 +1396,9 @@ public final class DB
|
||||||
else
|
else
|
||||||
log.info("No Value " + sql);
|
log.info("No Value " + sql);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (SQLException e)
|
||||||
{
|
{
|
||||||
log.log(Level.SEVERE, sql, getSQLException(e));
|
throw new DBException(e, sql);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|
@ -1349,6 +1407,39 @@ public final class DB
|
||||||
}
|
}
|
||||||
return retValue;
|
return retValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get BigDecimal Value from sql
|
||||||
|
* @param trxName trx
|
||||||
|
* @param sql sql
|
||||||
|
* @param params collection of parameters
|
||||||
|
* @return first value or null if not found
|
||||||
|
* @throws DBException if there is any SQLException
|
||||||
|
*/
|
||||||
|
public static Timestamp getSQLValueTSEx (String trxName, String sql, Collection<Object> params) throws DBException
|
||||||
|
{
|
||||||
|
return getSQLValueTSEx(trxName, sql, params.toArray(new Object[params.size()]));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Timestamp Value from sql
|
||||||
|
* @param trxName trx
|
||||||
|
* @param sql sql
|
||||||
|
* @param params array of parameters
|
||||||
|
* @return first value or null
|
||||||
|
*/
|
||||||
|
public static Timestamp getSQLValueTS (String trxName, String sql, Object... params)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return getSQLValueTSEx(trxName, sql, params);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
log.log(Level.SEVERE, sql, getSQLException(e));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Timestamp Value from sql
|
* Get Timestamp Value from sql
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,11 @@
|
||||||
package test.functional;
|
package test.functional;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
import org.adempiere.exceptions.DBException;
|
import org.adempiere.exceptions.DBException;
|
||||||
import org.compiere.util.DB;
|
import org.compiere.util.DB;
|
||||||
|
import org.compiere.util.TimeUtil;
|
||||||
|
|
||||||
import test.AdempiereTestCase;
|
import test.AdempiereTestCase;
|
||||||
|
|
||||||
|
|
@ -27,7 +29,7 @@ public class DBTest extends AdempiereTestCase
|
||||||
DBException ex = null;
|
DBException ex = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DB.getSQLValueEx(null, "SELECT 10 FROM INEXISTENT_TABLE");
|
result = DB.getSQLValueEx(null, "SELECT 10 FROM INEXISTENT_TABLE");
|
||||||
}
|
}
|
||||||
catch (DBException e)
|
catch (DBException e)
|
||||||
{
|
{
|
||||||
|
|
@ -59,7 +61,7 @@ public class DBTest extends AdempiereTestCase
|
||||||
DBException ex = null;
|
DBException ex = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DB.getSQLValueBDEx(null, "SELECT 10 FROM INEXISTENT_TABLE");
|
result = DB.getSQLValueBDEx(null, "SELECT 10 FROM INEXISTENT_TABLE");
|
||||||
}
|
}
|
||||||
catch (DBException e)
|
catch (DBException e)
|
||||||
{
|
{
|
||||||
|
|
@ -79,4 +81,72 @@ public class DBTest extends AdempiereTestCase
|
||||||
result = DB.getSQLValueBD(null, "SELECT 10 FROM INEXISTENT_TABLE");
|
result = DB.getSQLValueBD(null, "SELECT 10 FROM INEXISTENT_TABLE");
|
||||||
assertNull("Error should be signaled", result);
|
assertNull("Error should be signaled", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void test_getSQLValueStringEx() throws Exception
|
||||||
|
{
|
||||||
|
String result = DB.getSQLValueStringEx(null, "SELECT 'string' FROM DUAL");
|
||||||
|
assertEquals("string", result);
|
||||||
|
//
|
||||||
|
result = DB.getSQLValueStringEx(null, "SELECT 10 FROM AD_SYSTEM WHERE 1=2");
|
||||||
|
assertNull("No value should be returned", result);
|
||||||
|
//
|
||||||
|
DBException ex = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = DB.getSQLValueStringEx(null, "SELECT 'string' FROM INEXISTENT_TABLE");
|
||||||
|
}
|
||||||
|
catch (DBException e)
|
||||||
|
{
|
||||||
|
ex = e;
|
||||||
|
}
|
||||||
|
assertNotNull("No DBException Was Throwed", ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void test_getSQLValueString() throws Exception
|
||||||
|
{
|
||||||
|
String result = DB.getSQLValueString(null, "SELECT 'string' FROM DUAL");
|
||||||
|
assertEquals("string", result);
|
||||||
|
//
|
||||||
|
result = DB.getSQLValueString(null, "SELECT 'string' FROM AD_SYSTEM WHERE 1=2");
|
||||||
|
assertNull("No value should be returned", result);
|
||||||
|
//
|
||||||
|
result = DB.getSQLValueString(null, "SELECT 'string' FROM INEXISTENT_TABLE");
|
||||||
|
assertNull("Error should be signaled", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void test_getSQLValueTSEx() throws Exception
|
||||||
|
{
|
||||||
|
final Timestamp target = TimeUtil.getDay(2008, 01, 01);
|
||||||
|
//
|
||||||
|
Timestamp result = DB.getSQLValueTSEx(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM AD_SYSTEM");
|
||||||
|
assertEquals(target, result);
|
||||||
|
//
|
||||||
|
result = DB.getSQLValueTSEx(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM AD_SYSTEM WHERE 1=2");
|
||||||
|
assertNull("No value should be returned", result);
|
||||||
|
//
|
||||||
|
DBException ex = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = DB.getSQLValueTSEx(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM INEXISTENT_TABLE");
|
||||||
|
}
|
||||||
|
catch (DBException e)
|
||||||
|
{
|
||||||
|
ex = e;
|
||||||
|
}
|
||||||
|
assertNotNull("No DBException Was Throwed", ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void test_getSQLValueTS() throws Exception
|
||||||
|
{
|
||||||
|
final Timestamp target = TimeUtil.getDay(2008, 01, 01);
|
||||||
|
//
|
||||||
|
Timestamp result = DB.getSQLValueTS(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM DUAL");
|
||||||
|
assertEquals(target, result);
|
||||||
|
//
|
||||||
|
result = DB.getSQLValueTS(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM AD_SYSTEM WHERE 1=2");
|
||||||
|
assertNull("No value should be returned", result);
|
||||||
|
//
|
||||||
|
result = DB.getSQLValueTS(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM INEXISTENT_TABLE");
|
||||||
|
assertNull("Error should be signaled", result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue