KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > storage > Tuple


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.storage;
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.Arrays JavaDoc;
29
30 import org.xquark.mapper.mapping.ColumnMapping;
31 import org.xquark.mapper.mapping.TableMapping;
32 import org.xquark.mapper.metadata.CollectionMappingInfo;
33 import org.xquark.schema.Declaration;
34 import org.xquark.schema.validation.ValidationContextProvider;
35 import org.xquark.xml.xdbc.XMLDBCException;
36
37 public abstract class Tuple
38 {
39     
40     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
41     
42     private static final String JavaDoc RCSName = "$Name: $";
43     
44     protected Statement JavaDoc selectStmt = null;
45     
46     protected ResultSet JavaDoc resultSet = null;
47     
48     protected boolean notExhausted = false;
49     
50     protected CollectionMappingInfo table;
51     
52     protected long uoid = -1;
53     
54     protected String JavaDoc[] values = null; // buffered because getValue() is called several times
55
protected boolean[] fetched = null;
56     
57     
58     protected short pathoid = -2;
59     
60     Tuple(CollectionMappingInfo table)
61     {
62         this.table = table;
63         values = new String JavaDoc[getMapping().getColumnMappingCount()];
64         fetched = new boolean[getMapping().getColumnMappingCount()];
65     }
66     
67     boolean queryPending()
68     {
69         return notExhausted;
70     }
71     
72     /** To be overridden */
73     boolean resultActive()
74     {
75         return notExhausted;
76     }
77     
78     boolean isClosed()
79     {
80         return (resultSet == null);
81     }
82     
83     public boolean isInRange(long first, long last, short path)
84     {
85         return resultActive() && (uoid >= first) && (uoid <= last) && (pathoid == path);
86     }
87     
88     public boolean isInRangeAbs(long first, long last, short path)
89     {
90         return resultActive() && (uoid >= first) && (uoid <= last) && ((short)Math.abs(pathoid) == path);
91     }
92     
93     public boolean isInRange(long first, long last)
94     {
95         return resultActive() && (uoid >= first) && (uoid <= last);
96     }
97     
98     long getUOID()
99     {
100         return uoid;
101     }
102     
103     TableMapping getMapping()
104     {
105         return table.getTableMapping();
106     }
107     
108     short getPathID()
109     {
110         return (short)Math.abs(pathoid);
111     }
112     
113     boolean fetchNextRow() throws SQLException JavaDoc
114     {
115         Arrays.fill(fetched, false);
116         return true;
117     }
118     
119     String JavaDoc getValue(ColumnMapping column, Declaration decl, ValidationContextProvider nsContext)
120     throws SQLException JavaDoc, XMLDBCException
121     {
122         int index = column.getInsertColumnIndex();
123         
124         if (!fetched[index])
125         {
126             values[index] = fetchValue(column, decl, nsContext);
127             fetched[index] = true;
128         }
129         
130         return values[index];
131     }
132     
133     protected abstract String JavaDoc fetchValue(ColumnMapping column, Declaration decl, ValidationContextProvider nsContext)
134     throws SQLException JavaDoc, XMLDBCException;
135     
136     void close() throws SQLException JavaDoc
137     {
138         reset();
139         if (selectStmt != null) // table mapping may not be used for queries
140
{
141             selectStmt.close();
142             selectStmt = null;
143         }
144     }
145     
146     void reset() throws SQLException JavaDoc
147     {
148         if (!isClosed()) // rs isClosed !!!
149
{
150             resultSet.close();
151             resultSet = null;
152             uoid = -1;
153             pathoid = -2;
154             notExhausted = false;
155         }
156     }
157     
158 }
159
160
Popular Tags