KickJava   Java API By Example, From Geeks To Geeks.

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


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.PreparedStatement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.xquark.mapper.RepositoryException;
33 import org.xquark.mapper.dbms.AbstractConnection;
34 import org.xquark.mapper.mapping.ColumnMapping;
35 import org.xquark.mapper.mapping.TableMapping;
36 import org.xquark.mapper.metadata.*;
37 import org.xquark.mapper.util.RepositoryProperties;
38 import org.xquark.xml.xdbc.XMLDBCException;
39
40 class InteractiveQueryTuple extends QueryTuple
41 {
42     
43     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
44     private static final String JavaDoc RCSName = "$Name: $";
45
46     private static Log log = LogFactory.getLog(InteractiveQueryTuple.class);
47     private int pathCount = 0;
48     
49     InteractiveQueryTuple(CollectionMappingInfo table, short pathID)
50     {
51         super(table, pathID);
52         valueOffset = 2;
53     }
54     
55     /** To be used by query reconstruction */
56     void execQuery(long first, long last, short path) throws SQLException JavaDoc, RepositoryException
57     {
58         try
59         {
60             if (resultSet != null)
61                 resultSet.close();
62             for (int i = 0; i < pathCount; i++)
63             {
64                 ((PreparedStatement JavaDoc)selectStmt).setLong(3*i + 1, first);
65                 ((PreparedStatement JavaDoc)selectStmt).setLong(3*i + 2, last);
66                 ((PreparedStatement JavaDoc)selectStmt).setShort(3*i + 3, path);
67             }
68             resultSet = ((PreparedStatement JavaDoc)selectStmt).executeQuery();
69             currentResultExhausted = false;
70             currentSubPathIndex = 0;
71             notExhausted = true;
72         }
73         catch (SQLException JavaDoc e)
74         {
75             selectStmt.close();
76             throw e;
77         }
78     }
79     
80     void prepareStatement(List JavaDoc paths, CollectionMetadata collection, AbstractConnection connection)
81     throws SQLException JavaDoc, XMLDBCException
82     {
83         selectStmt = connection.getConnection().prepareStatement(generateStatement(paths, collection)); // TO CHECK : could keep the statement ?
84
connection.setFetchSize(selectStmt, RepositoryProperties.getIntProperty(RepositoryConstants.CONF_USER_FETCHSIZE));
85     }
86     
87     private String JavaDoc generateStatement(List JavaDoc paths, CollectionMetadata collection)
88     throws XMLDBCException
89     {
90         StringBuffer JavaDoc SQLStmt = new StringBuffer JavaDoc();
91         
92         SubPathInfo path;
93         ColumnMapping refColumn = null;
94         TableMapping mapping = table.getTableMapping();
95         String JavaDoc UOIDColumnName, pathColumnName;
96         if (mapping.isClustered())
97         {
98             UOIDColumnName = mapping.getUOIDColumnName();
99             pathColumnName = mapping.getPathIDColumnName();
100         }
101         else
102         {
103             UOIDColumnName = "uoid";
104             pathColumnName = "path";
105         }
106         pathCount = paths.size();
107         for (int i = 0; i < pathCount; i++)
108         {
109             path = (SubPathInfo)paths.get(i);
110             
111             if (i > 0)
112                 SQLStmt.append("\nunion all\n");
113             
114             SQLStmt.append("select ");
115             SQLStmt.append(path.subPathIndex);
116             SQLStmt.append(" as n");
117             if (!mapping.isClustered())
118             {
119                 SQLStmt.append(",v.");
120                 SQLStmt.append(UOIDColumnName);
121                 SQLStmt.append(",v.");
122                 SQLStmt.append(pathColumnName);
123             }
124             
125             
126             if (path.subPath.getPathID() != RepositoryConstants.ROOT_PATH_ID) // because root has default mapping ? TO CHANGE
127
refColumn = path.subPath.getReferenceColumnMapping();
128             
129             ColumnMapping[] columns = mapping.getColumnMappings();
130             ColumnMapping column;
131             for (int j = 0; j < columns.length; j++)
132             {
133                 column = columns[j];
134                 if ((refColumn == null)
135                 || (column == refColumn)
136                 || (mapping.isClustered() && ((column.getColumnIndex() == mapping.getUOIDIndex())
137                 || (column.getColumnIndex() == mapping.getPathIDIndex()))))
138                 {
139                     if (mapping.isClustered())
140                         SQLStmt.append(",v.");
141                     else
142                         SQLStmt.append(",w.");
143                     
144                     SQLStmt.append(column.getColumnName());
145                 }
146                 else
147                 {
148                     SQLStmt.append(',');
149                     SQLStmt.append(column.getTypeInfo().getTypedNullValue()); // TO IMPROVE : all column mappings do not need ordering
150
}
151             }
152             
153             SQLStmt.append("\nfrom "); // TO DO : optimize the query (join and condition order)
154
if (mapping.isClustered())
155             {
156                 SQLStmt.append(mapping.getTableName());
157                 SQLStmt.append(" v");
158             }
159             else
160             {
161                 SQLStmt.append(collection.getOIDTableName(mapping.getTableIndex()));
162                 SQLStmt.append(" v,");
163                 SQLStmt.append(mapping.getTableName());
164                 SQLStmt.append(" w");
165             }
166             SQLStmt.append("\nwhere v.");
167             SQLStmt.append(UOIDColumnName);
168             SQLStmt.append(" between ? and ? and ?=");
169             SQLStmt.append(path.varPath.getPathID());
170             if (!mapping.isClustered())
171             {
172                 SQLStmt.append(" and ");
173                 SQLStmt.append(mapping.getJoinCondition());
174             }
175             // path restriction
176
if (mapping.isTableShared())
177             {
178                 if (!mapping.isShared()) // no table mapping (optimization)
179
{
180                     SQLStmt.append(" and abs(v.");
181                     SQLStmt.append(pathColumnName);
182                     SQLStmt.append(")=");
183                     SQLStmt.append(tablePath);
184                 }
185                 else // complex element mapping or generic
186
{
187                     List JavaDoc subpaths = null;
188                     if (path.isText)
189                         subpaths =
190                             Collections.singletonList(
191                                 path.subPath.getBuilderAncestor());
192                     else
193                         subpaths =
194                             path
195                                 .subPath
196                                 .getBuilderAncestor()
197                                 .getModelSubPaths();
198                     int subpathsCount = subpaths.size();
199                     if (subpathsCount > 0)
200                     {
201                         SQLStmt.append(" and abs(v.");
202                         SQLStmt.append(pathColumnName);
203                         SQLStmt.append(')');
204
205                         if (subpathsCount == 1)
206                         {
207                             SQLStmt.append('=');
208                             SQLStmt.append(
209                                 ((PathNode) subpaths.get(0)).getPathID());
210                         }
211                         else
212                         {
213                             SQLStmt.append(" in(");
214
215                             for (int j = 0; j < subpathsCount; j++)
216                             {
217                                 SQLStmt.append(
218                                     ((PathNode) subpaths.get(j)).getPathID());
219                                 SQLStmt.append(',');
220                             }
221                             SQLStmt.setCharAt(SQLStmt.length() - 1, ')');
222                         }
223                     }
224                 }
225             }
226         }
227         
228         SQLStmt.append("\norder by n,");
229         SQLStmt.append(UOIDColumnName);
230         
231         if (log.isDebugEnabled())
232             log.debug("Value recontruction statement stmt [table,query]\n"
233                     + mapping.getTableName() + SQLStmt);
234         return SQLStmt.toString();
235     }
236     
237     boolean fetchNextRow() throws SQLException JavaDoc
238     {
239         super.fetchNextRow();
240         try
241         {
242             if (resultActive())
243             {
244                 notExhausted = resultSet.next();
245                 if (notExhausted)
246                 {
247                     TableMapping mapping = table.getTableMapping();
248                     subPathIndex = resultSet.getInt(1);
249                     uoid = resultSet.getLong(mapping.getUOIDIndex() + 2); // num + resultset offset/ List
250
pathoid = resultSet.getShort(mapping.getPathIDIndex() + 2);
251                     updateResultSegmentationFlag();
252                 }
253                 else
254                     reset();
255             }
256             else
257                 return false;
258             
259             return notExhausted;
260         }
261         catch (SQLException JavaDoc e)
262         {
263             selectStmt.close();
264             throw e;
265         }
266     }
267 }
268
269
Popular Tags