org.apache.torque.util
Class SqlExpression

java.lang.Object
  extended byorg.apache.torque.util.SqlExpression

public class SqlExpression
extends java.lang.Object

This class represents a part of an SQL query found in the WHERE section. For example:

 table_a.column_a = table_b.column_a
 column LIKE 'F%'
 table.column < 3
 
This class is used primarily by BasePeer.

Version:
$Id: SqlExpression.java,v 1.24 2003/08/27 22:50:11 mpoeschl Exp $
Author:
John D. McNally, Daniel Rall, Fedor Karpelevitch, Martin Poeschl

Field Summary
private static char BACKSLASH
          escaped backslash
private static char SINGLE_QUOTE
          escaped single quote
 
Constructor Summary
SqlExpression()
           
 
Method Summary
static java.lang.String build(java.lang.String columnName, java.lang.Object criteria, SqlEnum comparison)
          Builds a simple SQL expression.
static java.lang.String build(java.lang.String columnName, java.lang.Object criteria, SqlEnum comparison, boolean ignoreCase, DB db)
          Builds a simple SQL expression.
static void build(java.lang.String columnName, java.lang.Object criteria, SqlEnum comparison, boolean ignoreCase, DB db, java.lang.StringBuffer whereClause)
          Builds a simple SQL expression.
(package private) static java.lang.String buildIn(java.lang.String columnName, java.lang.Object criteria, SqlEnum comparison, boolean ignoreCase, DB db)
          Takes a columnName and criteria (which must be an array) and builds a SQL 'IN' expression taking into account the ignoreCase flag.
(package private) static void buildIn(java.lang.String columnName, java.lang.Object criteria, SqlEnum comparison, boolean ignoreCase, DB db, java.lang.StringBuffer whereClause)
          Takes a columnName and criteria (which must be an array) and builds a SQL 'IN' expression taking into account the ignoreCase flag.
static java.lang.String buildInnerJoin(java.lang.String column, java.lang.String relatedColumn)
          Used to specify a join on two columns.
static java.lang.String buildInnerJoin(java.lang.String column, java.lang.String relatedColumn, boolean ignoreCase, DB db)
          Used to specify a join on two columns.
static void buildInnerJoin(java.lang.String column, java.lang.String relatedColumn, boolean ignoreCase, DB db, java.lang.StringBuffer whereClause)
          Used to specify a join on two columns.
(package private) static java.lang.String buildLike(java.lang.String columnName, java.lang.String criteria, SqlEnum comparison, boolean ignoreCase, DB db)
          Takes a columnName and criteria and builds an SQL phrase based on whether wildcards are present and the state of the ignoreCase flag.
(package private) static void buildLike(java.lang.String columnName, java.lang.String criteria, SqlEnum comparison, boolean ignoreCase, DB db, java.lang.StringBuffer whereClause)
          Takes a columnName and criteria and builds an SQL phrase based on whether wildcards are present and the state of the ignoreCase flag.
(package private) static java.lang.String processInValue(java.lang.Object value, boolean ignoreCase, DB db)
          Creates an appropriate string for an 'IN' clause from an object.
static java.lang.String quoteAndEscapeText(java.lang.String rawText, DB db)
          Quotes and escapes raw text for placement in a SQL expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SINGLE_QUOTE

private static final char SINGLE_QUOTE
escaped single quote

See Also:
Constant Field Values

BACKSLASH

private static final char BACKSLASH
escaped backslash

See Also:
Constant Field Values
Constructor Detail

SqlExpression

public SqlExpression()
Method Detail

buildInnerJoin

public static java.lang.String buildInnerJoin(java.lang.String column,
                                              java.lang.String relatedColumn)
Used to specify a join on two columns.

Parameters:
column - A column in one of the tables to be joined.
relatedColumn - The column in the other table to be joined.
Returns:
A join expression, e.g. UPPER(table_a.column_a) = UPPER(table_b.column_b).

buildInnerJoin

public static java.lang.String buildInnerJoin(java.lang.String column,
                                              java.lang.String relatedColumn,
                                              boolean ignoreCase,
                                              DB db)
Used to specify a join on two columns.

Parameters:
column - A column in one of the tables to be joined.
relatedColumn - The column in the other table to be joined.
ignoreCase - If true and columns represent Strings, the appropriate function defined for the database will be used to ignore differences in case.
db - Represents the database in use for vendor-specific functions.
Returns:
A join expression, e.g. UPPER(table_a.column_a) = UPPER(table_b.column_b).

buildInnerJoin

public static void buildInnerJoin(java.lang.String column,
                                  java.lang.String relatedColumn,
                                  boolean ignoreCase,
                                  DB db,
                                  java.lang.StringBuffer whereClause)
Used to specify a join on two columns.

Parameters:
column - A column in one of the tables to be joined.
relatedColumn - The column in the other table to be joined.
ignoreCase - If true and columns represent Strings, the appropriate function defined for the database will be used to ignore differences in case.
db - Represents the database in use for vendor-specific functions.
whereClause - A StringBuffer to which the sql expression will be appended.

build

public static java.lang.String build(java.lang.String columnName,
                                     java.lang.Object criteria,
                                     SqlEnum comparison)
                              throws TorqueException
Builds a simple SQL expression.

Parameters:
columnName - A column.
criteria - The value to compare the column against.
comparison - One of =, <, >, ^lt;=, >=, <>, !=, LIKE, etc.
Returns:
A simple SQL expression, e.g. UPPER(table_a.column_a) LIKE UPPER('ab%c').
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

