Home

Awesome

🛑 THIS REPOSITORY IS OFFICIALLY NO LONGER UNDER MAINTENANCE since 10/02/2022 🛑

sqlite-analyzer CI status Download from Bintray Apache 2.0 Licence

Code generation for Java/Android database access.

Description

Generates java/android database access code by analysing sqlite migration files or sqlite databases, keeping full control of what code is generated.

sqlite-analyzer creates an in-memory sqlite database, either from a given database file or by reading sql migrations, and analyzes its tables to construct a DatabaseModel. This model is then used to generate database access code.

This project uses sqlite-jdbc to create and analyze the database. Groovy is used to generate code, Gradle to hook the functionality into the android build system.

Adding to your project

To integrate sqlite-analyzer into your project, add the following at the beginning of the build.gradle of your project:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.novoda:sqlite-analyzer:0.3.2'
    }
}

To use the library with sqlite-provider, add these lines to the build.gradle of your project:

apply plugin: 'com.novoda.sqlite-analyzer'

sqliteAccess {
    migrationsDir 'src/main/assets/migrations'
    packageName 'com.novoda.sqliteprovider.demo.simple'
}

See the sample projects for setup with android-sqlite-asset-helper.

Simple usage

Try ./gradlew clean assembleDebug and observe the generated code under build/generated/source/sqlite/debug/.

By default, it contains one single class DB that defines constants for the names of the tables and columns and introduces static accessor methods as well as model classes for all tables data.

The project comes with 2 demo applications that create and use database access code, one uses sqlite-provider, the other uses android-sqlite-asset-helper.

Use column names

queryBuilder.appendWhere(DB.Columns.Shop.Name + " like 'A%'")

Simplified use with static import

projection = new String[] {Employees.Firstname, Employees.Lastname};

Use model classes

public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
    DB.Shop shop = DB.Shop.fromCursor(cursor);
}

Use helpers for ContentValues

ContentValues values = new ContentValues(1);
DB.Shop.setName("New Shop", values);

Links

Here are a list of useful links: