com.stratelia.webactiv.util
Class DBUtil

java.lang.Object
  extended by com.stratelia.webactiv.util.DBUtil

public class DBUtil
extends Object


Nested Class Summary
static class DBUtil.SelectResultRowProcessor<ROW_ENTITY>
          Result Set Row Processor
 
Method Summary
static StringBuilder appendListOfParameters(StringBuilder sqlQuery, Collection<?> parameters)
          Centralization in order to build easily a SQL in clause.
static StringBuilder appendListOfParameters(StringBuilder sqlQuery, Collection<?> parameters, Collection<?> allParameters)
          Centralization in order to build easily a SQL in clause.
static
<O> StringBuilder
appendParameter(StringBuilder sqlQuery, String sqlPart, O paramValue, Collection<O> parameters)
          Centralization in order to populate the prepare statement parameters.
static
<O> StringBuilder
appendSaveParameter(StringBuilder sqlQuery, String paramName, O paramValue, boolean isInsert, Collection<O> parameters)
          Centralization in order to populate the prepare statement parameters.
static void clearTestInstance()
           
static void close(Connection connection)
           
static void close(ResultSet rs)
           
static void close(ResultSet rs, Statement st)
           
static void close(Statement st)
           
static
<O> long
executeUpdate(Connection con, List<org.apache.commons.lang3.tuple.Pair<String,List<O>>> updateQueries)
          Update query executor.
static long executeUpdate(Connection con, String updateQuery, Object... parameters)
           
static long executeUpdate(Connection con, String updateQuery, Object parameters)
          Update query executor.
static Set<String> getAllTableNames()
          Gets all table names.
static int getDateFieldLength()
           
static Date getDateFromLong(ResultSet resultSet, int index)
          Gets a Date value from a Long value from a current result set.
static DBUtil getInstance()
           
static DBUtil getInstanceForTest(Connection connectionForTest)
           
static Long getLong(ResultSet resultSet, int index)
          Gets a long value from a current result set.
static int getMaxFromTable(Connection con, String tableName, String idName)
           
protected static int getMaxId(Connection privateConnection, String tableName, String idName)
           
static int getNextId(Connection connection, String tableName, String idName)
          Return a new unique Id for a table.
static int getNextId(String tableName, String idName)
          Return a new unique Id for a table.
static int getTextAreaLength()
           
static int getTextFieldLength()
           
static int getTextMaxiLength()
           
static String getUniqueId()
          Returns a new unique identifier of type string.
static boolean isSqlDefined(String sqlValue)
          Indicates if the specified value is defined in point of view of SQL.
static Connection makeConnection(String dbName)
          fabrique une nouvelle connection
static void rollback(Connection connection)
           
static
<ROW_ENTITY>
List<ROW_ENTITY>
select(Connection con, String updateQuery, Object parameters, DBUtil.SelectResultRowProcessor<ROW_ENTITY> rowProcess)
          Update query executor.
static
<O> long
selectCount(Connection con, String selectCountQuery, Collection<O> params)
          Select count SQL query executor.
static
<O> long
selectCount(Connection con, String selectCountQuery, O idParam)
          Select count SQL query executor.
static
<O> void
setParameters(PreparedStatement preparedStatement, Collection<O> parameters)
          Centralization in order to sets the parameters on a prepare statement.
static
<ENTITY> ENTITY
unique(List<ENTITY> entities)
          Gets from a entity list the unique entity expected.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getDateFieldLength

public static int getDateFieldLength()
Returns:
the DateFieldLength

getTextMaxiLength

public static int getTextMaxiLength()
Returns:
the TextMaxiLength

getTextAreaLength

public static int getTextAreaLength()
Returns:
the TextAreaLength

getTextFieldLength

public static int getTextFieldLength()
Returns:
the TextFieldLength

getInstance

public static DBUtil getInstance()

getInstanceForTest

public static DBUtil getInstanceForTest(Connection connectionForTest)

clearTestInstance

public static void clearTestInstance()

makeConnection

public static Connection makeConnection(String dbName)
fabrique une nouvelle connection

Parameters:
dbName - le nom de la base de donnée
Returns:
a new connection to the database.
Throws:
UtilException

getUniqueId

public static String getUniqueId()
Returns a new unique identifier of type string.

Returns:
a new unique string identifier.

getNextId

public static int getNextId(String tableName,
                            String idName)
                     throws UtilException
Return a new unique Id for a table.

Parameters:
tableName - the name of the table.
idName - the name of the column.
Returns:
a unique id.
Throws:
UtilException

getNextId

public static int getNextId(Connection connection,
                            String tableName,
                            String idName)
                     throws SQLException
Return a new unique Id for a table.

