Search in sources :

Example 61 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class DB method getValueNamePairs.

/**
	 * 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()]);
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) POResultSet(org.compiere.model.POResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 62 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class DB method getIDsEx.

public static int[] getIDsEx(String trxName, String sql, Object... params) throws DBException {
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    ArrayList<Integer> list = new ArrayList<Integer>();
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            list.add(rs.getInt(1));
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //	Convert to array
    int[] retValue = new int[list.size()];
    for (int i = 0; i < retValue.length; i++) {
        retValue[i] = list.get(i);
    }
    return retValue;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) POResultSet(org.compiere.model.POResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 63 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class DB method getSQLValueStringEx.

/**
     * Get String Value from sql
     * @param trxName trx
     * @param sql sql
     * @param params array of parameters
     * @return first value or null
     * @throws DBException if there is any SQLException
     */
public static String getSQLValueStringEx(String trxName, String sql, Object... params) {
    String retValue = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = prepareStatement(sql, trxName);
        setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        if (rs.next())
            retValue = rs.getString(1);
        else
            log.fine("No Value " + sql);
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return retValue;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) POResultSet(org.compiere.model.POResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 64 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class MPPOrderBOMLine method loadStorage.

/**
	 * Load Storage Info
	 * @param reload
	 */
private void loadStorage(boolean reload) {
    if (!reload && m_qtyOnHand != null && m_qtyAvailable != null) {
        return;
    }
    //
    final String sql = "SELECT " + " bomQtyAvailable(" + COLUMNNAME_M_Product_ID + ", " + COLUMNNAME_M_Warehouse_ID + ", 0)" + ",bomQtyOnHand(" + COLUMNNAME_M_Product_ID + ", " + COLUMNNAME_M_Warehouse_ID + ", 0)" + " FROM " + Table_Name + " WHERE " + COLUMNNAME_PP_Order_BOMLine_ID + "=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, get_TrxName());
        DB.setParameters(pstmt, new Object[] { get_ID() });
        rs = pstmt.executeQuery();
        if (rs.next()) {
            m_qtyAvailable = rs.getBigDecimal(1);
            m_qtyOnHand = rs.getBigDecimal(2);
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 65 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class InventoryTestException method assertStorage.

private void assertStorage(MMDocument doc, String trxName) {
    MLocator locator = InventoryUtil.getCreateLocator(-1, doc.LocatorValue, doc.LocatorValue);
    MProduct product = InventoryUtil.getCreateProduct(doc);
    int M_ASI_ID = -1;
    if (!Util.isEmpty(doc.ASI, true)) {
        M_ASI_ID = doc.scenario.getM_ASI_ID(doc.ASI);
    }
    ArrayList<Object> params = new ArrayList<Object>();
    String sql = "SELECT" + " COALESCE(SUM(QtyOnHand),0)" + ",COALESCE(SUM(QtyReserved),0)" + ",COALESCE(SUM(QtyOrdered),0)" + " FROM M_Storage" + " WHERE M_Locator_ID=? AND M_Product_ID=?";
    params.add(locator.get_ID());
    params.add(product.get_ID());
    if (M_ASI_ID >= 0) {
        sql += " AND " + MStorage.COLUMNNAME_M_AttributeSetInstance_ID + "=?";
        params.add(M_ASI_ID);
    }
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    BigDecimal qtyOnHand = Env.ZERO;
    BigDecimal qtyOrdered = Env.ZERO;
    BigDecimal qtyReserved = Env.ZERO;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        DB.setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            qtyOnHand = rs.getBigDecimal(1);
            qtyReserved = rs.getBigDecimal(2);
            qtyOrdered = rs.getBigDecimal(3);
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //
    //
    assertEquals("QtyOnHand not match " + doc, doc.Qty, qtyOnHand);
    assertEquals("QtyReserved not match " + doc, doc.QtyReserved, qtyReserved);
    assertEquals("QtyOrdered not match " + doc, doc.QtyOrdered, qtyOrdered);
}
Also used : DBException(org.adempiere.exceptions.DBException) MProduct(org.compiere.model.MProduct) SQLException(java.sql.SQLException) MLocator(org.compiere.model.MLocator) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Aggregations

DBException (org.adempiere.exceptions.DBException)89 SQLException (java.sql.SQLException)82 PreparedStatement (java.sql.PreparedStatement)75 ResultSet (java.sql.ResultSet)75 BigDecimal (java.math.BigDecimal)27 ArrayList (java.util.ArrayList)23 Timestamp (java.sql.Timestamp)15 POResultSet (org.compiere.model.POResultSet)8 AdempiereException (org.adempiere.exceptions.AdempiereException)6 KeyNamePair (org.compiere.util.KeyNamePair)5 Connection (java.sql.Connection)4 Savepoint (java.sql.Savepoint)4 MProduct (org.compiere.model.MProduct)4 Trx (org.compiere.util.Trx)4 Date (java.util.Date)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IDColumn (org.compiere.minigrid.IDColumn)2 MLocator (org.compiere.model.MLocator)2 MTable (org.compiere.model.MTable)2 MUOM (org.compiere.model.MUOM)2