Room Extensions – Aggregate
Artifact it.czerwinski.android.room:room-extensions
aggregates artifacts:
room-database
,room-database-sql
,room-converters
(untilv1.1.0
, now deprecated).
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.3.0")
implementation("it.czerwinski.android.room:room-extensions:[VERSION]")
}
Groovy
dependencies {
implementation 'androidx.room:room-runtime:2.3.0'
implementation 'it.czerwinski.android.room:room-extensions:[VERSION]'
}
Room Database Extensions
Build Configuration
Kotlin
dependencies {
implementation("androidx.room:room-runtime:2.3.0")
implementation("it.czerwinski.android.room:room-database:[VERSION]")
}
Groovy
dependencies {
implementation 'androidx.room:room-runtime:2.3.0'
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
Build Configuration
Kotlin
dependencies {
implementation("androidx.room:room-runtime:2.3.0")
implementation("it.czerwinski.android.room:room-database-sql:[VERSION]")
}
Groovy
dependencies {
implementation 'androidx.room:room-runtime:2.3.0'
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()
Deprecated Room TypeConverter
s Generator
Deprecated: Room 2.3.0 offers built-in enum
support.
Future releases (after v1.1.0
) of the library will no longer include these artifacts:
it.czerwinski.android.room:room-converters
it.czerwinski.android.room:room-converters-processor
Generating Room TypeConverter
s
Deprecated @GenerateEnumTypeConverter
Annotation
Deprecated: Room 2.3.0 offers built-in enum
support.
An implicit TypeConverter
will be used for all enum classes.