Package com.im.commons.db.dml
Class SQLUpdateStatement
- java.lang.Object
-
- com.im.commons.db.dml.SQLUpdateStatement
-
public final class SQLUpdateStatement extends java.lang.Object
Defines a SQL update statement that allows data in a table to be selectively updated. Typical usage:SQLWriter sqlWriter = new SQLWriterRegistry() ; SQLUpdateStatement update = new SQLUpdateStatement(sqlWriter); update.setTable("foo"); SQLWhereClauseElement where = new SQLFilterElement( sqlWriter, "foo", new String[]{"baz"}, new int[]{Types.VARCHAR}, Operators.EQUALS, new Object[]{"mars"}); update.setWhereClause(where); update.addValue("txtcol", TypesProvider.ColumnType.VARCHAR, "hello world"); update.addValue("intcol", TypesProvider.ColumnType.INTEGER, new Integer(99)); String sql = instance.generateSql(); // now execute the SQL
Will generate SQL like this:UPDATE foo SET txtcol = 'hello world', intcol = 99 WHERE foo.baz = 'mars'
TODO: add the ability to use a join as part of the where statement - currently this implementation is limited to columns within the table being updated- Author:
- Tim Dudgeon
-
-
Constructor Summary
Constructors Constructor Description SQLUpdateStatement(TermEncoder termEncoder)
Creates a new instance of SQLUpdateStatement.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addValue(java.lang.String colName, int jdbcType, java.lang.Object value)
Add a value that will be updated.SQLUpdateStatement
addWhereClause(SQLWhereClauseElement where, SQLWhereClauseCompositeElement.Type type)
void
bindVariable(java.lang.String colName)
Bind a value that will be updated.java.lang.String
createSelectSql()
Creates a SELECT statement, which will list all IDs and values from columns scheduled for the update.java.lang.String
generateSql()
Generates the SQL that performs the update.java.lang.String
getIdColumnName()
SQLWhereClauseElement
getWhereClause()
void
setIdColumnName(java.lang.String idColumnName)
void
setTable(SchemaQualifiedName sqn)
Set the table to be updated.void
setUpdateAllRows()
Specifies that all rows are to be updated.protected void
setWhereClause(SQLWhereClauseElement where)
-
-
-
Constructor Detail
-
SQLUpdateStatement
public SQLUpdateStatement(TermEncoder termEncoder)
Creates a new instance of SQLUpdateStatement.- Parameters:
termEncoder
- How to write values to SQL
-
-
Method Detail
-
getIdColumnName
public java.lang.String getIdColumnName()
-
setIdColumnName
public void setIdColumnName(java.lang.String idColumnName)
-
setTable
public void setTable(SchemaQualifiedName sqn)
Set the table to be updated.- Parameters:
sqn
- The schema qualified name
-
setUpdateAllRows
public void setUpdateAllRows()
Specifies that all rows are to be updated. Use this instead of a where clause. Calling this method is mandatory if you want all rows to be updated to prevent inadvertant updates because you forgot to set a where clause. Sets any where clause to null
-
addValue
public void addValue(java.lang.String colName, int jdbcType, java.lang.Object value)
Add a value that will be updated. All rows matching the where clause will be updated with this value- Parameters:
colName
- The column namejdbcType
- The type of the column. Affects how the value is writen to SQL.value
- The value for the column
-
getWhereClause
public SQLWhereClauseElement getWhereClause()
-
addWhereClause
public SQLUpdateStatement addWhereClause(SQLWhereClauseElement where, SQLWhereClauseCompositeElement.Type type)
-
setWhereClause
protected void setWhereClause(SQLWhereClauseElement where)
-
createSelectSql
public java.lang.String createSelectSql()
Creates a SELECT statement, which will list all IDs and values from columns scheduled for the update.
-
bindVariable
public void bindVariable(java.lang.String colName)
Bind a value that will be updated. All rows matching the where clause will be updated with this value- Parameters:
colName
- The column name
-
generateSql
public java.lang.String generateSql()
Generates the SQL that performs the update. Depending on whether you used addValue or bindVariable determines whether the actual values are writen or ? placeholders for a PreparedStatement are written.- Returns:
- The SQL
-
-