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.RepositoryException; 31 import org.xquark.mapper.dbms.AbstractConnection; 32 import org.xquark.mapper.dbms.TableInfo; 33 import org.xquark.mapper.mapping.ColumnMapping; 34 import org.xquark.mapper.metadata.PathNode; 35 import org.xquark.mapper.metadata.RepositoryConstants; 36 import org.xquark.mapper.metadata.UOIDManager; 37 import org.xquark.mapper.util.RepositoryProperties; 38 39 43 public class InteractiveQueryStructExplorer extends QueryStructExplorer 44 { 45 private static final String RCSRevision = "$Revision: 1.2 $"; 46 private static final String RCSName = "$Name: $"; 47 48 private static Log log = LogFactory.getLog(InteractiveQueryStructExplorer.class); 49 50 private int pathCount = 0; 51 52 public InteractiveQueryStructExplorer(TableInfo tableInfo, UOIDManager manager) 53 { 54 super(tableInfo, manager); 55 } 56 57 60 public boolean fetchNextRow() throws SQLException 61 { 62 try 63 { 64 if (resultActive()) 65 { 66 notExhausted = rs.next(); 67 if (notExhausted) 68 { 69 subPathIndex = rs.getInt(1); 70 uoid = rs.getLong(2); 71 last = rs.getLong(3); 72 path = rs.getShort(4); 73 updateResultSegmentationFlag(); 74 } 75 else 76 reset(); 77 } 78 else 79 return false; 80 81 return notExhausted; 82 } 83 catch (SQLException e) 84 { 85 close(); 86 throw e; 87 } 88 } 89 90 91 void execQuery(long first, long last, short path) throws SQLException , RepositoryException 92 { 93 if (pStmt == null) 94 return; 95 try 96 { 97 if (rs != null) 98 rs.close(); 99 for (int i = 0; i < pathCount; i++) 100 { 101 pStmt.setLong(4*i + 1, first >>> manager.getDIDShift()); 102 pStmt.setLong(4*i + 2, first); 103 pStmt.setLong(4*i + 3, last); 104 pStmt.setShort(4*i + 4, path); 105 } 106 rs = pStmt.executeQuery(); 107 currentResultExhausted = false; 108 currentSubPathIndex = 0; 109 notExhausted = true; 110 } 111 catch (SQLException e) 112 { 113 pStmt.close(); 114 throw e; 115 } 116 } 117 118 void prepareStatement(List paths, AbstractConnection connection) throws SQLException , RepositoryException 119 { 120 String SQL = generateStatement(paths); 121 if (SQL != null) 122 { 123 pStmt = connection.getConnection().prepareStatement(SQL); connection.setFetchSize(pStmt, RepositoryProperties.getIntProperty(RepositoryConstants.CONF_TREE_FETCHSIZE)); 125 } 126 else 127 pStmt = null; 128 } 129 130 private String generateStatement(List paths) throws RepositoryException 131 { 132 StringBuffer SQLStmt = new StringBuffer (); 133 134 ColumnMapping refColumn; 135 SubPathInfo path; 136 137 for (int i = 0; i < paths.size(); i++) 138 { 139 path = (SubPathInfo)paths.get(i); 140 List subpaths = path.subPath.getModelSubPaths(); 141 int subpathsCount = subpaths.size(); 142 if ((subpathsCount > 0) && !path.isText) 143 { 144 pathCount++; 145 if (SQLStmt.length() > 0) SQLStmt.append("\nunion all\n"); 147 148 SQLStmt.append("select "); 149 SQLStmt.append(path.subPathIndex); 150 SQLStmt.append(" as n,v.uoid,v.last,v.path\nfrom "); 151 SQLStmt.append(tableInfo.getName()); 152 SQLStmt.append(" v\nwhere v.bucket=? and v.uoid between ? and ? and ?="); 153 SQLStmt.append(path.varPath.getPathID()); 154 SQLStmt.append(" and v.path"); 157 158 if (subpathsCount == 1) 159 { 160 SQLStmt.append("="); 161 SQLStmt.append(((PathNode)subpaths.get(0)).getPathID()); 162 } 163 else 164 { 165 SQLStmt.append(" in("); 166 167 for (int j = 0; j < subpathsCount; j++) 168 { 169 SQLStmt.append(((PathNode)subpaths.get(j)).getPathID()); 170 SQLStmt.append(','); 171 } 172 SQLStmt.setCharAt(SQLStmt.length()-1, ')'); 173 } 174 } 175 } 176 177 if (SQLStmt.length() > 0) 178 { 179 SQLStmt.append("\norder by n,uoid"); 180 181 if (log.isDebugEnabled()) 182 log.debug("Value stmt for struct table:\n" + SQLStmt); 183 184 return SQLStmt.toString(); 185 } 186 else 187 return null; 188 } 189 190 } 191 | Popular Tags |