1 package com.db4o.f1.chapter2; 2 3 import java.io.File ; 4 5 import com.db4o.Db4o; 6 import com.db4o.ObjectContainer; 7 import com.db4o.ObjectSet; 8 import com.db4o.f1.Util; 9 import com.db4o.query.Predicate; 10 import com.db4o.query.Query; 11 12 13 public class StructuredExample extends Util { 14 public static void main(String [] args) { 15 new File (Util.YAPFILENAME).delete(); 16 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 17 try { 18 storeFirstCar(db); 19 storeSecondCar(db); 20 retrieveAllCarsQBE(db); 21 retrieveAllPilotsQBE(db); 22 retrieveCarByPilotQBE(db); 23 retrieveCarByPilotNameQuery(db); 24 retrieveCarByPilotProtoQuery(db); 25 retrievePilotByCarModelQuery(db); 26 updateCar(db); 27 updatePilotSingleSession(db); 28 updatePilotSeparateSessionsPart1(db); 29 db.close(); 30 db=Db4o.openFile(Util.YAPFILENAME); 31 updatePilotSeparateSessionsPart2(db); 32 db.close(); 33 updatePilotSeparateSessionsImprovedPart1(); 34 db=Db4o.openFile(Util.YAPFILENAME); 35 updatePilotSeparateSessionsImprovedPart2(db); 36 db.close(); 37 db=Db4o.openFile(Util.YAPFILENAME); 38 updatePilotSeparateSessionsImprovedPart3(db); 39 deleteFlat(db); 40 db.close(); 41 deleteDeepPart1(); 42 db=Db4o.openFile(Util.YAPFILENAME); 43 deleteDeepPart2(db); 44 deleteDeepRevisited(db); 45 } 46 finally { 47 db.close(); 48 } 49 } 50 51 public static void storeFirstCar(ObjectContainer db) { 52 Car car1=new Car("Ferrari"); 53 Pilot pilot1=new Pilot("Michael Schumacher",100); 54 car1.setPilot(pilot1); 55 db.set(car1); 56 } 57 58 public static void storeSecondCar(ObjectContainer db) { 59 Pilot pilot2=new Pilot("Rubens Barrichello",99); 60 db.set(pilot2); 61 Car car2=new Car("BMW"); 62 car2.setPilot(pilot2); 63 db.set(car2); 64 } 65 66 public static void retrieveAllCarsQBE(ObjectContainer db) { 67 Car proto=new Car(null); 68 ObjectSet result=db.get(proto); 69 listResult(result); 70 } 71 72 public static void retrieveAllPilotsQBE(ObjectContainer db) { 73 Pilot proto=new Pilot(null,0); 74 ObjectSet result=db.get(proto); 75 listResult(result); 76 } 77 78 public static void retrieveAllPilots(ObjectContainer db) { 79 ObjectSet result=db.get(Pilot.class); 80 listResult(result); 81 } 82 83 public static void retrieveCarByPilotQBE( 84 ObjectContainer db) { 85 Pilot pilotproto=new Pilot("Rubens Barrichello",0); 86 Car carproto=new Car(null); 87 carproto.setPilot(pilotproto); 88 ObjectSet result=db.get(carproto); 89 listResult(result); 90 } 91 92 public static void retrieveCarByPilotNameQuery( 93 ObjectContainer db) { 94 Query query=db.query(); 95 query.constrain(Car.class); 96 query.descend("pilot").descend("name") 97 .constrain("Rubens Barrichello"); 98 ObjectSet result=query.execute(); 99 listResult(result); 100 } 101 102 public static void retrieveCarByPilotProtoQuery( 103 ObjectContainer db) { 104 Query query=db.query(); 105 query.constrain(Car.class); 106 Pilot proto=new Pilot("Rubens Barrichello",0); 107 query.descend("pilot").constrain(proto); 108 ObjectSet result=query.execute(); 109 listResult(result); 110 } 111 112 public static void retrievePilotByCarModelQuery(ObjectContainer db) { 113 Query carquery=db.query(); 114 carquery.constrain(Car.class); 115 carquery.descend("model").constrain("Ferrari"); 116 Query pilotquery=carquery.descend("pilot"); 117 ObjectSet result=pilotquery.execute(); 118 listResult(result); 119 } 120 121 public static void retrieveAllPilotsNative(ObjectContainer db) { 122 ObjectSet results = db.query(new Predicate() { 123 public boolean match(Pilot pilot){ 124 return true; 125 } 126 }); 127 listResult(results); 128 } 129 130 131 public static void retrieveAllCars(ObjectContainer db) { 132 ObjectSet results = db.get(Car.class); 133 listResult(results); 134 } 135 136 public static void retrieveCarsByPilotNameNative(ObjectContainer db) { 137 final String pilotName = "Rubens Barrichello"; 138 ObjectSet results = db.query(new Predicate() { 139 public boolean match(Car car){ 140 return car.getPilot().getName().equals(pilotName); 141 } 142 }); 143 listResult(results); 144 } 145 146 public static void updateCar(ObjectContainer db) { 147 ObjectSet result=db.query(new Predicate() { 148 public boolean match(Car car){ 149 return car.getModel().equals("Ferrari"); 150 } 151 }); 152 Car found=(Car)result.next(); 153 found.setPilot(new Pilot("Somebody else",0)); 154 db.set(found); 155 result=db.query(new Predicate() { 156 public boolean match(Car car){ 157 return car.getModel().equals("Ferrari"); 158 } 159 }); 160 listResult(result); 161 } 162 163 public static void updatePilotSingleSession( 164 ObjectContainer db) { 165 ObjectSet result=db.query(new Predicate() { 166 public boolean match(Car car){ 167 return car.getModel().equals("Ferrari"); 168 } 169 }); 170 Car found=(Car)result.next(); 171 found.getPilot().addPoints(1); 172 db.set(found); 173 result=db.query(new Predicate() { 174 public boolean match(Car car){ 175 return car.getModel().equals("Ferrari"); 176 } 177 }); 178 listResult(result); 179 } 180 181 public static void updatePilotSeparateSessionsPart1( 182 ObjectContainer db) { 183 ObjectSet result=db.query(new Predicate() { 184 public boolean match(Car car){ 185 return car.getModel().equals("Ferrari"); 186 } 187 }); 188 Car found=(Car)result.next(); 189 found.getPilot().addPoints(1); 190 db.set(found); 191 } 192 193 public static void updatePilotSeparateSessionsPart2( 194 ObjectContainer db) { 195 ObjectSet result=db.query(new Predicate() { 196 public boolean match(Car car){ 197 return car.getModel().equals("Ferrari"); 198 } 199 }); 200 listResult(result); 201 } 202 203 public static void updatePilotSeparateSessionsImprovedPart1() { 204 Db4o.configure().objectClass("com.db4o.f1.chapter2.Car") 205 .cascadeOnUpdate(true); 206 } 207 208 public static void updatePilotSeparateSessionsImprovedPart2( 209 ObjectContainer db) { 210 ObjectSet result=db.query(new Predicate() { 211 public boolean match(Car car){ 212 return car.getModel().equals("Ferrari"); 213 } 214 }); 215 Car found=(Car)result.next(); 216 found.getPilot().addPoints(1); 217 db.set(found); 218 } 219 220 public static void updatePilotSeparateSessionsImprovedPart3( 221 ObjectContainer db) { 222 ObjectSet result=db.query(new Predicate() { 223 public boolean match(Car car){ 224 return car.getModel().equals("Ferrari"); 225 } 226 }); 227 listResult(result); 228 } 229 230 public static void deleteFlat(ObjectContainer db) { 231 ObjectSet result=db.query(new Predicate() { 232 public boolean match(Car car){ 233 return car.getModel().equals("Ferrari"); 234 } 235 }); 236 Car found=(Car)result.next(); 237 db.delete(found); 238 result=db.get(new Car(null)); 239 listResult(result); 240 } 241 242 public static void deleteDeepPart1() { 243 Db4o.configure().objectClass("com.db4o.f1.chapter2.Car") 244 .cascadeOnDelete(true); 245 } 246 247 public static void deleteDeepPart2(ObjectContainer db) { 248 ObjectSet result=db.query(new Predicate() { 249 public boolean match(Car car){ 250 return car.getModel().equals("BMW"); 251 } 252 }); 253 Car found=(Car)result.next(); 254 db.delete(found); 255 result=db.query(new Predicate() { 256 public boolean match(Car car){ 257 return true; 258 } 259 }); 260 listResult(result); 261 } 262 263 public static void deleteDeepRevisited(ObjectContainer db) { 264 ObjectSet result=db.query(new Predicate() { 265 public boolean match(Pilot pilot){ 266 return pilot.getName().equals("Michael Schumacher"); 267 } 268 }); 269 if (!result.hasNext()) { 270 System.out.println("Pilot not found!"); 271 return; 272 } 273 Pilot pilot=(Pilot)result.next(); 274 Car car1=new Car("Ferrari"); 275 Car car2=new Car("BMW"); 276 car1.setPilot(pilot); 277 car2.setPilot(pilot); 278 db.set(car1); 279 db.set(car2); 280 db.delete(car2); 281 result=db.query(new Predicate() { 282 public boolean match(Car car){ 283 return true; 284 } 285 }); 286 listResult(result); 287 } 288 289 } 290 | Popular Tags |