1 21 package com.db4o.db4ounit.common.querying; 22 23 24 import java.lang.reflect.Field ; 25 26 import com.db4o.ObjectSet; 27 import com.db4o.config.*; 28 import com.db4o.query.*; 29 30 import db4ounit.Assert; 31 import db4ounit.extensions.AbstractDb4oTestCase; 32 33 36 public class MultiFieldIndexQueryTestCase extends AbstractDb4oTestCase { 37 38 public static void main(String [] args) { 39 new MultiFieldIndexQueryTestCase().runSolo(); 40 } 41 42 public static class Book { 43 44 public Person[] authors; 45 public String title; 46 47 public Book(){} 48 49 public Book(String title, Person[] authors){ 50 this.title = title; 51 this.authors = authors; 52 } 53 54 public String toString(){ 55 String ret = title; 56 if(authors != null){ 57 for (int i = 0; i < authors.length; i++) { 58 ret += "\n " + authors[i].toString(); 59 } 60 } 61 return ret; 62 } 63 } 64 65 public static class Person { 66 67 public String firstName; 68 public String lastName; 69 70 public Person(){} 71 72 public Person(String firstName, String lastName){ 73 this.firstName = firstName; 74 this.lastName = lastName; 75 } 76 77 public String toString(){ 78 return "Person " + firstName + " " + lastName; 79 } 80 } 81 82 protected void configure(Configuration config) { 83 indexAllFields(config,Book.class); 84 indexAllFields(config,Person.class); 85 } 86 87 protected void indexAllFields(Configuration config,Class clazz) { 88 final Field [] fields = clazz.getDeclaredFields(); 89 for (int i = 0; i < fields.length; i++) { 90 indexField(config,clazz, fields[i].getName()); 91 } 92 final Class superclass = clazz.getSuperclass(); 93 if (superclass != null) { 94 indexAllFields(config,superclass); 95 } 96 } 97 98 protected void store() throws Exception { 99 Person aaron = new Person("Aaron", "OneOK"); 100 Person bill = new Person("Bill", "TwoOK"); 101 Person chris = new Person("Chris", "ThreeOK"); 102 Person dave = new Person("Dave", "FourOK"); 103 Person neil = new Person("Neil", "Notwanted"); 104 Person nat = new Person("Nat", "Neverwanted"); 105 db().set(new Book("Persistence possibilities", new Person[] { aaron, 106 bill, chris })); 107 db().set(new Book("Persistence using S.O.D.A.", 108 new Person[] { aaron })); 109 db().set(new Book("Persistence using JDO", 110 new Person[] { bill, dave })); 111 db().set(new Book("Don't want to find Phil", new Person[] { aaron, 112 bill, neil })); 113 db().set(new Book("Persistence by Jeff", new Person[] { nat })); 114 } 115 116 public void test() { 117 Query qBooks = newQuery(); 118 qBooks.constrain(Book.class); 119 qBooks.descend("title").constrain("Persistence").like(); 120 Query qAuthors = qBooks.descend("authors"); 121 Query qFirstName = qAuthors.descend("firstName"); 122 Query qLastName = qAuthors.descend("lastName"); 123 Constraint cAaron = qFirstName.constrain("Aaron").and( 124 qLastName.constrain("OneOK")); 125 Constraint cBill = qFirstName.constrain("Bill").and( 126 qLastName.constrain("TwoOK")); 127 cAaron.or(cBill); 128 ObjectSet results = qAuthors.execute(); 129 Assert.areEqual(4, results.size()); 130 while (results.hasNext()) { 131 Person person = (Person) results.next(); 132 Assert.isTrue(person.lastName.endsWith("OK")); 133 } 134 } 135 136 } 137 | Popular Tags |