J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.sql.rowset; |
| 27 | |
| 28 | import javax.sql.*; |
| 29 | import java.sql.*; |
| 30 | |
| 31 | /** |
| 32 | * The standard interface that provides the framework for all |
| 33 | * <code>FilteredRowSet</code> objects to describe their filters. |
| 34 | * <p> |
| 35 | * <h3>1.0 Background</h3> |
| 36 | * The <code>Predicate</code> interface is a standard interface that |
| 37 | * applications can implement to define the filter they wish to apply to a |
| 38 | * a <code>FilteredRowSet</code> object. A <code>FilteredRowSet</code> |
| 39 | * object consumes implementations of this interface and enforces the |
| 40 | * constraints defined in the implementation of the method <code>evaluate</code>. |
| 41 | * A <code>FilteredRowSet</code> object enforces the filter constraints in a |
| 42 | * bi-directional manner: It outputs only rows that are within |
| 43 | * the constraints of the filter; and conversely, it inserts, modifies, or updates |
| 44 | * only rows that are within the constraints of the filter. |
| 45 | * |
| 46 | * <h3>2.0 Implementation Guidelines</h3> |
| 47 | * In order to supply a predicate for the <code>FilteredRowSet</code>. |
| 48 | * this interface must be implemented. At this time, the JDBC RowSet |
| 49 | * Implementations (JSR-114) does not specify any standard filters definitions. |
| 50 | * By specifying a standard means and mechanism for a range of filters to be |
| 51 | * defined and deployed with both the reference and vendor implementations |
| 52 | * of the <code>FilteredRowSet</code> interface, this allows for a flexible |
| 53 | * and application motivated implementations of <code>Predicate</code> to emerge. |
| 54 | * <p> |
| 55 | * A sample implementation would look something like this: |
| 56 | * <pre> |
| 57 | * <code> |
| 58 | * public class Range implements Predicate { |
| 59 | * |
| 60 | * private Object lo[]; |
| 61 | * private Object hi[]; |
| 62 | * private int idx[]; |
| 63 | * |
| 64 | * public Range(Object[] lo, Object[] hi, int[] idx) { |
| 65 | * this.lo = lo; |
| 66 | * this.hi = hi; |
| 67 | * this.idx = idx; |
| 68 | * } |
| 69 | * |
| 70 | * public boolean evaluate(RowSet rs) { |
| 71 | * CachedRowSet crs = (CachedRowSet)rs; |
| 72 | * boolean bool1,bool2; |
| 73 | * |
| 74 | * // Check the present row determine if it lies |
| 75 | * // within the filtering criteria. |
| 76 | * |
| 77 | * for (int i = 0; i < idx.length; i++) { |
| 78 | * |
| 79 | * if ((rs.getObject(idx[i]) >= lo[i]) && |
| 80 | * (rs.getObject(idx[i]) >= hi[i]) { |
| 81 | * bool1 = true; // within filter constraints |
| 82 | * } else { |
| 83 | * bool2 = true; // outside of filter constraints |
| 84 | * } |
| 85 | * } |
| 86 | * |
| 87 | * if (bool2) { |
| 88 | * return false; |
| 89 | * } else { |
| 90 | * return true; |
| 91 | * } |
| 92 | * } |
| 93 | * </code> |
| 94 | * </pre> |
| 95 | * <P> |
| 96 | * The example above implements a simple range predicate. Note, that |
| 97 | * implementations should but are not required to provider <code>String</code> |
| 98 | * and integer index based constructors to provide for JDBC RowSet Implementation |
| 99 | * applications that use both column identification conventions. |
| 100 | * |
| 101 | * @author Jonathan Bruce, Amit Handa |
| 102 | * |
| 103 | */ |
| 104 | |
| 105 | // <h3>3.0 FilteredRowSet Internals</h3> |
| 106 | // internalNext, Frist, Last. Discuss guidelines on how to approach this |
| 107 | // and cite examples in reference implementations. |
| 108 | public interface Predicate { |
| 109 | /** |
| 110 | * This method is typically called a <code>FilteredRowSet</code> object |
| 111 | * internal methods (not public) that control the <code>RowSet</code> object's |
| 112 | * cursor moving from row to the next. In addition, if this internal method |
| 113 | * moves the cursor onto a row that has been deleted, the internal method will |
| 114 | * continue to ove the cursor until a valid row is found. |
| 115 | * |
| 116 | * @return <code>true</code> if there are more rows in the filter; |
| 117 | * <code>false</code> otherwise |
| 118 | */ |
| 119 | public boolean evaluate(RowSet rs); |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * This method is called by a <code>FilteredRowSet</code> object |
| 124 | * to check whether the value lies between the filtering criterion (or criteria |
| 125 | * if multiple constraints exist) set using the <code>setFilter()</code> method. |
| 126 | * <P> |
| 127 | * The <code>FilteredRowSet</code> object will use this method internally |
| 128 | * while inserting new rows to a <code>FilteredRowSet</code> instance. |
| 129 | * |
| 130 | * @param value An <code>Object</code> value which needs to be checked, |
| 131 | * whether it can be part of this <code>FilterRowSet</code> object. |
| 132 | * @param column a <code>int</code> object that must match the |
| 133 | * SQL index of a column in this <code>RowSet</code> object. This must |
| 134 | * have been passed to <code>Predicate</code> as one of the columns |
| 135 | * for filtering while initializing a <code>Predicate</code> |
| 136 | * @return <code>true</code> ifrow value lies within the filter; |
| 137 | * <code>false</code> otherwise |
| 138 | * @throws SQLException if the column is not part of filtering criteria |
| 139 | */ |
| 140 | public boolean evaluate(Object value, int column) throws SQLException; |
| 141 | |
| 142 | /** |
| 143 | * This method is called by the <code>FilteredRowSet</code> object |
| 144 | * to check whether the value lies between the filtering criteria set |
| 145 | * using the setFilter method. |
| 146 | * <P> |
| 147 | * The <code>FilteredRowSet</code> object will use this method internally |
| 148 | * while inserting new rows to a <code>FilteredRowSet</code> instance. |
| 149 | * |
| 150 | * @param value An <code>Object</code> value which needs to be checked, |
| 151 | * whether it can be part of this <code>FilterRowSet</code>. |
| 152 | * |
| 153 | * @param columnName a <code>String</code> object that must match the |
| 154 | * SQL name of a column in this <code>RowSet</code>, ignoring case. This must |
| 155 | * have been passed to <code>Predicate</code> as one of the columns for filtering |
| 156 | * while initializing a <code>Predicate</code> |
| 157 | * |
| 158 | * @return <code>true</code> if value lies within the filter; <code>false</code> otherwise |
| 159 | * |
| 160 | * @throws SQLException if the column is not part of filtering criteria |
| 161 | */ |
| 162 | public boolean evaluate(Object value, String columnName) throws SQLException; |
| 163 | |
| 164 | } |