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