Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

requery - modern SQL based query & persistence for Java / Kotlin / Android

License

NotificationsYou must be signed in to change notification settings

requery/requery

Repository files navigation

requery

A light but powerful object mapping and SQL generator for Java/Kotlin/Android with RxJava and Java 8 support.Easily map to or create databases, perform queries and updates from any platform that uses Java.

Build StatusDownload

Examples

Define entities from an abstract class:

@EntityabstractclassAbstractPerson {@Key@Generatedintid;@Index("name_index")// table specificationStringname;@OneToMany// relationships 1:1, 1:many, many to manySet<Phone>phoneNumbers;@Converter(EmailToStringConverter.class)// custom type conversionEmailemail;@PostLoad// lifecycle callbacksvoidafterLoad() {updatePeopleList();    }// getter, setters, equals & hashCode automatically generated into Person.java}

or from an interface:

@EntitypublicinterfacePerson {@Key@GeneratedintgetId();StringgetName();@OneToManySet<Phone>getPhoneNumbers();StringgetEmail();}

or use immutable types such as those generated by@AutoValue:

@AutoValue@EntityabstractclassPerson {@AutoValue.BuilderstaticabstractclassBuilder {abstractBuildersetId(intid);abstractBuildersetName(Stringname);abstractBuildersetEmail(Stringemail);abstractPersonbuild();    }staticBuilderbuilder() {returnnewAutoValue_Person.Builder();    }@KeyabstractintgetId();abstractStringgetName();abstractStringgetEmail();}

(Note some features will not be available when using immutable types, seehere)

Queries: dsl based query that maps to SQL

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

Relationships: represent relations more efficiently with Java 8 Streams, RxJava Observables orplain iterables. (sets and lists are supported to)

@EntityabstractclassAbstractPerson {@Key@Generatedintid;@ManyToManyResult<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))}

Kotlin specific support using property references and infix functions:

data {val result= select(Person::class) where (Person::age gt21)and (Person::name eq"Bob") limit10}

Java 8streams:

data.select(Person.class)    .orderBy(Person.AGE.desc())    .get()    .stream().forEach(System.out::println);

Java 8 optional and time support:

publicinterfacePerson {@Key@GeneratedintgetId();StringgetName();Optional<String>getEmail();ZonedDateTimegetBirthday();}

RxJavaObservables:

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

RxJava observe query on table changes:

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

Read/write separation Along with immutable types optionally separate queries (reading)and updates (writing):

introws =data.update(Person.class)    .set(Person.ABOUT,"student")    .where(Person.AGE.lt(21)).get().value();

Features

  • No Reflection
  • Fast startup & performance
  • No dependencies (RxJava is optional)
  • Typed query language
  • Table generation
  • Supports JDBC and most popular databases (MySQL, Oracle, SQL Server, Postgres and more)
  • Supports Android (SQLite, RecyclerView, Databinding, SQLCipher)
  • Blocking and non-blocking API
  • Partial objects/refresh
  • Upsert support
  • Caching
  • Lifecycle callbacks
  • Custom type converters
  • Compile time entity validation
  • JPA annotations (however requery is not a JPA provider)

Reflection free

requery uses compile time annotation processing to generate entity model classes and mappingattributes. On Android this means you get about the same performance reading objects from a queryas if it was populated using the standard Cursor and ContentValues API.

Query with Java

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.

Relationships

You can define One-to-One, One-to-Many, Many-to-One, and Many-to-Many relations in your models usingannotations. Relationships can be navigated in both directions. Of many type relations can be loadedinto standard java collection objects or into a more efficientResult type.From aResulteasily create a Stream, RxJava Observable, Iterator, List or Map.

Many-to-Many junction tables can be generated automatically. Additionally the relation model isvalidated at compile time eliminating runtime errors.

vs JPA

requery provides a modern set of interfaces for persisting and performing queries. Some keydifferences between requery and JPA providers like Hibernate or EclipseLink:

  • Queries maps directly to SQL as opposed to JPQL.
  • Dynamic Queries easily done through a DSL as opposed to the verboseCriteriaQuery API.
  • Uses easily understandable extended/generated code instead of reflection/bytecode weaving forstate tracking and member access

Android

Designed specifically with Android support in mind. Seerequery-android/examplefor an example Android project using databinding and interface based entities. For more informationsee theAndroid page.

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 thexerial 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.Seehere for more information.

Upserts

Upserts are generated with the appropriate database specific query statements:

  • Oracle/SQL Server/HSQL:merge into when matched/not matched
  • PostgresSQL:on conflict do update (requires 9.5 or later)
  • MySQL:on duplicate key update

Using it

Versions are available on bintray jcenter / maven central.

repositories {    jcenter()}dependencies {    compile'io.requery:requery:1.6.1'    compile'io.requery:requery-android:1.6.1'// for android    annotationProcessor'io.requery:requery-processor:1.6.1'}

For information on gradle and annotation processing & gradle see thewiki.

License

Copyright (C) 2019 requery.ioLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

[8]ページ先頭

©2009-2025 Movatter.jp