org.apache.torque.util
Class SQLBuilder

java.lang.Object
  extended by org.apache.torque.util.SQLBuilder

public final class SQLBuilder
extends java.lang.Object

Factored out code that is used to process SQL tables. This code comes from BasePeer and is put here to reduce complexity in the BasePeer class. You should not use the methods here directly!

Version:
$Id: SQLBuilder.java 535596 2007-05-06 10:47:39Z tfischer $
Author:
Henning P. Schmiedehausen, Thomas Fischer

Nested Class Summary
static interface SQLBuilder.QueryCallback
          Inner Interface that defines the Callback method for the buildQuery Criterion evaluation
static interface SQLBuilder.TableCallback
          Inner Interface that defines the Callback method for the Table creation loop.
 
Field Summary
static java.lang.String[] COLUMN_CHARS
          Function Characters
static java.lang.String[] DELIMITERS
           
protected static org.apache.commons.logging.Log log
          Logging
 
Method Summary
static Query buildQueryClause(Criteria crit, java.util.List params, SQLBuilder.QueryCallback qc)
          Builds a Query clause for Updating and deleting
static boolean fromClauseContainsTableName(UniqueList fromClause, java.lang.String tableName)
          Checks if the Tablename tableName is already contained in a from clause.
static java.lang.String getFullTableName(java.lang.String table, java.lang.String dbName)
          Fully qualify a table name with an optional schema reference
static java.lang.String getTableName(java.lang.String name, java.lang.String dbName)
          Returns a table name from an identifier.
static java.lang.String getTableNameForFromClause(java.lang.String tableName, Criteria criteria)
          Returns the tablename which can be added to a From Clause.
static java.util.Set getTableSet(Criteria crit, SQLBuilder.TableCallback tableCallback)
          Returns a set of all tables and possible aliases referenced from a criterion.
static java.lang.String getUnqualifiedTableName(java.lang.String table)
          Remove a possible schema name from the table name.
static void throwMalformedColumnNameException(java.lang.String criteriaPhrase, java.lang.String columnName)
          Throws a TorqueException with the malformed column name error message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log
Logging


COLUMN_CHARS

public static final java.lang.String[] COLUMN_CHARS
Function Characters


DELIMITERS

public static final java.lang.String[] DELIMITERS
Method Detail

getFullTableName

public static java.lang.String getFullTableName(java.lang.String table,
                                                java.lang.String dbName)
                                         throws TorqueException
Fully qualify a table name with an optional schema reference

Parameters:
table - The table name to use. If null is passed in, null is returned.
dbName - The name of the database to which this tables belongs. If null is passed, the default database is used.
Returns:
The table name to use inside the SQL statement. If null is passed into this method, null is returned.
Throws:
TorqueException - if an error occurs

getUnqualifiedTableName

public static java.lang.String getUnqualifiedTableName(java.lang.String table)
Remove a possible schema name from the table name.

Parameters:
table - The table name to use
Returns:
The table name with a possible schema name stripped off

getTableName

public static java.lang.String getTableName(java.lang.String name,
                                            java.lang.String dbName)
                                     throws TorqueException
Returns a table name from an identifier. Each identifier is to be qualified as [schema.]table.column. This could also contain FUNCTION([schema.]table.column).

Parameters:
name - The (possible fully qualified) identifier name
Returns:
the fully qualified table name
Throws:
TorqueException - If the identifier name was malformed

getTableSet

public static java.util.Set getTableSet(Criteria crit,
                                        SQLBuilder.TableCallback tableCallback)
Returns a set of all tables and possible aliases referenced from a criterion. The resulting Set can be directly used to build a WHERE clause

Parameters:
crit - A Criteria object
tableCallback - A Callback Object
Returns:
A Set of tables.

buildQueryClause

public static Query buildQueryClause(Criteria crit,
                                     java.util.List params,
                                     SQLBuilder.QueryCallback qc)
                              throws TorqueException
Builds a Query clause for Updating and deleting

Parameters:
crit - a Criteria value
params - a List value
qc - a QueryCallback value
Returns:
a Query value
Throws:
TorqueException - if an error occurs

throwMalformedColumnNameException

public static void throwMalformedColumnNameException(java.lang.String criteriaPhrase,
                                                     java.lang.String columnName)
                                              throws TorqueException
Throws a TorqueException with the malformed column name error message. The error message looks like this:

Malformed column name in Criteria [criteriaPhrase]: '[columnName]' is not of the form 'table.column'

Parameters:
criteriaPhrase - a String, one of "select", "join", or "order by"
columnName - a String containing the offending column name
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

getTableNameForFromClause

public static java.lang.String getTableNameForFromClause(java.lang.String tableName,
                                                         Criteria criteria)
Returns the tablename which can be added to a From Clause. This takes care of any aliases that might be defined. For example, if an alias "a" for the table AUTHOR is defined in the Criteria criteria, getTableNameForFromClause("a", criteria) returns "AUTHOR a".

Parameters:
tableName - the name of a table or the alias for a table
criteria - a criteria object to resolve a possible alias
Returns:
either the tablename itself if tableOrAliasName is not an alias, or a String of the form "tableName tableOrAliasName" if tableOrAliasName is an alias for a table name

fromClauseContainsTableName

public static boolean fromClauseContainsTableName(UniqueList fromClause,
                                                  java.lang.String tableName)
Checks if the Tablename tableName is already contained in a from clause. If tableName and the tablenames in fromClause are generated by getTablenameForFromClause(String, Criteria), (which they usually are), then different aliases for the same table are treated as different tables: E.g. fromClauseContainsTableName(fromClause, "table_a a") returns false if fromClause contains only another alias for table_a , e.g. "table_a aa" and the unaliased tablename "table_a". Special case: If tableName is null, true is returned.

Parameters:
fromClause - a list containing only elements of type. Query.FromElement
tableName - the tablename to check
Returns:
if the Tablename tableName is already contained in a from clause. If tableName is null, true is returned.


Copyright © 2000-2012 Apache Software Foundation. All Rights Reserved.