001: package com.db4o.f1.chapter2;
002:
003: import java.io.File;
004:
005: import com.db4o.Db4o;
006: import com.db4o.ObjectContainer;
007: import com.db4o.ObjectSet;
008: import com.db4o.f1.Util;
009: import com.db4o.query.Predicate;
010: import com.db4o.query.Query;
011:
012: public class StructuredExample extends Util {
013: public static void main(String[] args) {
014: new File(Util.DB4OFILENAME).delete();
015: ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);
016: try {
017: storeFirstCar(db);
018: storeSecondCar(db);
019: retrieveAllCarsQBE(db);
020: retrieveAllPilotsQBE(db);
021: retrieveCarByPilotQBE(db);
022: retrieveCarByPilotNameQuery(db);
023: retrieveCarByPilotProtoQuery(db);
024: retrievePilotByCarModelQuery(db);
025: updateCar(db);
026: updatePilotSingleSession(db);
027: updatePilotSeparateSessionsPart1(db);
028: db.close();
029: db = Db4o.openFile(Util.DB4OFILENAME);
030: updatePilotSeparateSessionsPart2(db);
031: db.close();
032: updatePilotSeparateSessionsImprovedPart1();
033: db = Db4o.openFile(Util.DB4OFILENAME);
034: updatePilotSeparateSessionsImprovedPart2(db);
035: db.close();
036: db = Db4o.openFile(Util.DB4OFILENAME);
037: updatePilotSeparateSessionsImprovedPart3(db);
038: deleteFlat(db);
039: db.close();
040: deleteDeepPart1();
041: db = Db4o.openFile(Util.DB4OFILENAME);
042: deleteDeepPart2(db);
043: deleteDeepRevisited(db);
044: } finally {
045: db.close();
046: }
047: }
048:
049: public static void storeFirstCar(ObjectContainer db) {
050: Car car1 = new Car("Ferrari");
051: Pilot pilot1 = new Pilot("Michael Schumacher", 100);
052: car1.setPilot(pilot1);
053: db.set(car1);
054: }
055:
056: public static void storeSecondCar(ObjectContainer db) {
057: Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
058: db.set(pilot2);
059: Car car2 = new Car("BMW");
060: car2.setPilot(pilot2);
061: db.set(car2);
062: }
063:
064: public static void retrieveAllCarsQBE(ObjectContainer db) {
065: Car proto = new Car(null);
066: ObjectSet result = db.get(proto);
067: listResult(result);
068: }
069:
070: public static void retrieveAllPilotsQBE(ObjectContainer db) {
071: Pilot proto = new Pilot(null, 0);
072: ObjectSet result = db.get(proto);
073: listResult(result);
074: }
075:
076: public static void retrieveAllPilots(ObjectContainer db) {
077: ObjectSet result = db.get(Pilot.class);
078: listResult(result);
079: }
080:
081: public static void retrieveCarByPilotQBE(ObjectContainer db) {
082: Pilot pilotproto = new Pilot("Rubens Barrichello", 0);
083: Car carproto = new Car(null);
084: carproto.setPilot(pilotproto);
085: ObjectSet result = db.get(carproto);
086: listResult(result);
087: }
088:
089: public static void retrieveCarByPilotNameQuery(ObjectContainer db) {
090: Query query = db.query();
091: query.constrain(Car.class);
092: query.descend("pilot").descend("name").constrain(
093: "Rubens Barrichello");
094: ObjectSet result = query.execute();
095: listResult(result);
096: }
097:
098: public static void retrieveCarByPilotProtoQuery(ObjectContainer db) {
099: Query query = db.query();
100: query.constrain(Car.class);
101: Pilot proto = new Pilot("Rubens Barrichello", 0);
102: query.descend("pilot").constrain(proto);
103: ObjectSet result = query.execute();
104: listResult(result);
105: }
106:
107: public static void retrievePilotByCarModelQuery(ObjectContainer db) {
108: Query carquery = db.query();
109: carquery.constrain(Car.class);
110: carquery.descend("model").constrain("Ferrari");
111: Query pilotquery = carquery.descend("pilot");
112: ObjectSet result = pilotquery.execute();
113: listResult(result);
114: }
115:
116: public static void retrieveAllPilotsNative(ObjectContainer db) {
117: ObjectSet results = db.query(new Predicate() {
118: public boolean match(Pilot pilot) {
119: return true;
120: }
121: });
122: listResult(results);
123: }
124:
125: public static void retrieveAllCars(ObjectContainer db) {
126: ObjectSet results = db.get(Car.class);
127: listResult(results);
128: }
129:
130: public static void retrieveCarsByPilotNameNative(ObjectContainer db) {
131: final String pilotName = "Rubens Barrichello";
132: ObjectSet results = db.query(new Predicate() {
133: public boolean match(Car car) {
134: return car.getPilot().getName().equals(pilotName);
135: }
136: });
137: listResult(results);
138: }
139:
140: public static void updateCar(ObjectContainer db) {
141: ObjectSet result = db.query(new Predicate() {
142: public boolean match(Car car) {
143: return car.getModel().equals("Ferrari");
144: }
145: });
146: Car found = (Car) result.next();
147: found.setPilot(new Pilot("Somebody else", 0));
148: db.set(found);
149: result = db.query(new Predicate() {
150: public boolean match(Car car) {
151: return car.getModel().equals("Ferrari");
152: }
153: });
154: listResult(result);
155: }
156:
157: public static void updatePilotSingleSession(ObjectContainer db) {
158: ObjectSet result = db.query(new Predicate() {
159: public boolean match(Car car) {
160: return car.getModel().equals("Ferrari");
161: }
162: });
163: Car found = (Car) result.next();
164: found.getPilot().addPoints(1);
165: db.set(found);
166: result = db.query(new Predicate() {
167: public boolean match(Car car) {
168: return car.getModel().equals("Ferrari");
169: }
170: });
171: listResult(result);
172: }
173:
174: public static void updatePilotSeparateSessionsPart1(
175: ObjectContainer db) {
176: ObjectSet result = db.query(new Predicate() {
177: public boolean match(Car car) {
178: return car.getModel().equals("Ferrari");
179: }
180: });
181: Car found = (Car) result.next();
182: found.getPilot().addPoints(1);
183: db.set(found);
184: }
185:
186: public static void updatePilotSeparateSessionsPart2(
187: ObjectContainer db) {
188: ObjectSet result = db.query(new Predicate() {
189: public boolean match(Car car) {
190: return car.getModel().equals("Ferrari");
191: }
192: });
193: listResult(result);
194: }
195:
196: public static void updatePilotSeparateSessionsImprovedPart1() {
197: Db4o.configure().objectClass("com.db4o.f1.chapter2.Car")
198: .cascadeOnUpdate(true);
199: }
200:
201: public static void updatePilotSeparateSessionsImprovedPart2(
202: ObjectContainer db) {
203: ObjectSet result = db.query(new Predicate() {
204: public boolean match(Car car) {
205: return car.getModel().equals("Ferrari");
206: }
207: });
208: Car found = (Car) result.next();
209: found.getPilot().addPoints(1);
210: db.set(found);
211: }
212:
213: public static void updatePilotSeparateSessionsImprovedPart3(
214: ObjectContainer db) {
215: ObjectSet result = db.query(new Predicate() {
216: public boolean match(Car car) {
217: return car.getModel().equals("Ferrari");
218: }
219: });
220: listResult(result);
221: }
222:
223: public static void deleteFlat(ObjectContainer db) {
224: ObjectSet result = db.query(new Predicate() {
225: public boolean match(Car car) {
226: return car.getModel().equals("Ferrari");
227: }
228: });
229: Car found = (Car) result.next();
230: db.delete(found);
231: result = db.get(new Car(null));
232: listResult(result);
233: }
234:
235: public static void deleteDeepPart1() {
236: Db4o.configure().objectClass("com.db4o.f1.chapter2.Car")
237: .cascadeOnDelete(true);
238: }
239:
240: public static void deleteDeepPart2(ObjectContainer db) {
241: ObjectSet result = db.query(new Predicate() {
242: public boolean match(Car car) {
243: return car.getModel().equals("BMW");
244: }
245: });
246: Car found = (Car) result.next();
247: db.delete(found);
248: result = db.query(new Predicate() {
249: public boolean match(Car car) {
250: return true;
251: }
252: });
253: listResult(result);
254: }
255:
256: public static void deleteDeepRevisited(ObjectContainer db) {
257: ObjectSet result = db.query(new Predicate() {
258: public boolean match(Pilot pilot) {
259: return pilot.getName().equals("Michael Schumacher");
260: }
261: });
262: if (!result.hasNext()) {
263: System.out.println("Pilot not found!");
264: return;
265: }
266: Pilot pilot = (Pilot) result.next();
267: Car car1 = new Car("Ferrari");
268: Car car2 = new Car("BMW");
269: car1.setPilot(pilot);
270: car2.setPilot(pilot);
271: db.set(car1);
272: db.set(car2);
273: db.delete(car2);
274: result = db.query(new Predicate() {
275: public boolean match(Car car) {
276: return true;
277: }
278: });
279: listResult(result);
280: }
281:
282: }
|