From 7c3dd909e7aab171df8e63ed5165cb77f3ac74b6 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Wed, 16 Apr 2014 13:29:34 -0500 Subject: [PATCH] IDEMPIERE-1907 DB helpers for multi-row and multi-column queries / committing into r2 as is harmless to add unused new helpers, but worthy for plugins and extensions --- .../src/org/compiere/util/DB.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/org.adempiere.base/src/org/compiere/util/DB.java b/org.adempiere.base/src/org/compiere/util/DB.java index f3078b7920..2b97a1a571 100644 --- a/org.adempiere.base/src/org/compiere/util/DB.java +++ b/org.adempiere.base/src/org/compiere/util/DB.java @@ -25,6 +25,7 @@ import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.Statement; @@ -2361,4 +2362,92 @@ public final class DB } return false; } + + /** + * Get an array of objects from sql (one per each column on the select clause), column indexing starts with 0 + * @param trxName trx + * @param sql sql + * @param params array of parameters + * @return null if not found + * @throws DBException if there is any SQLException + */ + public static List getSQLValueObjectsEx(String trxName, String sql, Object... params) { + List retValue = new ArrayList(); + PreparedStatement pstmt = null; + ResultSet rs = null; + try + { + pstmt = prepareStatement(sql, trxName); + setParameters(pstmt, params); + rs = pstmt.executeQuery(); + ResultSetMetaData rsmd = rs.getMetaData(); + if (rs.next()) { + for (int i=1; i<=rsmd.getColumnCount(); i++) { + Object obj = rs.getObject(i); + if (rs.wasNull()) + retValue.add(null); + else + retValue.add(obj); + } + } else { + retValue = null; + } + } + catch (SQLException e) + { + throw new DBException(e, sql); + } + finally + { + close(rs, pstmt); + rs = null; pstmt = null; + } + return retValue; + } + + /** + * Get an array of arrays of objects from sql (one per each row, and one per each column on the select clause), column indexing starts with 0 + * WARNING: This method must be used just for queries returning few records, using it for many records implies heavy memory consumption + * @param trxName trx + * @param sql sql + * @param params array of parameters + * @return null if not found + * @throws DBException if there is any SQLException + */ + public static List> getSQLArrayObjectsEx(String trxName, String sql, Object... params) { + List> rowsArray = new ArrayList>(); + PreparedStatement pstmt = null; + ResultSet rs = null; + try + { + pstmt = prepareStatement(sql, trxName); + setParameters(pstmt, params); + rs = pstmt.executeQuery(); + ResultSetMetaData rsmd = rs.getMetaData(); + while (rs.next()) { + List retValue = new ArrayList(); + for (int i=1; i<=rsmd.getColumnCount(); i++) { + Object obj = rs.getObject(i); + if (rs.wasNull()) + retValue.add(null); + else + retValue.add(obj); + } + rowsArray.add(retValue); + } + } + catch (SQLException e) + { + throw new DBException(e, sql); + } + finally + { + close(rs, pstmt); + rs = null; pstmt = null; + } + if (rowsArray.size() == 0) + return null; + return rowsArray; + } + } // DB