01: package com.db4o.f1.chapter1;
02:
03: import com.db4o.*;
04: import com.db4o.f1.*;
05: import com.db4o.query.*;
06:
07: public class NQExample extends Util {
08:
09: public static void main(String[] args) {
10: ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);
11: try {
12: storePilots(db);
13: retrieveComplexSODA(db);
14: retrieveComplexNQ(db);
15: retrieveArbitraryCodeNQ(db);
16: clearDatabase(db);
17: } finally {
18: db.close();
19: }
20: }
21:
22: public static void storePilots(ObjectContainer db) {
23: db.set(new Pilot("Michael Schumacher", 100));
24: db.set(new Pilot("Rubens Barrichello", 99));
25: }
26:
27: public static void retrieveComplexSODA(ObjectContainer db) {
28: Query query = db.query();
29: query.constrain(Pilot.class);
30: Query pointQuery = query.descend("points");
31: query.descend("name").constrain("Rubens Barrichello").or(
32: pointQuery.constrain(new Integer(99)).greater().and(
33: pointQuery.constrain(new Integer(199))
34: .smaller()));
35: ObjectSet result = query.execute();
36: listResult(result);
37: }
38:
39: public static void retrieveComplexNQ(ObjectContainer db) {
40: ObjectSet result = db.query(new Predicate() {
41: public boolean match(Pilot pilot) {
42: return pilot.getPoints() > 99
43: && pilot.getPoints() < 199
44: || pilot.getName().equals("Rubens Barrichello");
45: }
46: });
47: listResult(result);
48: }
49:
50: public static void retrieveArbitraryCodeNQ(ObjectContainer db) {
51: final int[] points = { 1, 100 };
52: ObjectSet result = db.query(new Predicate() {
53: public boolean match(Pilot pilot) {
54: for (int i = 0; i < points.length; i++) {
55: if (pilot.getPoints() == points[i]) {
56: return true;
57: }
58: }
59: return pilot.getName().startsWith("Rubens");
60: }
61: });
62: listResult(result);
63: }
64:
65: public static void clearDatabase(ObjectContainer db) {
66: ObjectSet result = db.get(Pilot.class);
67: while (result.hasNext()) {
68: db.delete(result.next());
69: }
70: }
71: }
|