Build Source License

Room Extensions – Aggregate

Maven Central Sonatype Nexus (Snapshots)

Artifact it.czerwinski.android.room:room-extensions aggregates artifacts:

  • room-database,
  • room-database-sql,
  • room-converters.

You can either use room-extensions or any combination of the other artifacts, whichever suites your project.

Build Configuration

Kotlin

dependencies {
    implementation("androidx.room:room-runtime:2.2.6")
    implementation("it.czerwinski.android.room:room-extensions:[VERSION]")
}

Groovy

dependencies {
    implementation 'androidx.room:room-runtime:2.2.6'
    implementation 'it.czerwinski.android.room:room-extensions:[VERSION]'
}

Room Database Extensions

Maven Central Sonatype Nexus (Snapshots)

Build Configuration

Kotlin

dependencies {
    implementation("androidx.room:room-runtime:2.2.6")
    implementation("it.czerwinski.android.room:room-database:[VERSION]")
}

Groovy

dependencies {
    implementation 'androidx.room:room-runtime:2.2.6'
    implementation 'it.czerwinski.android.room:room-database:[VERSION]'
}

Room Database Builders

roomDatabaseBuilder

Creates a RoomDatabase.Builder for a persistent database with type T and the given name.

val database = context.roomDatabaseBuilder<MyDatabase>().build()

or:

val database = context.roomDatabaseBuilder<MyDatabase>(name = "database").build()

roomInMemoryDatabaseBuilder

Creates a RoomDatabase.Builder for an in memory database with type T.

val database = context.roomInMemoryDatabaseBuilder<MyDatabase>().build()

Room Database SQL Extensions

Maven Central Sonatype Nexus (Snapshots)

Build Configuration

Kotlin

dependencies {
    implementation("androidx.room:room-runtime:2.2.6")
    implementation("it.czerwinski.android.room:room-database-sql:[VERSION]")
}

Groovy

dependencies {
    implementation 'androidx.room:room-runtime:2.2.6'
    implementation 'it.czerwinski.android.room:room-database-sql:[VERSION]'
}

SQL Script Executor

val database: SupportSQLiteDatabase
val sqlScriptExecutor = SQLScriptExecutor {
    +"""
        insert into users(id, username, password) values (1, 'root', 'qwerty');
        insert into user_roles(user_id, role_id) values (1, 1);
    """
}
sqlScriptExecutor.executeOn(database)

Room Database Builder Extensions

Populate From SQL

Configures Room to populate a newly created database with an SQL script.

val database = context.roomDatabaseBuilder<MyDatabase>()
    .populateFromSql {
        +"""
            insert into users(id, username, password) values (1, 'root', 'qwerty');
            insert into user_roles(user_id, role_id) values (1, 1);
        """
    }
    .build()

Populate From SQL Asset

Configures Room to populate a newly created database with an SQL script located in the application assets/ folder.

val database = context.roomDatabaseBuilder<MyDatabase>()
    .populateFromSqlAsset(context, "sql/populate.sql")
    .build()

Add Migration From SQL

Adds an SQL script migration to the builder.

val database = context.roomDatabaseBuilder<MyDatabase>()
    .addMigrationFromSql(startVersion = 1, endVersion = 2) {
        +"""
            create table if not exists users (
                id integer primary key asc,
                username text not null,
                password text not null
            );
        """
    }
    .build()

Add Migration From SQL Asset

Adds a migration to the builder, executing an SQL script located in the application assets/ folder.

val database = context.roomDatabaseBuilder<MyDatabase>()
    .addMigrationFromSqlAsset(startVersion = 1, endVersion = 2, context, "sql/migrate_1_2.sql")
    .build()

Add Migrations From SQL Assets

Adds migrations to the builder, executing an SQL scripts located in the application assets/ folder, matching the given format.

val database = context.roomDatabaseBuilder<MyDatabase>()
    .addMigrationsFromSqlAssets(context, sqlFilePathFormat = "sql/migrate_{}_{}.sql")
    .build()

Room TypeConverters Generator

Maven Central Sonatype Nexus (Snapshots)

Build Configuration

Kotlin

dependencies {
    implementation("androidx.room:room-runtime:2.2.6")
    implementation("it.czerwinski.android.room:room-converters:[VERSION]")
    kapt("it.czerwinski.android.room:room-converters-processor:[VERSION]")
}

Groovy

dependencies {
    implementation 'androidx.room:room-runtime:2.2.6'
    implementation 'it.czerwinski.android.room:room-converters:[VERSION]'
    kapt 'it.czerwinski.android.room:room-converters-processor:[VERSION]'
}

Generating Room TypeConverters

@GenerateEnumTypeConverter

Marks an enum class to have automatically generated @TypeConverters to and from the given type.

Enum can be stored in the database as name (String – default) or as ordinal (Int).

To store enum in a text field (as name):

@GenerateEnumTypeConverter(type = EnumType.STRING)
enum class MyEnum {
    FOO,
    BAR
}

To store enum in an integer field (as ordinal):

@GenerateEnumTypeConverter(type = EnumType.ORDINAL)
enum class MyEnum {
    FOO,
    BAR
}