All Projects → SiimKinks → Sqlitemagic

SiimKinks / Sqlitemagic

Licence: apache-2.0
Compile time processed, annotation driven, no reflection SQLite database layer for Android

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to Sqlitemagic

Iron
Fast and easy to use NoSQL data storage with RxJava support
Stars: ✭ 112 (-6.67%)
Mutual labels:  rxjava
Rxjavainaction
《RxJava2.x 实战》一书中包含的例子。
Stars: ✭ 116 (-3.33%)
Mutual labels:  rxjava
Foodsearch
Showcase project of MVP+Dagger+RxJava+StorIO
Stars: ✭ 117 (-2.5%)
Mutual labels:  rxjava
Router
🍭灵活的组件化路由框架.
Stars: ✭ 1,502 (+1151.67%)
Mutual labels:  annotations
Unidirectional Architecture On Mobile
Dive into 📱 Unidirectional Architecture!
Stars: ✭ 115 (-4.17%)
Mutual labels:  rxjava
Rxpermissions
Android runtime permissions powered by RxJava2
Stars: ✭ 10,288 (+8473.33%)
Mutual labels:  rxjava
Voice
Minimalistic audiobook player
Stars: ✭ 1,559 (+1199.17%)
Mutual labels:  rxjava
Knotx
Knot.x is a highly-efficient and scalable integration framework designed to build backend APIs
Stars: ✭ 119 (-0.83%)
Mutual labels:  rxjava
Rxlocationmanager
RxJava wrap around standard Android LocationManager without Google Play Services
Stars: ✭ 115 (-4.17%)
Mutual labels:  rxjava
Frodo
Android Library for Logging RxJava Observables and Subscribers.
Stars: ✭ 1,496 (+1146.67%)
Mutual labels:  rxjava
Jupytergraffiti
Create interactive screencasts inside Jupyter Notebook that anybody can play back
Stars: ✭ 114 (-5%)
Mutual labels:  annotations
Mvvm Architecture
The practice of MVVM + Jetpack architecture in Android.
Stars: ✭ 1,634 (+1261.67%)
Mutual labels:  rxjava
Mentorship Android
Mentorship System is an application that matches women in tech to mentor each other, on career development, through 1:1 relations during a certain period of time. This is the Android application of this project.
Stars: ✭ 117 (-2.5%)
Mutual labels:  rxjava
Marginalia
📜 marginalia.el - Marginalia in the minibuffer
Stars: ✭ 111 (-7.5%)
Mutual labels:  annotations
Appmanager
一款直接显示App所有信息的App
Stars: ✭ 118 (-1.67%)
Mutual labels:  rxjava
Android Architecture
🌇该项目结合 MVP 与 Clean 架构思想,探索在 Android 项目上的最佳实践。
Stars: ✭ 112 (-6.67%)
Mutual labels:  rxjava
Mvparms
⚔️ A common architecture for Android applications developing based on MVP, integrates many open source projects, to make your developing quicker and easier (一个整合了大量主流开源项目高度可配置化的 Android MVP 快速集成框架).
Stars: ✭ 10,146 (+8355%)
Mutual labels:  rxjava
Scallop
干货集中营Android app(MVP + RxJava2 + Dagger2 + Retrofit)
Stars: ✭ 120 (+0%)
Mutual labels:  rxjava
Sensorannotations
Android - Annotate methods to use as listeners for a sensor.
Stars: ✭ 118 (-1.67%)
Mutual labels:  annotations
Vertx Mqtt
Vert.x MQTT
Stars: ✭ 117 (-2.5%)
Mutual labels:  rxjava

SqliteMagic

Simple yet powerful SQLite database layer for Android that makes database handling feel like magic.