build

public static java.lang.String build(java.lang.String columnName,
                                     java.lang.Object criteria,
                                     SqlEnum comparison,
                                     boolean ignoreCase,
                                     DB db)
                              throws TorqueException
Builds a simple SQL expression.

Parameters:
columnName - A column.
criteria - The value to compare the column against.
comparison - One of =, <, >, ^lt;=, >=, <>, !=, LIKE, etc.
ignoreCase - If true and columns represent Strings, the appropriate function defined for the database will be used to ignore differences in case.
db - Represents the database in use, for vendor specific functions.
Returns:
A simple sql expression, e.g. UPPER(table_a.column_a) LIKE UPPER('ab%c').
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

build

public static void build(java.lang.String columnName,
                         java.lang.Object criteria,
                         SqlEnum comparison,
                         boolean ignoreCase,
                         DB db,
                         java.lang.StringBuffer whereClause)
Builds a simple SQL expression.

Parameters:
columnName - A column.
criteria - The value to compare the column against.
comparison - One of =, <, >, ^lt;=, >=, <>, !=, LIKE, etc.
ignoreCase - If true and columns represent Strings, the appropriate function defined for the database will be used to ignore differences in case.
db - Represents the database in use, for vendor specific functions.
whereClause - A StringBuffer to which the sql expression will be appended.

buildLike

static java.lang.String buildLike(java.lang.String columnName,
                                  java.lang.String criteria,
                                  SqlEnum comparison,
                                  boolean ignoreCase,
                                  DB db)
Takes a columnName and criteria and builds an SQL phrase based on whether wildcards are present and the state of the ignoreCase flag. Multicharacter wildcards % and * may be used as well as single character wildcards, _ and ?. These characters can be escaped with \. e.g. criteria = "fre%" -> columnName LIKE 'fre%' -> UPPER(columnName) LIKE UPPER('fre%') criteria = "50\%" -> columnName = '50%'

Parameters:
columnName - A column.
criteria - The value to compare the column against.
comparison - Whether to do a LIKE or a NOT LIKE
ignoreCase - If true and columns represent Strings, the appropriate function defined for the database will be used to ignore differences in case.
db - Represents the database in use, for vendor specific functions.
Returns:
An SQL expression.

buildLike

static void buildLike(java.lang.String columnName,
                      java.lang.String criteria,
                      SqlEnum comparison,
                      boolean ignoreCase,
                      DB db,
                      java.lang.StringBuffer whereClause)
Takes a columnName and criteria and builds an SQL phrase based on whether wildcards are present and the state of the ignoreCase flag. Multicharacter wildcards % and * may be used as well as single character wildcards, _ and ?. These characters can be escaped with \. e.g. criteria = "fre%" -> columnName LIKE 'fre%' -> UPPER(columnName) LIKE UPPER('fre%') criteria = "50\%" -> columnName = '50%'

Parameters:
columnName - A column name.
criteria - The value to compare the column against.
comparison - Whether to do a LIKE or a NOT LIKE
ignoreCase - If true and columns represent Strings, the appropriate function defined for the database will be used to ignore differences in case.
db - Represents the database in use, for vendor specific functions.
whereClause - A StringBuffer to which the sql expression will be appended.

buildIn

static java.lang.String buildIn(java.lang.String columnName,
                                java.lang.Object criteria,
                                SqlEnum comparison,
                                boolean ignoreCase,
                                DB db)
Takes a columnName and criteria (which must be an array) and builds a SQL 'IN' expression taking into account the ignoreCase flag.

Parameters:
columnName - A column.
criteria - The value to compare the column against.
comparison - Either " IN " or " NOT IN ".
ignoreCase - If true and columns represent Strings, the appropriate function defined for the database will be used to ignore differences in case.
db - Represents the database in use, for vendor specific functions.
Returns:
An SQL expression.

buildIn

static void buildIn(java.lang.String columnName,
                    java.lang.Object criteria,
                    SqlEnum comparison,
                    boolean ignoreCase,
                    DB db,
                    java.lang.StringBuffer whereClause)
Takes a columnName and criteria (which must be an array) and builds a SQL 'IN' expression taking into account the ignoreCase flag.

Parameters:
columnName - A column.
criteria - The value to compare the column against.
comparison - Either " IN " or " NOT IN ".
ignoreCase - If true and columns represent Strings, the appropriate function defined for the database will be used to ignore differences in case.
db - Represents the database in use, for vendor specific functions.
whereClause - A StringBuffer to which the sql expression will be appended.

processInValue

static java.lang.String processInValue(java.lang.Object value,
                                       boolean ignoreCase,
                                       DB db)
Creates an appropriate string for an 'IN' clause from an object. Adds quoting and/or UPPER() as appropriate. This is broken out into a seperate method as it is used in two places in buildIn, depending on whether an array or List is being looped over.

Parameters:
value - The value to process.
ignoreCase - Coerce the value suitably for ignoring case.
db - Represents the database in use for vendor specific functions.
Returns:
Processed value as String.

quoteAndEscapeText

public static java.lang.String quoteAndEscapeText(java.lang.String rawText,
                                                  DB db)
Quotes and escapes raw text for placement in a SQL expression. For simplicity, the text is assumed to be neither quoted nor escaped.

Parameters:
rawText - The unquoted, unescaped text to process.
db - the db
Returns:
Quoted and escaped text.


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