requery

介绍:

一个轻量但是强大的编译时ORM与SQL查询生成工具,支持RxJava和Java 8。

运行效果:

使用说明:

定义实体:

@Entity
abstract class AbstractPerson {
    @Key @Generated
    int id;
    @Index(name = "name_index")              // table specification
    String name;
    @OneToMany                               // relationships 1:1, 1:many, many to many
    Set<Phone> phoneNumbers;
    @Converter(EmailToStringConverter.class) // custom type conversion
    Email email;
    @PostLoad                                // lifecycle callbacks
    void afterLoad() {
        updatePeopleList();
    }
    // getter, setters, equals & hashCode automatically generated into Person.java
}

或者从一个interface:

@Entity
public interface Person {
    @Key @Generated
    int getId();
    String getName();
    @OneToMany
    Set<Phone> getPhoneNumbers();
    String getEmail();
}

查询:

List<Person> query = data
    .select(Person.class)
    .where(Person.NAME.lower().like("b%"))
    .orderBy(Person.AGE.desc())
    .limit(5)
    .get().list();

Relationships: rather than collections such as sets, and lists which have to be materialized with all the results, you can use query results directly in side an entity: (sets and lists are supported to)

@Entity
abstract class AbstractPerson {
    @Key @Generated
    int id;
    @ManyToMany
    Result<Group> groups;
    // equivalent to:
    // data.select(Group.class)
    // .join(Group_Person.class).on(Group_ID.equal(Group_Person.GROUP_ID))
    // .join(Person.class).on(Group_Person.PERSON_ID.equal(Person.ID))
    // .where(Person.ID.equal(id))
}

Java 8 streams:

data.select(Person.class)
    .orderBy(Person.AGE.desc())
    .get()
    .stream().forEach(System.out::println);
Java 8 optional and time support:
public interface Person {
    @Key @Generated
    int getId();
    String getName();
    Optional<String> getEmail();
    ZonedDateTime getBirthday();
}

RxJava Observables:

Observable<Person> observable = data
    .select(Person.class)
    .orderBy(Person.AGE.desc())
    .get()
    .toObservable();

当表发生变化时的RxJava observe query:

Observable<Person> observable = data
    .select(Person.class)
    .orderBy(Person.AGE.desc())
    .get()
    .toSelfObservable().subscribe(::updateFromResult);

可选的读写分离。如果你喜欢把读和写分离,请把entity标记为@ReadOnly,并使用update statements去修改数据。

int rows = data.update(Person.class)
    .set(Person.ABOUT, "nothing")
    .set(Person.AGE, 50)
    .where(Person.AGE.equal(100)).get();

特点

  • 没有反射

  • 快速启动

  • 类型化查询语言

  • 生成表

  • 支持JDBC和许多流行的数据库

  • 支持安卓 (SQLite, RecyclerView, Databinding)

  • 支持RxJava

  • API阻塞与非阻塞API

  • Partial objects/refresh

  • 缓存

  • 生命周期回调

  • 自定义的类型转换

  • JPA annotations (however requery is not a JPA provider)

无反射

requery使用编译时注解去生成实体对象类。在安卓上这意味着你从查询中读取对象的性能和使用标准的Cursor和ContentValues API读取是一样的。

Type safe query

The compiled classes work with the query API to take advantage of compile time generated attributes. Create type safe queries and avoid hard to maintain, error prone string concatenated queries.

关系

使用注解,你可以在model中定义一对一,一对多,多对一,多对多的关系。

Android

设计的时候专门考虑了对安卓的支持。

与其它类似的安卓库的比较:

Feature

requery

ORMLite

Squidb

DBFlow

GreenDao

Relational mapping

Y

Y(1)

N

Y(1)

Y(1)

Inverse relationships

Y

N

N

N

N

Compile time

Y

N

Y

Y

Y(2)

JDBC Support

Y

Y

N

N

N

query language

Y

N

Y(3)

Y(3)

Y(3)

Table Generation

Y

Y

Y

Y

Y

JPA annotations

Y

Y

N

N

N

  1. Excludes Many-to-Many 2) Not annotation based 3) Builder only

See requery-android/example for an example Android project using databinding and interface based entities. For more information see the wiki page.

Code generation

Generate entities from Abstract or Interface classes. Use JPA annotations or requery annotations. requery will generate getter/setters, equals() and hashcode() when needed.

Supported Databases

Tested on some of the most popular databases:

  • PostgresSQL (9.1+)

  • MySQL 5.x

  • Oracle 12c+

  • Microsoft SQL Server 2012 or later

  • SQLite (Android or with xerial JDBC driver)

  • Apache Derby 10.11+

  • H2 1.4+

  • HSQLDB 2.3+

JPA Annotations

A subset of the JPA annotations that map onto the requery annotations are supported. See here for more information.

获取

目前的SNAPSHOT版本可在 http://oss.jfrog.org上得到。

repositories {
    jcenter()
    maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
}
dependencies {
    compile 'io.requery:requery:1.0-SNAPSHOT'
    compile 'io.requery:requery-android:1.0-SNAPSHOT' // for android
    apt 'io.requery:requery-processor:1.0-SNAPSHOT'   // prefer an APT plugin
}
已下载
0