Overview:

  • Simple, intuitive & typesafe API
  • Minimal setup needed
  • Built in RxJava support with reactive stream semantics on queries and operations
  • Built in AutoValue immutable objects support
  • Built in kotlin support
  • Full support for complex columns
  • Support for SQLite views
  • Persist any third party object with fully customizable object transformers
  • Support for migrations
  • No reflection
  • Compile time annotation processing
  • Probably the fastest library for Android SQLite database operations (without memory caching)

Getting Started

Install IntelliJ Plugin (for non-kotlin project):

The Intellij plugin can be installed from Android Studio by navigating Android Studio -> Preferences -> Plugins -> Browse repositories -> Search for SqliteMagic

Add SqliteMagic to Project:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:<latest version>'
    classpath 'com.siimkinks.sqlitemagic:sqlitemagic-plugin:0.25.1'
  }
}

apply plugin: 'com.android.application'
apply plugin: 'com.siimkinks.sqlitemagic'

Initialize Library:

SqliteMagic.builder(applicationContext)
    .sqliteFactory(new FrameworkSQLiteOpenHelperFactory())
    .openDefaultConnection();

Note: any place with a reference to Application context is ok to use for initialization, but it must happen before a database is accessed. During initialization default db connection is opened, db schema is created and migration scripts are executed - no other hidden runtime performance costs.

Define Database:

Note that there is no need to extend or implement any base classes or interfaces

POJO AutoValue
@Table(persistAll = true)
public class Author {

  @Id(autoIncrement = false)
  long id;
  
  String firstName;
  
  String lastName;
  
  ...
}



@Table(persistAll = true)
public class Book {

  @Id(autoIncrement = false)
  long id();
  
  String title;
  
  Author author;
  
  ...
}


@Table(persistAll = true)
@AutoValue
public abstract class Author {

  @Id(autoIncrement = false)
  public abstract long id();
  
  public abstract String firstName();
  
  public abstract String lastName();
  
  ...
}

@Table(persistAll = true)
@AutoValue
public abstract class Book {

  @Id(autoIncrement = false)
  public abstract long id();
  
  public abstract String title();
  
  public abstract Author author();
  
  ...
}
Kotlin
@Table(persistAll = true, useAccessMethods = true)
data class Author(
  @Id(autoIncrement = false) val id: Long,
  val firstName: String,
  val lastName: String
)



@Table(persistAll = true, useAccessMethods = true)
data class Book(
  @Id(autoIncrement = false) val id: Long,
  val title: String,
  val author: Author
)

Database operation builder methods for Java are "automagically" generated during compile time on objects with @Table annotation using bytecode manipulation and AST transformations. These methods may seem like "magic", but actually they are only glue methods that call corresponding table generated class methods. This way one can still see human readable code during debugging - just press "step into" when magic method is encountered.

For kotlin, database operation builder methods are generated as extensions functions.

Do Operations With Objects:

Synchronous RxJava
Author author = new Author(73, "Foo", "Bar");
Book book = new Book(77, "Bar", author);

// insert -- NOTE: author object also gets
// inserted and the whole operation
// is wrapped in transaction
long id = book
    .insert()
    .execute();

// update
boolean success = author
    .update()
    .execute();

// update or insert
id = author
    .persist()
    .execute();
    
// update or insert but ignore null values
id = author
    .persist()
    .ignoreNullValues()
    .execute();
    
// delete
int nrOfDeletedRows = author
    .delete()
    .execute();
    
// Bulk operations are also supported
success = Author
    .persist(someAuthors)
    .ignoreNullValues()
    .execute();

Author author = new Author(73, "Foo", "Bar");
Book book = new Book(77, "Bar", author);

// insert -- NOTE: author object also gets
// inserted and the whole operation is
// wrapped in transaction when result
// object gets subscribed
Single<Long> insert = book
    .insert()
    .observe();

// update
Completable update = author
    .update()
    .observe();

// update or insert
Single<Long> persist = author
    .persist()
    .observe();
    
// update or insert but ignore null values
persist = author
    .persist()
    .ignoreNullValues()
    .observe();
    
