org.castor.cpa.persistence.sql.query
Class QueryContext

java.lang.Object
  extended by org.castor.cpa.persistence.sql.query.QueryContext

public class QueryContext
extends java.lang.Object

Function of QueryContext is 3 fold. It offers information about small syntax differences of database engines. For example about quoting of qualifier and column names. In addition it is a builder for SQL query strings. Third function is to map parameter names to indices and offer a method to bind values to these named parameters.

Version:
$Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
Author:
Ahmad Hassan, Ralf Joachim

Constructor Summary
QueryContext()
          Default constructor for a delete query that does not quote qualifier and column names.
QueryContext(PersistenceFactory factory)
          Constructor that uses given factory instance to quote qualifier and column names.
 
Method Summary
 void addParameter(java.lang.String name)
          Add a named parameter to the query context.
 QueryContext append(char chr)
          Append the given character to end of SQL query string.
 QueryContext append(java.lang.String str)
          Append the given string to end of SQL query string.
 void bindParameter(java.sql.PreparedStatement stmt, java.lang.String name, java.lang.Object value, int type)
          Bind value of named parameter to prepared statement.
 java.lang.String getSequenceNextValString(java.lang.String seqName)
          Returns the database engine specific string to fetch sequence next value.
 int parameterSize()
          Returns the number of parameter in sql insert statement.
 java.lang.String quoteName(java.lang.String name)
          Returns the quoted identifier suitable for preventing conflicts between database identifiers and reserved keywords.
 java.lang.String toString()
          Returns the SQL query string build by previous calls to one of the append methods.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

QueryContext

public QueryContext()
Default constructor for a delete query that does not quote qualifier and column names.


QueryContext

public QueryContext(PersistenceFactory factory)
Constructor that uses given factory instance to quote qualifier and column names.

Parameters:
factory - Persistence factory for the database engine the entity is persisted in. Used to format the SQL statement.
Method Detail

quoteName

public java.lang.String quoteName(java.lang.String name)
Returns the quoted identifier suitable for preventing conflicts between database identifiers and reserved keywords.

Parameters:
name - The identifier (table, column, etc).
Returns:
The quoted identifier.

getSequenceNextValString

public java.lang.String getSequenceNextValString(java.lang.String seqName)
Returns the database engine specific string to fetch sequence next value.

Parameters:
seqName - Name of the sequence.
Returns:
String to fetch sequence next value.

append

public final QueryContext append(char chr)
Append the given character to end of SQL query string.

Parameters:
chr - Character to append.
Returns:
This QueryContext instance.

append

public final QueryContext append(java.lang.String str)
Append the given string to end of SQL query string.

Parameters:
str - String to append.
Returns:
This QueryContext instance.

toString

public final java.lang.String toString()
Returns the SQL query string build by previous calls to one of the append methods.

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

addParameter

public final void addParameter(java.lang.String name)
Add a named parameter to the query context. It is important to add the parameter names in the sequence the parameters appear in the SQL query string for binding to work properly.

Parameters:
name - Name of the parameter.

bindParameter

public final void bindParameter(java.sql.PreparedStatement stmt,
                                java.lang.String name,
                                java.lang.Object value,
                                int type)
                         throws java.sql.SQLException
Bind value of named parameter to prepared statement. If parameter name is unknown no binding takes place and its name will only be logged.

Parameters:
stmt - Prepared statement to bind value of named parameter to.
name - Name of the parameter to bind.
value - Value of the named parameter to bind.
type - SQL column type.
Throws:
java.sql.SQLException - If a database access error occurs or the type of the given object is ambiguous.

parameterSize

public final int parameterSize()
Returns the number of parameter in sql insert statement.

Returns:
parameter size.


Intalio Inc. (C) 1999-2008. All rights reserved http://www.intalio.com