1 22 23 package org.xquark.mapper.storage; 24 25 import java.sql.SQLException ; 26 import java.util.List ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.xquark.mapper.dbms.AbstractConnection; 31 import org.xquark.mapper.dbms.TableInfo; 32 import org.xquark.mapper.mapping.ColumnMapping; 33 import org.xquark.mapper.metadata.PathNode; 34 import org.xquark.mapper.metadata.UOIDManager; 35 import org.xquark.mapper.util.RepositoryProperties; 36 37 41 public class BatchQueryStructExplorer extends QueryStructExplorer 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(BatchQueryStructExplorer.class); 47 48 private int currentRank = -2; 49 private int rank = -1; 50 51 public BatchQueryStructExplorer(TableInfo tableInfo, UOIDManager manager) 52 { 53 super(tableInfo, manager); 54 } 55 56 59 public void getBranch(List paths, VarInfoSet tmpTable, 60 AbstractConnection connection) 61 throws SQLException 62 { 63 stmt = connection.getConnection().createStatement(); 64 try 65 { 66 connection.setFetchSize(stmt, RepositoryProperties.getIntProperty(CONF_TREE_FETCHSIZE)); 67 String stmtString = generateStatement(paths, tmpTable); 68 if (stmtString != null) 69 { 70 rs = stmt.executeQuery(stmtString); 71 notExhausted = true; 72 currentResultExhausted = false; 73 } 74 } 75 catch (SQLException e) 76 { 77 stmt.close(); 78 throw e; 79 } 80 } 82 83 private String generateStatement(List paths, VarInfoSet tmpTable) 84 { 85 StringBuffer SQLStmt = new StringBuffer (); 86 87 ColumnMapping refColumn; 88 SubPathInfo path; 89 90 for (int i = 0; i < paths.size(); i++) 91 { 92 path = (SubPathInfo)paths.get(i); 93 List subpaths = path.subPath.getModelSubPaths(); 94 int subpathsCount = subpaths.size(); 95 if ((subpathsCount > 0) && !path.isText) 96 { 97 if (SQLStmt.length() > 0) SQLStmt.append("\nunion all\n"); 99 100 SQLStmt.append("select t."); 101 SQLStmt.append(tmpTable.getRankColumnName()); 102 SQLStmt.append(','); 103 SQLStmt.append(path.subPathIndex); 104 SQLStmt.append(" as n,v.uoid,v.last,v.path\nfrom "); 105 SQLStmt.append(tmpTable.getTableName()); 106 SQLStmt.append(" t,"); 107 SQLStmt.append(tableInfo.getName()); 108 SQLStmt.append(" v\nwhere v.bucket=floor(t."); 109 SQLStmt.append(tmpTable.getFirstColumnName()); 110 SQLStmt.append('/'); 111 SQLStmt.append(manager.getDocMultiplier()); 112 SQLStmt.append(") and v.uoid between t."); 113 SQLStmt.append(tmpTable.getFirstColumnName()); 114 SQLStmt.append(" and t."); 115 SQLStmt.append(tmpTable.getLastColumnName()); 116 SQLStmt.append(" and t."); 117 SQLStmt.append(tmpTable.getPathColumnName()); 118 SQLStmt.append('='); 119 SQLStmt.append(path.varPath.getPathID()); 120 SQLStmt.append(" and v.path"); 123 124 if (subpathsCount == 1) 125 { 126 SQLStmt.append("="); 127 SQLStmt.append(((PathNode)subpaths.get(0)).getPathID()); 128 } 129 else 130 { 131 SQLStmt.append(" in("); 132 133 for (int j = 0; j < subpathsCount; j++) 134 { 135 SQLStmt.append(((PathNode)subpaths.get(j)).getPathID()); 136 SQLStmt.append(','); 137 } 138 SQLStmt.setCharAt(SQLStmt.length()-1, ')'); 139 } 140 } 141 } 142 143 if (SQLStmt.length() > 0) 144 { 145 SQLStmt.append("\norder by "); 146 SQLStmt.append(tmpTable.getRankColumnName()); 147 SQLStmt.append(",n,uoid"); 148 149 if (log.isDebugEnabled()) 150 log.debug("Value stmt for struct table:\n" + SQLStmt); 151 152 return SQLStmt.toString(); 153 } 154 else 155 return null; 156 } 157 158 161 public boolean fetchNextRow() throws SQLException 162 { 163 try 164 { 165 if (resultActive()) 166 { 167 notExhausted = rs.next(); 168 if (notExhausted) 169 { 170 rank = rs.getInt(1); 171 subPathIndex = rs.getInt(2); 172 uoid = rs.getLong(3); 173 last = rs.getLong(4); 174 path = rs.getShort(5); 175 updateResultSegmentationFlag(); 176 } 177 else 178 reset(); 179 } 180 else 181 return false; 182 183 return notExhausted; 185 } 186 catch (SQLException e) 187 { 188 close(); 189 throw e; 190 } 191 } 192 193 void setCurrentRank(int rank) 194 { 195 currentSubPathIndex = 0; 196 currentRank = rank; 197 updateResultSegmentationFlag(); 198 } 199 200 public void reset() throws SQLException 201 { 202 super.reset(); 203 currentRank = -2; 204 rank = -1; 205 } 206 207 protected void updateResultSegmentationFlag() 208 { 209 currentResultExhausted = (rank != currentRank) || (subPathIndex != currentSubPathIndex); 210 } 211 } 212 | Popular Tags |