Package net.tirasa.connid.commons.db
Class SQLUtil
- java.lang.Object
-
- net.tirasa.connid.commons.db.SQLUtil
-
public final class SQLUtil extends Object
The SQL helper/util class.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Objectattribute2jdbcValue(Object value, int sqlType)Convert the attribute to expected jdbc type using java conversions Some database strategy sets all attributes as string, other convert them first and than set as native.static byte[]blob2ByteArray(Blob blobValue)Make a blob conversion.static voidcloseQuietly(Connection conn)Ignores any exception thrown by theConnectionparameter when closed, it also checks fornull.static voidcloseQuietly(ResultSet rset)static voidcloseQuietly(Statement stmt)Ignores any exception thrown by theStatement.close()method.static voidcloseQuietly(DatabaseConnection conn)Ignores any exception thrown by theConnectionparameter when closed, it also checks fornull.static Stringdate2String(Date value)Date to string.static intexecuteUpdateStatement(Connection conn, String sql, SQLParam... params)Executes DML sql statement.static Map<String,SQLParam>getColumnValues(ResultSet resultSet)Read one row from database result set and convert a columns to attribute set.static ConnectiongetDatasourceConnection(String datasourceName)Get the connection from the datasource.static ConnectiongetDatasourceConnection(String datasourceName, String user, org.identityconnectors.common.security.GuardedString password)Get the connection from the dataSource with specified user and password.static ConnectiongetDatasourceConnection(String datasourceName, String user, org.identityconnectors.common.security.GuardedString password, Properties env)Get the connection from the dataSource with specified user and password.static ConnectiongetDatasourceConnection(String datasourceName, Properties env)Get the connection from the datasource.static ConnectiongetDriverMangerConnection(String driver, String url, String login, org.identityconnectors.common.security.GuardedString password)Gets aConnectionusing the basic driver manager.static Class<?>getSQLAttributeType(int sqlType)Convert database type to connector supported set of attribute types Can be redefined for different databases.static SQLParamgetSQLParam(ResultSet resultSet, int i, String name, int sqlType)Retrieve the SQL value from result set.static Objectjdbc2AttributeValue(Object value)The conversion to required attribute type.static StringnormalizeNullValues(String sql, List<SQLParam> params, List<SQLParam> out)The null param vlaue normalizator.static voidrollbackQuietly(Connection conn)Ignores any exception thrown by theConnectionparameter when closed, it also checks fornull.static voidrollbackQuietly(DatabaseConnection conn)Ignores any exception thrown by theDatabaseConnectionparameter when closed, it also checks fornull.static List<Object[]>selectRows(Connection conn, String sql, SQLParam... params)Selects all rows from select.static ObjectselectSingleValue(Connection conn, String sql, SQLParam... params)Selects single value (first column) from select.static voidsetGuardedStringParam(PreparedStatement stmt, int idx, org.identityconnectors.common.security.GuardedString guard)The helper guardedString bind method.static voidsetParams(CallableStatement statement, List<SQLParam> params)Binds the "?"static voidsetParams(PreparedStatement statement, List<SQLParam> params)Binds the "?"static voidsetSQLParam(PreparedStatement stmt, int idx, SQLParam parm)Set a parameter to statement.static Booleanstring2Boolean(String val)Convert String to boolean.static Datestring2Date(String param)String to Date.static Timestring2Time(String param)String to Time.static Timestampstring2Timestamp(String param)Convert string to Timestampstatic Stringtime2String(Time value)Time to String format.static Stringtimestamp2String(Timestamp value)Convert timestamp to string.
-
-
-
Method Detail
-
getDatasourceConnection
public static Connection getDatasourceConnection(String datasourceName, Properties env)
Get the connection from the datasource.- Parameters:
datasourceName- datasource JNDI nameenv- properties- Returns:
- the connection get from default jndi context
-
getDatasourceConnection
public static Connection getDatasourceConnection(String datasourceName, String user, org.identityconnectors.common.security.GuardedString password, Properties env)
Get the connection from the dataSource with specified user and password.- Parameters:
datasourceName- datasource JNDI nameuser- DB userpassword- DB passwordenv- properties- Returns:
- the connection get from dataSource
-
getDatasourceConnection
public static Connection getDatasourceConnection(String datasourceName, String user, org.identityconnectors.common.security.GuardedString password)
Get the connection from the dataSource with specified user and password.- Parameters:
datasourceName- datasource JNDI nameuser- DB userpassword- DB password- Returns:
- the connection get from dataSource
-
getDatasourceConnection
public static Connection getDatasourceConnection(String datasourceName)
Get the connection from the datasource.- Parameters:
datasourceName- datasource JNDI name- Returns:
- the connection get from default jndi context
-
getDriverMangerConnection
public static Connection getDriverMangerConnection(String driver, String url, String login, org.identityconnectors.common.security.GuardedString password)
Gets aConnectionusing the basic driver manager.- Parameters:
driver- jdbc driver nameurl- jdbc connection urllogin- jdbc login namepassword- jdbc password- Returns:
- a valid connection
-
rollbackQuietly
public static void rollbackQuietly(Connection conn)
Ignores any exception thrown by theConnectionparameter when closed, it also checks fornull.- Parameters:
conn- JDBC connection to rollback.
-
rollbackQuietly
public static void rollbackQuietly(DatabaseConnection conn)
Ignores any exception thrown by theDatabaseConnectionparameter when closed, it also checks fornull.- Parameters:
conn- DatabaseConnection to rollback.
-
closeQuietly
public static void closeQuietly(Connection conn)
Ignores any exception thrown by theConnectionparameter when closed, it also checks fornull.- Parameters:
conn- JDBC connection to close.
-
closeQuietly
public static void closeQuietly(DatabaseConnection conn)
Ignores any exception thrown by theConnectionparameter when closed, it also checks fornull.- Parameters:
conn- DatabaseConnection to close.
-
closeQuietly
public static void closeQuietly(Statement stmt)
Ignores any exception thrown by theStatement.close()method.- Parameters:
stmt-Statementto close.
-
closeQuietly
public static void closeQuietly(ResultSet rset)
- Parameters:
rset-ResultSetto close quitely.
-
date2String
public static String date2String(Date value)
Date to string.- Parameters:
value- Date value- Returns:
- String value
-
time2String
public static String time2String(Time value)
Time to String format.- Parameters:
value- Time value- Returns:
- String value
-
timestamp2String
public static String timestamp2String(Timestamp value)
Convert timestamp to string.- Parameters:
value-Timestamp- Returns:
- the string value
-
string2Time
public static Time string2Time(String param)
String to Time.- Parameters:
param- String- Returns:
- the Time value
-
string2Date
public static Date string2Date(String param)
String to Date.- Parameters:
param- the String value- Returns:
- Date value
-
string2Timestamp
public static Timestamp string2Timestamp(String param)
Convert string to Timestamp- Parameters:
param- String value- Returns:
- Timestamp value
-
string2Boolean
public static Boolean string2Boolean(String val)
Convert String to boolean.- Parameters:
val- string value- Returns:
- Boolean value
-
normalizeNullValues
public static String normalizeNullValues(String sql, List<SQLParam> params, List<SQLParam> out)
The null param vlaue normalizator.- Parameters:
sql- SQL queryparams- listout- out param list- Returns:
- the modified string
-
blob2ByteArray
public static byte[] blob2ByteArray(Blob blobValue) throws SQLException
Make a blob conversion.- Parameters:
blobValue- blob- Returns:
- a converted value
- Throws:
SQLException- if anything goes wrong
-
setParams
public static void setParams(PreparedStatement statement, List<SQLParam> params) throws SQLException
Binds the "?" markers in SQL statement with the parameters given as values. It concentrates the replacement of all params.GuardedStringare handled so the password is never visible.- Parameters:
statement- SQL statementparams- aListof the object arguments- Throws:
SQLException- an exception in statement
-
setParams
public static void setParams(CallableStatement statement, List<SQLParam> params) throws SQLException
Binds the "?" markers in SQL statement with the parameters given as values. It concentrates the replacement of all params.GuardedStringare handled so the password is never visible.- Parameters:
statement- SQL statementparams- aListof the object arguments- Throws:
SQLException- an exception in statement
-
getColumnValues
public static Map<String,SQLParam> getColumnValues(ResultSet resultSet) throws SQLException
Read one row from database result set and convert a columns to attribute set.- Parameters:
resultSet- database data- Returns:
- The transformed attribute set
- Throws:
SQLException- if anything goes wrong
-
getSQLParam
public static SQLParam getSQLParam(ResultSet resultSet, int i, String name, int sqlType) throws SQLException
Retrieve the SQL value from result set.- Parameters:
resultSet- the result seti- indexname- param namesqlType- expected SQL type or Types.NULL for generic- Returns:
- the object return the retrieved object
- Throws:
SQLException- any SQL error
-
getSQLAttributeType
public static Class<?> getSQLAttributeType(int sqlType)
Convert database type to connector supported set of attribute types Can be redefined for different databases.- Parameters:
sqlType- #Types- Returns:
- a connector supported class
-
setSQLParam
public static void setSQLParam(PreparedStatement stmt, int idx, SQLParam parm) throws SQLException
Set a parameter to statement. The conversion to required database type is expected to be done.- Parameters:
stmt- the statement to setidx- index of the parameterparm- theSQLParamvalue- Throws:
SQLException- something wrong
-
jdbc2AttributeValue
public static Object jdbc2AttributeValue(Object value) throws SQLException
The conversion to required attribute type.- Parameters:
value- to be converted to an attribute- Returns:
- a attribute's supported object
- Throws:
SQLException- something is not ok
-
attribute2jdbcValue
public static Object attribute2jdbcValue(Object value, int sqlType) throws SQLException
Convert the attribute to expected jdbc type using java conversions Some database strategy sets all attributes as string, other convert them first and than set as native.- Parameters:
value- the value to be convertedsqlType- the target sql type- Returns:
- the converted object value
- Throws:
SQLException- any SQL error
-
setGuardedStringParam
public static void setGuardedStringParam(PreparedStatement stmt, int idx, org.identityconnectors.common.security.GuardedString guard) throws SQLException
The helper guardedString bind method.- Parameters:
stmt- to bind toidx- index of the objectguard- aGuardedStringparameter- Throws:
SQLException- any SQL error
-
selectSingleValue
public static Object selectSingleValue(Connection conn, String sql, SQLParam... params) throws SQLException
Selects single value (first column) from select. It fetches only first row, does not check whether more rows are returned by select. If no row is returned, returns null- Parameters:
conn- JDBC connectionsql- Select statement with or without parametersparams- Parameters to use in statement- Returns:
- first row and first column value
- Throws:
SQLException- any SQL error
-
selectRows
public static List<Object[]> selectRows(Connection conn, String sql, SQLParam... params) throws SQLException
Selects all rows from select. It usesResultSet.getMetaData()to find columns count and useResultSet.getObject(int)to retrieve column value.- Parameters:
conn- JDBC connectionsql- SQL select with or without paramsparams- SQL parameters- Returns:
- list of selected rows
- Throws:
SQLException- any SQL error
-
executeUpdateStatement
public static int executeUpdateStatement(Connection conn, String sql, SQLParam... params) throws SQLException
Executes DML sql statement. This can be useful to execute insert/update/delete or some database specific statement in one call- Parameters:
conn- connectionsql- SQL queryparams- SQL parameters- Returns:
- number of rows affected as defined by
PreparedStatement.executeUpdate() - Throws:
SQLException- any SQL error
-
-