1 17 package org.apache.servicemix.jdbc.adapter; 18 19 import java.io.IOException ; 20 import java.sql.Connection ; 21 import java.sql.PreparedStatement ; 22 import java.sql.ResultSet ; 23 import java.sql.SQLException ; 24 import java.sql.Statement ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.servicemix.jdbc.JDBCAdapter; 31 import org.apache.servicemix.jdbc.JDBCAdapterFactory; 32 import org.apache.servicemix.jdbc.Statements; 33 34 54 public class DefaultJDBCAdapter implements JDBCAdapter { 55 56 private static final Log log = LogFactory.getLog(DefaultJDBCAdapter.class); 57 58 protected Statements statements; 59 60 protected void setBinaryData(PreparedStatement s, int index, byte data[]) throws SQLException { 61 s.setBytes(index, data); 62 } 63 64 protected byte[] getBinaryData(ResultSet rs, int index) throws SQLException { 65 return rs.getBytes(index); 66 } 67 68 public void doCreateTables(Connection connection) throws SQLException , IOException { 69 Statement s = null; 70 try { 71 connection.setAutoCommit(false); 72 73 boolean alreadyExists = false; 76 ResultSet rs=null; 77 try { 78 rs= connection.getMetaData().getTables(null,null, statements.getFullStoreTableName(), new String [] {"TABLE"}); 79 alreadyExists = rs.next(); 80 } catch (Throwable ignore) { 81 } finally { 82 close(rs); 83 } 84 85 if (alreadyExists) { 89 return; 90 } 91 92 s = connection.createStatement(); 93 String [] createStatments = statements.getCreateSchemaStatements(); 94 for (int i = 0; i < createStatments.length; i++) { 95 try { 98 log.debug("Executing SQL: " + createStatments[i]); 99 s.execute(createStatments[i]); 100 } 101 catch (SQLException e) { 102 if( alreadyExists ) { 103 log.debug("Could not create JDBC tables; The message table already existed." + 104 " Failure was: " + createStatments[i] + " Message: " + e.getMessage() + 105 " SQLState: " + e.getSQLState() + " Vendor code: " + e.getErrorCode() ); 106 } else { 107 log.warn("Could not create JDBC tables; they could already exist." + 108 " Failure was: " + createStatments[i] + " Message: " + e.getMessage() + 109 " SQLState: " + e.getSQLState() + " Vendor code: " + e.getErrorCode() ); 110 JDBCAdapterFactory.log("Failure details: ", e); 111 } 112 } 113 } 114 connection.commit(); 115 116 } 117 finally { 118 close(s); 119 } 120 } 121 122 public void doDropTables(Connection connection) throws SQLException , IOException { 123 Statement s = null; 124 try { 125 s = connection.createStatement(); 126 String [] dropStatments = statements.getDropSchemaStatements(); 127 for (int i = 0; i < dropStatments.length; i++) { 128 try { 131 s.execute(dropStatments[i]); 132 } 133 catch (SQLException e) { 134 log.warn("Could not drop JDBC tables; they may not exist." + 135 " Failure was: " + dropStatments[i] + " Message: " + e.getMessage() + 136 " SQLState: " + e.getSQLState() + " Vendor code: " + e.getErrorCode() ); 137 JDBCAdapterFactory.log("Failure details: ", e); 138 } 139 } 140 connection.commit(); 141 } 142 finally { 143 close(s); 144 } 145 } 146 147 public void doStoreData(Connection connection, String id, byte[] data) throws SQLException , IOException { 148 PreparedStatement s = null; 149 try { 150 if (s == null) { 151 s = connection.prepareStatement(statements.getStoreDataStatement()); 152 } 153 s.setString(1, id); 154 setBinaryData(s, 2, data); 155 if ( s.executeUpdate() != 1 ) { 156 throw new SQLException ("Failed to insert data"); 157 } 158 } finally { 159 close(s); 160 } 161 } 162 163 public byte[] doLoadData(Connection connection, String id) throws SQLException , IOException { 164 PreparedStatement s = null; 165 ResultSet rs = null; 166 try { 167 s = connection.prepareStatement(statements.getFindDataStatement()); 168 s.setString(1, id); 169 rs = s.executeQuery(); 170 if (!rs.next()) { 171 return null; 172 } 173 return getBinaryData(rs, 1); 174 } 175 finally { 176 close(rs); 177 close(s); 178 } 179 } 180 181 public void doUpdateData(Connection connection, String id, byte[] data) throws SQLException , IOException { 182 PreparedStatement s = null; 183 try { 184 if( s == null ) { 185 s = connection.prepareStatement(statements.getUpdateDataStatement()); 186 } 187 s.setString(2, id); 188 setBinaryData(s, 1, data); 189 if ( s.executeUpdate() != 1 ) { 190 throw new SQLException ("Failed to update data"); 191 } 192 } finally { 193 close(s); 194 } 195 } 196 197 public void doRemoveData(Connection connection, String id) throws SQLException , IOException { 198 PreparedStatement s = null; 199 try { 200 s = connection.prepareStatement(statements.getRemoveDataStatement()); 201 s.setString(1, id); 202 if (s.executeUpdate() != 1) { 203 throw new SQLException ("Failed to remove data"); 204 } 205 } finally { 206 close(s); 207 } 208 } 209 210 static private void close(Statement s) { 211 try { 212 s.close(); 213 } catch (Throwable e) { 214 } 215 } 216 217 static private void close(ResultSet rs) { 218 try { 219 rs.close(); 220 } catch (Throwable e) { 221 } 222 } 223 224 public Statements getStatements() { 225 return statements; 226 } 227 228 public void setStatements(Statements statements) { 229 this.statements = statements; 230 } 231 232 public byte[][] doLoadData(Connection connection, String [] ids) throws SQLException , IOException { 233 PreparedStatement s = null; 234 byte[][] datas = new byte[ids.length][]; 235 try { 236 s = connection.prepareStatement(statements.getFindDataStatement()); 237 for (int i = 0; i < ids.length; i++) { 238 s.setString(1, ids[i]); 239 ResultSet rs = s.executeQuery(); 240 if (rs.next()) { 241 datas[i] = getBinaryData(rs, 1); 242 } 243 close(rs); 244 } 245 return datas; 246 } 247 finally { 248 close(s); 249 } 250 } 251 252 public void doRemoveData(Connection connection, String [] ids) throws SQLException , IOException { 253 PreparedStatement s = null; 254 try { 255 s = connection.prepareStatement(statements.getRemoveDataStatement()); 256 for (int i = 0; i < ids.length; i++) { 257 s.setString(1, ids[i]); 258 s.executeUpdate(); 259 } 260 } 261 finally { 262 close(s); 263 } 264 } 265 266 public int doGetCount(Connection connection) throws SQLException , IOException { 267 PreparedStatement s = null; 268 ResultSet rs = null; 269 try { 270 s = connection.prepareStatement(statements.getCountStatement()); 271 rs = s.executeQuery(); 272 rs.next(); 273 return rs.getInt(1); 274 } 275 finally { 276 close(rs); 277 close(s); 278 } 279 } 280 281 public String [] doGetIds(Connection connection) throws SQLException , IOException { 282 PreparedStatement s = null; 283 ResultSet rs = null; 284 try { 285 List ids = new ArrayList (); 286 s = connection.prepareStatement(statements.getFindAllIdsStatement()); 287 rs = s.executeQuery(); 288 while (!rs.next()) { 289 ids.add(rs.getString(1)); 290 } 291 return (String []) ids.toArray(new String [ids.size()]); 292 } 293 finally { 294 close(rs); 295 close(s); 296 } 297 } 298 299 public String [] doGetIds(Connection connection, int fromIndex, int toIndex) throws SQLException , IOException { 300 Statement s = null; 301 ResultSet rs = null; 302 try { 303 s = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 304 s.setFetchSize(toIndex - fromIndex); 305 rs = s.executeQuery(statements.getFindAllIdsStatement()); 306 rs.absolute(fromIndex + 1); 307 String [] ids = new String [toIndex - fromIndex]; 308 for (int row = 0; row < toIndex - fromIndex; row++) { 309 ids[row] = rs.getString(1); 310 if (!rs.next()) { 311 break; 312 } 313 } 314 return ids; 315 } 316 finally { 317 close(rs); 318 close(s); 319 } 320 } 321 322 } 323 | Popular Tags |