1 22 23 package org.xquark.mapper.storage; 24 25 import java.sql.SQLException ; 26 import java.util.Collections ; 27 import java.util.List ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.xquark.mapper.dbms.AbstractConnection; 32 import org.xquark.mapper.mapping.ColumnMapping; 33 import org.xquark.mapper.mapping.TableMapping; 34 import org.xquark.mapper.metadata.*; 35 import org.xquark.mapper.util.RepositoryProperties; 36 import org.xquark.xml.xdbc.XMLDBCException; 37 38 class BatchQueryTuple extends QueryTuple 39 { 40 private static final String RCSRevision = "$Revision: 1.2 $"; 41 private static final String RCSName = "$Name: $"; 42 43 private static Log log = LogFactory.getLog(BatchQueryTuple.class); 44 45 protected int rank = -1; 46 47 private int currentRank = -2; 48 49 BatchQueryTuple(CollectionMappingInfo table, short pathID) 50 { 51 super(table, pathID); 52 valueOffset = 3; 53 } 54 55 56 void execQuery(List paths, VarInfoSet tmpTable, CollectionMetadata collection, 57 AbstractConnection connection) 58 throws SQLException , XMLDBCException 59 { 60 try 61 { 62 selectStmt = connection.getConnection().createStatement(); connection.setFetchSize(selectStmt, RepositoryProperties.getIntProperty(RepositoryConstants.CONF_USER_FETCHSIZE)); 64 if (resultSet != null) 65 resultSet.close(); 66 resultSet = selectStmt.executeQuery(generateStatement(paths, tmpTable, collection)); 67 notExhausted = true; 68 currentResultExhausted = false; 69 } 70 catch (SQLException e) 71 { 72 selectStmt.close(); 73 throw e; 74 } 75 } 76 77 private String generateStatement(List paths, VarInfoSet tmpTable, CollectionMetadata collection) 78 throws XMLDBCException 79 { 80 StringBuffer SQLStmt = new StringBuffer (); 81 82 SubPathInfo path; 83 ColumnMapping refColumn = null; 84 TableMapping mapping = table.getTableMapping(); 85 String UOIDColumnName, pathColumnName; 86 if (mapping.isClustered()) 87 { 88 UOIDColumnName = mapping.getUOIDColumnName(); 89 pathColumnName = mapping.getPathIDColumnName(); 90 } 91 else 92 { 93 UOIDColumnName = "uoid"; 94 pathColumnName = "path"; 95 } 96 97 for (int i = 0; i < paths.size(); i++) 98 { 99 path = (SubPathInfo)paths.get(i); 100 101 if (i > 0) 102 SQLStmt.append("\nunion all\n"); 103 104 SQLStmt.append("select t."); 105 SQLStmt.append(tmpTable.getRankColumnName()); 106 SQLStmt.append(','); 107 SQLStmt.append(path.subPathIndex); 108 SQLStmt.append(" as n"); 109 if (!mapping.isClustered()) 110 { 111 SQLStmt.append(",v."); 112 SQLStmt.append(UOIDColumnName); 113 SQLStmt.append(",v."); 114 SQLStmt.append(pathColumnName); 115 } 116 117 118 if (path.subPath.getPathID() != RepositoryConstants.ROOT_PATH_ID) refColumn = path.subPath.getReferenceColumnMapping(); 120 121 ColumnMapping[] columns = mapping.getColumnMappings(); 122 ColumnMapping column; 123 for (int j = 0; j < columns.length; j++) 124 { 125 column = columns[j]; 126 if ((refColumn == null) 127 || (column == refColumn) 128 || (mapping.isClustered() && ((column.getColumnIndex() == mapping.getUOIDIndex()) 129 || (column.getColumnIndex() == mapping.getPathIDIndex())))) 130 { 131 if (mapping.isClustered()) 132 SQLStmt.append(",v."); 133 else 134 SQLStmt.append(",w."); 135 136 SQLStmt.append(column.getColumnName()); 137 } 138 else 139 { 140 SQLStmt.append(','); 141 SQLStmt.append(column.getTypeInfo().getTypedNullValue()); } 143 } 144 145 SQLStmt.append("\nfrom "); SQLStmt.append(tmpTable.getTableName()); 147 SQLStmt.append(" t,"); 148 if (mapping.isClustered()) 149 { 150 SQLStmt.append(mapping.getTableName()); 151 SQLStmt.append(" v"); 152 } 153 else 154 { 155 SQLStmt.append(collection.getOIDTableName(mapping.getTableIndex())); 156 SQLStmt.append(" v,"); 157 SQLStmt.append(mapping.getTableName()); 158 SQLStmt.append(" w"); 159 } 160 SQLStmt.append("\nwhere v."); 161 SQLStmt.append(UOIDColumnName); 162 SQLStmt.append(" between t."); 163 SQLStmt.append(tmpTable.getFirstColumnName()); 164 SQLStmt.append(" and t."); 165 SQLStmt.append(tmpTable.getLastColumnName()); 166 SQLStmt.append(" and t."); 167 SQLStmt.append(tmpTable.getPathColumnName()); 168 SQLStmt.append('='); 169 SQLStmt.append(path.varPath.getPathID()); 170 if (!mapping.isClustered()) 171 { 172 SQLStmt.append(" and "); 173 SQLStmt.append(mapping.getJoinCondition()); 174 } 175 if (mapping.isTableShared()) 177 { 178 if (!mapping.isShared()) { 180 SQLStmt.append(" and abs(v."); 181 SQLStmt.append(pathColumnName); 182 SQLStmt.append(")="); 183 SQLStmt.append(tablePath); 184 } 185 else { 187 List subpaths = null; 188 if (path.isText) 189 subpaths = Collections.singletonList(path.subPath.getBuilderAncestor()); 190 else 191 subpaths = path.subPath.getBuilderAncestor().getModelSubPaths(); 192 int subpathsCount = subpaths.size(); 193 if (subpathsCount > 0) 194 { 195 SQLStmt.append(" and abs(v."); 196 SQLStmt.append(pathColumnName); 197 SQLStmt.append(')'); 198 199 if (subpathsCount == 1) 200 { 201 SQLStmt.append('='); 202 SQLStmt.append(((PathNode)subpaths.get(0)).getPathID()); 203 } 204 else 205 { 206 SQLStmt.append(" in("); 207 208 for (int j = 0; j < subpathsCount; j++) 209 { 210 SQLStmt.append(((PathNode)subpaths.get(j)).getPathID()); 211 SQLStmt.append(','); 212 } 213 SQLStmt.setCharAt(SQLStmt.length()-1, ')'); 214 } 215 } 216 } 217 } 218 } 219 220 SQLStmt.append("\norder by "); 221 SQLStmt.append(tmpTable.getRankColumnName()); 222 SQLStmt.append(",n,"); 223 SQLStmt.append(UOIDColumnName); 224 225 if (log.isDebugEnabled()) 226 log.debug("Value recontruction statement stmt [table,query]\n" 227 + mapping.getTableName() + '\n' + SQLStmt); 228 return SQLStmt.toString(); 229 } 230 231 boolean fetchNextRow() throws SQLException 232 { 233 super.fetchNextRow(); 234 try 235 { 236 if (resultActive()) 237 { 238 notExhausted = resultSet.next(); 239 if (notExhausted) 240 { 241 TableMapping mapping = table.getTableMapping(); 242 rank = resultSet.getInt(1); 243 subPathIndex = resultSet.getInt(2); 244 uoid = resultSet.getLong(mapping.getUOIDIndex() + 3); pathoid = resultSet.getShort(mapping.getPathIDIndex() + 3); 246 updateResultSegmentationFlag(); 247 } 248 else 249 reset(); 250 } 251 else 252 return false; 253 254 return notExhausted; 255 } 256 catch (SQLException e) 257 { 258 selectStmt.close(); 259 throw e; 260 } 261 } 262 263 void reset() throws SQLException 264 { 265 super.reset(); 266 rank = -1; 267 currentRank = -2; 268 } 269 270 void setCurrentRank(int rank) 271 { 272 currentSubPathIndex = 0; 273 currentRank = rank; 274 updateResultSegmentationFlag(); 275 } 276 277 protected void updateResultSegmentationFlag() 278 { 279 currentResultExhausted = (rank != currentRank) || (subPathIndex != currentSubPathIndex); 280 } 281 } 282 283 | Popular Tags |