Parameters:
connection - the JDBC connection.
tableName - the name of the table.
idName - the name of the column.
Returns:
a unique id.
Throws:
SQLException

getMaxId

protected static int getMaxId(Connection privateConnection,
                              String tableName,
                              String idName)
                       throws SQLException
Throws:
SQLException

getMaxFromTable

public static int getMaxFromTable(Connection con,
                                  String tableName,
                                  String idName)

close

public static void close(ResultSet rs,
                         Statement st)

close

public static void close(Statement st)

close

public static void close(ResultSet rs)

close

public static void close(Connection connection)

rollback

public static void rollback(Connection connection)

getAllTableNames

public static Set<String> getAllTableNames()
Gets all table names.

Returns:

isSqlDefined

public static boolean isSqlDefined(String sqlValue)
Indicates if the specified value is defined in point of view of SQL.

Parameters:
sqlValue - the value to verify.
Returns:
true if defined, false otherwise.

selectCount

public static <O> long selectCount(Connection con,
                                   String selectCountQuery,
                                   O idParam)
                        throws SQLException
Select count SQL query executor.

Parameters:
con -
selectCountQuery -
idParam -
Throws:
SQLException

selectCount

public static <O> long selectCount(Connection con,
                                   String selectCountQuery,
                                   Collection<O> params)
                        throws SQLException
Select count SQL query executor.

Parameters:
con -
selectCountQuery -
params -
Throws:
SQLException

select

public static <ROW_ENTITY> List<ROW_ENTITY> select(Connection con,
                                                   String updateQuery,
                                                   Object parameters,
                                                   DBUtil.SelectResultRowProcessor<ROW_ENTITY> rowProcess)
                               throws SQLException
Update query executor.

Parameters:
con -
updateQuery -
parameters -
Throws:
SQLException

unique

public static <ENTITY> ENTITY unique(List<ENTITY> entities)
Gets from a entity list the unique entity expected.

Parameters:
entities - the entity list.
Returns:
the unique entity result.
Throws:
IllegalArgumentException - if it exists more than one entity in the specified list.

executeUpdate

public static <O> long executeUpdate(Connection con,
                                     List<org.apache.commons.lang3.tuple.Pair<String,List<O>>> updateQueries)
                          throws SQLException
Update query executor.

Parameters:
con -
updateQueries -
Throws:
SQLException

executeUpdate

public static long executeUpdate(Connection con,
                                 String updateQuery,
                                 Object... parameters)
                          throws SQLException
Throws:
SQLException

executeUpdate

public static long executeUpdate(Connection con,
                                 String updateQuery,
                                 Object parameters)
                          throws SQLException
Update query executor.

Parameters:
con -
updateQuery -
parameters -
Throws:
SQLException

appendSaveParameter

public static <O> StringBuilder appendSaveParameter(StringBuilder sqlQuery,
                                                    String paramName,
                                                    O paramValue,
                                                    boolean isInsert,
                                                    Collection<O> parameters)
Centralization in order to populate the prepare statement parameters.

Parameters:
sqlQuery -
paramName -
paramValue -
isInsert -
parameters -
Returns:

appendParameter

public static <O> StringBuilder appendParameter(StringBuilder sqlQuery,
                                                String sqlPart,
                                                O paramValue,
                                                Collection<O> parameters)
Centralization in order to populate the prepare statement parameters.

Parameters:
sqlQuery -
sqlPart -
parameters -
Returns:

appendListOfParameters

public static StringBuilder appendListOfParameters(StringBuilder sqlQuery,
                                                   Collection<?> parameters)
Centralization in order to build easily a SQL in clause.

Parameters:
sqlQuery -
parameters -
Returns:

appendListOfParameters

public static StringBuilder appendListOfParameters(StringBuilder sqlQuery,
                                                   Collection<?> parameters,
                                                   Collection<?> allParameters)
Centralization in order to build easily a SQL in clause.

Parameters:
sqlQuery -
parameters -
allParameters -
Returns:

setParameters

public static <O> void setParameters(PreparedStatement preparedStatement,
                                     Collection<O> parameters)
                          throws SQLException
Centralization in order to sets the parameters on a prepare statement.

Parameters:
preparedStatement -
parameters -
Throws:
SQLException

getLong

public static Long getLong(ResultSet resultSet,
                           int index)
                    throws SQLException
Gets a long value from a current result set.

Parameters:
resultSet -
index -
Returns:
the long value if it exists, null otherwise.
Throws:
SQLException

getDateFromLong

public static Date getDateFromLong(ResultSet resultSet,
                                   int index)
                            throws SQLException
Gets a Date value from a Long value from a current result set.

Parameters:
resultSet -
index -
Returns:
the Date value if it exists a long value, null otherwise.
Throws:
SQLException


Copyright © 2016 Silverpeas. All Rights Reserved.