1 22 23 28 29 package org.xquark.mapper.storage; 30 31 import java.sql.SQLException ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.xquark.mapper.RepositoryException; 39 import org.xquark.mapper.dbms.*; 40 import org.xquark.mapper.mapping.TableMapping; 41 import org.xquark.mapper.metadata.CollectionMappingInfo; 42 import org.xquark.mapper.metadata.CollectionMetadata; 43 import org.xquark.xml.xdbc.XMLDBCException; 44 45 48 class BatchQueryExplorer extends QueryExplorer 49 { 50 private static final String RCSRevision = "$Revision: 1.2 $"; 51 private static final String RCSName = "$Name: $"; 52 53 private static Log log = LogFactory.getLog(BatchQueryExplorer.class); 54 55 private VarInfoSet tempTable; 56 57 private static TableSpec tmpTableSpecs; 58 59 static 60 { 61 tmpTableSpecs = TableSpecLoader.getInstance().getTableSpec( 62 TableSpec.CAT_QUERY_TMP, 63 TableSpec.TYPE_QUERY_TMP 64 ); 65 } 66 67 68 BatchQueryExplorer(_RepositoryCollection collection) throws SQLException , XMLDBCException 69 { 70 super(collection); 71 CollectionMetadata meta = collection.getMetadata(); 72 wNode = qNode = new BatchQueryStructExplorer(meta.getTableInfo(TableSpec.TYPE_STRUCT), meta.getUOIDManager()); 73 extraNode = qExtraNode = new BatchQueryExtraDataExplorer(meta.getTableInfo(TableSpec.TYPE_EXTRA)); 74 } 75 76 private VarInfoSet prepareVarTable(VarInfoSet varInfoSet) throws SQLException 77 { 78 VarInfoSet tempTable = null; 79 List collapseVars = varInfoSet.getCollapseColumns(); 80 if (collapseVars.size() > 0) 81 { 82 AbstractConnection connection = collection.getRepositoryConnection().getConnection(); 83 ColumnSpec[] columns = tmpTableSpecs.getColumns(); 84 StringBuffer subQuery = new StringBuffer ("SELECT MIN("); 85 subQuery.append(varInfoSet.getRankColumnName()); 86 subQuery.append(") as "); 87 subQuery.append(columns[0].getName()); 88 subQuery.append(','); 89 subQuery.append(varInfoSet.getFirstColumnName()); 90 subQuery.append(" as "); 91 subQuery.append(columns[1].getName()); 92 subQuery.append(','); 93 subQuery.append(varInfoSet.getLastColumnName()); 94 subQuery.append(" as "); 95 subQuery.append(columns[2].getName()); 96 subQuery.append(','); 97 subQuery.append(varInfoSet.getPathColumnName()); 98 subQuery.append(" as "); 99 subQuery.append(columns[3].getName()); 100 subQuery.append(" FROM "); 101 subQuery.append(varInfoSet.getTableName()); 102 subQuery.append(" GROUP BY "); 103 104 for (int i = 0; i < collapseVars.size(); i++) 105 { 106 if (i != 0) 107 subQuery.append(','); 108 if (collapseVars.get(i).equals(varInfoSet.getFirstColumnName())) 109 { 110 subQuery.append(varInfoSet.getFirstColumnName()); 111 subQuery.append(','); 112 subQuery.append(varInfoSet.getLastColumnName()); 113 subQuery.append(','); 114 subQuery.append(varInfoSet.getPathColumnName()); 115 } 116 else 117 subQuery.append((String )collapseVars.get(i)); 118 119 } 120 String tableName = connection.createTemporaryTableAs(subQuery.toString()); 121 tempTable = new VarBuildInfoSet 122 ( 123 tableName, 124 columns[0].getName(), 125 columns[1].getName(), 126 columns[2].getName(), 127 columns[3].getName() 128 ); 129 } 130 return tempTable; 131 } 132 133 135 void close() throws SQLException , RepositoryException 136 { 137 super.close(); 138 139 if (tempTable != null) 140 { 141 collection.getRepositoryConnection().getConnection().dropTemporaryTable(tempTable.getTableName()); 142 tempTable = null; 143 } 144 } 145 146 148 void reset() throws SQLException , RepositoryException 149 { 150 super.reset(); 151 152 if (tempTable != null) 153 { 154 collection.getRepositoryConnection().getConnection().dropTemporaryTable(tempTable.getTableName()); 155 tempTable = null; 156 } 157 } 158 159 165 void initExploration(VarInfoSet varInfoSet, AbstractConnection connection) throws SQLException , XMLDBCException 166 { 167 if (log.isDebugEnabled()) 168 log.debug("QueryExplorer::initExploration [varInfoSet]=" + varInfoSet); 169 170 tempTable = prepareVarTable(varInfoSet); 172 173 Object [] ret = dispatchSubPaths(varInfoSet); 175 Map tableMappingSubset = (Map )ret[0]; 176 List subPathNodes = (List )ret[1]; 177 178 Iterator it = tableMappingSubset.entrySet().iterator(); 180 Map.Entry entry; 181 CollectionMappingInfo tableStmts; 182 int tableIndex; 183 184 CollectionMetadata meta = collection.getMetadata(); 185 while (it.hasNext()) 186 { 187 entry = (Map.Entry )it.next(); 188 tableIndex = ((TableMapping)entry.getKey()).getTableIndex(); 189 tableStmts = (CollectionMappingInfo)meta.getMappingStatements(tableIndex); 190 if (queryTuples[tableIndex] == null) 191 queryTuples[tableIndex] = new BatchQueryTuple(tableStmts, meta.getMappingPID(tableIndex)); 192 ((BatchQueryTuple)queryTuples[tableIndex]).execQuery((List )entry.getValue(), 193 tempTable == null?varInfoSet:tempTable, meta, connection); 194 } 195 196 ((BatchQueryStructExplorer)qNode).getBranch(subPathNodes, 198 tempTable == null?varInfoSet:tempTable, connection); 199 ((BatchQueryExtraDataExplorer)qExtraNode).getBranch(subPathNodes, 200 tempTable == null?varInfoSet:tempTable, connection); 201 } 202 203 void setCurrentRank(int rank) 204 { 205 for (int i=0; i < queryTuples.length; i++) 206 { 207 if (queryTuples[i] != null) 208 ((BatchQueryTuple)queryTuples[i]).setCurrentRank(rank); 209 } 210 ((BatchQueryStructExplorer)qNode).setCurrentRank(rank); 211 ((BatchQueryExtraDataExplorer)qExtraNode).setCurrentRank(rank); 212 } 213 214 } 215 | Popular Tags |