Package com.im.commons.db.dml
Class SQLUpdateStatement
- java.lang.Object
-
- com.im.commons.db.dml.SQLUpdateStatement
-
public final class SQLUpdateStatement extends java.lang.ObjectDefines 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 SQLWill 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 voidaddValue(java.lang.String colName, int jdbcType, java.lang.Object value)Add a value that will be updated.SQLUpdateStatementaddWhereClause(SQLWhereClauseElement where, SQLWhereClauseCompositeElement.Type type)voidbindVariable(java.lang.String colName)Bind a value that will be updated.java.lang.StringcreateSelectSql()Creates a SELECT statement, which will list all IDs and values from columns scheduled for the update.java.lang.StringgenerateSql()Generates the SQL that performs the update.java.lang.StringgetIdColumnName()SQLWhereClauseElementgetWhereClause()voidsetIdColumnName(java.lang.String idColumnName)voidsetTable(SchemaQualifiedName sqn)Set the table to be updated.voidsetUpdateAllRows()Specifies that all rows are to be updated.protected voidsetWhereClause(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
-
-