Package org.compiere.util
Class DB
- java.lang.Object
-
- org.compiere.util.DB
-
public final class DB extends Object
General Database Interface- Version:
- $Id: DB.java,v 1.8 2006/10/09 00:22:29 jjanke Exp $ ---
- Author:
- Jorg Janke, Ashley Ramdass (Posterita)
- Modifications: removed static references to database connection and instead always get a new connection from database pool manager which manages all connections set rw/ro properties for the connection accordingly., Teo Sarca, SC ARHIPAC SERVICE SRL
- BF [ 1647864 ] WAN: delete record error
- FR [ 1884435 ] Add more DB.getSQLValue helper methods
- FR [ 1904460 ] DB.executeUpdate should handle Boolean params
- BF [ 1962568 ] DB.executeUpdate should handle null params
- FR [ 1984268 ] DB.executeUpdateEx should throw DBException
- FR [ 1986583 ] Add DB.executeUpdateEx(String, Object[], String)
- BF [ 2030233 ] Remove duplicate code from DB class
- FR [ 2107062 ] Add more DB.getKeyNamePairs methods
- FR [ 2448461 ] Introduce DB.getSQLValue*Ex methods
- FR [ 2781053 ] Introduce DB.getValueNamePairs
- FR [ 2818480 ] Introduce DB.createT_Selection helper method https://sourceforge.net/p/adempiere/feature-requests/757/, Teo Sarca, teo.sarca@gmail.com
- BF [ 2873324 ] DB.TO_NUMBER should be a static method https://sourceforge.net/p/adempiere/bugs/2160/
- FR [ 2873891 ] DB.getKeyNamePairs should use trxName https://sourceforge.net/p/adempiere/feature-requests/847/, Paul Bowden, phib BF 2900767 Zoom to child tab - inefficient queries
-
-
Field Summary
Fields Modifier and Type Field Description static StringSQLSTATEMENT_SEPARATORSQL Statement Separator "; "
-
Constructor Summary
Constructors Constructor Description DB()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static booleanafterMigration(Properties ctx)Check need for post Upgradestatic voidclose(ResultSet rs)convenient method to close result setstatic voidclose(ResultSet rs, Statement st)convenient method to close result set and statementstatic voidclose(Statement st)convenient method to close statementstatic voidclose(POResultSet<?> rs)convenient method to close aPOResultSetstatic voidcloseTarget()Close Targetstatic booleancommit(boolean throwException, String trxName)Commit - commit on RW connection.static booleanconnect()Connect to database and initialise all connections.static ConnectioncreateConnection(boolean autoCommit, boolean readOnly, int trxLevel)Create new Connection.static ConnectioncreateConnection(boolean autoCommit, int trxLevel)Create new Connection.static StatementcreateStatement()Create Read Only Statementstatic StatementcreateStatement(int resultSetType, int resultSetConcurrency, String trxName)Create Statement.static voidcreateT_Selection(int AD_PInstance_ID, Collection<Integer> selection, String trxName)Create persistent selection in T_Selection table remain this function for backward compatibility.static voidcreateT_SelectionNew(int AD_PInstance_ID, Collection<KeyNamePair> saveKeys, String trxName)Create persistent selection in T_Selection table saveKeys is map with key is rowID, value is list value of all viewID viewIDIndex is index of viewID need save.static intexecuteUpdate(String sql)Deprecated.static intexecuteUpdate(String sql, boolean ignoreError)Deprecated.static intexecuteUpdate(String sql, boolean ignoreError, String trxName)Execute Update.static intexecuteUpdate(String sql, boolean ignoreError, String trxName, int timeOut)Execute Update.static intexecuteUpdate(String sql, int param, boolean ignoreError, String trxName)Execute Update.static intexecuteUpdate(String sql, int param, boolean ignoreError, String trxName, int timeOut)Execute Update.static intexecuteUpdate(String sql, int param, String trxName)Execute Update.static intexecuteUpdate(String sql, int param, String trxName, int timeOut)Execute Update.static intexecuteUpdate(String sql, Object[] params, boolean ignoreError, String trxName)Execute Update.static intexecuteUpdate(String sql, Object[] params, boolean ignoreError, String trxName, int timeOut)Execute Update.static intexecuteUpdate(String sql, String trxName)Execute Update.static intexecuteUpdate(String sql, String trxName, int timeOut)Execute Update.static intexecuteUpdateEx(String sql, Object[] params, String trxName)Execute Update and throw exception.static intexecuteUpdateEx(String sql, Object[] params, String trxName, int timeOut)Execute Update and throw exception.static intexecuteUpdateEx(String sql, String trxName)Execute Update and throw exception.static intexecuteUpdateEx(String sql, String trxName, int timeOut)Execute Update and throw exception.static intexecuteUpdateMultiple(String sql, boolean ignoreError, String trxName)Execute multiple Update statements.static ConnectiongetConnectionID()Return everytime a new r/w no AutoCommit, Serializable connection.static ConnectiongetConnectionRO()Return read committed, read/only from pool.static ConnectiongetConnectionRW()static ConnectiongetConnectionRW(boolean createNew)Return (pooled) r/w AutoCommit, Serializable connection.static AdempiereDatabasegetDatabase()Get Database Driver.static AdempiereDatabasegetDatabase(String URL)Get Database Driver.static StringgetDatabaseInfo()Get Database Infostatic StringgetDocumentNo(int C_DocType_ID, String trxName)Deprecated.static StringgetDocumentNo(int C_DocType_ID, String trxName, boolean definite)Get Document No based on Document Typestatic StringgetDocumentNo(int C_DocType_ID, String trxName, boolean definite, PO po)Get Document No based on Document Typestatic StringgetDocumentNo(int AD_Client_ID, String TableName, String trxName)Get Document No from tablestatic StringgetDocumentNo(int AD_Client_ID, String TableName, String trxName, PO po)Get Document No from tablestatic StringgetDocumentNo(Properties ctx, int WindowNo, String TableName, boolean onlyDocType, String trxName)Get Document Number for current document.static int[]getIDsEx(String trxName, String sql, Object... params)Get Array of IDsstatic KeyNamePair[]getKeyNamePairs(String sql, boolean optional)Get Array of Key Name Pairsstatic KeyNamePair[]getKeyNamePairs(String sql, boolean optional, Object... params)Get Array of Key Name Pairsstatic KeyNamePair[]getKeyNamePairs(String sql, boolean optional, List<Object> params)Get Array of KeyNamePair items.static KeyNamePair[]getKeyNamePairs(String trxName, String sql, boolean optional, Object... params)Get Array of Key Name Pairsstatic intgetNextID(int AD_Client_ID, String TableName, String trxName)Get next number for Key column = 0 is Error.static intgetNextID(Properties ctx, String TableName, String trxName)Get next number for Key column = 0 is Errorstatic ConnectiongetReportingConnectionRO()Return a replica connection if possible, otherwise read committed, read/only from pool.static RowSetgetRowSet(String sql)Get Row Set.static List<List<Object>>getSQLArrayObjectsEx(String trxName, String sql, Object... params)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 consumptionstatic ExceptiongetSQLException(Exception e)Try to get the SQLException from Exceptionstatic intgetSQLValue(String trxName, String sql)static intgetSQLValue(String trxName, String sql, int int_param1)static intgetSQLValue(String trxName, String sql, int int_param1, int int_param2)static intgetSQLValue(String trxName, String sql, int int_param1, String str_param2)static intgetSQLValue(String trxName, String sql, Object... params)Get int Value from sqlstatic intgetSQLValue(String trxName, String sql, String str_param1)static intgetSQLValue(String trxName, String sql, List<Object> params)Get int Value from sqlstatic BigDecimalgetSQLValueBD(String trxName, String sql, int int_param1)static BigDecimalgetSQLValueBD(String trxName, String sql, Object... params)Get BigDecimal Value from sqlstatic BigDecimalgetSQLValueBD(String trxName, String sql, List<Object> params)Get BigDecimal Value from sqlstatic BigDecimalgetSQLValueBDEx(String trxName, String sql, Object... params)Get BigDecimal Value from sqlstatic BigDecimalgetSQLValueBDEx(String trxName, String sql, List<Object> params)Get BigDecimal Value from sqlstatic intgetSQLValueEx(String trxName, String sql, Object... params)Get int Value from sqlstatic intgetSQLValueEx(String trxName, String sql, List<Object> params)Get String Value from sqlstatic List<Object>getSQLValueObjectsEx(String trxName, String sql, Object... params)Get an array of objects from sql (one per each column on the select clause), column indexing starts with 0static StringgetSQLValueString(String trxName, String sql, int int_param1)static StringgetSQLValueString(String trxName, String sql, Object... params)Get String Value from sqlstatic StringgetSQLValueString(String trxName, String sql, List<Object> params)Get String Value from sqlstatic StringgetSQLValueStringEx(String trxName, String sql, Object... params)Get String Value from sqlstatic StringgetSQLValueStringEx(String trxName, String sql, List<Object> params)Get String Value from sqlstatic TimestampgetSQLValueTS(String trxName, String sql, Object... params)Get Timestamp Value from sqlstatic TimestampgetSQLValueTS(String trxName, String sql, List<Object> params)Get Timestamp Value from sqlstatic TimestampgetSQLValueTSEx(String trxName, String sql, Object... params)Get Timestamp Value from sqlstatic TimestampgetSQLValueTSEx(String trxName, String sql, List<Object> params)Get BigDecimal Value from sqlstatic ValueNamePair[]getValueNamePairs(String sql, boolean optional, List<Object> params)Get Array of ValueNamePair items.static StringinClauseForCSV(String columnName, String csv)static StringintersectClauseForCSV(String columnName, String csv)static booleanisBuildOK(Properties ctx)Check Build Version of Database against running clientstatic booleanisConnected()static booleanisConnected(boolean createNew)Is there a connection to the database ?static booleanisDatabaseOK(Properties ctx)Check database Version with Code versionstatic booleanisGenerateUUIDSupported()static booleanisOracle()Do we have an Oracle DB ?static booleanisPostgreSQL()Do we have a Postgre DB ?static booleanisRemoteObjects()Deprecated.static booleanisRemoteProcess()Deprecated.static booleanisSelectStatement(String sql)static booleanisSOTrx(String TableName, String whereClause)static booleanisSOTrx(String TableName, String whereClause, int windowNo)Is Sales Order Trx.static booleanisTableOrViewExists(String tableName)static voidmain(String[] args)Run Post Migration manuallystatic CallableStatementprepareCall(String sql)Prepare Forward Read Only Callstatic CallableStatementprepareCall(String SQL, int resultSetConcurrency, String trxName)Prepare Callstatic PreparedStatementprepareNormalReadReplicaStatement(String sql, String trxName)Prepare Read Replica Statementstatic CPreparedStatementprepareStatement(String sql)Deprecated.static CPreparedStatementprepareStatement(String sql, int resultSetType, int resultSetConcurrency)Deprecated.static CPreparedStatementprepareStatement(String sql, int resultSetType, int resultSetConcurrency, String trxName)Prepare Statement.static CPreparedStatementprepareStatement(String sql, String trxName)Prepare Statementstatic voidprintWarning(String comment, SQLWarning warning)Print SQL Warnings.static booleanrollback(boolean throwException, String trxName)Rollback - rollback on RW connection.static voidsetDBTarget(CConnection cc)Set connectionstatic voidsetParameter(PreparedStatement pstmt, int index, Object param)Set PreparedStatement's parameter.static voidsetParameters(PreparedStatement stmt, Object[] params)Set parameters for given statementstatic voidsetParameters(PreparedStatement stmt, List<?> params)Set parameters for given statementstatic StringsubsetClauseForCSV(String columnName, String csv)static StringTO_CHAR(String columnName, int displayType, String AD_Language)Create SQL for formatted Date, Numberstatic StringTO_DATE(Timestamp day)Create SQL TO Date String from Timestampstatic StringTO_DATE(Timestamp time, boolean dayOnly)Create SQL TO Date String from Timestampstatic StringTO_NUMBER(BigDecimal number, int displayType)Return number as string for INSERT statements with correct precisionstatic StringTO_STRING(String txt)Package Strings for SQL command in quotesstatic StringTO_STRING(String txt, int maxLength)Package Strings for SQL command in quotes.static voidupdateMail()Update Mail Settings for System Client and System User
-
-
-
Field Detail
-
SQLSTATEMENT_SEPARATOR
public static final String SQLSTATEMENT_SEPARATOR
SQL Statement Separator "; "- See Also:
- Constant Field Values
-
-
Method Detail
-
afterMigration
public static boolean afterMigration(Properties ctx)
Check need for post Upgrade- Parameters:
ctx- context- Returns:
- true if post upgrade ran - false if there was no need
-
updateMail
public static void updateMail()
Update Mail Settings for System Client and System User
-
setDBTarget
public static void setDBTarget(CConnection cc)
Set connection- Parameters:
cc- connection
-
connect
public static boolean connect()
Connect to database and initialise all connections.- Returns:
- True if success, false otherwise
-
isConnected
public static boolean isConnected()
- Returns:
- true, if connected to database
-
isConnected
public static boolean isConnected(boolean createNew)
Is there a connection to the database ?- Parameters:
createNew- If true, try to connect it not already connected- Returns:
- true, if connected to database
-
getConnectionRW
public static Connection getConnectionRW()
- Returns:
- Connection (r/w)
-
getConnectionRW
public static Connection getConnectionRW(boolean createNew)
Return (pooled) r/w AutoCommit, Serializable connection. For Transaction control use Trx.getConnection()- Parameters:
createNew- If true, try to create new connection if no existing connection- Returns:
- Connection (r/w)
-
getConnectionID
public static Connection getConnectionID()
Return everytime a new r/w no AutoCommit, Serializable connection. To be used to ID- Returns:
- Connection (r/w)
-
getConnectionRO
public static Connection getConnectionRO()
Return read committed, read/only from pool.- Returns:
- Connection (r/o)
-
getReportingConnectionRO
public static Connection getReportingConnectionRO()
Return a replica connection if possible, otherwise read committed, read/only from pool.- Returns:
- Connection (r/o)
-
createConnection
public static Connection createConnection(boolean autoCommit, int trxLevel)
Create new Connection. The connection must be closed explicitly by the application- Parameters:
autoCommit- auto committrxLevel- - Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, or Connection.TRANSACTION_READ_COMMITTED.- Returns:
- Connection connection
-
createConnection
public static Connection createConnection(boolean autoCommit, boolean readOnly, int trxLevel)
Create new Connection. The connection must be closed explicitly by the application- Parameters:
autoCommit- auto committrxLevel- - Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, or Connection.TRANSACTION_READ_COMMITTED.- Returns:
- Connection connection
-
getDatabase
public static AdempiereDatabase getDatabase()
Get Database Driver. Access to database specific functionality.- Returns:
- Adempiere Database Driver
-
getDatabase
public static AdempiereDatabase getDatabase(String URL)
Get Database Driver. Access to database specific functionality.- Parameters:
URL- JDBC connection url- Returns:
- Adempiere Database Driver
-
isOracle
public static boolean isOracle()
Do we have an Oracle DB ?- Returns:
- true if connected to Oracle
-
isPostgreSQL
public static boolean isPostgreSQL()
Do we have a Postgre DB ?- Returns:
- true if connected to PostgreSQL
-
getDatabaseInfo
public static String getDatabaseInfo()
Get Database Info- Returns:
- info
-
isDatabaseOK
public static boolean isDatabaseOK(Properties ctx)
Check database Version with Code version- Parameters:
ctx- context- Returns:
- true if Database version (date) is the same
-
isBuildOK
public static boolean isBuildOK(Properties ctx)
Check Build Version of Database against running client- Parameters:
ctx- context- Returns:
- true if Database version (date) is the same
-
closeTarget
public static void closeTarget()
Close Target
-
prepareCall
public static CallableStatement prepareCall(String sql)
Prepare Forward Read Only Call- Parameters:
sql- SQL- Returns:
- Callable Statement
-
prepareCall
public static CallableStatement prepareCall(String SQL, int resultSetConcurrency, String trxName)
Prepare Call- Parameters:
SQL- sqlresultSetConcurrency-trxName-- Returns:
- Callable Statement
-
prepareStatement
public static CPreparedStatement prepareStatement(String sql)
Deprecated.Prepare Statement- Parameters:
sql-- Returns:
- Prepared Statement
-
prepareStatement
public static CPreparedStatement prepareStatement(String sql, String trxName)
Prepare Statement- Parameters:
sql-trxName- transaction- Returns:
- Prepared Statement
-
prepareStatement
public static CPreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
Deprecated.Prepare Statement.- Parameters:
sql- sql statementresultSetType- - ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency- - ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE- Returns:
- Prepared Statement r/o or r/w depending on concur
-
prepareStatement
public static CPreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, String trxName)
Prepare Statement.- Parameters:
sql- sql statementresultSetType- - ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency- - ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLEtrxName- transaction name- Returns:
- Prepared Statement r/o or r/w depending on concur
-
createStatement
public static Statement createStatement()
Create Read Only Statement- Returns:
- Statement
-
createStatement
public static Statement createStatement(int resultSetType, int resultSetConcurrency, String trxName)
Create Statement.- Parameters:
resultSetType- - ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency- - ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLEtrxName- transaction name- Returns:
- Statement - either r/w ir r/o depending on concur
-
setParameters
public static void setParameters(PreparedStatement stmt, Object[] params) throws SQLException
Set parameters for given statement- Parameters:
stmt- statementsparams- parameters array; if null or empty array, no parameters are set- Throws:
SQLException
-
setParameters
public static void setParameters(PreparedStatement stmt, List<?> params) throws SQLException
Set parameters for given statement- Parameters:
stmt- statementsparams- parameters list; if null or empty list, no parameters are set- Throws:
SQLException
-
setParameter
public static void setParameter(PreparedStatement pstmt, int index, Object param) throws SQLException
Set PreparedStatement's parameter. Similar with callingpstmt.setObject(index, param)- Parameters:
pstmt-index-param-- Throws:
SQLException
-
executeUpdate
public static int executeUpdate(String sql)
Deprecated.Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sql- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, String trxName)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqltrxName- optional transaction name- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, String trxName, int timeOut)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqltrxName- optional transaction nametimeOut- optional timeout parameter- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, boolean ignoreError)
Deprecated.Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqlignoreError- if true, no execution error is reported- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, boolean ignoreError, String trxName)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqlignoreError- if true, no execution error is reportedtrxName- transaction- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, boolean ignoreError, String trxName, int timeOut)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqlignoreError- if true, no execution error is reportedtrxName- transactiontimeOut- optional timeOut parameter- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, int param, String trxName)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqlparam- int paramtrxName- transaction- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, int param, String trxName, int timeOut)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqlparam- int paramtrxName- transactiontimeOut- optional timeOut parameter- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, int param, boolean ignoreError, String trxName)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqlparam- int parameterignoreError- if true, no execution error is reportedtrxName- transaction- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, int param, boolean ignoreError, String trxName, int timeOut)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqlparam- int parameterignoreError- if true, no execution error is reportedtrxName- transactiontimeOut- optional timeOut parameter- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, Object[] params, boolean ignoreError, String trxName)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqlparams- array of parametersignoreError- if true, no execution error is reportedtrxName- optional transaction name- Returns:
- number of rows updated or -1 if error
-
executeUpdate
public static int executeUpdate(String sql, Object[] params, boolean ignoreError, String trxName, int timeOut)
Execute Update. saves "DBExecuteError" in Log- Parameters:
sql- sqlparams- array of parametersignoreError- if true, no execution error is reportedtrxName- optional transaction nametimeOut- optional timeOut parameter- Returns:
- number of rows updated or -1 if error
-
executeUpdateEx
public static int executeUpdateEx(String sql, Object[] params, String trxName) throws DBException
Execute Update and throw exception.- Parameters:
sql-params- statement parameterstrxName- transaction- Returns:
- number of rows updated
- Throws:
SQLExceptionDBException
-
executeUpdateEx
public static int executeUpdateEx(String sql, Object[] params, String trxName, int timeOut) throws DBException
Execute Update and throw exception.- Parameters:
sql-params- statement parameterstrxName- transactiontimeOut- optional timeOut parameter- Returns:
- number of rows updated
- Throws:
SQLExceptionDBException
-
executeUpdateMultiple
public static int executeUpdateMultiple(String sql, boolean ignoreError, String trxName)
Execute multiple Update statements. saves (last) "DBExecuteError" in Log- Parameters:
sql- multiple sql statements separated by "; " SQLSTATEMENT_SEPARATORignoreError- if true, no execution error is reportedtrxName- optional transaction name- Returns:
- number of rows updated or -1 if error
-
executeUpdateEx
public static int executeUpdateEx(String sql, String trxName) throws DBException
Execute Update and throw exception.- Throws:
DBException
-
executeUpdateEx
public static int executeUpdateEx(String sql, String trxName, int timeOut) throws DBException
Execute Update and throw exception.- Throws:
DBException
-
commit
public static boolean commit(boolean throwException, String trxName) throws SQLException, IllegalStateExceptionCommit - commit on RW connection. Is not required as RW connection is AutoCommit (exception: with transaction)- Parameters:
throwException- if true, re-throws exceptiontrxName- transaction name- Returns:
- true if not needed or success
- Throws:
SQLExceptionIllegalStateException
-
rollback
public static boolean rollback(boolean throwException, String trxName) throws SQLExceptionRollback - rollback on RW connection. Is has no effect as RW connection is AutoCommit (exception: with transaction)- Parameters:
throwException- if true, re-throws exceptiontrxName- transaction name- Returns:
- true if not needed or success
- Throws:
SQLException
-
getRowSet
public static RowSet getRowSet(String sql)
Get Row Set. When a Rowset is closed, it also closes the underlying connection. If the created RowSet is transfered by RMI, closing it makes no difference- Parameters:
sql- sql- Returns:
- row set or null
-
getSQLValueEx
public static int getSQLValueEx(String trxName, String sql, Object... params) throws DBException
Get int Value from sql- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- first value or -1 if not found
- Throws:
DBException- if there is any SQLException
-
getSQLValueEx
public static int getSQLValueEx(String trxName, String sql, List<Object> params)
Get String Value from sql- Parameters:
trxName- trxsql- sqlparams- collection of parameters- Returns:
- first value or -1
- Throws:
DBException- if there is any SQLException
-
getSQLValue
public static int getSQLValue(String trxName, String sql, Object... params)
Get int Value from sql- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- first value or -1 if not found or error
-
getSQLValue
public static int getSQLValue(String trxName, String sql, List<Object> params)
Get int Value from sql- Parameters:
trxName- trxsql- sqlparams- collection of parameters- Returns:
- first value or null
-
getSQLValueStringEx
public static String getSQLValueStringEx(String trxName, String sql, Object... params)
Get String Value from sql- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- first value or null
- Throws:
DBException- if there is any SQLException
-
getSQLValueStringEx
public static String getSQLValueStringEx(String trxName, String sql, List<Object> params)
Get String Value from sql- Parameters:
trxName- trxsql- sqlparams- collection of parameters- Returns:
- first value or null
- Throws:
DBException- if there is any SQLException
-
getSQLValueString
public static String getSQLValueString(String trxName, String sql, Object... params)
Get String Value from sql- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- first value or null
-
getSQLValueString
public static String getSQLValueString(String trxName, String sql, List<Object> params)
Get String Value from sql- Parameters:
trxName- trxsql- sqlparams- collection of parameters- Returns:
- first value or null
-
getSQLValueBDEx
public static BigDecimal getSQLValueBDEx(String trxName, String sql, Object... params) throws DBException
Get BigDecimal Value from sql- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- first value or null if not found
- Throws:
DBException- if there is any SQLException
-
getSQLValueBDEx
public static BigDecimal getSQLValueBDEx(String trxName, String sql, List<Object> params) throws DBException
Get BigDecimal Value from sql- Parameters:
trxName- trxsql- sqlparams- collection of parameters- Returns:
- first value or null if not found
- Throws:
DBException- if there is any SQLException
-
getSQLValueBD
public static BigDecimal getSQLValueBD(String trxName, String sql, Object... params)
Get BigDecimal Value from sql- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- first value or null
-
getSQLValueBD
public static BigDecimal getSQLValueBD(String trxName, String sql, List<Object> params)
Get BigDecimal Value from sql- Parameters:
trxName- trxsql- sqlparams- collection of parameters- Returns:
- first value or null
-
getSQLValueTSEx
public static Timestamp getSQLValueTSEx(String trxName, String sql, Object... params)
Get Timestamp Value from sql- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- first value or null
- Throws:
DBException- if there is any SQLException
-
getSQLValueTSEx
public static Timestamp getSQLValueTSEx(String trxName, String sql, List<Object> params) throws DBException
Get BigDecimal Value from sql- Parameters:
trxName- trxsql- sqlparams- collection of parameters- Returns:
- first value or null if not found
- Throws:
DBException- if there is any SQLException
-
getSQLValueTS
public static Timestamp getSQLValueTS(String trxName, String sql, Object... params)
Get Timestamp Value from sql- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- first value or null
-
getSQLValueTS
public static Timestamp getSQLValueTS(String trxName, String sql, List<Object> params)
Get Timestamp Value from sql- Parameters:
trxName- trxsql- sqlparams- collection of parameters- Returns:
- first value or null
-
getKeyNamePairs
public static KeyNamePair[] getKeyNamePairs(String sql, boolean optional)
Get Array of Key Name Pairs- Parameters:
sql- select with id / name as first / second columnoptional- if true (-1,"") is added- Returns:
- array of
KeyNamePair - See Also:
getKeyNamePairs(String, boolean, Object...)
-
getKeyNamePairs
public static KeyNamePair[] getKeyNamePairs(String sql, boolean optional, Object... params)
Get Array of Key Name Pairs- Parameters:
sql- select with id / name as first / second columnoptional- if true (-1,"") is addedparams- query parameters
-
getKeyNamePairs
public static KeyNamePair[] getKeyNamePairs(String trxName, String sql, boolean optional, Object... params)
Get Array of Key Name Pairs- Parameters:
trxName-sql- select with id / name as first / second columnoptional- if true (-1,"") is addedparams- query parameters
-
getIDsEx
public static int[] getIDsEx(String trxName, String sql, Object... params) throws DBException
Get Array of IDs- Parameters:
trxName-sql- select with id as first columnparams- query parameters- Throws:
DBException- if there is any SQLException
-
isSOTrx
public static boolean isSOTrx(String TableName, String whereClause, int windowNo)
Is Sales Order Trx. Assumes Sales Order. Queries IsSOTrx of table with where clause- Parameters:
TableName- tablewhereClause- where clausewindowNo-- Returns:
- true (default) or false if tested that not SO
-
getNextID
public static int getNextID(Properties ctx, String TableName, String trxName)
Get next number for Key column = 0 is Error. * @param ctx client- Parameters:
TableName- table nametrxName- optionl transaction name- Returns:
- next no
-
getNextID
public static int getNextID(int AD_Client_ID, String TableName, String trxName)Get next number for Key column = 0 is Error.- Parameters:
AD_Client_ID- clientTableName- table nametrxName- optional Transaction Name- Returns:
- next no
-
getDocumentNo
public static String getDocumentNo(int C_DocType_ID, String trxName)
Deprecated.Get Document No based on Document Type (backward compatibility)- Parameters:
C_DocType_ID- document typetrxName- optional Transaction Name- Returns:
- document no or null
-
getDocumentNo
public static String getDocumentNo(int C_DocType_ID, String trxName, boolean definite)
Get Document No based on Document Type- Parameters:
C_DocType_ID- document typetrxName- optional Transaction Namedefinite- asking for a definitive or temporary sequence- Returns:
- document no or null
-
getDocumentNo
public static String getDocumentNo(int C_DocType_ID, String trxName, boolean definite, PO po)
Get Document No based on Document Type- Parameters:
C_DocType_ID- document typetrxName- optional Transaction Namedefinite- asking for a definitive or temporary sequencepo- PO- Returns:
- document no or null
-
getDocumentNo
public static String getDocumentNo(int AD_Client_ID, String TableName, String trxName)
Get Document No from table- Parameters:
AD_Client_ID- clientTableName- table nametrxName- optional Transaction Name- Returns:
- document no or null
-
getDocumentNo
public static String getDocumentNo(int AD_Client_ID, String TableName, String trxName, PO po)
Get Document No from table- Parameters:
AD_Client_ID- clientTableName- table nametrxName- optional Transaction Namepo-- Returns:
- document no or null
-
getDocumentNo
public static String getDocumentNo(Properties ctx, int WindowNo, String TableName, boolean onlyDocType, String trxName)
Get Document Number for current document.
- first search for DocType based Document No - then Search for DocumentNo based on TableName- Parameters:
ctx- contextWindowNo- windowTableName- tableonlyDocType- Do not search for document no based on TableNametrxName- optional Transaction Name- Returns:
- DocumentNo or null, if no doc number defined
-
isRemoteObjects
public static boolean isRemoteObjects()
Deprecated.Is this a remote client connection. Deprecated, always return false.- Returns:
- true if client and RMI or Objects on Server
-
isRemoteProcess
public static boolean isRemoteProcess()
Deprecated.Is this a remote client connection Deprecated, always return false.- Returns:
- true if client and RMI or Process on Server
-
printWarning
public static void printWarning(String comment, SQLWarning warning)
Print SQL Warnings.
Usage: DB.printWarning("comment", rs.getWarnings());- Parameters:
comment- commentwarning- warning
-
TO_DATE
public static String TO_DATE(Timestamp time, boolean dayOnly)
Create SQL TO Date String from Timestamp- Parameters:
time- Date to be converteddayOnly- true if time set to 00:00:00- Returns:
- TO_DATE('2001-01-30 18:10:20',''YYYY-MM-DD HH24:MI:SS') or TO_DATE('2001-01-30',''YYYY-MM-DD')
-
TO_DATE
public static String TO_DATE(Timestamp day)
Create SQL TO Date String from Timestamp- Parameters:
day- day time- Returns:
- TO_DATE String (day only)
-
TO_CHAR
public static String TO_CHAR(String columnName, int displayType, String AD_Language)
Create SQL for formatted Date, Number- Parameters:
columnName- the column name in the SQLdisplayType- Display TypeAD_Language- 6 character language setting (from Env.LANG_*)- Returns:
- TRIM(TO_CHAR(columnName,'999G999G999G990D00','NLS_NUMERIC_CHARACTERS='',.''')) or TRIM(TO_CHAR(columnName,'TM9')) depending on DisplayType and Language
- See Also:
DisplayType,Env
-
TO_NUMBER
public static String TO_NUMBER(BigDecimal number, int displayType)
Return number as string for INSERT statements with correct precision- Parameters:
number- numberdisplayType- display Type- Returns:
- number as string
-
TO_STRING
public static String TO_STRING(String txt)
Package Strings for SQL command in quotes- Parameters:
txt- String with text- Returns:
- escaped string for insert statement (NULL if null)
-
TO_STRING
public static String TO_STRING(String txt, int maxLength)
Package Strings for SQL command in quotes.- include in ' (single quotes) - replace ' with ''- Parameters:
txt- String with textmaxLength- Maximum Length of content or 0 to ignore- Returns:
- escaped string for insert statement (NULL if null)
-
close
public static void close(ResultSet rs)
convenient method to close result set- Parameters:
rs-
-
close
public static void close(Statement st)
convenient method to close statement- Parameters:
st-
-
close
public static void close(ResultSet rs, Statement st)
convenient method to close result set and statement- Parameters:
rs- result setst- statement- See Also:
close(ResultSet),close(Statement)
-
close
public static void close(POResultSet<?> rs)
convenient method to close aPOResultSet- Parameters:
rs- result set- See Also:
POResultSet.close()
-
getSQLException
public static Exception getSQLException(Exception e)
Try to get the SQLException from Exception- Parameters:
e- Exception- Returns:
- SQLException if found or provided exception elsewhere
-
main
public static void main(String[] args)
Run Post Migration manually- Parameters:
args- ignored
-
getSQLValue
public static int getSQLValue(String trxName, String sql, int int_param1, int int_param2)
-
getSQLValue
public static int getSQLValue(String trxName, String sql, int int_param1, String str_param2)
-
getSQLValueString
public static String getSQLValueString(String trxName, String sql, int int_param1)
-
getSQLValueBD
public static BigDecimal getSQLValueBD(String trxName, String sql, int int_param1)
-
getValueNamePairs
public static ValueNamePair[] getValueNamePairs(String sql, boolean optional, List<Object> params)
Get Array of ValueNamePair items.Example: String sql = "SELECT Name, Description FROM AD_Ref_List WHERE AD_Reference_ID=?"; ValueNamePair[] list = DB.getValueNamePairs(sql, false, params);
- Parameters:
sql- SELECT Value_Column, Name_Column FROM ...optional- ifValueNamePair.EMPTYis addedparams- query parameters- Returns:
- array of
ValueNamePairor empty array - Throws:
DBException- if there is any SQLException
-
getKeyNamePairs
public static KeyNamePair[] getKeyNamePairs(String sql, boolean optional, List<Object> params)
Get Array of KeyNamePair items.Example: String sql = "SELECT C_City_ID, Name FROM C_City WHERE C_City_ID=?"; KeyNamePair[] list = DB.getKeyNamePairs(sql, false, params);
- Parameters:
sql- SELECT ID_Column, Name_Column FROM ...optional- ifValueNamePair.EMPTYis addedparams- query parameters- Returns:
- array of
KeyNamePairor empty array - Throws:
DBException- if there is any SQLException
-
createT_Selection
public static void createT_Selection(int AD_PInstance_ID, Collection<Integer> selection, String trxName)Create persistent selection in T_Selection table remain this function for backward compatibility. refer: IDEMPIERE-1970- Parameters:
AD_PInstance_ID-selection-trxName-
-
createT_SelectionNew
public static void createT_SelectionNew(int AD_PInstance_ID, Collection<KeyNamePair> saveKeys, String trxName)Create persistent selection in T_Selection table saveKeys is map with key is rowID, value is list value of all viewID viewIDIndex is index of viewID need save.- Parameters:
AD_PInstance_ID-saveKeys-trxName-
-
isGenerateUUIDSupported
public static boolean isGenerateUUIDSupported()
- Returns:
- true if current db have working generate_uuid function. generate_uuid doesn't work on 64 bit postgresql on windows yet.
-
isTableOrViewExists
public static boolean isTableOrViewExists(String tableName)
- Parameters:
tableName-- Returns:
- true if table or view with name=tableName exists in db
-
getSQLValueObjectsEx
public static List<Object> getSQLValueObjectsEx(String trxName, String sql, Object... params)
Get an array of objects from sql (one per each column on the select clause), column indexing starts with 0- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- null if not found
- Throws:
DBException- if there is any SQLException
-
getSQLArrayObjectsEx
public static List<List<Object>> getSQLArrayObjectsEx(String trxName, String sql, Object... params)
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- Parameters:
trxName- trxsql- sqlparams- array of parameters- Returns:
- null if not found
- Throws:
DBException- if there is any SQLException
-
prepareNormalReadReplicaStatement
public static PreparedStatement prepareNormalReadReplicaStatement(String sql, String trxName)
Prepare Read Replica Statement- Parameters:
sql- sql statementtrxName- transaction- Returns:
- Prepared Statement (from replica if possible, otherwise normal statement)
-
inClauseForCSV
public static String inClauseForCSV(String columnName, String csv)
- Parameters:
columnName-csv- comma separated value- Returns:
- IN clause
-
subsetClauseForCSV
public static String subsetClauseForCSV(String columnName, String csv)
- Parameters:
columnName-csv-- Returns:
- subset sql clause
-
intersectClauseForCSV
public static String intersectClauseForCSV(String columnName, String csv)
- Parameters:
columnName-csv-- Returns:
- intersect sql clause
-
isSelectStatement
public static boolean isSelectStatement(String sql)
- Parameters:
sql-- Returns:
- true if it is select sql statement
-
-