com.im.commons.db 5.9.2

com.im.commons.db.dml
Class SQLUpdateStatement

java.lang.Object
  extended by com.im.commons.db.dml.SQLUpdateStatement

public class SQLUpdateStatement
extends 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


Field Summary
static String BIND_CHARACTER
           
 
Constructor Summary
SQLUpdateStatement(SQLWriter sqlWriter)
          Creates a new instance of SQLUpdateStatement
 
Method Summary
 void addValue(String colName, DBDatabaseInfo.ColumnSQLType colType, Object value)
          Add a value that will be updated.
 SQLUpdateStatement addWhereClause(SQLWhereClauseElement where, SQLWhereClauseCompositeElement.Type type)
           
 void bindVariable(String colName)
          Bind a value that will be updated.
 String createSelectSql()
          Creates a SELECT statement, which will list all IDs and values from columns scheduled for the update.
 String generateSql()
          Generates the SQL that performs the update.
 String getIdColumnName()
           
 SQLWhereClauseElement getWhereClause()
           
 void setIdColumnName(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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BIND_CHARACTER

public static final String BIND_CHARACTER
See Also:
Constant Field Values
Constructor Detail

SQLUpdateStatement

public SQLUpdateStatement(SQLWriter sqlWriter)
Creates a new instance of SQLUpdateStatement

Parameters:
sqlWriter - How to write values to SQL
Method Detail

getIdColumnName

public String getIdColumnName()

setIdColumnName

public void setIdColumnName(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(String colName,
                     DBDatabaseInfo.ColumnSQLType colType,
                     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 name
colType - 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 String createSelectSql()
Creates a SELECT statement, which will list all IDs and values from columns scheduled for the update.


bindVariable

public void bindVariable(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 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

com.im.commons.db 5.9.2