go4it

just do it

DB4O学习(五)--SODA

1.SODA Query Graphs

   ----a graph data structure

image

查找所有name为“Lincoln”的Person

 

2.SODA关键字

(1)查询关键字

image

(2)约束关键字

image

 

3.查询示例

(1)查询全部

db = Db4o.openFile("customer.yap");
 Query query=db.query();
query.constrain(Customer.class);
List<Customer> rs=query.execute();
 for(Customer c:rs){
 System.out.println(c.getName());
 }
 
(2)条件查询
Query query=db.query();
query.constrain(Customer.class);//类级别的约束
query.descend("name").constrain("customer2");//字段级别的约束
List<Customer> rs=query.execute();

 

(3)使用关键字

db = Db4o.openFile("customer.yap");
Query query=db.query();
query.constrain(Customer.class);
query.descend("name").constrain("customer2").not();
List<Customer> rs=query.execute();
 
(4)复合条件
 
and的用法:
Query query=db.query();
query.constrain(Customer.class);
Constraint firstConstr=query.descend("phoneNumber").constrain("phone2");
query.descend("name").constrain("customer2").and(firstConstr);
List<Customer> rs=query.execute();
等价于:
Query query=db.query();
query.constrain(Customer.class);
query.descend("name").constrain("customer2");
query.descend("phoneNumber").constrain("phone2");
List<Customer> rs=query.execute();
Query query=db.query();
 
or的用法:
query.constrain(Customer.class);
Constraint firstConstr=query.descend("phoneNumber").constrain("phone3");
query.descend("name").constrain("customer2").or(firstConstr);
List<Customer> rs=query.execute();
 
(5)greater/smaller
Query query=db.query();
query.constrain(Person.class);
query.descend("_age").constrain(80).greater();
ObjectSet result=query.execute();

 

(6)范围查询

Query query=db.query();
query.constrain(Person.class);
Constraint firstConstr = query.descend("_age").constrain(60).greater();
query.descend("_age").constrain(80).smaller().and(firstConstr);

 

(7)模糊查询

Query query=db.query();
query.constrain(Person.class);
query.descend("_name").constrain("Ma").like(); // also works with "ma"
ObjectSet result= query.execute();

 

image

image

(8)查询空值

Query query=db.query();
query.constrain(Person.class);
query.descend("age").constrain(0); // field has been set
ObjectSet result=query.execute();

 

(9)结果排序

// JAVA
Query query=db.query();
query.constrain(Person.class);
query.descend("_name").orderAscending(); // the list should start with the lowest
ObjectSet result=query.execute();

posted on 2009-02-19 22:56  cxccbv  阅读(1065)  评论(0编辑  收藏  举报

导航