KickJava   Java API By Example, From Geeks To Geeks.

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


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