| 1 package com.db4o.f1.chapter1; 2 3 import com.db4o.*; 4 import com.db4o.f1.*; 5 import com.db4o.query.*; 6 7 public class NQExample extends Util { 8 9 public static void main(String [] args) { 10 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 11 try { 12 storePilots(db); 13 retrieveComplexSODA(db); 14 retrieveComplexNQ(db); 15 retrieveArbitraryCodeNQ(db); 16 clearDatabase(db); 17 } 18 finally { 19 db.close(); 20 } 21 } 22 23 public static void storePilots(ObjectContainer db) { 24 db.set(new Pilot("Michael Schumacher",100)); 25 db.set(new Pilot("Rubens Barrichello",99)); 26 } 27 28 public static void retrieveComplexSODA(ObjectContainer db) { 29 Query query=db.query(); 30 query.constrain(Pilot.class); 31 Query pointQuery=query.descend("points"); 32 query.descend("name").constrain("Rubens Barrichello") 33 .or(pointQuery.constrain(new Integer (99)).greater() 34 .and(pointQuery.constrain(new Integer (199)).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 } 72 | Popular Tags |