Class DefaultSqlUpdate
- java.lang.Object
-
- io.ebeaninternal.server.core.DefaultSqlUpdate
-
- All Implemented Interfaces:
SqlUpdate
,SpiSqlUpdate
,Serializable
public final class DefaultSqlUpdate extends Object implements Serializable, SpiSqlUpdate
A SQL Update Delete or Insert statement that can be executed. For the times when you want to use Sql DML rather than a ORM bean approach. Refer to the Ebean execute() method.There is also
Update
which is similar except should use logical bean and property names rather than physical table and column names.SqlUpdate is designed for general DML sql and CallableSql is designed for use with stored procedures.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DefaultSqlUpdate(SpiEbeanServer server, String sql)
Create with a specific server.DefaultSqlUpdate(SpiEbeanServer server, String sql, BindParams bindParams)
Create with server sql and bindParams object.DefaultSqlUpdate(String sql)
Create with some sql.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBatch()
Add the statement to batch processing to then later execute via executeBatch().SpiSqlUpdate
copy()
Return a copy of the SqlUpdate with empty bind parameters.int
execute()
Execute the update returning the number of rows modified.int[]
executeBatch()
Execute when addBatch() has been used to batch multiple bind executions.Object
executeGetKey()
Execute and return the generated key.int
executeNow()
Execute the statement now regardless of the JDBC batch mode of the transaction.String
getBaseSql()
Return the sql taking into account bind parameter expansion.BindParams
getBindParams()
Return the bind parameters.Object
getGeneratedKey()
Return the generated key value.String
getGeneratedSql()
Return the generated sql that has named parameters converted to positioned parameters.String
getLabel()
Return the label that can be seen in the transaction logs.String
getSql()
Return the sql statement.int
getTimeout()
Return the timeout used to execute this statement.boolean
isAutoTableMod()
Return true if eBean should automatically deduce the table modification information and process it.boolean
isGetGeneratedKeys()
Return true if we are using getGeneratedKeys.void
reset()
Reset bind position to be ready for another bind execute.SqlUpdate
setAutoTableMod(boolean isAutoTableMod)
Set this to false if you don't want eBean to automatically deduce the table modification information and process it.void
setGeneratedKey(Object idValue)
Set the generated key value.void
setGeneratedSql(String generatedSql)
Set the final sql being executed with named parameters replaced etc.SqlUpdate
setGetGeneratedKeys(boolean getGeneratedKeys)
Set to true when we want to use getGeneratedKeys with this statement.SqlUpdate
setLabel(String label)
Set a descriptive text that can be put into the transaction log.SqlUpdate
setNextParameter(Object value)
Deprecated migrate to setParameter(value).SqlUpdate
setNull(int position, int jdbcType)
Set a null parameter via its index position.SqlUpdate
setNull(String name, int jdbcType)
Set a named parameter that has a null value.SqlUpdate
setNullParameter(int position, int jdbcType)
Set a null valued parameter using its index position.SqlUpdate
setNullParameter(String name, int jdbcType)
Set a named parameter that has a null value.SqlUpdate
setParameter(int position, Object value)
Set a parameter via its index position.SqlUpdate
setParameter(Object value)
Set the next bind parameter by position.SqlUpdate
setParameter(String name, Object param)
Set a named parameter value.SqlUpdate
setParameters(Object... values)
Set one of more positioned parameters.SqlUpdate
setParams(Object... values)
Deprecated migrate to setParameters(Object...SqlUpdate
setTimeout(int secs)
Set the timeout in seconds.
-
-
-
Constructor Detail
-
DefaultSqlUpdate
public DefaultSqlUpdate(SpiEbeanServer server, String sql, BindParams bindParams)
Create with server sql and bindParams object.Useful if you are building the sql and binding parameters at the same time.
-
DefaultSqlUpdate
public DefaultSqlUpdate(SpiEbeanServer server, String sql)
Create with a specific server. This means you can use the SqlUpdate.execute() method.
-
DefaultSqlUpdate
public DefaultSqlUpdate(String sql)
Create with some sql.
-
-
Method Detail
-
copy
public SpiSqlUpdate copy()
Description copied from interface:SpiSqlUpdate
Return a copy of the SqlUpdate with empty bind parameters.- Specified by:
copy
in interfaceSpiSqlUpdate
-
reset
public void reset()
Description copied from interface:SpiSqlUpdate
Reset bind position to be ready for another bind execute.- Specified by:
reset
in interfaceSpiSqlUpdate
-
executeGetKey
public Object executeGetKey()
Description copied from interface:SqlUpdate
Execute and return the generated key. This is effectively a short cut for:sqlUpdate.execute(); Object key = sqlUpdate.getGeneratedKey();
- Specified by:
executeGetKey
in interfaceSqlUpdate
- Returns:
- The generated key value
-
execute
public int execute()
Description copied from interface:SqlUpdate
Execute the update returning the number of rows modified.Note that if the transaction has batch mode on then this update will use JDBC batch and may not execute until later - at commit time or a transaction flush. In this case this method returns -1 indicating that the update has been batched for later execution.
After you have executed the SqlUpdate you can bind new variables using
SqlUpdate.setParameter(String, Object)
etc and then execute the SqlUpdate again.For JDBC batch processing refer to
Transaction.setBatchMode(boolean)
andTransaction.setBatchSize(int)
.
-
executeNow
public int executeNow()
Description copied from interface:SqlUpdate
Execute the statement now regardless of the JDBC batch mode of the transaction.- Specified by:
executeNow
in interfaceSqlUpdate
-
executeBatch
public int[] executeBatch()
Description copied from interface:SqlUpdate
Execute when addBatch() has been used to batch multiple bind executions.- Specified by:
executeBatch
in interfaceSqlUpdate
- Returns:
- The row counts for each of the batched statements.
-
addBatch
public void addBatch()
Description copied from interface:SqlUpdate
Add the statement to batch processing to then later execute via executeBatch().
-
getGeneratedKey
public Object getGeneratedKey()
Description copied from interface:SqlUpdate
Return the generated key value.- Specified by:
getGeneratedKey
in interfaceSqlUpdate
-
setGeneratedKey
public void setGeneratedKey(Object idValue)
Description copied from interface:SpiSqlUpdate
Set the generated key value.- Specified by:
setGeneratedKey
in interfaceSpiSqlUpdate
-
isAutoTableMod
public boolean isAutoTableMod()
Description copied from interface:SqlUpdate
Return true if eBean should automatically deduce the table modification information and process it.If this is true then cache invalidation and text index management are aware of the modification.
- Specified by:
isAutoTableMod
in interfaceSqlUpdate
-
setAutoTableMod
public SqlUpdate setAutoTableMod(boolean isAutoTableMod)
Description copied from interface:SqlUpdate
Set this to false if you don't want eBean to automatically deduce the table modification information and process it.Set this to false if you don't want any cache invalidation or text index management to occur. You may do this when say you update only one column and you know that it is not important for cached objects or text indexes.
- Specified by:
setAutoTableMod
in interfaceSqlUpdate
-
getLabel
public String getLabel()
Description copied from interface:SqlUpdate
Return the label that can be seen in the transaction logs.
-
setLabel
public SqlUpdate setLabel(String label)
Description copied from interface:SqlUpdate
Set a descriptive text that can be put into the transaction log.Useful when identifying the statement in the transaction log.
-
isGetGeneratedKeys
public boolean isGetGeneratedKeys()
Description copied from interface:SpiSqlUpdate
Return true if we are using getGeneratedKeys.- Specified by:
isGetGeneratedKeys
in interfaceSpiSqlUpdate
-
setGetGeneratedKeys
public SqlUpdate setGetGeneratedKeys(boolean getGeneratedKeys)
Description copied from interface:SqlUpdate
Set to true when we want to use getGeneratedKeys with this statement.- Specified by:
setGetGeneratedKeys
in interfaceSqlUpdate
-
getGeneratedSql
public String getGeneratedSql()
Description copied from interface:SqlUpdate
Return the generated sql that has named parameters converted to positioned parameters.- Specified by:
getGeneratedSql
in interfaceSqlUpdate
-
setGeneratedSql
public void setGeneratedSql(String generatedSql)
Description copied from interface:SpiSqlUpdate
Set the final sql being executed with named parameters replaced etc.- Specified by:
setGeneratedSql
in interfaceSpiSqlUpdate
-
getSql
public String getSql()
Description copied from interface:SqlUpdate
Return the sql statement.
-
getBaseSql
public String getBaseSql()
Description copied from interface:SpiSqlUpdate
Return the sql taking into account bind parameter expansion.- Specified by:
getBaseSql
in interfaceSpiSqlUpdate
-
getTimeout
public int getTimeout()
Description copied from interface:SqlUpdate
Return the timeout used to execute this statement.- Specified by:
getTimeout
in interfaceSqlUpdate
-
setTimeout
public SqlUpdate setTimeout(int secs)
Description copied from interface:SqlUpdate
Set the timeout in seconds. Zero implies no limit.This will set the query timeout on the underlying PreparedStatement. If the timeout expires a SQLException will be throw and wrapped in a PersistenceException.
- Specified by:
setTimeout
in interfaceSqlUpdate
-
setParams
public SqlUpdate setParams(Object... values)
Description copied from interface:SqlUpdate
Deprecated migrate to setParameters(Object... values).
-
setNextParameter
public SqlUpdate setNextParameter(Object value)
Description copied from interface:SqlUpdate
Deprecated migrate to setParameter(value).- Specified by:
setNextParameter
in interfaceSqlUpdate
-
setParameters
public SqlUpdate setParameters(Object... values)
Description copied from interface:SqlUpdate
Set one of more positioned parameters.This is a convenient alternative to multiple setParameter() calls.
String sql = "insert into audit_log (id, name, version) values (?,?,?)"; DB.sqlUpdate(sql) .setParameters(UUID.randomUUID(), "Hello", 1) .executeNow(); // is the same as ... DB.sqlUpdate(sql) .setParameter(UUID.randomUUID()) .setParameter("Hello") .setParameter(1) .executeNow(); // which is the same as ... DB.sqlUpdate(sql) .setParameter(1, UUID.randomUUID()) .setParameter(2, "Hello") .setParameter(3, 1) .executeNow();
- Specified by:
setParameters
in interfaceSqlUpdate
-
setParameter
public SqlUpdate setParameter(Object value)
Description copied from interface:SqlUpdate
Set the next bind parameter by position.- Specified by:
setParameter
in interfaceSqlUpdate
- Parameters:
value
- The value to bind
-
setParameter
public SqlUpdate setParameter(int position, Object value)
Description copied from interface:SqlUpdate
Set a parameter via its index position.- Specified by:
setParameter
in interfaceSqlUpdate
-
setNull
public SqlUpdate setNull(int position, int jdbcType)
Description copied from interface:SqlUpdate
Set a null parameter via its index position.
-
setNullParameter
public SqlUpdate setNullParameter(int position, int jdbcType)
Description copied from interface:SqlUpdate
Set a null valued parameter using its index position.- Specified by:
setNullParameter
in interfaceSqlUpdate
-
setParameter
public SqlUpdate setParameter(String name, Object param)
Description copied from interface:SqlUpdate
Set a named parameter value.- Specified by:
setParameter
in interfaceSqlUpdate
-
setNull
public SqlUpdate setNull(String name, int jdbcType)
Description copied from interface:SqlUpdate
Set a named parameter that has a null value. Exactly the same asSqlUpdate.setNullParameter(String, int)
.
-
setNullParameter
public SqlUpdate setNullParameter(String name, int jdbcType)
Description copied from interface:SqlUpdate
Set a named parameter that has a null value.- Specified by:
setNullParameter
in interfaceSqlUpdate
-
getBindParams
public BindParams getBindParams()
Return the bind parameters.- Specified by:
getBindParams
in interfaceSpiSqlUpdate
-
-