com.almworks.sqlite4java
Class SQLParts

java.lang.Object
  extended by com.almworks.sqlite4java.SQLParts

public final class SQLParts
extends java.lang.Object

SQLParts is a means to avoid excessive garbage production during String concatenation when SQL is constructed.

Often, same SQL statements get constructed over and over again and passed to SQLiteConnecton.prepare. To avoid producing garbage and to facilitate quick look-up in the cache of prepared statements, better use SQLParts than String concatenation.

SQLParts object may be fixed, which means it cannot be changed anymore.

This class is not thread-safe and not intended to be used from different threads.

Author:
Igor Sereda

Constructor Summary
SQLParts()
          Create empty SQLParts object.
SQLParts(SQLParts copyFrom)
          Create a copy of another SQLParts object.
SQLParts(java.lang.String sql)
          Create an instance of SQLParts containing only single piece of SQL.
 
Method Summary
 SQLParts append(SQLParts parts)
          Adds all parts from a different SQLParts to the SQL.
 SQLParts append(java.lang.String part)
          Adds a part to the SQL.
 SQLParts appendParams(int count)
          Appends an SQL part consisting of a list of bind parameters.
 void clear()
          Empties this SQLParts instance.
 boolean equals(java.lang.Object o)
           
 SQLParts fix()
          Makes instance immutable.
 SQLParts getFixedParts()
          If this object is fixed, returns itself, otherwise returns a fixed copy of this object.
 java.util.List<java.lang.String> getParts()
          Gets the list of SQL parts.
 int hashCode()
           
 boolean isFixed()
          Checks if this instance is fixed.
 java.lang.String toString()
          Returns the SQL representation of this params
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SQLParts

public SQLParts()
Create empty SQLParts object.


SQLParts

public SQLParts(SQLParts copyFrom)
Create a copy of another SQLParts object. SQL pieces are copied, but the new object is not fixed even if the original object is fixed.

Parameters:
copyFrom - the original object

SQLParts

public SQLParts(java.lang.String sql)
Create an instance of SQLParts containing only single piece of SQL.

Parameters:
sql - SQL piece
Method Detail

fix

public SQLParts fix()
Makes instance immutable. Further calls to append(java.lang.String) will throw an exception.

Returns:
this object, fixed

getFixedParts

public SQLParts getFixedParts()
If this object is fixed, returns itself, otherwise returns a fixed copy of this object.

Returns:
fixed SQLParts, representing the same SQL

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

clear

public void clear()
Empties this SQLParts instance.

Throws:
java.lang.IllegalStateException - if instance is fixed

append

public SQLParts append(java.lang.String part)
Adds a part to the SQL.

Parameters:
part - a piece of SQL added
Returns:
this instance
Throws:
java.lang.IllegalStateException - if instance is fixed

append

public SQLParts append(SQLParts parts)
Adds all parts from a different SQLParts to the SQL.

Parameters:
parts - source object to copy parts from, may be null
Returns:
this instance
Throws:
java.lang.IllegalStateException - if instance is fixed

appendParams

public SQLParts appendParams(int count)
Appends an SQL part consisting of a list of bind parameters.

That is, appendParams(1) appends ?, appendParams(2) appends ?,? and so on.

Parameters:
count - the number of parameters ("?" symbols) to be added
Returns:
this instance
Throws:
java.lang.IllegalStateException - if instance is fixed

toString

public java.lang.String toString()
Returns the SQL representation of this params

Overrides:
toString in class java.lang.Object
Returns:
SQL

getParts

public java.util.List<java.lang.String> getParts()
Gets the list of SQL parts.

Returns:
unmodifiable list of SQL pieces.

isFixed

public boolean isFixed()
Checks if this instance is fixed.

Returns:
true if the instance is fixed, that is, read-only
See Also:
fix()