属性,条件和条件组

DBFlow,通过Java注释处理,生成一个_Table表示你的 Model。生成的每个领域都是子类的IProperty。每个的IProperty代表在相应表中的列,并提供了一个类型安全的条件操作把它变成SQLCondition或变异成另一种属性。

SQLCondition是一个代表SQL语句中的条件语句的接口。这是一个接口,这样其他类型的条件可以使用,并且允许最大的灵活性,以满足您的需求。

例如,写在原始的SQLite:

2. `name` = 'Test'

4. `name` = `SomeTable`.`Test`

6. `name` LIKE '%Test%'

8. `name` != 'Test'

10. `salary` BETWEEN 15000 AND 40000

12. `name` IN('Test','Test2','TestN')

14. ((`name`='Test' AND `rank`=6) OR (`name`='Bob' AND `rank`=8))

如何使用条件

建议我们的查询从Model的属性中创造条件。

我们有一个简单的表:

1. @Table(database = TestDatabase.class)
2. public class TestModel3 {

4. @PrimaryKey
5. String name;

7. @Column
8. String type;
9. }

有了这个定义, DBFlow将帮我们生产一个TestModel3_Table类:

2. public final class TestModel3_Table {
3. public static final PropertyConverter PROPERTY_CONVERTER = new PropertyConverter(){
4. public IProperty fromName(String columnName) {
5. return com.raizlabs.android.dbflow.test.sql.TestModel3_Table.getProperty(columnName);
6. }
7. };

9. public static final Property<String> type = new Property<String>(TestModel3.class, "type");

11. public static final Property<String> name = new Property<String>(TestModel3.class, "name");

13. public static final IProperty[] getAllColumnProperties() {
14. return new IProperty[]{type,name};
15. }

17. public static BaseProperty getProperty(String columnName) {
18. columnName = QueryBuilder.quoteIfNeeded(columnName);
19. switch (columnName)  {
20. case "`type`":  {
21. return type;
22. }
23. case "`name`":  {
24. return name;
25. }
26. default:  {
27. throw new IllegalArgumentException("Invalid column name passed. Ensure you are calling the correct table's column");
28. }
29. }
30. }
31. }

从生成的类文件中使用的字段,我们现在可以在我们的查询中使用属性生成SQLCondition:

2. TestModel3_Table.name.is("Test"); // `name` = 'Test'
3. TestModel3_Table.name.withTable().is("Test"); // `TestModel3`.`name` = 'Test'
4. TestModel3_Table.name.like("%Test%");

6. // `name`=`AnotherTable`.`name`
7. TestModel3_Table.name.eq(AnotherTable_Table.name);

A whole set of SQLCondition operations are supported for Property generated for a Table including:

  1. is(), eq() -> =
  2. isNot(), notEq() -> !=
  3. isNull() -> IS NULL / isNotNull()IS NOT NULL
  4. like(), glob()
  5. greaterThan(), greaterThanOrEqual(), lessThan(), lessThanOrEqual()
  6. between() -> BETWEEN
  7. in(), notIn()

此外,我们可以做加法和减法:

2. SomeTable_Table.latitude.plus(SomeTable_Table.longitude).lessThan(45.0); // `latitude` + `longitude` < 45.0

4. SomeTable_Table.latitude.minus(SomeTable_Table.longitude).greaterThan(45.0); // `latitude` - `longitude` > 45.0

条件组

该ConditionGroup是ConditionQueryBuilder的继任者。这是有缺陷的,就是它符合的QueryBuilder,还包含 Condition,以及所需要的类型的参数都需要属于它自己的表。

ConditionGroup是任意集合 SQLCondition,可以组合成一个单一的条件,SELECT projection,或者被用作SQLCondition在另一个ConditionGroup内。

这用于包装查询语句,支持其他各种查询和类。

2. SQLite.select()
3. .from(MyTable.class)
4. .where(MyTable_Table.someColumn.is("SomeValue"))
5. .and(MyTable_Table.anotherColumn.is("ThisValue"));

7. // SELECT * FROM `MyTable` WHERE `someColumn`='OneValue' OR (`someColumn`='SomeValue' AND `anotherColumn`='ThisValue')
8. SQLite.select()
9. .from(MyTable.class)
10. .where(MyTable.someColumn.is("OneValue"))
11. .or(ConditionGroup.clause()
12. .and(MyTable_Table.someColumn.is("SomeValue")
13. .AND(MyTable_Table.anotherColumn.is("ThisValue"));