?? criteria.java
字號:
package org.apache.torque.util;/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import java.io.Serializable;import java.lang.reflect.Array;import java.math.BigDecimal;import java.util.ArrayList;import java.util.Arrays;import java.util.GregorianCalendar;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.commons.collections.OrderedMap;import org.apache.commons.collections.map.ListOrderedMap;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.torque.Torque;import org.apache.torque.adapter.DB;import org.apache.torque.om.DateKey;import org.apache.torque.om.ObjectKey;/** * This is a utility class that is used for retrieving different types * of values from a hashtable based on a simple name string. This * class is meant to minimize the amount of casting that needs to be * done when working with Hashtables. * * NOTE: other methods will be added as needed and as time permits. * * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a> * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> * @author <a href="mailto:eric@dobbse.net">Eric Dobbs</a> * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> * @author <a href="mailto:sam@neurogrid.com">Sam Joseph</a> * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a> * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a> * @version $Id: Criteria.java,v 1.47 2005/07/02 15:22:32 tfischer Exp $ */public class Criteria extends Hashtable{ /** Comparison type. */ public static final SqlEnum EQUAL = SqlEnum.EQUAL; /** Comparison type. */ public static final SqlEnum NOT_EQUAL = SqlEnum.NOT_EQUAL; /** Comparison type. */ public static final SqlEnum ALT_NOT_EQUAL = SqlEnum.ALT_NOT_EQUAL; /** Comparison type. */ public static final SqlEnum GREATER_THAN = SqlEnum.GREATER_THAN; /** Comparison type. */ public static final SqlEnum LESS_THAN = SqlEnum.LESS_THAN; /** Comparison type. */ public static final SqlEnum GREATER_EQUAL = SqlEnum.GREATER_EQUAL; /** Comparison type. */ public static final SqlEnum LESS_EQUAL = SqlEnum.LESS_EQUAL; /** Comparison type. */ public static final SqlEnum LIKE = SqlEnum.LIKE; /** Comparison type. */ public static final SqlEnum NOT_LIKE = SqlEnum.NOT_LIKE; /** Comparison type. */ public static final SqlEnum ILIKE = SqlEnum.ILIKE; /** Comparison type. */ public static final SqlEnum NOT_ILIKE = SqlEnum.NOT_ILIKE; /** Comparison type. */ public static final SqlEnum CUSTOM = SqlEnum.CUSTOM; /** Comparison type. */ public static final SqlEnum DISTINCT = SqlEnum.DISTINCT; /** Comparison type. */ public static final SqlEnum IN = SqlEnum.IN; /** Comparison type. */ public static final SqlEnum NOT_IN = SqlEnum.NOT_IN; /** Comparison type. */ public static final SqlEnum ALL = SqlEnum.ALL; /** Comparison type. */ public static final SqlEnum JOIN = SqlEnum.JOIN; /** "Order by" qualifier - ascending */ private static final SqlEnum ASC = SqlEnum.ASC; /** "Order by" qualifier - descending */ private static final SqlEnum DESC = SqlEnum.DESC; /** "IS NULL" null comparison */ public static final SqlEnum ISNULL = SqlEnum.ISNULL; /** "IS NOT NULL" null comparison */ public static final SqlEnum ISNOTNULL = SqlEnum.ISNOTNULL; /** "CURRENT_DATE" ANSI SQL function */ public static final SqlEnum CURRENT_DATE = SqlEnum.CURRENT_DATE; /** "CURRENT_TIME" ANSI SQL function */ public static final SqlEnum CURRENT_TIME = SqlEnum.CURRENT_TIME; /** "LEFT JOIN" SQL statement */ public static final SqlEnum LEFT_JOIN = SqlEnum.LEFT_JOIN; /** "RIGHT JOIN" SQL statement */ public static final SqlEnum RIGHT_JOIN = SqlEnum.RIGHT_JOIN; /** "INNER JOIN" SQL statement */ public static final SqlEnum INNER_JOIN = SqlEnum.INNER_JOIN; private static final int DEFAULT_CAPACITY = 10; private boolean ignoreCase = false; private boolean singleRecord = false; private boolean cascade = false; private UniqueList selectModifiers = new UniqueList(); private UniqueList selectColumns = new UniqueList(); private UniqueList orderByColumns = new UniqueList(); private UniqueList groupByColumns = new UniqueList(); private Criterion having = null; private OrderedMap asColumns = ListOrderedMap.decorate(new HashMap()); private List joins = null; /** The name of the database. */ private String dbName; /** The name of the database as given in the contructor. */ private String originalDbName; /** * To limit the number of rows to return. <code>-1</code> means return all * rows. */ private int limit = -1; /** To start the results at a row other than the first one. */ private int offset = 0; private HashMap aliases = null; private boolean useTransaction = false; /** the log. */ private static Log log = LogFactory.getLog(Criteria.class); /** * Creates a new instance with the default capacity. */ public Criteria() { this(DEFAULT_CAPACITY); } /** * Creates a new instance with the specified capacity. * * @param initialCapacity An int. */ public Criteria(int initialCapacity) { this(Torque.getDefaultDB(), initialCapacity); } /** * Creates a new instance with the default capacity which corresponds to * the specified database. * * @param dbName The dabase name. */ public Criteria(String dbName) { this(dbName, DEFAULT_CAPACITY); } /** * Creates a new instance with the specified capacity which corresponds to * the specified database. * * @param dbName The dabase name. * @param initialCapacity The initial capacity. */ public Criteria(String dbName, int initialCapacity) { super(initialCapacity); this.dbName = dbName; this.originalDbName = dbName; } /** * Brings this criteria back to its initial state, so that it * can be reused as if it was new. Except if the criteria has grown in * capacity, it is left at the current capacity. */ public void clear() { super.clear(); ignoreCase = false; singleRecord = false; cascade = false; selectModifiers.clear(); selectColumns.clear(); orderByColumns.clear(); groupByColumns.clear(); having = null; asColumns.clear(); joins = null; dbName = originalDbName; offset = 0; limit = -1; aliases = null; useTransaction = false; } /** * Add an AS clause to the select columns. Usage: * <p> * <code> * * Criteria myCrit = new Criteria(); * myCrit.addAsColumn("alias", "ALIAS("+MyPeer.ID+")"); * * </code> * * @param name wanted Name of the column * @param clause SQL clause to select from the table * * If the name already exists, it is replaced by the new clause. * * @return A modified Criteria object. */ public Criteria addAsColumn(String name, String clause) { asColumns.put(name, clause); return this; } /** * Get the column aliases. * * @return A Map which map the column alias names * to the alias clauses. */ public Map getAsColumns() { return asColumns; } /** * Allows one to specify an alias for a table that can * be used in various parts of the SQL. * * @param alias a <code>String</code> value * @param table a <code>String</code> value */ public void addAlias(String alias, String table) { if (aliases == null) { aliases = new HashMap(8); } aliases.put(alias, table); } /** * Returns the table name associated with an alias. * * @param alias a <code>String</code> value * @return a <code>String</code> value */ public String getTableForAlias(String alias) { if (aliases == null) { return null; } return (String) aliases.get(alias); } /** * Does this Criteria Object contain the specified key? * * @param table The name of the table. * @param column The name of the column. * @return True if this Criteria Object contain the specified key. */ public boolean containsKey(String table, String column) { return containsKey(table + '.' + column); } /** * Convenience method to return value as a boolean. * * @param column String name of column. * @return A boolean. */ public boolean getBoolean(String column) { return ((Boolean) getCriterion(column).getValue()).booleanValue(); } /** * Convenience method to return value as a boolean. * * @param table String name of table. * @param column String name of column. * @return A boolean. */ public boolean getBoolean(String table, String column) { return getBoolean(new StringBuffer(table.length() + column.length() + 1) .append(table).append('.').append(column) .toString()); } /** * Will force the sql represented by this criteria to be executed within * a transaction. This is here primarily to support the oid type in * postgresql. Though it can be used to require any single sql statement * to use a transaction. */ public void setUseTransaction(boolean v) { useTransaction = v; } /** * called by BasePeer to determine whether the sql command specified by * this criteria must be wrapped in a transaction. * * @return a <code>boolean</code> value */ protected boolean isUseTransaction() { return useTransaction; } /** * Method to return criteria related to columns in a table. * * @param column String name of column. * @return A Criterion. */ public Criterion getCriterion(String column) { return (Criterion) super.get(column); } /** * Method to return criteria related to a column in a table. * * @param table String name of table. * @param column String name of column. * @return A Criterion. */ public Criterion getCriterion(String table, String column) { return getCriterion( new StringBuffer(table.length() + column.length() + 1) .append(table).append('.').append(column) .toString()); } /** * Method to return criterion that is not added automatically * to this Criteria. This can be used to chain the * Criterions to form a more complex where clause. * * @param column String full name of column (for example TABLE.COLUMN). * @return A Criterion. */ public Criterion getNewCriterion(String column, Object value, SqlEnum comparison) { return new Criterion(column, value, comparison); } /** * Method to return criterion that is not added automatically * to this Criteria. This can be used to chain the * Criterions to form a more complex where clause. * * @param table String name of table. * @param column String name of column. * @return A Criterion. */ public Criterion getNewCriterion(String table, String column, Object value, SqlEnum comparison) { return new Criterion(table, column, value, comparison); } /** * This method adds a prepared Criterion object to the Criteria. * You can get a new, empty Criterion object with the * getNewCriterion() method. If a criterion for the requested column * already exists, it is replaced. This is used as follows: * * <p> * <code> * Criteria crit = new Criteria(); * Criteria.Criterion c = crit * .getNewCriterion(BasePeer.ID, new Integer(5), Criteria.LESS_THAN); * crit.add(c); * </code> * * @param c A Criterion object * * @return A modified Criteria object. */ public Criteria add(Criterion c) { StringBuffer sb = new StringBuffer(c.getTable().length() + c.getColumn().length() + 1); sb.append(c.getTable()); sb.append('.'); sb.append(c.getColumn()); super.put(sb.toString(), c); return this; } /** * Method to return a String table name. * * @param name A String with the name of the key. * @return A String with the value of the object at key. */ public String getColumnName(String name)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -