1 22 23 package org.xquark.mapper.storage; 24 25 import java.sql.*; 26 27 import org.xquark.mapper.dbms.AbstractConnection; 28 import org.xquark.mapper.dbms.TableInfo; 29 import org.xquark.mapper.metadata.DocumentInfo; 30 31 36 37 public class PersistentDocumentInfo 38 { 39 private static final String RCSRevision = "$Revision: 1.1 $"; 40 private static final String RCSName = "$Name: $"; 41 42 private DocumentInfo wDocInfo; 43 private TableInfo docTable; 44 private String docInfoStmt; 45 46 50 51 private AbstractConnection connection; 52 53 54 private Statement stmt = null; 55 private ResultSet rs = null; 56 57 public PersistentDocumentInfo(TableInfo docTable, AbstractConnection connection) 58 { 59 this.connection = connection; 60 this.docTable = docTable; 61 initStatements(); 62 } 63 64 private void initStatements() 65 { 66 StringBuffer sql = new StringBuffer (); 67 sql.append(docTable.getSelectAllStatement()); 68 sql.append(" WHERE "); 69 sql.append(docTable.getColumns()[0].getName()); 70 sql.append("=?"); 71 docInfoStmt = sql.toString(); 72 } 73 74 public DocumentInfo getDocumentInfo() 75 { 76 return wDocInfo; 77 } 78 79 84 public void save() 85 throws SQLException 86 { 87 PreparedStatement pStmt = connection.getConnection().prepareStatement(docTable.getInsertStatement()); 89 try 90 { 91 pStmt.setString(1, wDocInfo.docID); 92 pStmt.setLong(2, wDocInfo.udid); 93 pStmt.setTimestamp(3, wDocInfo.creationDate); 94 pStmt.setLong(4, wDocInfo.nodeCount); 95 pStmt.setInt(5, wDocInfo.avgNodeLen); 96 pStmt.executeUpdate(); 97 } 98 finally 99 { 100 pStmt.close(); 101 } 102 } 103 104 107 public void close() 108 { 109 try { 111 if (rs != null) 112 rs.close(); 113 if (stmt != null) 114 stmt.close(); 115 connection = null; 116 } 117 catch(SQLException e) { 118 } 120 } 121 122 public void retrieveDataSet() 123 throws SQLException 124 { 125 stmt = connection.getConnection().createStatement(); 126 try { 127 rs = stmt.executeQuery(docTable.getSelectAllStatement()); 128 } 129 catch (SQLException e) { 130 stmt.close(); 131 throw e; 132 } 133 } 134 135 139 public boolean fetchNextRow() 140 throws SQLException 141 { 142 if (stmt == null) 143 return false; 144 try { 145 if (fetchNextResult(rs)) 146 return true; 147 else 148 { 149 rs.close(); 150 stmt.close(); 151 stmt = null; return false; } 154 } 155 catch (SQLException e) { 156 rs.close(); 157 stmt.close(); 158 throw e; 159 } 160 } 161 162 public boolean fetchRow(String docID) 163 throws SQLException 164 { 165 PreparedStatement stmt = null; 166 ResultSet rs = null; 167 try { 168 stmt = connection.getConnection().prepareStatement(docInfoStmt); 169 stmt.setString(1, docID); 170 rs = stmt.executeQuery(); 171 172 if (fetchNextResult(rs)) 173 return true; else 175 return false; } 177 finally 178 { 179 if (rs != null) 180 rs.close(); 181 if (stmt != null) 182 stmt.close(); 183 } 184 } 185 186 private boolean fetchNextResult(ResultSet rs) 187 throws SQLException 188 { 189 if(rs.next()) 190 { 191 wDocInfo = new DocumentInfo( 192 rs.getString(1), 193 rs.getLong(2), 194 rs.getTimestamp(3), 195 rs.getLong(4), 196 rs.getInt(5) 197 ); 198 return true; 199 } 200 else 201 return false; } 203 } 204 | Popular Tags |