Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sql/rowset/JoinRowSet.java
38918 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sql.rowset;2627import java.sql.*;28import javax.sql.*;29import javax.naming.*;30import java.io.*;31import java.math.*;32import java.util.*;3334import javax.sql.rowset.*;3536/**37* The <code>JoinRowSet</code> interface provides a mechanism for combining related38* data from different <code>RowSet</code> objects into one <code>JoinRowSet</code>39* object, which represents an SQL <code>JOIN</code>.40* In other words, a <code>JoinRowSet</code> object acts as a41* container for the data from <code>RowSet</code> objects that form an SQL42* <code>JOIN</code> relationship.43* <P>44* The <code>Joinable</code> interface provides the methods for setting,45* retrieving, and unsetting a match column, the basis for46* establishing an SQL <code>JOIN</code> relationship. The match column may47* alternatively be set by supplying it to the appropriate version of the48* <code>JointRowSet</code> method <code>addRowSet</code>.49*50* <h3>1.0 Overview</h3>51* Disconnected <code>RowSet</code> objects (<code>CachedRowSet</code> objects52* and implementations extending the <code>CachedRowSet</code> interface)53* do not have a standard way to establish an SQL <code>JOIN</code> between54* <code>RowSet</code> objects without the expensive operation of55* reconnecting to the data source. The <code>JoinRowSet</code>56* interface is specifically designed to address this need.57* <P>58* Any <code>RowSet</code> object59* can be added to a <code>JoinRowSet</code> object to become60* part of an SQL <code>JOIN</code> relationship. This means that both connected61* and disconnected <code>RowSet</code> objects can be part of a <code>JOIN</code>.62* <code>RowSet</code> objects operating in a connected environment63* (<code>JdbcRowSet</code> objects) are64* encouraged to use the database to which they are already65* connected to establish SQL <code>JOIN</code> relationships between66* tables directly. However, it is possible for a67* <code>JdbcRowSet</code> object to be added to a <code>JoinRowSet</code> object68* if necessary.69* <P>70* Any number of <code>RowSet</code> objects can be added to an71* instance of <code>JoinRowSet</code> provided that they72* can be related in an SQL <code>JOIN</code>.73* By definition, the SQL <code>JOIN</code> statement is used to74* combine the data contained in two or more relational database tables based75* upon a common attribute. The <code>Joinable</code> interface provides the methods76* for establishing a common attribute, which is done by setting a77* <i>match column</i>. The match column commonly coincides with78* the primary key, but there is79* no requirement that the match column be the same as the primary key.80* By establishing and then enforcing column matches,81* a <code>JoinRowSet</code> object establishes <code>JOIN</code> relationships82* between <code>RowSet</code> objects without the assistance of an available83* relational database.84* <P>85* The type of <code>JOIN</code> to be established is determined by setting86* one of the <code>JoinRowSet</code> constants using the method87* <code>setJoinType</code>. The following SQL <code>JOIN</code> types can be set:88* <UL>89* <LI><code>CROSS_JOIN</code>90* <LI><code>FULL_JOIN</code>91* <LI><code>INNER_JOIN</code> - the default if no <code>JOIN</code> type has been set92* <LI><code>LEFT_OUTER_JOIN</code>93* <LI><code>RIGHT_OUTER_JOIN</code>94* </UL>95* Note that if no type is set, the <code>JOIN</code> will automatically be an96* inner join. The comments for the fields in the97* <code>JoinRowSet</code> interface explain these <code>JOIN</code> types, which are98* standard SQL <code>JOIN</code> types.99*100* <h3>2.0 Using a <code>JoinRowSet</code> Object for Creating a <code>JOIN</code></h3>101* When a <code>JoinRowSet</code> object is created, it is empty.102* The first <code>RowSet</code> object to be added becomes the basis for the103* <code>JOIN</code> relationship.104* Applications must determine which column in each of the105* <code>RowSet</code> objects to be added to the <code>JoinRowSet</code> object106* should be the match column. All of the107* <code>RowSet</code> objects must contain a match column, and the values in108* each match column must be ones that can be compared to values in the other match109* columns. The columns do not have to have the same name, though they often do,110* and they do not have to store the exact same data type as long as the data types111* can be compared.112* <P>113* A match column can be be set in two ways:114* <ul>115* <li>By calling the <code>Joinable</code> method <code>setMatchColumn</code><br>116* This is the only method that can set the match column before a <code>RowSet</code>117* object is added to a <code>JoinRowSet</code> object. The <code>RowSet</code> object118* must have implemented the <code>Joinable</code> interface in order to use the method119* <code>setMatchColumn</code>. Once the match column value120* has been set, this method can be used to reset the match column at any time.121* <li>By calling one of the versions of the <code>JoinRowSet</code> method122* <code>addRowSet</code> that takes a column name or number (or an array of123* column names or numbers)<BR>124* Four of the five <code>addRowSet</code> methods take a match column as a parameter.125* These four methods set or reset the match column at the time a <code>RowSet</code>126* object is being added to a <code>JoinRowSet</code> object.127* </ul>128* <h3>3.0 Sample Usage</h3>129* <p>130* The following code fragment adds two <code>CachedRowSet</code>131* objects to a <code>JoinRowSet</code> object. Note that in this example,132* no SQL <code>JOIN</code> type is set, so the default <code>JOIN</code> type,133* which is <i>INNER_JOIN</i>, is established.134* <p>135* In the following code fragment, the table <code>EMPLOYEES</code>, whose match136* column is set to the first column (<code>EMP_ID</code>), is added to the137* <code>JoinRowSet</code> object <i>jrs</i>. Then138* the table <code>ESSP_BONUS_PLAN</code>, whose match column is likewise139* the <code>EMP_ID</code> column, is added. When this second140* table is added to <i>jrs</i>, only the rows in141* <code>ESSP_BONUS_PLAN</code> whose <code>EMP_ID</code> value matches an142* <code>EMP_ID</code> value in the <code>EMPLOYEES</code> table are added.143* In this case, everyone in the bonus plan is an employee, so all of the rows144* in the table <code>ESSP_BONUS_PLAN</code> are added to the <code>JoinRowSet</code>145* object. In this example, both <code>CachedRowSet</code> objects being added146* have implemented the <code>Joinable</code> interface and can therefore call147* the <code>Joinable</code> method <code>setMatchColumn</code>.148* <PRE>149* JoinRowSet jrs = new JoinRowSetImpl();150*151* ResultSet rs1 = stmt.executeQuery("SELECT * FROM EMPLOYEES");152* CachedRowSet empl = new CachedRowSetImpl();153* empl.populate(rs1);154* empl.setMatchColumn(1);155* jrs.addRowSet(empl);156*157* ResultSet rs2 = stmt.executeQuery("SELECT * FROM ESSP_BONUS_PLAN");158* CachedRowSet bonus = new CachedRowSetImpl();159* bonus.populate(rs2);160* bonus.setMatchColumn(1); // EMP_ID is the first column161* jrs.addRowSet(bonus);162* </PRE>163* <P>164* At this point, <i>jrs</i> is an inside JOIN of the two <code>RowSet</code> objects165* based on their <code>EMP_ID</code> columns. The application can now browse the166* combined data as if it were browsing one single <code>RowSet</code> object.167* Because <i>jrs</i> is itself a <code>RowSet</code> object, an application can168* navigate or modify it using <code>RowSet</code> methods.169* <PRE>170* jrs.first();171* int employeeID = jrs.getInt(1);172* String employeeName = jrs.getString(2);173* </PRE>174* <P>175* Note that because the SQL <code>JOIN</code> must be enforced when an application176* adds a second or subsequent <code>RowSet</code> object, there177* may be an initial degradation in performance while the <code>JOIN</code> is178* being performed.179* <P>180* The following code fragment adds an additional <code>CachedRowSet</code> object.181* In this case, the match column (<code>EMP_ID</code>) is set when the182* <code>CachedRowSet</code> object is added to the <code>JoinRowSet</code> object.183* <PRE>184* ResultSet rs3 = stmt.executeQuery("SELECT * FROM 401K_CONTRIB");185* CachedRowSet fourO1k = new CachedRowSetImpl();186* four01k.populate(rs3);187* jrs.addRowSet(four01k, 1);188* </PRE>189* <P>190* The <code>JoinRowSet</code> object <i>jrs</i> now contains values from all three191* tables. The data in each row in <i>four01k</i> in which the value for the192* <code>EMP_ID</code> column matches a value for the <code>EMP_ID</code> column193* in <i>jrs</i> has been added to <i>jrs</i>.194*195* <h3>4.0 <code>JoinRowSet</code> Methods</h3>196* The <code>JoinRowSet</code> interface supplies several methods for adding197* <code>RowSet</code> objects and for getting information about the198* <code>JoinRowSet</code> object.199* <UL>200* <LI>Methods for adding one or more <code>RowSet</code> objects<BR>201* These methods allow an application to add one <code>RowSet</code> object202* at a time or to add multiple <code>RowSet</code> objects at one time. In203* either case, the methods may specify the match column for each204* <code>RowSet</code> object being added.205* <LI>Methods for getting information<BR>206* One method retrieves the <code>RowSet</code> objects in the207* <code>JoinRowSet</code> object, and another method retrieves the208* <code>RowSet</code> names. A third method retrieves either the SQL209* <code>WHERE</code> clause used behind the scenes to form the210* <code>JOIN</code> or a text description of what the <code>WHERE</code>211* clause does.212* <LI>Methods related to the type of <code>JOIN</code><BR>213* One method sets the <code>JOIN</code> type, and five methods find out whether214* the <code>JoinRowSet</code> object supports a given type.215* <LI>A method to make a separate copy of the <code>JoinRowSet</code> object<BR>216* This method creates a copy that can be persisted to the data source.217* </UL>218*219*/220221public interface JoinRowSet extends WebRowSet {222223/**224* Adds the given <code>RowSet</code> object to this <code>JoinRowSet</code>225* object. If the <code>RowSet</code> object226* is the first to be added to this <code>JoinRowSet</code>227* object, it forms the basis of the <code>JOIN</code> relationship to be228* established.229* <P>230* This method should be used only when the given <code>RowSet</code>231* object already has a match column that was set with the <code>Joinable</code>232* method <code>setMatchColumn</code>.233* <p>234* Note: A <code>Joinable</code> object is any <code>RowSet</code> object235* that has implemented the <code>Joinable</code> interface.236*237* @param rowset the <code>RowSet</code> object that is to be added to this238* <code>JoinRowSet</code> object; it must implement the239* <code>Joinable</code> interface and have a match column set240* @throws SQLException if (1) an empty rowset is added to the to this241* <code>JoinRowSet</code> object, (2) a match column has not been242* set for <i>rowset</i>, or (3) <i>rowset</i>243* violates the active <code>JOIN</code>244* @see Joinable#setMatchColumn245*/246public void addRowSet(Joinable rowset) throws SQLException;247248/**249* Adds the given <code>RowSet</code> object to this <code>JoinRowSet</code>250* object and sets the designated column as the match column for251* the <code>RowSet</code> object. If the <code>RowSet</code> object252* is the first to be added to this <code>JoinRowSet</code>253* object, it forms the basis of the <code>JOIN</code> relationship to be254* established.255* <P>256* This method should be used when <i>RowSet</i> does not already have a match257* column set.258*259* @param rowset the <code>RowSet</code> object that is to be added to this260* <code>JoinRowSet</code> object; it may implement the261* <code>Joinable</code> interface262* @param columnIdx an <code>int</code> that identifies the column to become the263* match column264* @throws SQLException if (1) <i>rowset</i> is an empty rowset or265* (2) <i>rowset</i> violates the active <code>JOIN</code>266* @see Joinable#unsetMatchColumn267*/268public void addRowSet(RowSet rowset, int columnIdx) throws SQLException;269270/**271* Adds <i>rowset</i> to this <code>JoinRowSet</code> object and272* sets the designated column as the match column. If <i>rowset</i>273* is the first to be added to this <code>JoinRowSet</code>274* object, it forms the basis for the <code>JOIN</code> relationship to be275* established.276* <P>277* This method should be used when the given <code>RowSet</code> object278* does not already have a match column.279*280* @param rowset the <code>RowSet</code> object that is to be added to this281* <code>JoinRowSet</code> object; it may implement the282* <code>Joinable</code> interface283* @param columnName the <code>String</code> object giving the name of the284* column to be set as the match column285* @throws SQLException if (1) <i>rowset</i> is an empty rowset or286* (2) the match column for <i>rowset</i> does not satisfy the287* conditions of the <code>JOIN</code>288*/289public void addRowSet(RowSet rowset,290String columnName) throws SQLException;291292/**293* Adds one or more <code>RowSet</code> objects contained in the given294* array of <code>RowSet</code> objects to this <code>JoinRowSet</code>295* object and sets the match column for296* each of the <code>RowSet</code> objects to the match columns297* in the given array of column indexes. The first element in298* <i>columnIdx</i> is set as the match column for the first299* <code>RowSet</code> object in <i>rowset</i>, the second element of300* <i>columnIdx</i> is set as the match column for the second element301* in <i>rowset</i>, and so on.302* <P>303* The first <code>RowSet</code> object added to this <code>JoinRowSet</code>304* object forms the basis for the <code>JOIN</code> relationship.305* <P>306* This method should be used when the given <code>RowSet</code> object307* does not already have a match column.308*309* @param rowset an array of one or more <code>RowSet</code> objects310* to be added to the <code>JOIN</code>; it may implement the311* <code>Joinable</code> interface312* @param columnIdx an array of <code>int</code> values indicating the index(es)313* of the columns to be set as the match columns for the <code>RowSet</code>314* objects in <i>rowset</i>315* @throws SQLException if (1) an empty rowset is added to this316* <code>JoinRowSet</code> object, (2) a match column is not set317* for a <code>RowSet</code> object in <i>rowset</i>, or (3)318* a <code>RowSet</code> object being added violates the active319* <code>JOIN</code>320*/321public void addRowSet(RowSet[] rowset,322int[] columnIdx) throws SQLException;323324/**325* Adds one or more <code>RowSet</code> objects contained in the given326* array of <code>RowSet</code> objects to this <code>JoinRowSet</code>327* object and sets the match column for328* each of the <code>RowSet</code> objects to the match columns329* in the given array of column names. The first element in330* <i>columnName</i> is set as the match column for the first331* <code>RowSet</code> object in <i>rowset</i>, the second element of332* <i>columnName</i> is set as the match column for the second element333* in <i>rowset</i>, and so on.334* <P>335* The first <code>RowSet</code> object added to this <code>JoinRowSet</code>336* object forms the basis for the <code>JOIN</code> relationship.337* <P>338* This method should be used when the given <code>RowSet</code> object(s)339* does not already have a match column.340*341* @param rowset an array of one or more <code>RowSet</code> objects342* to be added to the <code>JOIN</code>; it may implement the343* <code>Joinable</code> interface344* @param columnName an array of <code>String</code> values indicating the345* names of the columns to be set as the match columns for the346* <code>RowSet</code> objects in <i>rowset</i>347* @throws SQLException if (1) an empty rowset is added to this348* <code>JoinRowSet</code> object, (2) a match column is not set349* for a <code>RowSet</code> object in <i>rowset</i>, or (3)350* a <code>RowSet</code> object being added violates the active351* <code>JOIN</code>352*/353public void addRowSet(RowSet[] rowset,354String[] columnName) throws SQLException;355356/**357* Returns a <code>Collection</code> object containing the358* <code>RowSet</code> objects that have been added to this359* <code>JoinRowSet</code> object.360* This should return the 'n' number of RowSet contained361* within the <code>JOIN</code> and maintain any updates that have occurred while in362* this union.363*364* @return a <code>Collection</code> object consisting of the365* <code>RowSet</code> objects added to this <code>JoinRowSet</code>366* object367* @throws SQLException if an error occurs generating the368* <code>Collection</code> object to be returned369*/370public Collection<?> getRowSets() throws java.sql.SQLException;371372/**373* Returns a <code>String</code> array containing the names of the374* <code>RowSet</code> objects added to this <code>JoinRowSet</code>375* object.376*377* @return a <code>String</code> array of the names of the378* <code>RowSet</code> objects in this <code>JoinRowSet</code>379* object380* @throws SQLException if an error occurs retrieving the names of381* the <code>RowSet</code> objects382* @see CachedRowSet#setTableName383*/384public String[] getRowSetNames() throws java.sql.SQLException;385386/**387* Creates a new <code>CachedRowSet</code> object containing the388* data in this <code>JoinRowSet</code> object, which can be saved389* to a data source using the <code>SyncProvider</code> object for390* the <code>CachedRowSet</code> object.391* <P>392* If any updates or modifications have been applied to the JoinRowSet393* the CachedRowSet returned by the method will not be able to persist394* it's changes back to the originating rows and tables in the395* in the datasource. The CachedRowSet instance returned should not396* contain modification data and it should clear all properties of397* it's originating SQL statement. An application should reset the398* SQL statement using the <code>RowSet.setCommand</code> method.399* <p>400* In order to allow changes to be persisted back to the datasource401* to the originating tables, the <code>acceptChanges</code> method402* should be used and called on a JoinRowSet object instance. Implementations403* can leverage the internal data and update tracking in their404* implementations to interact with the SyncProvider to persist any405* changes.406*407* @return a CachedRowSet containing the contents of the JoinRowSet408* @throws SQLException if an error occurs assembling the CachedRowSet409* object410* @see javax.sql.RowSet411* @see javax.sql.rowset.CachedRowSet412* @see javax.sql.rowset.spi.SyncProvider413*/414public CachedRowSet toCachedRowSet() throws java.sql.SQLException;415416/**417* Indicates if CROSS_JOIN is supported by a JoinRowSet418* implementation419*420* @return true if the CROSS_JOIN is supported; false otherwise421*/422public boolean supportsCrossJoin();423424/**425* Indicates if INNER_JOIN is supported by a JoinRowSet426* implementation427*428* @return true is the INNER_JOIN is supported; false otherwise429*/430public boolean supportsInnerJoin();431432/**433* Indicates if LEFT_OUTER_JOIN is supported by a JoinRowSet434* implementation435*436* @return true is the LEFT_OUTER_JOIN is supported; false otherwise437*/438public boolean supportsLeftOuterJoin();439440/**441* Indicates if RIGHT_OUTER_JOIN is supported by a JoinRowSet442* implementation443*444* @return true is the RIGHT_OUTER_JOIN is supported; false otherwise445*/446public boolean supportsRightOuterJoin();447448/**449* Indicates if FULL_JOIN is supported by a JoinRowSet450* implementation451*452* @return true is the FULL_JOIN is supported; false otherwise453*/454public boolean supportsFullJoin();455456/**457* Allow the application to adjust the type of <code>JOIN</code> imposed458* on tables contained within the JoinRowSet object instance.459* Implementations should throw a SQLException if they do460* not support a given <code>JOIN</code> type.461*462* @param joinType the standard JoinRowSet.XXX static field definition463* of a SQL <code>JOIN</code> to re-configure a JoinRowSet instance on464* the fly.465* @throws SQLException if an unsupported <code>JOIN</code> type is set466* @see #getJoinType467*/468public void setJoinType(int joinType) throws SQLException;469470/**471* Return a SQL-like description of the WHERE clause being used472* in a JoinRowSet object. An implementation can describe473* the WHERE clause of the SQL <code>JOIN</code> by supplying a SQL474* strings description of <code>JOIN</code> or provide a textual475* description to assist applications using a <code>JoinRowSet</code>476*477* @return whereClause a textual or SQL description of the logical478* WHERE clause used in the JoinRowSet instance479* @throws SQLException if an error occurs in generating a representation480* of the WHERE clause.481*/482public String getWhereClause() throws SQLException;483484/**485* Returns a <code>int</code> describing the set SQL <code>JOIN</code> type486* governing this JoinRowSet instance. The returned type will be one of487* standard JoinRowSet types: <code>CROSS_JOIN</code>, <code>INNER_JOIN</code>,488* <code>LEFT_OUTER_JOIN</code>, <code>RIGHT_OUTER_JOIN</code> or489* <code>FULL_JOIN</code>.490*491* @return joinType one of the standard JoinRowSet static field492* definitions of a SQL <code>JOIN</code>. <code>JoinRowSet.INNER_JOIN</code>493* is returned as the default <code>JOIN</code> type is no type has been494* explicitly set.495* @throws SQLException if an error occurs determining the SQL <code>JOIN</code>496* type supported by the JoinRowSet instance.497* @see #setJoinType498*/499public int getJoinType() throws SQLException;500501/**502* An ANSI-style <code>JOIN</code> providing a cross product of two tables503*/504public static int CROSS_JOIN = 0;505506/**507* An ANSI-style <code>JOIN</code> providing a inner join between two tables. Any508* unmatched rows in either table of the join should be discarded.509*/510public static int INNER_JOIN = 1;511512/**513* An ANSI-style <code>JOIN</code> providing a left outer join between two514* tables. In SQL, this is described where all records should be515* returned from the left side of the JOIN statement.516*/517public static int LEFT_OUTER_JOIN = 2;518519/**520* An ANSI-style <code>JOIN</code> providing a right outer join between521* two tables. In SQL, this is described where all records from the522* table on the right side of the JOIN statement even if the table523* on the left has no matching record.524*/525public static int RIGHT_OUTER_JOIN = 3;526527/**528* An ANSI-style <code>JOIN</code> providing a a full JOIN. Specifies that all529* rows from either table be returned regardless of matching530* records on the other table.531*/532public static int FULL_JOIN = 4;533534535}536537538