public class Utils
extends java.lang.Object
Constructor and Description |
---|
Utils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
buildQuestionMarksString(int pParametersCount)
Returns a
String
containing pParametersCount question marks separated by
commas. |
static java.lang.String |
concatStrings(java.util.List pStrings,
java.lang.String pPrefix,
java.lang.String pSuffix,
java.lang.String pSeparator)
Returns a
String containing the input strings concatenated
together. |
static java.lang.String |
createDeleteQuery(java.lang.String pFullyQualifiedTableName,
java.lang.String pWhereClause)
Returns a
String containing a SQL DELETE statement with a
WHERE clause for a given table. |
static java.lang.String |
createInsertQuery(java.util.List pColumnsToInsert,
java.lang.String pFullyQualifiedTableName)
Returns a
String containing a SQL INSERT statement for a
given table with a list of columns to insert into. |
static java.lang.String |
createSelectQuery(java.util.List pColumnsToSelect,
java.lang.String pFullyQualifiedTableName,
java.lang.String pWhereClause,
java.lang.String pOrderByClause)
Returns a
String containing a SQL SELECT query for the given
table, list of columns, WHERE clause and ORDER BY clause. |
static java.lang.String |
createUpdateQuery(java.util.List pColumnsToUpdate,
java.lang.String pFullyQualifiedTableName,
java.lang.String pWhereClause)
Returns a
String containing a SQL UPDATE query for a given
table with a list of columns to update and a given WHERE clause. |
static java.lang.String |
createWhereClause(QueryFilter pQueryFilter)
Returns a
String containing a SQL WHERE clause generated from a QueryFilter . |
static java.lang.String |
createWhereClause(UpdateDeleteFilter pUpdateDeleteFilter)
Returns a
String containing a SQL WHERE clause generated from a UpdateDeleteFilter . |
static java.sql.Connection |
getConnectionFromDataSource(java.lang.String pDataSourceName,
boolean pAutoCommit)
Returns a JDBC Connection created from the local JNDI datasource called
pDataSourceName . |
static void |
setParameters(ColumnsMeta pColumnsMeta,
java.sql.PreparedStatement pStatement,
ManagedEntity pSourceManagedEntity,
java.util.List pColsToSet)
Sets the parameters of a JDBC
PreparedStatement using values
from a ManagedEntity . |
static void |
setParameters(ColumnsMeta pColumnsMeta,
java.sql.PreparedStatement pStatement,
ManagedEntity pSourceManagedEntity,
java.util.List pColsToSet,
int pFirstParameterIdx)
Sets the parameters of a JDBC
PreparedStatement using values
from a ManagedEntity , starting with the given parameter
number.For each column name in the pColsToSet list, the value from
the matching column in pSourceManagedEntity is set to each
parameter in the pStatement , starting at parameter number
pFirstParameterIdx . |
public static java.sql.Connection getConnectionFromDataSource(java.lang.String pDataSourceName, boolean pAutoCommit) throws DataServiceException
pDataSourceName
. The datasource must be defined as a local
datasource in the application server executing the web service.pDataSourceName
- JNDI datasource namepAutoCommit
- autocommit mode for the connection. True
for Autocommit.DataServiceException
- if an error occurs while retrieving the
datasource or creating the connectionpublic static java.lang.String concatStrings(java.util.List pStrings, java.lang.String pPrefix, java.lang.String pSuffix, java.lang.String pSeparator)
String
containing the input strings concatenated
together. Each item of the list is prefixed by pPrefix
and suffixed by pSuffix
. The strings are separated by
pSeparator
.
Utils.concatStrings(Arrays.asList(new String[]{"a", "b", "c"}, null, null, ", ")
returns "a, b, c"
Utils.concatStrings(Arrays.asList(new String[]{"a", "b", "c"}, null, " AND ", "=?")
returns "a=? AND b=? AND c=?"
pStrings
- list of string
objects to concatenatepPrefix
- string to add before each itempSuffix
- string to add after each itempSeparator
- string separatorpublic static java.lang.String buildQuestionMarksString(int pParametersCount)
String
containing pParametersCount
question marks separated by
commas. For example, Utils.buildQuestionMarksString(3)
returns "?, ?, ?"
pParametersCount
- number of question marks in the listString
containing a comma-separated list of question
markspublic static void setParameters(ColumnsMeta pColumnsMeta, java.sql.PreparedStatement pStatement, ManagedEntity pSourceManagedEntity, java.util.List pColsToSet, int pFirstParameterIdx) throws java.sql.SQLException
PreparedStatement
using values
from a ManagedEntity
, starting with the given parameter
number.pColsToSet
list, the value from
the matching column in pSourceManagedEntity
is set to each
parameter in the pStatement
, starting at parameter number
pFirstParameterIdx
. The JDBC type for the parameters are
taken from the ColumnsMeta
object.pColumnsMeta
- ColumnsMeta
object containing the
columns' JDBC data typespStatement
- PreparedStatement
with parameters to be
setpSourceManagedEntity
- ManagedEntity
containing the
values to setpColsToSet
- list of column names to setpFirstParameterIdx
- position of the first parameter in the
PreparedStatement
to set. Note that JDBC parameters
indexes start at position 1.java.sql.SQLException
- thrown if an error occurs while setting the parameterpublic static void setParameters(ColumnsMeta pColumnsMeta, java.sql.PreparedStatement pStatement, ManagedEntity pSourceManagedEntity, java.util.List pColsToSet) throws java.sql.SQLException
PreparedStatement
using values
from a ManagedEntity
. This method is equivalent to a call
to setParameters
with pFirstParametersIdx
equals to 1.pColumnsMeta
- ColumnsMeta
object containing the
columns' JDBC data typespStatement
- PreparedStatement
with parameters to be
setpSourceManagedEntity
- ManagedEntity
containing the
values to setpColsToSet
- list of column names to setjava.sql.SQLException
- thrown if an error occurs while setting the parameterpublic static java.lang.String createSelectQuery(java.util.List pColumnsToSelect, java.lang.String pFullyQualifiedTableName, java.lang.String pWhereClause, java.lang.String pOrderByClause) throws DataServiceException
String
containing a SQL SELECT query for the given
table, list of columns, WHERE clause and ORDER BY clause.pColumnsToSelect
- list of column names to retrieve in the SELECT
statementpFullyQualifiedTableName
- fully qualified name of the table on which
the operation is carried outpWhereClause
- SQL WHERE Clause for filtering data from the table.
This clause should not include the WHERE keyword. If it is not
specified, the clause will not be generated in the querypOrderByClause
- SQL ORDER BY Clause for ordering data from the table.
This clause should not include the ORDER BY keyword. If it is not
specified, the ORDER BY clause will not be generated in the queryDataServiceException
public static java.lang.String createInsertQuery(java.util.List pColumnsToInsert, java.lang.String pFullyQualifiedTableName) throws DataServiceException
String
containing a SQL INSERT statement for a
given table with a list of columns to insert into.pColumnsToInsert
- list of column names to insert via the INSERT
statementpFullyQualifiedTableName
- fully qualified name of the table on which
the operation is carried outDataServiceException
- thrown if the list of columns is empty.public static java.lang.String createUpdateQuery(java.util.List pColumnsToUpdate, java.lang.String pFullyQualifiedTableName, java.lang.String pWhereClause) throws DataServiceException
String
containing a SQL UPDATE query for a given
table with a list of columns to update and a given WHERE clause.pColumnsToUpdate
- list of column names to update.pFullyQualifiedTableName
- fully qualified name of the table on which
the operation is carried outpWhereClause
- SQL WHERE Clause filtering data to update in the table.
This clause should not include the WHERE keyword. If it is not
specified, the clause will not be generated in the queryDataServiceException
- thrown if the list of columns is empty.public static java.lang.String createDeleteQuery(java.lang.String pFullyQualifiedTableName, java.lang.String pWhereClause)
String
containing a SQL DELETE statement with a
WHERE clause for a given table.pFullyQualifiedTableName
- fully qualified name of the table on which
the operation is carried outpWhereClause
- SQL WHERE Clause filtering data to delete in the table.
This clause should not include the WHERE keyword. If it is not
specified, the clause will not be generated in the querypublic static java.lang.String createWhereClause(QueryFilter pQueryFilter)
String
containing a SQL WHERE clause generated from a QueryFilter
. This clause includes both the QueryFilter
's ManagedEntity
used for filtering and the additional WHERE clausepQueryFilter
- QueryFilter
object used to generate the filterpublic static java.lang.String createWhereClause(UpdateDeleteFilter pUpdateDeleteFilter)
String
containing a SQL WHERE clause generated from a UpdateDeleteFilter
. This clause includes both the UpdateDeleteFilter
's ManagedEntity
used for filtering and the additional WHERE clausepUpdateDeleteFilter
- UpdateDeleteFilter
object used to generate the filter