public class DbQuery<Q extends DbQuery>
extends java.lang.Object
Statement
and all its operations.
It may be:
DbSession
;DbThreadSession
i.e. ThreadDbSessionHolder
.Query life-cycle states:
Modifier and Type | Class and Description |
---|---|
static class |
jodd.db.DbQueryBase.State
Query states.
|
Modifier and Type | Field and Description |
---|---|
protected boolean |
autoClose |
protected java.sql.CallableStatement |
callableStatement |
protected QueryConcurrencyType |
concurrencyType |
protected java.sql.Connection |
connection |
protected DbOom |
dbOom |
protected boolean |
debug |
protected long |
elapsed |
protected int |
fetchSize |
protected boolean |
forcePreparedStatement
If set to
true all created statements will be prepared. |
protected java.lang.String[] |
generatedColumns |
protected QueryHoldability |
holdability |
protected int |
maxRows |
protected java.sql.PreparedStatement |
preparedStatement |
protected jodd.db.DbQueryParser |
query |
protected jodd.db.DbQueryBase.State |
queryState |
protected java.util.Set<java.sql.ResultSet> |
resultSets |
protected DbSession |
session |
protected java.lang.String |
sqlString |
protected long |
start |
protected java.sql.Statement |
statement |
protected static int |
totalOpenResultSetCount |
protected QueryScrollType |
type |
Constructor and Description |
---|
DbQuery(DbOom dbOom,
java.sql.Connection conn,
java.lang.String sqlString)
Creates new query.
|
DbQuery(DbOom dbOom,
DbSession session,
java.lang.String sqlString)
Creates a new query from
DbSession . |
DbQuery(DbOom dbOom,
java.lang.String sqlString)
Creates a new query using default session provider.
|
Modifier and Type | Method and Description |
---|---|
protected Q |
_this() |
Q |
autoClose()
Defines that query should be automatically closed immediately after using.
|
protected void |
checkCreated()
Checks if query is created (and not yet initialized or closed) and throws an exception if it is not.
|
protected void |
checkInitialized()
Checks if query is initialized and throws an exception if it is not.
|
protected void |
checkNotClosed()
Checks if query is not closed and throws an exception if it is.
|
Q |
clearParameters()
Clears the current parameter values immediately.
|
void |
close()
Closes the query and all created results sets and detaches itself from the session.
|
Q |
closeAllResultSets()
Closes all result sets created by this query.
|
Q |
closeCursorsAtCommit() |
protected java.sql.SQLException |
closeQuery()
Closes all assigned result sets and then closes the query.
|
void |
closeResultSet(java.sql.ResultSet rs)
Closes single result set that was created by this query.
|
Q |
concurrentReadOnly() |
Q |
concurrentUpdatable() |
java.sql.ResultSet |
execute()
Executes the query.
|
DbCallResult |
executeCall() |
long |
executeCount()
Special execute() for 'select count(*)' queries.
|
protected long |
executeCount(boolean close)
Executes count queries and optionally closes query afterwards.
|
int |
executeUpdate()
Executes UPDATE, INSERT or DELETE queries.
|
protected int |
executeUpdate(boolean closeQuery)
Executes UPDATE, INSERT or DELETE queries and optionally closes the query.
|
<T> T |
find(QueryMapper<T> queryMapper)
|
Q |
forcePreparedStatement()
Forces creation of prepared statements.
|
QueryConcurrencyType |
getConcurrencyType() |
long |
getExecutionTime()
Returns query execution elapsed time in milliseconds.
|
int |
getFetchSize()
Returns fetch size.
|
java.lang.String[] |
getGeneratedColumnNames()
Returns generated column names.
|
java.sql.ResultSet |
getGeneratedColumns()
Returns generated columns.
|
long |
getGeneratedKey()
Returns generated key i.e. first generated column as
long . |
java.lang.Object |
getGeneratedKeyObject() |
QueryHoldability |
getHoldability() |
int |
getMaxRows()
Returns max rows.
|
int |
getOpenResultSetCount()
Returns number of created result sets that are still not explicitly closed.
|
jodd.db.DbQueryBase.State |
getQueryState()
Returns query state.
|
java.lang.String |
getQueryString()
Returns query SQL string.
|
DbSession |
getSession()
Returns used
DbSession . |
static int |
getTotalOpenResultSetCount()
Returns total number of open result sets.
|
QueryScrollType |
getType() |
Q |
holdCursorsOverCommit() |
void |
init()
Initializes the query if not already initialized.
|
protected void |
initializeJdbc()
Performs JDBC initialization of the query.
|
protected void |
initSession(DbSession session)
Initializes session.
|
boolean |
isActive()
Returns
true if query is active: created and possibly initialized. |
boolean |
isClosed()
Returns
true if query is closed. |
boolean |
isInDebugMode() |
boolean |
isInitialized()
Returns
true if query is initialized. |
<T> java.util.List<T> |
list(QueryMapper<T> queryMapper)
|
<T> java.util.Set<T> |
listSet(QueryMapper<T> queryMapper)
|
Q |
outBoolean(int index) |
Q |
outBoolean(java.lang.String param) |
Q |
outByte(int index) |
Q |
outByte(java.lang.String param) |
Q |
outDouble(int index) |
Q |
outDouble(java.lang.String param) |
Q |
outFloat(int index) |
Q |
outFloat(java.lang.String param) |
Q |
outInteger(int index) |
Q |
outInteger(java.lang.String param) |
Q |
outLong(int index) |
Q |
outLong(java.lang.String param) |
Q |
outString(int index) |
Q |
outString(java.lang.String param) |
protected void |
prepareQuery()
Prepares the query just after the initialization.
|
static DbQuery |
query(java.sql.Connection conn,
java.lang.String sqlString) |
static DbQuery |
query(DbSession session,
java.lang.String sqlString) |
static DbQuery |
query(java.lang.String sqlString) |
Q |
resetGeneratedColumns()
Resets creating generated columns.
|
protected void |
saveResultSet(java.sql.ResultSet rs)
Stores result set.
|
Q |
setArray(int index,
java.sql.Array value) |
Q |
setArray(java.lang.String param,
java.sql.Array value) |
Q |
setAsciiStream(int index,
java.io.InputStream stream) |
Q |
setAsciiStream(int index,
java.io.InputStream stream,
int length) |
Q |
setAsciiStream(java.lang.String param,
java.io.InputStream stream) |
Q |
setAsciiStream(java.lang.String param,
java.io.InputStream stream,
int length) |
Q |
setBatch(java.lang.String name,
int[] array,
int startingIndex)
Sets batch parameters with given array of values.
|
Q |
setBatch(java.lang.String name,
long[] array,
int startingIndex)
Sets batch parameters with given array of values.
|
Q |
setBatch(java.lang.String name,
java.lang.Object[] array,
int startingIndex)
Sets batch parameters with given array of values.
|
Q |
setBean(java.lang.String beanName,
java.lang.Object bean)
Sets bean parameters from bean.
|
Q |
setBigDecimal(int index,
java.math.BigDecimal value) |
Q |
setBigDecimal(java.lang.String param,
java.math.BigDecimal value) |
Q |
setBigInteger(int index,
java.math.BigInteger value) |
Q |
setBigInteger(java.lang.String param,
java.math.BigInteger value) |
Q |
setBlob(int index,
java.sql.Blob value) |
Q |
setBlob(java.lang.String param,
java.sql.Blob value) |
Q |
setBoolean(int index,
boolean value) |
Q |
setBoolean(int index,
java.lang.Boolean value) |
Q |
setBoolean(java.lang.String param,
boolean value) |
Q |
setBoolean(java.lang.String param,
java.lang.Boolean value) |
Q |
setByte(int index,
byte value) |
Q |
setByte(int index,
java.lang.Number value) |
Q |
setByte(java.lang.String param,
byte value) |
Q |
setByte(java.lang.String param,
java.lang.Number value) |
Q |
setBytes(int index,
byte[] value) |
Q |
setBytes(java.lang.String param,
byte[] value) |
Q |
setCharacterStream(int index,
java.io.Reader reader,
int length) |
Q |
setCharacterStream(java.lang.String param,
java.io.Reader reader,
int length) |
Q |
setClob(int index,
java.sql.Clob value) |
Q |
setClob(java.lang.String param,
java.sql.Clob value) |
Q |
setConcurrencyType(QueryConcurrencyType concurrencyType) |
Q |
setDate(int index,
java.sql.Date value) |
Q |
setDate(java.lang.String param,
java.sql.Date value) |
Q |
setDebug(boolean debug) |
Q |
setDebugMode() |
Q |
setDouble(int index,
double value) |
Q |
setDouble(int index,
java.lang.Number value) |
Q |
setDouble(java.lang.String param,
double value) |
Q |
setDouble(java.lang.String param,
java.lang.Number value) |
Q |
setFetchSize(int rows)
Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when
more rows are needed.
|
Q |
setFloat(int index,
float value) |
Q |
setFloat(int index,
java.lang.Number value) |
Q |
setFloat(java.lang.String param,
float value) |
Q |
setFloat(java.lang.String param,
java.lang.Number value) |
Q |
setGeneratedColumns(java.lang.String... columns)
Specifies columns which values will be generated by database.
|
Q |
setGeneratedKey()
Specifies that database will generate some columns values,
usually the single id.
|
Q |
setHoldability(QueryHoldability holdability) |
Q |
setInteger(int index,
int value) |
Q |
setInteger(int index,
java.lang.Number value) |
Q |
setInteger(java.lang.String param,
int value) |
Q |
setInteger(java.lang.String param,
java.lang.Number value) |
Q |
setLong(int index,
long value) |
Q |
setLong(int index,
java.lang.Number value) |
Q |
setLong(java.lang.String param,
long value) |
Q |
setLong(java.lang.String param,
java.lang.Number value) |
Q |
setMap(java.util.Map parameters)
Sets properties from the map.
|
Q |
setMaxRows(int maxRows)
Sets the limit for the maximum number of rows that any ResultSet object can contain to the given number.
|
Q |
setNull(int index,
int type) |
Q |
setNull(int index,
int type,
java.lang.String typeName) |
Q |
setNull(java.lang.String param,
int type) |
Q |
setNull(java.lang.String param,
int value,
java.lang.String typeName) |
Q |
setObject(int index,
java.lang.Object value) |
Q |
setObject(int index,
java.lang.Object value,
java.lang.Class<? extends SqlType> sqlTypeClass,
int dbSqlType)
Sets object parameter in an advanced way.
|
Q |
setObject(int index,
java.lang.Object object,
int targetSqlType)
Sets the value of the designated parameter with the given object.
|
Q |
setObject(java.lang.String param,
java.lang.Object value) |
Q |
setObject(java.lang.String param,
java.lang.Object value,
java.lang.Class<? extends SqlType> sqlTypeClass,
int dbSqlType) |
Q |
setObject(java.lang.String param,
java.lang.Object object,
int targetSqlType)
Sets the value of the designated parameter with the given object.
|
Q |
setObjects(java.lang.Object... objects)
Sets an array of objects parameters in given order.
|
Q |
setObjects(java.lang.String[] names,
java.lang.Object[] values)
Sets sql parameters from two arrays: names and values.
|
Q |
setRef(int index,
java.sql.Ref value) |
Q |
setRef(java.lang.String param,
java.sql.Ref value) |
Q |
setShort(int index,
java.lang.Number value) |
Q |
setShort(int index,
short value) |
Q |
setShort(java.lang.String param,
java.lang.Number value) |
Q |
setShort(java.lang.String param,
short value) |
Q |
setString(int index,
java.lang.String value) |
Q |
setString(java.lang.String param,
java.lang.String value) |
Q |
setTime(int index,
java.sql.Time value) |
Q |
setTime(java.lang.String param,
java.sql.Time value) |
Q |
setTimestamp(int index,
java.sql.Timestamp value) |
Q |
setTimestamp(java.lang.String param,
java.sql.Timestamp value) |
Q |
setType(QueryScrollType type) |
Q |
setURL(int index,
java.net.URL value) |
Q |
setURL(java.lang.String param,
java.net.URL value) |
java.lang.String |
toString() |
Q |
typeForwardOnly() |
Q |
typeScrollInsensitive() |
Q |
typeScrollSensitive() |
protected final DbOom dbOom
protected jodd.db.DbQueryBase.State queryState
protected java.sql.Connection connection
protected DbSession session
protected java.lang.String sqlString
protected java.sql.Statement statement
protected java.sql.PreparedStatement preparedStatement
protected java.sql.CallableStatement callableStatement
protected java.util.Set<java.sql.ResultSet> resultSets
protected jodd.db.DbQueryParser query
protected boolean forcePreparedStatement
true
all created statements will be prepared.protected boolean autoClose
protected QueryScrollType type
protected QueryConcurrencyType concurrencyType
protected QueryHoldability holdability
protected boolean debug
protected java.lang.String[] generatedColumns
protected int fetchSize
protected int maxRows
protected long start
protected long elapsed
protected static int totalOpenResultSetCount
public DbQuery(DbOom dbOom, java.sql.Connection conn, java.lang.String sqlString)
public DbQuery(DbOom dbOom, DbSession session, java.lang.String sqlString)
DbSession
.public DbQuery(DbOom dbOom, java.lang.String sqlString)
public static DbQuery query(java.sql.Connection conn, java.lang.String sqlString)
public static DbQuery query(java.lang.String sqlString)
public Q clearParameters()
In general, parameter values remain in force for repeated use of a
statement. Setting a parameter value automatically clears its
previous value. However, in some cases it is useful to immediately
release the resources used by the current parameter values; this can
be done by calling the method clearParameters
.
public Q setNull(int index, int type)
public Q setNull(java.lang.String param, int type)
public Q setNull(int index, int type, java.lang.String typeName)
public Q setNull(java.lang.String param, int value, java.lang.String typeName)
public Q setInteger(int index, int value)
public Q setInteger(java.lang.String param, int value)
public Q outInteger(int index)
public Q outInteger(java.lang.String param)
public Q setInteger(int index, java.lang.Number value)
public Q setInteger(java.lang.String param, java.lang.Number value)
public Q setBoolean(int index, boolean value)
public Q setBoolean(java.lang.String param, boolean value)
public Q outBoolean(int index)
public Q outBoolean(java.lang.String param)
public Q setBoolean(int index, java.lang.Boolean value)
public Q setBoolean(java.lang.String param, java.lang.Boolean value)
public Q setLong(int index, long value)
public Q setLong(java.lang.String param, long value)
public Q setLong(int index, java.lang.Number value)
public Q setLong(java.lang.String param, java.lang.Number value)
public Q outLong(int index)
public Q outLong(java.lang.String param)
public Q setByte(int index, byte value)
public Q setByte(java.lang.String param, byte value)
public Q outByte(int index)
public Q outByte(java.lang.String param)
public Q setByte(int index, java.lang.Number value)
public Q setByte(java.lang.String param, java.lang.Number value)
public Q setBytes(int index, byte[] value)
public Q setBytes(java.lang.String param, byte[] value)
public Q setDouble(int index, double value)
public Q setDouble(java.lang.String param, double value)
public Q outDouble(int index)
public Q outDouble(java.lang.String param)
public Q setDouble(int index, java.lang.Number value)
public Q setDouble(java.lang.String param, java.lang.Number value)
public Q setFloat(int index, float value)
public Q setFloat(java.lang.String param, float value)
public Q outFloat(int index)
public Q outFloat(java.lang.String param)
public Q setFloat(int index, java.lang.Number value)
public Q setFloat(java.lang.String param, java.lang.Number value)
public Q setShort(int index, short value)
public Q setShort(java.lang.String param, short value)
public Q setShort(int index, java.lang.Number value)
public Q setShort(java.lang.String param, java.lang.Number value)
public Q setString(int index, java.lang.String value)
public Q setString(java.lang.String param, java.lang.String value)
public Q outString(int index)
public Q outString(java.lang.String param)
public Q setDate(int index, java.sql.Date value)
public Q setDate(java.lang.String param, java.sql.Date value)
public Q setTime(int index, java.sql.Time value)
public Q setTime(java.lang.String param, java.sql.Time value)
public Q setTimestamp(int index, java.sql.Timestamp value)
public Q setTimestamp(java.lang.String param, java.sql.Timestamp value)
public Q setBigDecimal(int index, java.math.BigDecimal value)
public Q setBigDecimal(java.lang.String param, java.math.BigDecimal value)
public Q setBigInteger(int index, java.math.BigInteger value)
public Q setBigInteger(java.lang.String param, java.math.BigInteger value)
public Q setURL(int index, java.net.URL value)
public Q setURL(java.lang.String param, java.net.URL value)
public Q setBlob(int index, java.sql.Blob value)
public Q setBlob(java.lang.String param, java.sql.Blob value)
public Q setClob(int index, java.sql.Clob value)
public Q setClob(java.lang.String param, java.sql.Clob value)
public Q setArray(int index, java.sql.Array value)
public Q setArray(java.lang.String param, java.sql.Array value)
public Q setRef(int index, java.sql.Ref value)
public Q setRef(java.lang.String param, java.sql.Ref value)
public Q setAsciiStream(int index, java.io.InputStream stream)
public Q setAsciiStream(java.lang.String param, java.io.InputStream stream)
public Q setAsciiStream(int index, java.io.InputStream stream, int length)
public Q setAsciiStream(java.lang.String param, java.io.InputStream stream, int length)
public Q setCharacterStream(int index, java.io.Reader reader, int length)
public Q setCharacterStream(java.lang.String param, java.io.Reader reader, int length)
public Q setBean(java.lang.String beanName, java.lang.Object bean)
public Q setMap(java.util.Map parameters)
public Q setObject(int index, java.lang.Object object, int targetSqlType)
setObject
above, except that it assumes a scale of zero.public Q setObject(java.lang.String param, java.lang.Object object, int targetSqlType)
setObject
above, except that it assumes a scale of zero.public Q setObject(int index, java.lang.Object value)
setObject(String, Object, Class, int)
public Q setObject(int index, java.lang.Object value, java.lang.Class<? extends SqlType> sqlTypeClass, int dbSqlType)
First, it checks if object is null
and invokes setNull
if so.
If object is not null
, it tries to resolve sql type
(by looking up
or using provided class) and use it for setting data.
If sql type is not found, default setObject
is invoked.
public Q setObject(java.lang.String param, java.lang.Object value)
setObject(String, Object, Class, int)
public Q setObject(java.lang.String param, java.lang.Object value, java.lang.Class<? extends SqlType> sqlTypeClass, int dbSqlType)
setObject(String, Object, Class, int)
public Q setObjects(java.lang.Object... objects)
public Q setObjects(java.lang.String[] names, java.lang.Object[] values)
public Q setBatch(java.lang.String name, int[] array, int startingIndex)
public Q setBatch(java.lang.String name, long[] array, int startingIndex)
public Q setBatch(java.lang.String name, java.lang.Object[] array, int startingIndex)
public Q autoClose()
protected Q _this()
public jodd.db.DbQueryBase.State getQueryState()
protected void checkNotClosed()
protected void checkCreated()
protected void checkInitialized()
public boolean isClosed()
true
if query is closed.public boolean isActive()
true
if query is active: created and possibly initialized.
Opened query may be not initialized.public boolean isInitialized()
true
if query is initialized. Initialized query is the one that has
created JDBC statements.protected void saveResultSet(java.sql.ResultSet rs)
public Q forcePreparedStatement()
public final void init()
protected void initSession(DbSession session)
null
),
session is fetched from session provider.protected void initializeJdbc()
protected void prepareQuery()
public Q closeAllResultSets()
protected java.sql.SQLException closeQuery()
public void close()
close
in interface java.lang.AutoCloseable
public void closeResultSet(java.sql.ResultSet rs)
DbQueryBase.close()
method closes all created result sets.
Query remains active.public QueryScrollType getType()
public Q setType(QueryScrollType type)
public Q typeForwardOnly()
public Q typeScrollSensitive()
public Q typeScrollInsensitive()
public QueryConcurrencyType getConcurrencyType()
public Q setConcurrencyType(QueryConcurrencyType concurrencyType)
public Q concurrentReadOnly()
public Q concurrentUpdatable()
public QueryHoldability getHoldability()
public Q setHoldability(QueryHoldability holdability)
public Q holdCursorsOverCommit()
public Q closeCursorsAtCommit()
public boolean isInDebugMode()
public Q setDebug(boolean debug)
public Q setDebugMode()
public java.lang.String[] getGeneratedColumnNames()
public Q setGeneratedColumns(java.lang.String... columns)
public Q setGeneratedKey()
public Q resetGeneratedColumns()
public int getFetchSize()
setFetchSize(int)
public Q setFetchSize(int rows)
Statement.setFetchSize(int)
public int getMaxRows()
setMaxRows(int)
public Q setMaxRows(int maxRows)
Statement.setMaxRows(int)
public long getExecutionTime()
-1
if query is still not executed.public java.sql.ResultSet execute()
DbQueryBase.closeResultSet(ResultSet)
оr implicitly, during DbQueryBase.close()
.Statement.execute(String)
public DbCallResult executeCall()
public int executeUpdate()
auto close mode
is set.Statement.executeUpdate(String)
protected int executeUpdate(boolean closeQuery)
Statement.executeUpdate(String)
public long executeCount()
auto-close mode
is set.
Doesn't check if query is really a count query, so it would work for any
query that has number in the first column of result.
If result set returns zero rows (very unlikely), returns -1
.protected long executeCount(boolean close)
public <T> java.util.List<T> list(QueryMapper<T> queryMapper)
public <T> T find(QueryMapper<T> queryMapper)
public <T> java.util.Set<T> listSet(QueryMapper<T> queryMapper)
public java.sql.ResultSet getGeneratedColumns()
public long getGeneratedKey()
long
.public java.lang.Object getGeneratedKeyObject()
public java.lang.String getQueryString()
public java.lang.String toString()
toString
in class java.lang.Object
getQueryString()
public static int getTotalOpenResultSetCount()
getOpenResultSetCount()
public int getOpenResultSetCount()
getTotalOpenResultSetCount()
Copyright © 2003-present Jodd Team