public class SQLQuery
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
baseSql
The SQL statement, possibly with placeholders.
|
private boolean |
canRaiseSqlWarning
Flag indicating that the query can raise SQLWarning.
|
private static java.lang.String |
CFG_FETCH_SIZE
Directory path for fetch size override
|
private static int |
DEFAULT_FETCH_SIZE
Default JDBC fetch size (may be overridden by runtime configuration)
|
private static int |
fetchSize
JDBC fetch size (same for all databases)
|
private static java.util.logging.Logger |
log
Logger
|
private int |
maxParamIndex
The higher index of the parameter set.
|
private java.lang.Object[] |
parameters
The current list of positional parameters.
|
private java.util.List<HQLPreprocessor.SessionAttr> |
sessionAttrs
Mutable session attributes' values used in the query.
|
private java.lang.String |
sql
The SQL statement to be executed.
|
private java.sql.PreparedStatement |
stmt
The
PreparedStatement . |
Constructor and Description |
---|
SQLQuery(java.lang.String sql)
Constructs a new object.
|
SQLQuery(java.lang.String sql,
java.util.List<HQLPreprocessor.SessionAttr> sessionAttrs)
Constructs a new object.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canRaiseSqlWarning()
Checks if the query can raise
SQLWarning . |
SQLQuery |
canRaiseSqlWarning(boolean canRaiseSqlWarning)
Set the flag indicating if the query can raise
SQLWarning . |
private static void |
checkClosed(java.sql.Connection conn)
Test whether the connection is alive (not closed).
|
private void |
close()
Closes this object by releasing all acquired resources.
|
private void |
ensureSize(int fitIndex)
Makes sure the array that holds the parameters for this query is large enough to accommodate
the
fitIndex -th parameter. |
int |
executeUpdate(Session session)
Execute the update SQL query.
|
SQLQuery |
expandPlaceholders()
Expand placeholder.
|
(package private) static BaseRecord |
hydrateRecord(java.sql.ResultSet resultSet,
java.lang.Class<? extends DataModelObject> iface,
int rsOffset,
int count,
Session session)
Hydrate a
Record from a ResultSet , ie a new object that implements the DMO
interface and having the properties set from the current row of the record set. |
<T> java.util.List<T> |
list(Session session,
java.util.List<RowStructure> rowStructure)
Returns a list of rows of type
<T> by executing the query against the SQL server. |
private void |
log(java.util.function.Supplier<java.lang.String> msg)
Logs a message with
Level.FINE tracing level to the logger associated with this
class. |
private void |
reportSQLWarnings(Session session)
Report SQLWarnings raised by UDFs.
|
<T> ScrollableResults<T> |
scroll(Session session,
int scrollMode,
boolean readOnly,
java.util.List<RowStructure> rowStructure)
Executes the query against the SQL server.
|
<T> ScrollableResults<T> |
scroll(Session session,
java.util.List<RowStructure> rowStructure)
Returns a scrollable list of rows of type
<T> by executing the query against the database
server. |
void |
setParameter(int index,
java.lang.Object arg)
Sets the value of a parameter for this query.
|
private void |
setParameter(java.sql.PreparedStatement stmt,
java.util.concurrent.atomic.AtomicInteger pos,
java.lang.Object val)
Sets the value for a parameter at a specified position in a statement.
|
private void |
setParameters(java.sql.PreparedStatement stmt)
Copy the set of parameters temporarily stored in this object to the
PreparedStatement . |
<T> T |
uniqueResult(Session session,
int rowSize,
java.lang.Class<? extends DataModelObject> dmoType)
Returns an unique row of type
<T> by executing the query against the SQL server. |
private static final java.util.logging.Logger log
private static final java.lang.String CFG_FETCH_SIZE
private static final int DEFAULT_FETCH_SIZE
private static final int fetchSize
private final java.lang.String baseSql
private java.lang.String sql
private final java.util.List<HQLPreprocessor.SessionAttr> sessionAttrs
private java.sql.PreparedStatement stmt
PreparedStatement
.private java.lang.Object[] parameters
private int maxParamIndex
private boolean canRaiseSqlWarning
public SQLQuery(java.lang.String sql, java.util.List<HQLPreprocessor.SessionAttr> sessionAttrs)
sql
- The statement to be executed.sessionAttrs
- Mutable session attributes' values used in the querypublic SQLQuery(java.lang.String sql)
sql
- The statement to be executed.public boolean canRaiseSqlWarning()
SQLWarning
.true
if the query can raise SQLWarning
.public SQLQuery canRaiseSqlWarning(boolean canRaiseSqlWarning)
SQLWarning
.canRaiseSqlWarning
- The value of the flagthis
public SQLQuery expandPlaceholders()
public void setParameter(int index, java.lang.Object arg)
null
value will be used. At that time, the index of parameters will be
shifted to 1-based JDBC standard.index
- The index of the parameter to be set (0-base).arg
- The parameter value.public <T> ScrollableResults<T> scroll(Session session, int scrollMode, boolean readOnly, java.util.List<RowStructure> rowStructure) throws PersistenceException
T
- The type of the rows returned in the list.session
- The Session
to be used.scrollMode
- The scroll mode.readOnly
- Use true
to get a ResultSet.CONCUR_READ_ONLY
result set and
false
for a ResultSet.CONCUR_UPDATABLE
one.rowStructure
- The expected structure of a query, as generated by the FqlToSqlConverter
.<T>
from the database server, as specified by the
sql
query.PersistenceException
- when an error occurred while performing the requested operations.public <T> ScrollableResults<T> scroll(Session session, java.util.List<RowStructure> rowStructure) throws PersistenceException
<T>
by executing the query against the database
server.T
- The type of the rows returned in the list.session
- The Session
to be used.rowStructure
- The expected structure of a query, as generated by the FqlToSqlConverter
.<T>
from SQL server, as specified by the
sql
query.PersistenceException
- when an error occurred while performing the requested operations.public <T> T uniqueResult(Session session, int rowSize, java.lang.Class<? extends DataModelObject> dmoType) throws PersistenceException
<T>
by executing the query against the SQL server. The result may
be either a scalar value, but also can be a full Record
object.T
- The type of the row to be returned.session
- The Session
to be used.rowSize
- The expected row size of the query, as generated by the FqlToSqlConverter
. Usually 1
if a single/plain result is expected, but can be larger if a full Record
object is
expected to be returned.dmoType
- The Class
of the expected object, when rowSize > 1
. Used for hydration of row.<T>
from SQL server, as specified by the sql
query.PersistenceException
- when an error occurred while performing the requested operations.public int executeUpdate(Session session) throws PersistenceException
session
- The Session
whose database Connection
to be used.PersistenceException
- when an error occurred while performing the requested operations.public <T> java.util.List<T> list(Session session, java.util.List<RowStructure> rowStructure) throws PersistenceException
<T>
by executing the query against the SQL server. The returned
list is composed either from PKs (recid/id) or from fully re-hydrated Record
s. It's up to the
caller logic to detect these case and use the PKs (for example, in a Cursor
) or later fetch the
full Record
using the Loader
of the Persistence
.T
- The type of the rows returned in the list.session
- The Session
to be used.rowStructure
- The expected structure of a query, as generated by the FqlToSqlConverter
.<T>
from SQL server, as specified by the sql
query.PersistenceException
- when an error occurred while performing the requested operations.private void reportSQLWarnings(Session session) throws java.sql.SQLException
session
- database session.java.sql.SQLException
- on error in the database layer.static BaseRecord hydrateRecord(java.sql.ResultSet resultSet, java.lang.Class<? extends DataModelObject> iface, int rsOffset, int count, Session session) throws PersistenceException
Record
from a ResultSet
, ie a new object that implements the DMO
interface and having the properties set from the current row of the record set. The method
takes care of all the DMO properties, record identifier, and including the special
properties of the temp-tables (_multiplex, before table flags).resultSet
- The data source. Contain the result of a query with the "main" properties. The
extent properties are queried and hydrated before the record object is returned.iface
- The DMO of the object to be returned. This governs the order and the types of the
fields and the number of columns used for each one.rsOffset
- The offset in the ResultSet
's row where the hydration process begins (in
case when multiple objects are queried at once).count
- The expected number of properties of this object, as the select query was
constructed.session
- The Session
to be used.PersistenceException
- When an issue was encountered in the process.private void close()
private void ensureSize(int fitIndex)
fitIndex
-th parameter. If the index fits the current size nothing is performed.
Otherwise, the array is reallocated and the existing parameters are copied to their fixed
positions. To improve performance, more space may be allocated so that this operation will
not happen at each method call.fitIndex
- The index of the latest added parameter.private void setParameters(java.sql.PreparedStatement stmt) throws java.sql.SQLException
PreparedStatement
.stmt
- The statement to be processed.java.sql.SQLException
- If an error occurred in the process.private void setParameter(java.sql.PreparedStatement stmt, java.util.concurrent.atomic.AtomicInteger pos, java.lang.Object val) throws java.sql.SQLException
List
s. The pos
is incremented automatically for each scalar
parameter configured.stmt
- The statement.pos
- The start position.val
- The valuejava.sql.SQLException
- If an error occurred in the process.private static void checkClosed(java.sql.Connection conn) throws PersistenceException
PersistenceException
is thrown.conn
- The database Connection
to be used.PersistenceException
- when the connection is not usable because it was closed.private void log(java.util.function.Supplier<java.lang.String> msg)
Level.FINE
tracing level to the logger associated with this
class.msg
- The message to be printed to log.