KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.RepositoryProperties;
35
36 /**
37  * Object used to perform XML structure node storage.
38  *
39  */

40 public class BatchQueryExtraDataExplorer extends QueryExtraDataExplorer
41 {
42     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
43     private static final String JavaDoc RCSName = "$Name: $";
44     
45     private static Log log = LogFactory.getLog(BatchQueryExtraDataExplorer.class);
46     
47     private int currentRank = -2;
48     private int rank = -1;
49     
50     public BatchQueryExtraDataExplorer(TableInfo tableInfo)
51     {
52         super(tableInfo);
53     }
54     /**
55      * Launch the table entry retrieving for a particular bukect ID and a OID range.
56      */

57     public void getBranch(List JavaDoc paths, VarInfoSet tmpTable,
58     AbstractConnection connection)
59     throws SQLException JavaDoc
60     {
61         useStringDelimitor = connection.useStringDelimitor();
62         stmt = connection.getConnection().createStatement();
63         try
64         {
65             connection.setFetchSize(stmt, RepositoryProperties.getIntProperty(CONF_DATA_FETCHSIZE));
66             rs = stmt.executeQuery(generateStatement(paths, tmpTable));
67             notExhausted = true;
68             currentResultExhausted = false;
69         }
70         catch (SQLException JavaDoc e)
71         {
72             stmt.close();
73             throw e;
74         }
75     }
76     
77     private String JavaDoc generateStatement(List JavaDoc paths, VarInfoSet tmpTable)
78     {
79         StringBuffer JavaDoc SQLStmt = new StringBuffer JavaDoc();
80         
81         ColumnMapping refColumn;
82         SubPathInfo path;
83         
84         for (int i = 0; i < paths.size(); i++)
85         {
86             path = (SubPathInfo)paths.get(i);
87             
88             if (i > 0)
89                 SQLStmt.append("\nunion all\n");
90             
91             SQLStmt.append("select t.");
92             SQLStmt.append(tmpTable.getRankColumnName());
93             SQLStmt.append(',');
94             SQLStmt.append(path.subPathIndex);
95             SQLStmt.append(" as n,v.uoid,v.path,v.type,v.pos,v.offset,v.data,v.num\nfrom ");
96             SQLStmt.append(tmpTable.getTableName());
97             SQLStmt.append(" t,");
98             SQLStmt.append(tableInfo.getName());
99             SQLStmt.append(" v\nwhere v.uoid between t.");
100             SQLStmt.append(tmpTable.getFirstColumnName());
101             SQLStmt.append(" and t.");
102             SQLStmt.append(tmpTable.getLastColumnName());
103             SQLStmt.append(" and t.");
104             SQLStmt.append(tmpTable.getPathColumnName());
105             SQLStmt.append('=');
106             SQLStmt.append(path.varPath.getPathID());
107             // path restriction
108
List JavaDoc subpaths = path.subPath.getSubPaths();
109             if (subpaths != null)
110             {
111                 // last struct ancestor is added in order for the builder to initiate range
112
SQLStmt.append(" and v.path");
113                 
114                 int subpathsCount = subpaths.size();
115                 if (subpathsCount == 1)
116                 {
117                     SQLStmt.append("=");
118                     SQLStmt.append(((PathNode)subpaths.get(0)).getPathID());
119                 }
120                 else
121                 {
122                     SQLStmt.append(" in(");
123                     
124                     for (int j = 0; j < subpathsCount; j++)
125                     {
126                         SQLStmt.append(((PathNode)subpaths.get(j)).getPathID());
127                         SQLStmt.append(',');
128                     }
129                     SQLStmt.setCharAt(SQLStmt.length()-1, ')');
130                 }
131             }
132         }
133         
134         SQLStmt.append("\norder by ");
135         SQLStmt.append(tmpTable.getRankColumnName());
136         SQLStmt.append(",n,num");
137         
138         if (log.isDebugEnabled())
139             log.debug(">>> Value stmt for extra table:\n" + SQLStmt);
140         
141         return SQLStmt.toString();
142     }
143     /**
144      * get the next row from database and store it in the current object.
145      */

146     public boolean fetchNextRow() throws SQLException JavaDoc
147     {
148         try
149         {
150             if (resultActive())
151             {
152                 notExhausted = rs.next();
153                 if (notExhausted)
154                 {
155                     rank = rs.getInt(1);
156                     subPathIndex = rs.getInt(2);
157                     uoid = rs.getLong(3);
158                     node.path = rs.getShort(4);
159                     node.type = rs.getShort(5);
160                     node.position = rs.getShort(6);
161                     node.offset = rs.getInt(7);
162                     node.data = rs.getString(8);
163                     if (useStringDelimitor)
164                         node.data = node.data.substring(0, node.data.length() - STRING_DELIMITOR_LENGTH);
165                     node.rowNum = rs.getShort(9);
166                     updateResultSegmentationFlag();
167                 }
168                 else
169                     reset();
170             }
171             else
172                 return false;
173             
174             // Debug.print("Tuple :: fetchNextRow for table " + mapping.getTableName() + " UOID=" + getUOID());
175
return notExhausted;
176         }
177         catch (SQLException JavaDoc e)
178         {
179             close();
180             throw e;
181         }
182     }
183     
184     void setCurrentRank(int rank)
185     {
186         currentSubPathIndex = 0;
187         currentRank = rank;
188         updateResultSegmentationFlag();
189     }
190     
191     
192     public void reset() throws SQLException JavaDoc
193     {
194         super.reset();
195         currentRank = -2;
196         rank = -1;
197     }
198     
199     protected void updateResultSegmentationFlag()
200     {
201         currentResultExhausted = (rank != currentRank) || (subPathIndex != currentSubPathIndex);
202     }
203 }
204
Popular Tags