KickJava   Java API By Example, From Geeks To Geeks.

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


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.RepositoryException;
31 import org.xquark.mapper.dbms.AbstractConnection;
32 import org.xquark.mapper.dbms.TableInfo;
33 import org.xquark.mapper.mapping.ColumnMapping;
34 import org.xquark.mapper.metadata.PathNode;
35 import org.xquark.mapper.metadata.RepositoryConstants;
36 import org.xquark.mapper.util.RepositoryProperties;
37
38 /**
39  * Object used to perform XML structure node storage.
40  *
41  */

42 public class InteractiveQueryExtraDataExplorer extends QueryExtraDataExplorer
43 {
44     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
45     private static final String JavaDoc RCSName = "$Name: $";
46     
47     private static Log log = LogFactory.getLog(InteractiveQueryExtraDataExplorer.class);
48     
49     private int pathCount = 0;
50     private boolean useStringDelimitor = false;
51     
52     public InteractiveQueryExtraDataExplorer(TableInfo tableInfo)
53     {
54         super(tableInfo);
55     }
56     
57     void prepareStatement(List JavaDoc paths, AbstractConnection connection)
58     throws SQLException JavaDoc, RepositoryException
59     {
60         useStringDelimitor = connection.useStringDelimitor();
61         pStmt = connection.getConnection().prepareStatement(generateStatement(paths)); // TO CHECK : could keep the statement ?
62
connection.setFetchSize(pStmt, RepositoryProperties.getIntProperty(RepositoryConstants.CONF_DATA_FETCHSIZE));
63     }
64     
65     private String JavaDoc generateStatement(List JavaDoc paths) throws RepositoryException
66     {
67         StringBuffer JavaDoc SQLStmt = new StringBuffer JavaDoc();
68         
69         ColumnMapping refColumn;
70         SubPathInfo path;
71         
72         pathCount = paths.size();
73         for (int i = 0; i < pathCount; i++)
74         {
75             path = (SubPathInfo)paths.get(i);
76             
77             if (i > 0)
78                 SQLStmt.append("\nunion all\n");
79             
80             SQLStmt.append("select ");
81             SQLStmt.append(path.subPathIndex);
82             SQLStmt.append(" as n,v.uoid,v.path,v.type,v.pos,v.offset,v.data,v.num\nfrom ");
83             SQLStmt.append(tableInfo.getName());
84             SQLStmt.append(" v\nwhere v.uoid between ? and ? and ?=");
85             SQLStmt.append(path.varPath.getPathID());
86             
87             // path restriction
88
List JavaDoc subpaths = path.subPath.getSubPaths();
89             if (subpaths != null)
90             {
91                 // last struct ancestor is added in order for the builder to initiate range
92
SQLStmt.append(" and v.path");
93                 
94                 int subpathsCount = subpaths.size();
95                 if (subpathsCount == 1)
96                 {
97                     SQLStmt.append("=");
98                     SQLStmt.append(((PathNode)subpaths.get(0)).getPathID());
99                 }
100                 else
101                 {
102                     SQLStmt.append(" in(");
103                     
104                     for (int j = 0; j < subpathsCount; j++)
105                     {
106                         SQLStmt.append(((PathNode)subpaths.get(j)).getPathID());
107                         SQLStmt.append(',');
108                     }
109                     SQLStmt.setCharAt(SQLStmt.length()-1, ')');
110                 }
111             }
112         }
113         
114         SQLStmt.append("\norder by n,num");
115         
116         if (log.isDebugEnabled())
117             log.debug("Value stmt for extra table:\n" + SQLStmt);
118         
119         return SQLStmt.toString();
120     }
121     /**
122      * get the next row from database and store it in the current object.
123      */

124     public boolean fetchNextRow() throws SQLException JavaDoc
125     {
126         try
127         {
128             if (resultActive())
129             {
130                 notExhausted = rs.next();
131                 if (notExhausted)
132                 {
133                     subPathIndex = rs.getInt(1);
134                     uoid = rs.getLong(2);
135                     node.path = rs.getShort(3);
136                     node.type = rs.getShort(4);
137                     node.position = rs.getShort(5);
138                     node.offset = rs.getInt(6);
139                     node.data = rs.getString(7);
140                     if (useStringDelimitor)
141                         node.data = node.data.substring(0, node.data.length() - STRING_DELIMITOR_LENGTH);
142                     node.rowNum = rs.getShort(8);
143                     updateResultSegmentationFlag();
144                 }
145                 else
146                     reset();
147             }
148             else
149                 return false;
150             
151             return notExhausted;
152         }
153         catch (SQLException JavaDoc e)
154         {
155             close();
156             throw e;
157         }
158     }
159     
160     /** To be used by query reconstruction */
161     void execQuery(long first, long last, short path) throws SQLException JavaDoc, RepositoryException
162     {
163         try
164         {
165             if (rs != null)
166                 rs.close();
167             for (int i = 0; i < pathCount; i++)
168             {
169                 pStmt.setLong(3*i + 1, first);
170                 pStmt.setLong(3*i + 2, last);
171                 pStmt.setShort(3*i + 3, path);
172             }
173             rs = pStmt.executeQuery();
174             currentResultExhausted = false;
175             currentSubPathIndex = 0;
176             notExhausted = true;
177         }
178         catch (SQLException JavaDoc e)
179         {
180             pStmt.close();
181             throw e;
182         }
183     }
184     
185 }
186
Popular Tags