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