Search in sources :

Example 26 with DBException

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

the class Query method firstId.

private int firstId(boolean assumeOnlyOneResult) throws DBException {
    String[] keys = table.getKeyColumns();
    if (keys.length != 1) {
        throw new DBException("Table " + table + " has 0 or more than 1 key columns");
    }
    StringBuffer selectClause = new StringBuffer("SELECT ");
    selectClause.append(keys[0]);
    selectClause.append(" FROM ").append(table.getTableName());
    String sql = buildSQL(selectClause, true);
    int id = -1;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        rs = createResultSet(pstmt);
        if (rs.next()) {
            id = rs.getInt(1);
        }
        if (assumeOnlyOneResult && rs.next()) {
            // TODO : translate
            throw new DBException("QueryMoreThanOneRecordsFound");
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //
    return id;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 27 with DBException

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

the class PO method load.

//	load
/**
	 *  (re)Load record with m_ID[*]
	 *  @param trxName transaction
	 *  @return true if loaded
	 */
public boolean load(String trxName) {
    m_trxName = trxName;
    boolean success = true;
    StringBuffer sql = p_info.buildSelect();
    sql.append(" WHERE ").append(get_WhereClause(false));
    int size = get_ColumnCount();
    //	int index = -1;
    if (CLogMgt.isLevelFinest())
        log.finest(get_WhereClause(true));
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        //	local trx only
        pstmt = DB.prepareStatement(sql.toString(), m_trxName);
        for (int i = 0; i < m_IDs.length; i++) {
            Object oo = m_IDs[i];
            if (oo instanceof Integer)
                pstmt.setInt(i + 1, ((Integer) m_IDs[i]).intValue());
            else
                pstmt.setString(i + 1, m_IDs[i].toString());
        }
        rs = pstmt.executeQuery();
        if (rs.next()) {
            success = load(rs);
        } else {
            if (!log.getLevel().equals(Level.CONFIG))
                log.log(Level.WARNING, "NO Data found for " + get_WhereClause(true));
            m_IDs = new Object[] { I_ZERO };
            success = false;
        //	throw new DBException("NO Data found for " + get_WhereClause(true));
        }
        m_createNew = false;
        //	reset new values
        m_newValues = new Object[size];
    } catch (Exception e) {
        String msg = "";
        if (m_trxName != null)
            msg = "[" + m_trxName + "] - ";
        msg += get_WhereClause(true) + //	+ ", " + p_info.toString(index)
        ", SQL=" + sql.toString();
        success = false;
        m_IDs = new Object[] { I_ZERO };
        log.log(Level.SEVERE, msg, e);
    //	throw new DBException(e);
    } finally //	Finish
    {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    loadComplete(success);
    return success;
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Savepoint(java.sql.Savepoint) SQLException(java.sql.SQLException) DBException(org.adempiere.exceptions.DBException) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 28 with DBException

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

the class Query method first.

/**
	 * Return first PO that match query criteria
	 * @return first PO
	 * @throws DBException
	 */
@SuppressWarnings("unchecked")
public <T extends PO> T first() throws DBException {
    T po = null;
    String sql = buildSQL(null, true);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        rs = createResultSet(pstmt);
        if (rs.next()) {
            po = (T) table.getPO(rs, trxName);
        }
    } catch (SQLException e) {
        log.log(Level.SEVERE, sql, e);
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return po;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 29 with DBException

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

the class Query method getIDs.

/**
	 * Get a Array with the IDs for this Query
	 * @return Get a Array with the IDs
	 */
public int[] getIDs() {
    String[] keys = table.getKeyColumns();
    if (keys.length != 1) {
        throw new DBException("Table " + table + " has 0 or more than 1 key columns");
    }
    StringBuffer selectClause = new StringBuffer("SELECT ");
    selectClause.append(keys[0]);
    selectClause.append(" FROM ").append(table.getTableName());
    String sql = buildSQL(selectClause, true);
    ArrayList<Integer> list = new ArrayList<Integer>();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        rs = createResultSet(pstmt);
        while (rs.next()) {
            list.add(rs.getInt(1));
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.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) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 30 with DBException

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

the class DB method getSQLValueTSEx.

/**
     * Get Timestamp 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 Timestamp getSQLValueTSEx(String trxName, String sql, Object... params) {
    Timestamp retValue = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = prepareStatement(sql, trxName);
        setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        if (rs.next())
            retValue = rs.getTimestamp(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) Timestamp(java.sql.Timestamp)

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