KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.xquark.mapper.dbms.AbstractConnection;
31 import org.xquark.mapper.dbms.TableInfo;
32 import org.xquark.mapper.mapping.ColumnMapping;
33 import org.xquark.mapper.metadata.PathNode;
34 import org.xquark.mapper.metadata.UOIDManager;
35 import org.xquark.mapper.util.RepositoryProperties;
36
37 /**
38  * Object used to perform XML structure node storage.
39  *
40  */

41 public class BatchQueryStructExplorer extends QueryStructExplorer
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(BatchQueryStructExplorer.class);
47     
48     private int currentRank = -2;
49     private int rank = -1;
50     
51     public BatchQueryStructExplorer(TableInfo tableInfo, UOIDManager manager)
52     {
53         super(tableInfo, manager);
54     }
55     
56     /**
57      * Launch the table entry retrieving for a particular bukect ID and a OID range.
58      */

59     public void getBranch(List JavaDoc paths, VarInfoSet tmpTable,
60     AbstractConnection connection)
61     throws SQLException JavaDoc
62     {
63         stmt = connection.getConnection().createStatement();
64         try
65         {
66             connection.setFetchSize(stmt, RepositoryProperties.getIntProperty(CONF_TREE_FETCHSIZE));
67             String JavaDoc stmtString = generateStatement(paths, tmpTable);
68             if (stmtString != null)
69             {
70                 rs = stmt.executeQuery(stmtString);
71                 notExhausted = true;
72                 currentResultExhausted = false;
73             }
74         }
75         catch (SQLException JavaDoc e)
76         {
77             stmt.close();
78             throw e;
79         }
80         // NOTE test ORDER BY source, rank to see if equivalent (would match any mapping)
81
}
82     
83     private String JavaDoc generateStatement(List JavaDoc paths, VarInfoSet tmpTable)
84     {
85         StringBuffer JavaDoc SQLStmt = new StringBuffer JavaDoc();
86         
87         ColumnMapping refColumn;
88         SubPathInfo path;
89         
90         for (int i = 0; i < paths.size(); i++)
91         {
92             path = (SubPathInfo)paths.get(i);
93             List JavaDoc subpaths = path.subPath.getModelSubPaths();
94             int subpathsCount = subpaths.size();
95             if ((subpathsCount > 0) && !path.isText)
96             {
97                 if (SQLStmt.length() > 0) // i was not a correct flag because index 0 coul be discarded
98
SQLStmt.append("\nunion all\n");
99                 
100                 SQLStmt.append("select t.");
101                 SQLStmt.append(tmpTable.getRankColumnName());
102                 SQLStmt.append(',');
103                 SQLStmt.append(path.subPathIndex);
104                 SQLStmt.append(" as n,v.uoid,v.last,v.path\nfrom ");
105                 SQLStmt.append(tmpTable.getTableName());
106                 SQLStmt.append(" t,");
107                 SQLStmt.append(tableInfo.getName());
108                 SQLStmt.append(" v\nwhere v.bucket=floor(t.");
109                 SQLStmt.append(tmpTable.getFirstColumnName());
110                 SQLStmt.append('/');
111                 SQLStmt.append(manager.getDocMultiplier());
112                 SQLStmt.append(") and v.uoid between t.");
113                 SQLStmt.append(tmpTable.getFirstColumnName());
114                 SQLStmt.append(" and t.");
115                 SQLStmt.append(tmpTable.getLastColumnName());
116                 SQLStmt.append(" and t.");
117                 SQLStmt.append(tmpTable.getPathColumnName());
118                 SQLStmt.append('=');
119                 SQLStmt.append(path.varPath.getPathID());
120                 // path restriction
121
// last struct ancestor is added in order for the builder to initiate range
122
SQLStmt.append(" and v.path");
123                 
124                 if (subpathsCount == 1)
125                 {
126                     SQLStmt.append("=");
127                     SQLStmt.append(((PathNode)subpaths.get(0)).getPathID());
128                 }
129                 else
130                 {
131                     SQLStmt.append(" in(");
132                     
133                     for (int j = 0; j < subpathsCount; j++)
134                     {
135                         SQLStmt.append(((PathNode)subpaths.get(j)).getPathID());
136                         SQLStmt.append(',');
137                     }
138                     SQLStmt.setCharAt(SQLStmt.length()-1, ')');
139                 }
140             }
141         }
142         
143         if (SQLStmt.length() > 0)
144         {
145             SQLStmt.append("\norder by ");
146             SQLStmt.append(tmpTable.getRankColumnName());
147             SQLStmt.append(",n,uoid");
148             
149             if (log.isDebugEnabled())
150                 log.debug("Value stmt for struct table:\n" + SQLStmt);
151             
152             return SQLStmt.toString();
153         }
154         else
155             return null;
156     }
157     
158     /**
159      * get the next row from database and store it in the current object.
160      */

161     public boolean fetchNextRow() throws SQLException JavaDoc
162     {
163         try
164         {
165             if (resultActive())
166             {
167                 notExhausted = rs.next();
168                 if (notExhausted)
169                 {
170                     rank = rs.getInt(1);
171                     subPathIndex = rs.getInt(2);
172                     uoid = rs.getLong(3);
173                     last = rs.getLong(4);
174                     path = rs.getShort(5);
175                     updateResultSegmentationFlag();
176                 }
177                 else
178                     reset();
179             }
180             else
181                 return false;
182             
183             // Debug.print("Tuple :: fetchNextRow for table " + mapping.getTableName() + " UOID=" + getUOID());
184
return notExhausted;
185         }
186         catch (SQLException JavaDoc e)
187         {
188             close();
189             throw e;
190         }
191     }
192     
193     void setCurrentRank(int rank)
194     {
195         currentSubPathIndex = 0;
196         currentRank = rank;
197         updateResultSegmentationFlag();
198     }
199     
200     public void reset() throws SQLException JavaDoc
201     {
202         super.reset();
203         currentRank = -2;
204         rank = -1;
205     }
206     
207     protected void updateResultSegmentationFlag()
208     {
209         currentResultExhausted = (rank != currentRank) || (subPathIndex != currentSubPathIndex);
210     }
211 }
212
Popular Tags