001: package com.db4o.f1.chapter1;
002:
003: import com.db4o.Db4o;
004: import com.db4o.ObjectContainer;
005: import com.db4o.ObjectSet;
006: import com.db4o.f1.Util;
007: import com.db4o.query.Constraint;
008: import com.db4o.query.Query;
009:
010: public class QueryExample extends Util {
011: public static void main(String[] args) {
012: ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);
013: try {
014: storeFirstPilot(db);
015: storeSecondPilot(db);
016: retrieveAllPilots(db);
017: retrievePilotByName(db);
018: retrievePilotByExactPoints(db);
019: retrieveByNegation(db);
020: retrieveByConjunction(db);
021: retrieveByDisjunction(db);
022: retrieveByComparison(db);
023: retrieveByDefaultFieldValue(db);
024: retrieveSorted(db);
025: clearDatabase(db);
026: } finally {
027: db.close();
028: }
029: }
030:
031: public static void storeFirstPilot(ObjectContainer db) {
032: Pilot pilot1 = new Pilot("Michael Schumacher", 100);
033: db.set(pilot1);
034: System.out.println("Stored " + pilot1);
035: }
036:
037: public static void storeSecondPilot(ObjectContainer db) {
038: Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
039: db.set(pilot2);
040: System.out.println("Stored " + pilot2);
041: }
042:
043: public static void retrieveAllPilots(ObjectContainer db) {
044: Query query = db.query();
045: query.constrain(Pilot.class);
046: ObjectSet result = query.execute();
047: listResult(result);
048: }
049:
050: public static void retrievePilotByName(ObjectContainer db) {
051: Query query = db.query();
052: query.constrain(Pilot.class);
053: query.descend("name").constrain("Michael Schumacher");
054: ObjectSet result = query.execute();
055: listResult(result);
056: }
057:
058: public static void retrievePilotByExactPoints(ObjectContainer db) {
059: Query query = db.query();
060: query.constrain(Pilot.class);
061: query.descend("points").constrain(new Integer(100));
062: ObjectSet result = query.execute();
063: listResult(result);
064: }
065:
066: public static void retrieveByNegation(ObjectContainer db) {
067: Query query = db.query();
068: query.constrain(Pilot.class);
069: query.descend("name").constrain("Michael Schumacher").not();
070: ObjectSet result = query.execute();
071: listResult(result);
072: }
073:
074: public static void retrieveByConjunction(ObjectContainer db) {
075: Query query = db.query();
076: query.constrain(Pilot.class);
077: Constraint constr = query.descend("name").constrain(
078: "Michael Schumacher");
079: query.descend("points").constrain(new Integer(99)).and(constr);
080: ObjectSet result = query.execute();
081: listResult(result);
082: }
083:
084: public static void retrieveByDisjunction(ObjectContainer db) {
085: Query query = db.query();
086: query.constrain(Pilot.class);
087: Constraint constr = query.descend("name").constrain(
088: "Michael Schumacher");
089: query.descend("points").constrain(new Integer(99)).or(constr);
090: ObjectSet result = query.execute();
091: listResult(result);
092: }
093:
094: public static void retrieveByComparison(ObjectContainer db) {
095: Query query = db.query();
096: query.constrain(Pilot.class);
097: query.descend("points").constrain(new Integer(99)).greater();
098: ObjectSet result = query.execute();
099: listResult(result);
100: }
101:
102: public static void retrieveByDefaultFieldValue(ObjectContainer db) {
103: Pilot somebody = new Pilot("Somebody else", 0);
104: db.set(somebody);
105: Query query = db.query();
106: query.constrain(Pilot.class);
107: query.descend("points").constrain(new Integer(0));
108: ObjectSet result = query.execute();
109: listResult(result);
110: db.delete(somebody);
111: }
112:
113: public static void retrieveSorted(ObjectContainer db) {
114: Query query = db.query();
115: query.constrain(Pilot.class);
116: query.descend("name").orderAscending();
117: ObjectSet result = query.execute();
118: listResult(result);
119: query.descend("name").orderDescending();
120: result = query.execute();
121: listResult(result);
122: }
123:
124: public static void clearDatabase(ObjectContainer db) {
125: ObjectSet result = db.get(Pilot.class);
126: while (result.hasNext()) {
127: db.delete(result.next());
128: }
129: }
130: }
|