KickJava   Java API By Example, From Geeks To Geeks.

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


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.metadata.UOIDManager;
37 import org.xquark.mapper.util.RepositoryProperties;
38
39 /**
40  * Object used to perform XML structure node storage.
41  *
42  */

43 public class InteractiveQueryStructExplorer extends QueryStructExplorer
44 {
45     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
46     private static final String JavaDoc RCSName = "$Name: $";
47     
48     private static Log log = LogFactory.getLog(InteractiveQueryStructExplorer.class);
49     
50     private int pathCount = 0;
51     
52     public InteractiveQueryStructExplorer(TableInfo tableInfo, UOIDManager manager)
53     {
54         super(tableInfo, manager);
55     }
56     
57     /**
58      * get the next row from database and store it in the current object.
59      */

60     public boolean fetchNextRow() throws SQLException JavaDoc
61     {
62         try
63         {
64             if (resultActive())
65             {
66                 notExhausted = rs.next();
67                 if (notExhausted)
68                 {
69                     subPathIndex = rs.getInt(1);
70                     uoid = rs.getLong(2);
71                     last = rs.getLong(3);
72                     path = rs.getShort(4);
73                     updateResultSegmentationFlag();
74                 }
75                 else
76                     reset();
77             }
78             else
79                 return false;
80             
81             return notExhausted;
82         }
83         catch (SQLException JavaDoc e)
84         {
85             close();
86             throw e;
87         }
88     }
89     
90     /** To be used by query reconstruction */
91     void execQuery(long first, long last, short path) throws SQLException JavaDoc, RepositoryException
92     {
93         if (pStmt == null)
94             return;
95         try
96         {
97             if (rs != null)
98                 rs.close();
99             for (int i = 0; i < pathCount; i++)
100             {
101                 pStmt.setLong(4*i + 1, first >>> manager.getDIDShift());
102                 pStmt.setLong(4*i + 2, first);
103                 pStmt.setLong(4*i + 3, last);
104                 pStmt.setShort(4*i + 4, path);
105             }
106             rs = pStmt.executeQuery();
107             currentResultExhausted = false;
108             currentSubPathIndex = 0;
109             notExhausted = true;
110         }
111         catch (SQLException JavaDoc e)
112         {
113             pStmt.close();
114             throw e;
115         }
116     }
117     
118     void prepareStatement(List JavaDoc paths, AbstractConnection connection) throws SQLException JavaDoc, RepositoryException
119     {
120         String JavaDoc SQL = generateStatement(paths);
121         if (SQL != null)
122         {
123             pStmt = connection.getConnection().prepareStatement(SQL); // TO CHECK : could keep the statement ?
124
connection.setFetchSize(pStmt, RepositoryProperties.getIntProperty(RepositoryConstants.CONF_TREE_FETCHSIZE));
125         }
126         else
127             pStmt = null;
128     }
129     
130     private String JavaDoc generateStatement(List JavaDoc paths) throws RepositoryException
131     {
132         StringBuffer JavaDoc SQLStmt = new StringBuffer JavaDoc();
133         
134         ColumnMapping refColumn;
135         SubPathInfo path;
136         
137         for (int i = 0; i < paths.size(); i++)
138         {
139             path = (SubPathInfo)paths.get(i);
140             List JavaDoc subpaths = path.subPath.getModelSubPaths();
141             int subpathsCount = subpaths.size();
142             if ((subpathsCount > 0) && !path.isText)
143             {
144                 pathCount++;
145                 if (SQLStmt.length() > 0) // i was not a correct flag because index 0 coul be discarded
146
SQLStmt.append("\nunion all\n");
147                 
148                 SQLStmt.append("select ");
149                 SQLStmt.append(path.subPathIndex);
150                 SQLStmt.append(" as n,v.uoid,v.last,v.path\nfrom ");
151                 SQLStmt.append(tableInfo.getName());
152                 SQLStmt.append(" v\nwhere v.bucket=? and v.uoid between ? and ? and ?=");
153                 SQLStmt.append(path.varPath.getPathID());
154                 // path restriction
155
// last struct ancestor is added in order for the builder to initiate range
156
SQLStmt.append(" and v.path");
157                 
158                 if (subpathsCount == 1)
159                 {
160                     SQLStmt.append("=");
161                     SQLStmt.append(((PathNode)subpaths.get(0)).getPathID());
162                 }
163                 else
164                 {
165                     SQLStmt.append(" in(");
166                     
167                     for (int j = 0; j < subpathsCount; j++)
168                     {
169                         SQLStmt.append(((PathNode)subpaths.get(j)).getPathID());
170                         SQLStmt.append(',');
171                     }
172                     SQLStmt.setCharAt(SQLStmt.length()-1, ')');
173                 }
174             }
175         }
176         
177         if (SQLStmt.length() > 0)
178         {
179             SQLStmt.append("\norder by n,uoid");
180             
181             if (log.isDebugEnabled())
182                 log.debug("Value stmt for struct table:\n" + SQLStmt);
183             
184             return SQLStmt.toString();
185         }
186         else
187             return null;
188     }
189     
190 }
191
Popular Tags