1 17 18 package org.objectweb.jac.aspects.distrans.persistence; 19 20 import org.objectweb.jac.core.NameRepository; 21 import org.objectweb.jac.core.rtti.ClassItem; 22 import org.objectweb.jac.core.rtti.ClassRepository; 23 import org.objectweb.jac.core.rtti.FieldItem; 24 import org.objectweb.jac.util.Repository; 25 26 import java.sql.Connection ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.sql.Statement ; 31 import java.util.Collection ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 36 import javax.transaction.HeuristicMixedException ; 37 import javax.transaction.HeuristicRollbackException ; 38 import javax.transaction.NotSupportedException ; 39 import javax.transaction.RollbackException ; 40 import javax.transaction.SystemException ; 41 42 import org.enhydra.jdbc.standard.StandardXADataSource; 43 44 55 public class SimpleDbPersistence implements PersistenceItf { 56 57 61 final static private String objectAttributeName = "_objname"; 62 63 64 76 public void initStorageIfNeeded( String className, StandardXADataSource ds ) { 77 try { 78 _initStorageIfNeeded(className,ds); 79 } catch (SQLException e) { 80 e.printStackTrace(); 81 System.exit(1); 82 } 83 } 84 85 private void _initStorageIfNeeded( String className, StandardXADataSource ds ) 86 throws SQLException { 87 88 93 Connection connection = XAPoolCache.getConnection(ds); 94 95 String tableName = className.replace('.','_'); 96 Statement st = connection.createStatement(); 97 try { 98 st.executeQuery("SELECT * FROM "+tableName); 99 } 100 catch (SQLException sqle) { 101 105 initStorage(className,connection); 106 } 107 } 108 109 110 118 public void initStorage( String className, StandardXADataSource ds ) { 119 try { 120 _initStorage(className,ds); 121 } catch (SQLException e) { 122 e.printStackTrace(); 123 System.exit(1); 124 } 125 } 126 127 private void _initStorage( String className, StandardXADataSource ds ) 128 throws SQLException { 129 130 133 Connection connection = XAPoolCache.getConnection(ds); 134 135 139 String tableName = className.replace('.','_'); 140 Statement st = connection.createStatement(); 141 try { 142 st.executeQuery("DROP TABLE "+tableName); 143 } 144 catch (SQLException sqle) { 145 149 } 150 151 initStorage(className,connection); 152 } 153 154 155 161 private void initStorage( String className, Connection connection ) 162 throws SQLException { 163 164 168 String tableName = className.replace('.','_'); 169 170 String sql = 171 "CREATE TABLE "+tableName+" ( "+ 172 objectAttributeName+" VARCHAR PRIMARY KEY"; 173 174 ClassItem classItem = classes.getClass(className); 175 Collection fields = classItem.getAllFields(); 176 for (Iterator iter = fields.iterator(); iter.hasNext();) { 177 FieldItem field = (FieldItem) iter.next(); 178 String fieldName = field.getName(); 179 String fieldType = field.getType().getName(); 180 String sqlType = (String ) SimpleDbPersistence.javaTypesToSQL.get(fieldType); 181 sql += ", "+fieldName+" "+sqlType; 182 } 183 sql += ");"; 184 185 Statement st = connection.createStatement(); 186 st.executeUpdate(sql); 187 st.close(); 188 } 189 190 195 private static Map javaTypesToSQL = new HashMap (); 196 static { 197 javaTypesToSQL.put("java.lang.String","VARCHAR"); 198 javaTypesToSQL.put("int","INT"); 199 javaTypesToSQL.put("double","DOUBLE PRECISION"); 200 } 201 202 203 211 public void load( Object wrappee, String name, StandardXADataSource ds ) 212 throws SQLException , IllegalArgumentException , IllegalAccessException , NotSupportedException , SecurityException , IllegalStateException , RollbackException , HeuristicMixedException , HeuristicRollbackException , SystemException { 213 214 ClassItem classItem = classes.getClass(wrappee); 215 216 220 Connection connection = XAPoolCache.getConnection(ds); 221 222 PreparedStatement ps = 223 createSelectStatement(connection,classItem); 224 ps.setString(1,name); 225 ResultSet rs = ps.executeQuery(); 226 227 233 boolean found = rs.next(); 234 if (!found) return; 235 236 240 Collection fields = classItem.getAllFields(); 241 int i=2; 242 for (Iterator iter = fields.iterator(); iter.hasNext();i++) { 243 FieldItem field = (FieldItem) iter.next(); 244 Object value = rs.getObject(i); 245 field.set(wrappee,value); 246 } 247 rs.close(); 248 } 249 250 251 260 public void store( Object wrappee, String name, StandardXADataSource ds ) 261 throws Exception { 262 263 ClassItem classItem = classes.getClass(wrappee); 264 265 270 Connection connection = XAPoolCache.getConnection(ds); 271 272 PreparedStatement ps = 273 createUpdateStatement(connection,classItem); 274 275 Collection fields = classItem.getAllFields(); 276 int i=1; 277 for (Iterator iter = fields.iterator(); iter.hasNext();i++) { 278 FieldItem field = (FieldItem) iter.next(); 279 Object value = field.get(wrappee); 280 ps.setObject(i,value); 281 } 282 ps.setString(i,name); 283 284 288 int rowCount = ps.executeUpdate(); 289 290 if ( rowCount == 0 ) { 291 PreparedStatement insertps = 292 createInsertStatement( 293 connection,classItem,wrappee,name 294 ); 295 insertps.executeUpdate(); 296 insertps.close(); 297 ps.executeUpdate(); 298 } 299 } 300 301 313 private static PreparedStatement createInsertStatement( 314 Connection connection, ClassItem cl, 315 Object wrappee, String wrappeeName ) throws SQLException { 316 317 321 String tableName = cl.getName(); 322 tableName = tableName.replace('.','_'); 323 String request = "INSERT INTO "+tableName+" VALUES (?"; 324 325 Collection fields = cl.getAllFields(); 326 for (Iterator iter = fields.iterator(); iter.hasNext();) { 327 iter.next(); 328 request += ",?"; 329 } 330 331 request += ");"; 332 333 PreparedStatement ps = connection.prepareStatement(request); 334 335 ps.setString(1,wrappeeName); 336 int i=2; 337 for (Iterator iter = fields.iterator(); iter.hasNext();i++) { 338 iter.next(); 339 ps.setObject(i,null); 340 } 341 342 return ps; 343 } 344 345 355 static private PreparedStatement createSelectStatement( 356 Connection connection, ClassItem cl ) throws SQLException { 357 358 362 String tableName = cl.getName(); 363 tableName = tableName.replace('.','_'); 364 String request = 365 "SELECT * FROM "+tableName+" WHERE "+ 366 objectAttributeName+"=?;"; 367 368 return connection.prepareStatement(request); 369 } 370 371 381 static private PreparedStatement createUpdateStatement( 382 Connection connection, ClassItem cl ) throws SQLException { 383 384 388 String tableName = cl.getName(); 389 tableName = tableName.replace('.','_'); 390 String request = "UPDATE "+tableName+" SET "; 391 392 Collection fields = cl.getAllFields(); 393 boolean first = true; 394 for (Iterator iter = fields.iterator(); iter.hasNext();) { 395 FieldItem field = (FieldItem) iter.next(); 396 String name = field.getName(); 397 398 if (first) first = false; 399 else request += ","; 400 request += name + "=?"; 401 } 402 403 request += " WHERE "+objectAttributeName+"=?;"; 404 405 return connection.prepareStatement(request); 406 } 407 408 411 private ClassRepository classes = ClassRepository.get(); 412 413 417 private Repository names = NameRepository.get(); 418 } 419 | Popular Tags |