package org.litesoft.orsup.selection; // Copyright Status: // // All Software available from LiteSoft.org (including this file) is // hereby released into the public domain. // // It is free! As in, you may use it freely in both commercial and // non-commercial applications, bundle it with your software // distribution, include it on a CD-ROM, list the source code in a book, // mirror the documentation at your own web site, or use it in any other // way you see fit. // // NO Warranty! // // All software is provided "as is". // // There is ABSOLUTELY NO WARRANTY OF ANY KIND: not for the design, fitness // (for a particular purpose), level of errors (or lack thereof), or // applicability of this software. The entire risk as to the quality // and performance of this software is with you. Should this software // prove defective, you assume the cost of all necessary servicing, repair // or correction. // // In no event unless required by applicable law or agreed to in writing // will any party who created or may modify and/or redistribute this // software, be liable to you for damages, including any general, // special, incidental or consequential damages arising out of the use or // inability to use this software (including but not limited to loss of // data or data being rendered inaccurate or losses sustained by you or // third parties or a failure of this software to operate with any // other programs), even if such holder or other party has been advised // of the possibility of such damages. // // NOTE: Should you discover a bug, have a recogmendation for a change, wish // to submit modifications, or wish to add new classes/functionality, // please email them to: // // changes44@litesoft.org // /** * An abstract representation of a SQL WHERE clause.
* * Using the WhereClauseFactory a complex SQL WHERE clause * can be generated. The concrete extensions of WhereClause generated * by the WhereClauseFactory can form a tree that represents any * arbitrarily complex expression.
* * Two methods are provided to translate a tree of WhereClauses * into Strings:
*
* toString() ; * toSQL() ; ** * The toString() is intended for debuging purposes.
* * If you need to create/support a specific SQL WHERE clause that * is not provided, you would need to extend the WhereClauseFactory, * and extend this class. However, you will probably not extend this class * directly, but extend one of the following abstract helper classes:
*
* * By using the Types (public static final ints), * direct interpretation / alternate translation can be accomplished.
* * See WhereClauseFactory
* * Exceptions: All problems caught when the parameter(s) are checked (as * indicated/implied in the @param tags) will generate an IllegalArgumentException, * and means the API user has a problem. If a NullPointerException (or some * others, like: ClassCastException or ArrayIndexOutOfBoundsException) is thrown, * it means the API developer has a problem. Any Exception that is explicitly * thrown in the API, but unrelated to a parameter, will be listed in the throws * clause (and hopefully in the tag @throws). These may (but probably won't) be * checked Exceptions. * * @author George Smith * @version 1.03 02/07/02 Exception Policy, Use of IllegalArgument class. * @version 1.02 11/05/01 Completed? JavaDocs * @version 1.0 10/07/01 */ public abstract class WhereClause { /** Type for Equals. Instantiated as AbstractWhereClauseColumnAndValue */ public static final int EQUALS = 0; /** Type for Less Than. Instantiated as AbstractWhereClauseColumnAndValue */ public static final int LESSTHAN = 1; /** Type for Greater Than. Instantiated as AbstractWhereClauseColumnAndValue */ public static final int GREATERTHAN = 2; /** Type for Between. Instantiated as AbstractWhereClauseColumnAndTwoValues */ public static final int BETWEEN = 3; /** Type for Or. Instantiated as AbstractWhereClauseAssociativeList */ public static final int OR = 4; /** Type for And. Instantiated as AbstractWhereClauseAssociativeList */ public static final int AND = 5; /** Type for Not. Instantiated as AbstractWhereClauseWrapper */ public static final int NOT = 6; /** Type for String Contains. Instantiated as AbstractWhereClauseColumnAndLikeValue */ public static final int CONTAINS = 7; /** Type for String Starts With. Instantiated as AbstractWhereClauseColumnAndLikeValue */ public static final int STARTS_WITH = 8; /** Type for String Ends With. Instantiated as AbstractWhereClauseColumnAndLikeValue */ public static final int ENDS_WITH = 9; /** Type for Is Null. Instantiated as AbstractWhereClauseColumnReference */ public static final int IS_NULL = 10; /** Type for Is In. Instantiated as AbstractWhereClauseColumnIsIn */ public static final int IS_IN = 11; private int zType; /** * Constructor that simply sets the Type of this WhereClause.
* * Please see the public static final ints for the currently * supported Types.
* * @param pType the Type for this WhereClause. */ protected WhereClause( int pType ) { zType = pType; } /** * Accessor for the Type of this WhereClause.
* * Please see the public static final ints for the currently * supported Types.
* * @return the Type. */ public final int getType() { return zType; } /** * Generate a Debug friendly representation.
* * @return a String for Debuging. */ public final String toString() { StringBuffer sb = new StringBuffer( "Where " ); toStringHelper( sb ); return sb.toString(); } /** * Generate a String that may be used in a SQL statement, where the * WHERE clause would go.
* * Note: This String can get quite large.
* * @return the WHERE clause for SQL statement as a String. */ public final String toSQL() { StringBuffer sb = new StringBuffer( "WHERE " ); toSqlHelper( sb ); return sb.toString(); } /** * Helper method for toString() that provides a more efficient * mechanism for the recursive decent of a WhereClause tree.
* * @param pSB the StringBuffer to build the WhereClause into.
* * @see #toString() */ protected abstract void toStringHelper( StringBuffer pSB ); /** * Helper method for toSQL() that provides a more efficient * mechanism for the recursive decent of a WhereClause tree.
* * @param pSB the StringBuffer to build the WhereClause into.
* * @see #toSQL() */ protected abstract void toSqlHelper( StringBuffer pSB ); /** * Helper method for toSqlHelper() that adds a prefix and then * parenthesizes a nested WhereClause.
* * @param pSB the StringBuffer to build the WhereClause into. * @param pPreFix the prefix to append. * @param pWhereClause the nested WhereClause to wrap with parenthesizes * and append.
* * @see #toSqlHelper(StringBuffer) */ protected static void toSqlHelperParenthesizer( StringBuffer pSB , String pPreFix , WhereClause pWhereClause ) { pSB.append( pPreFix ); pSB.append( ' ' ); toSqlHelperParenthesizer( pSB , pWhereClause ); } /** * Helper method for toSqlHelper() that adds a parenthesized * nested WhereClause.
* * @param pSB the StringBuffer to build the WhereClause into. * @param pWhereClause the nested WhereClause to wrap with parenthesizes * and append.
* * @see #toSqlHelper(StringBuffer) */ protected static void toSqlHelperParenthesizer( StringBuffer pSB , WhereClause pWhereClause ) { pSB.append( '(' ); pWhereClause.toSqlHelper( pSB ); pSB.append( ')' ); } /** * Helper method for toStringHelper() that adds a parenthesized * nested WhereClause.
* * @param pSB the StringBuffer to build the WhereClause into. * @param pWhereClause the nested WhereClause to wrap with parenthesizes * and append.
* * @see #toStringHelper(StringBuffer) */ protected static void toStringHelperParenthesizer( StringBuffer pSB , WhereClause pWhereClause ) { pSB.append( '(' ); pWhereClause.toStringHelper( pSB ); pSB.append( ')' ); } /** * Helper method for toStringHelper() that adds a prefix and then * parenthesizes a nested WhereClause.
* * @param pSB the StringBuffer to build the WhereClause into. * @param pPreFix the prefix to append. * @param pWhereClause the nested WhereClause to wrap with parenthesizes * and append.
* * @see #toStringHelper(StringBuffer) */ protected static void toStringHelperParenthesizer( StringBuffer pSB , String pPreFix , WhereClause pWhereClause ) { pSB.append( pPreFix ); pSB.append( ' ' ); toStringHelperParenthesizer( pSB , pWhereClause ); } }