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.util.RepositoryProperties; 35 36 40 public class BatchQueryExtraDataExplorer extends QueryExtraDataExplorer 41 { 42 private static final String RCSRevision = "$Revision: 1.2 $"; 43 private static final String RCSName = "$Name: $"; 44 45 private static Log log = LogFactory.getLog(BatchQueryExtraDataExplorer.class); 46 47 private int currentRank = -2; 48 private int rank = -1; 49 50 public BatchQueryExtraDataExplorer(TableInfo tableInfo) 51 { 52 super(tableInfo); 53 } 54 57 public void getBranch(List paths, VarInfoSet tmpTable, 58 AbstractConnection connection) 59 throws SQLException  60 { 61 useStringDelimitor = connection.useStringDelimitor(); 62 stmt = connection.getConnection().createStatement(); 63 try 64 { 65 connection.setFetchSize(stmt, RepositoryProperties.getIntProperty(CONF_DATA_FETCHSIZE)); 66 rs = stmt.executeQuery(generateStatement(paths, tmpTable)); 67 notExhausted = true; 68 currentResultExhausted = false; 69 } 70 catch (SQLException e) 71 { 72 stmt.close(); 73 throw e; 74 } 75 } 76 77 private String generateStatement(List paths, VarInfoSet tmpTable) 78 { 79 StringBuffer SQLStmt = new StringBuffer (); 80 81 ColumnMapping refColumn; 82 SubPathInfo path; 83 84 for (int i = 0; i < paths.size(); i++) 85 { 86 path = (SubPathInfo)paths.get(i); 87 88 if (i > 0) 89 SQLStmt.append("\nunion all\n"); 90 91 SQLStmt.append("select t."); 92 SQLStmt.append(tmpTable.getRankColumnName()); 93 SQLStmt.append(','); 94 SQLStmt.append(path.subPathIndex); 95 SQLStmt.append(" as n,v.uoid,v.path,v.type,v.pos,v.offset,v.data,v.num\nfrom "); 96 SQLStmt.append(tmpTable.getTableName()); 97 SQLStmt.append(" t,"); 98 SQLStmt.append(tableInfo.getName()); 99 SQLStmt.append(" v\nwhere v.uoid between t."); 100 SQLStmt.append(tmpTable.getFirstColumnName()); 101 SQLStmt.append(" and t."); 102 SQLStmt.append(tmpTable.getLastColumnName()); 103 SQLStmt.append(" and t."); 104 SQLStmt.append(tmpTable.getPathColumnName()); 105 SQLStmt.append('='); 106 SQLStmt.append(path.varPath.getPathID()); 107 List subpaths = path.subPath.getSubPaths(); 109 if (subpaths != null) 110 { 111 SQLStmt.append(" and v.path"); 113 114 int subpathsCount = subpaths.size(); 115 if (subpathsCount == 1) 116 { 117 SQLStmt.append("="); 118 SQLStmt.append(((PathNode)subpaths.get(0)).getPathID()); 119 } 120 else 121 { 122 SQLStmt.append(" in("); 123 124 for (int j = 0; j < subpathsCount; j++) 125 { 126 SQLStmt.append(((PathNode)subpaths.get(j)).getPathID()); 127 SQLStmt.append(','); 128 } 129 SQLStmt.setCharAt(SQLStmt.length()-1, ')'); 130 } 131 } 132 } 133 134 SQLStmt.append("\norder by "); 135 SQLStmt.append(tmpTable.getRankColumnName()); 136 SQLStmt.append(",n,num"); 137 138 if (log.isDebugEnabled()) 139 log.debug(">>> Value stmt for extra table:\n" + SQLStmt); 140 141 return SQLStmt.toString(); 142 } 143 146 public boolean fetchNextRow() throws SQLException  147 { 148 try 149 { 150 if (resultActive()) 151 { 152 notExhausted = rs.next(); 153 if (notExhausted) 154 { 155 rank = rs.getInt(1); 156 subPathIndex = rs.getInt(2); 157 uoid = rs.getLong(3); 158 node.path = rs.getShort(4); 159 node.type = rs.getShort(5); 160 node.position = rs.getShort(6); 161 node.offset = rs.getInt(7); 162 node.data = rs.getString(8); 163 if (useStringDelimitor) 164 node.data = node.data.substring(0, node.data.length() - STRING_DELIMITOR_LENGTH); 165 node.rowNum = rs.getShort(9); 166 updateResultSegmentationFlag(); 167 } 168 else 169 reset(); 170 } 171 else 172 return false; 173 174 return notExhausted; 176 } 177 catch (SQLException e) 178 { 179 close(); 180 throw e; 181 } 182 } 183 184 void setCurrentRank(int rank) 185 { 186 currentSubPathIndex = 0; 187 currentRank = rank; 188 updateResultSegmentationFlag(); 189 } 190 191 192 public void reset() throws SQLException  193 { 194 super.reset(); 195 currentRank = -2; 196 rank = -1; 197 } 198 199 protected void updateResultSegmentationFlag() 200 { 201 currentResultExhausted = (rank != currentRank) || (subPathIndex != currentSubPathIndex); 202 } 203 } 204
| Popular Tags
|