// delete
Single<Integer> delete = author
    .delete()
    .observe();
    
// Bulk operations are also supported
Completable bulkPersist = Author
    .persist(someAuthors)
    .ignoreNullValues()
    .observe();

(All database operations trigger RxJava notifications on active queries that listen to table that is being modified)

Use Typesafe Operation Builders:

Synchronous RxJava
import static com.siimkinks.sqlitemagic
    .BookTable.BOOK;
...

int nrOfUpdatedRows = Update
    .table(BOOK)
    .set(BOOK.TITLE, "Foo")
    .where(BOOK.ID.is(77L))
    .execute();

int nrOfDeletedRows = Delete
    .from(BOOK)
    .where(BOOK.ID.isNot(77L)
        .and(BOOK.TITLE.is("Foo")))
    .execute();
import static com.siimkinks.sqlitemagic
    .BookTable.BOOK;
...

Single<Integer> update = Update
    .table(BOOK)
    .set(BOOK.TITLE, "Foo")
    .where(BOOK.ID.is(77L))
    .observe();

Single<Integer> delete = Delete
    .from(BOOK)
    .where(BOOK.ID.isNot(77L)
        .and(BOOK.TITLE.is("Foo")))
    .observe();

Query Data:

SqliteMagic ships with its own DSL (or Domain Specific Language) that emulates SQL in Java (inspired by JOOQ).

Synchronous RxJava
import static com.siimkinks.sqlitemagic
    .AuthorTable.AUTHOR;
...

List<Author> authors = Select
    .from(AUTHOR)
    .where(AUTHOR.FIRST_NAME.like("Foo%")
        .and(AUTHOR.LAST_NAME.isNot("Bar")))
    .orderBy(AUTHOR.LAST_NAME.desc())
    .limit(10)
    .execute();


import static com.siimkinks.sqlitemagic
    .AuthorTable.AUTHOR;
...

// QueryObservable is an rx.Observable of Query
// which offers query-specific convenience
// operators.
QueryObservable<List<Author>> observable = Select
    .from(AUTHOR)
    .where(AUTHOR.FIRST_NAME.like("Foo%")
        .and(AUTHOR.LAST_NAME.isNot("Bar")))
    .orderBy(AUTHOR.LAST_NAME.desc())
    .limit(10)
    .observe();

Query Complex Data:

Synchronous RxJava
import static com.siimkinks.sqlitemagic
    .AuthorTable.AUTHOR;
import static com.siimkinks.sqlitemagic
    .BookTable.BOOK;
...

// the resulting Book objects also contain
// Author objects
List<Book> books = Select
    .from(BOOK)
    .where(BOOK.TITLE.is("Bar")
        .and(AUTHOR.is(someAuthorObject)))
    .orderBy(AUTHOR.LAST_NAME.asc())
    .limit(10)
    // this tells to query all complex data
    // which is queried in a single
    // SELECT statement
    .queryDeep()
    .execute();
import static com.siimkinks.sqlitemagic
    .AuthorTable.AUTHOR;
import static com.siimkinks.sqlitemagic
    .BookTable.BOOK;
...

// the resulting Book objects also contain
// Author objects
QueryObservable<List<Book>> observable = Select
    .from(BOOK)
    .where(BOOK.TITLE.is("Bar")
        .and(AUTHOR.is(someAuthorObject)))
    .orderBy(AUTHOR.LAST_NAME.asc())
    .limit(10)
    // this tells to query all complex data
    // which is queried in a single
    // SELECT statement
    .queryDeep()
    .observe();

There is so much more to querying data like SQL functions, views, more type safety, selecting columns, querying only the first result, counting, RxJava convenience operators, etc. Take a deeper look at the wiki.

Documentation

Updates

All updates can be found in the CHANGELOG.

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

License

Copyright 2020 Siim Kinks

Licensed 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 at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed 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 and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].