13. Simple entity
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long
)
14. Simple entity
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long
)
15. Simple entity
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long
)
16. Simple entity
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long
)
17. Indexes
@Entity(tableName = "inventory",
indices = arrayOf(Index("provider_id", "product_id", unique = true)))
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
18. Composed PK
@Entity(tableName = "inventory",
primaryKeys = arrayOf("provider_id", "product_id"))
class InventoryEntity(
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
19. Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
20. Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
21. Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
22. Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
23. Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
24. Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
25. Nested objects
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@Embedded var info: ProductInfo
)
class ProductInfo(
@ColumnInfo(name = "color") var color: String,
@ColumnInfo(name = "size") var size: String
)
products
id name description color size
26. Nested objects
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@Embedded var info: ProductInfo
)
class ProductInfo(
@ColumnInfo(name = "color") var color: String,
@ColumnInfo(name = "size") var size: String
)
products
id name description color size
27. Nested objects
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@Embedded var info: ProductInfo
)
class ProductInfo(
@ColumnInfo(name = "color") var color: String,
@ColumnInfo(name = "size") var size: String
)
products
id name description color size
28. Nested objects
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@Embedded var info: ProductInfo
)
class ProductInfo(
@ColumnInfo(name = "color") var color: String,
@ColumnInfo(name = "size") var size: String
)
products
id name description color size
30. Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
31. Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
32. Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
Row id
33. Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
34. Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
# Modified rows
35. Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
36. Insert
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(product: ProductEntity): Long
@Insert(onConflict = REPLACE)
fun insertAll(vararg products: ProductEntity) : List<Long>
@Insert(onConflict = ROLLBACK)
fun insertTwo(product: ProductEntity, otherProduct: ProductEntity): List<Long>
@Insert(onConflict = ABORT)
fun weirdInsert(product: ProductEntity, products: List<ProductEntity>): List<Long>
}
39. Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
40. Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
41. Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
42. Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
43. Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
44. Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
45. Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
46. Query
@Dao
interface ProductsDao {
@Query("SELECT * FROM products WHERE id = :id")
fun getProducts(id: Long): ProductEntity
@Query("SELECT COUNT(*) FROM products")
fun getProductsCount(): Int
@Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name")
fun getProductsCountByName(): List<ProductCountEntity>
}
class ProductCountEntity(
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "count") var count: Int
)
47. Query
@Dao
interface ProductsDao {
@Query("SELECT * FROM products WHERE id = :id")
fun getProducts(id: Long): ProductEntity
@Query("SELECT COUNT(*) FROM products")
fun getProductsCount(): Int
@Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name")
fun getProductsCountByName(): List<ProductCountEntity>
}
class ProductCountEntity(
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "count") var count: Int
)
48. Query
@Dao
interface ProductsDao {
@Query("SELECT * FROM products WHERE id = :id")
fun getProducts(id: Long): ProductEntity
@Query("SELECT COUNT(*) FROM products")
fun getProductsCount(): Int
@Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name")
fun getProductsCountByName(): List<ProductCountEntity>
}
class ProductCountEntity(
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "count") var count: Int
)
49. Query
@Dao
interface ProductsDao {
@Query("SELECT * FROM products WHERE id = :id")
fun getProducts(id: Long): ProductEntity
@Query("SELECT COUNT(*) FROM products")
fun getProductsCount(): Int
@Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name")
fun getProductsCountByName(): List<ProductCountEntity>
}
class ProductCountEntity(
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "count") var count: Int
)
65. Type converters
@Dao
interface InventoryDao {
@Query("Select * from inventory where date = :date")
@TypeConverters(DateTimeConverter::class)
fun getByDate(date: LocalDateTime)
@Query("Select * from inventory where date = :date")
fun getByDate2(date: LocalDateTime)
}
66. Type converters
@Dao
interface InventoryDao {
@Query("Select * from inventory where date = :date")
fun getByDate(date: LocalDateTime)
@Query("Select * from inventory where date = :date")
fun getByDate2(@TypeConverters(DateTimeConverter::class) date: LocalDateTime)
}
67. Type converters
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long,
@ColumnInfo(name = "purchase_date") var purchaseDate: LocalDateTime
)
68. Type converters
@Entity(tableName = "products")
@TypeConverters(DateTimeConverter::class)
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long,
@ColumnInfo(name = "purchase_date") var purchaseDate: LocalDateTime
)
69. Type converters
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long,
@TypeConverters(DateTimeConverter::class)
@ColumnInfo(name = "purchase_date") var purchaseDate: LocalDateTime
)
70. Migrations
val migrationFrom1To2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER")
}
}
val migrationFrom2To3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN color TEXT")
}
}
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.addMigrations(migrationFrom1To2, migrationFrom2To3)
.build()
71. Migrations
val migrationFrom1To2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER")
}
}
val migrationFrom2To3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN color TEXT")
}
}
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.addMigrations(migrationFrom1To2, migrationFrom2To3)
.build()
72. Migrations
val migrationFrom1To2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER")
}
}
val migrationFrom2To3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN color TEXT")
}
}
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.addMigrations(migrationFrom1To2, migrationFrom2To3)
.build()
73. Migrations
val migrationFrom1To2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER")
}
}
val migrationFrom2To3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN color TEXT")
}
}
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.addMigrations(migrationFrom1To2, migrationFrom2To3)
.build()
78. RxJava 2
@Query("Select * from products")
fun getAllAsList(): Flowable<List<ProductEntity>>
@Query("SELECT * FROM products WHERE id = :id")
fun getProductById(id: Long): Single<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductByName(name: String): Maybe<ProductEntity>
79. RxJava 2
Flowable Maybe Single
No data - Just onComplete onError
Data onNext onSuccess onSuccess
Update onNext - -