Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Room 2.2.0-alpha01

Room 2.2.0-alpha01

Masatoshi Kubode

August 01, 2019
Tweet

More Decks by Masatoshi Kubode

Other Decks in Programming

Transcript

  1. ©2019 Wantedly, Inc.  1SFQBDLBHFE%BUBCBTF  4DIFNB%FGBVMU7BMVFT  .BOZUP.BOZ3FMBUJPOT 

    0OFUP0OF3FMBUJPOT  5BSHFU&OUJUZ  (SBEMF*ODSFNFOUBM"OOPUBUJPO1SPDFTTPS ৽ػೳ
  2. ©2019 Wantedly, Inc. Code // createFromAsset/File can't use with inMemoryDb

    Room.databaseBuilder(…) .createFromAsset("assets-path-to.db") // OR .createFromFile(File("path-to.db")) .build() Room.inMemoryDatabaseBuilder ͸ϥϯλΠϜΤϥʔʹͳΔ
  3. ©2019 Wantedly, Inc. Code @Entity data class Memo( @PrimaryKey(autoGenerate =

    true) val id: Long, val text: String, @ColumnInfo(defaultValue = "('Created at ' || CURRENT_TIMESTAMP)") val createdAt: String ) @Dao interface MemoDao { @Query("INSERT INTO Memo (text) VALUES (:text)") fun insertNewRecord(text: String) } ੑ্࣭@Insertʹ͸࢖͑ͳ͍
  4. ©2019 Wantedly, Inc. Code @Entity data class Playlist( @PrimaryKey(autoGenerate =

    true) val playlistId: Long, val title: String ) @Entity data class Song( @PrimaryKey(autoGenerate = true) val songId: Long, val title: String ) @Entity(primaryKeys = ["playlistId", "songId"]) data class PlaylistSongRef( val playlistId: Long, val songId: Long )
  5. ©2019 Wantedly, Inc. Code data class PlaylistWithSongs( @Embedded val playlist:

    Playlist, @Relation( parentColumn = "playlistId", entity = Song::class, entityColumn = "songId", associateBy = Junction(PlaylistSongRef::class) ) val songs: List<Song> ) @Dao interface MusicDao { @Transaction @Query("""SELECT * FROM Playlist""") fun loadAllPlaylistWithSongs(): List<PlaylistWithSongs> } ORDER BY͸ࢦఆͰ͖ͳ͍
  6. ©2019 Wantedly, Inc. Code @Entity data class User( @PrimaryKey val

    userId: Long, val name: String ) @Entity data class Profile( @PrimaryKey val userId: Long, val selfIntroduction: String )
  7. ©2019 Wantedly, Inc. Code data class UserWithProfile( @Embedded val user:

    User, @Relation( parentColumn = "userId", entity = Profile::class, entityColumn = "userId" ) val profile: Profile? ) @Dao interface UserDao { @Transaction @Query("""SELECT * FROM User""") fun loadAllUserWithProfile(): List<UserWithProfile> } Nullableʹ͢Ε͹1:0..1΋දݱͰ͖Δ
  8. ©2019 Wantedly, Inc. Code @Entity data class Project( @PrimaryKey val

    id: Long, val title: String, @ColumnInfo(defaultValue = "''") val longDescription: String ) data class ProjectMiniApiEntity( var id: Long, var title: String ) PartialʹͳΔColumn͸ Nullable͔defaultValueඞਢ
  9. ©2019 Wantedly, Inc. Code @Dao interface ProjectDao { @Insert(entity =

    Project::class) fun insertNewProject(projectMini: ProjectMiniApiEntity) @Update(entity = Project::class) fun updateProject(projectMini: ProjectMiniApiEntity) } INSERT OR ABORT INTO `Project` (`id`,`title`) VALUES (?,?) UPDATE OR ABORT `Project` SET `id` = ?,`title` = ? WHERE `id` = ?
  10. ©2019 Wantedly, Inc. Code class CharSequenceConverter { @TypeConverter fun toString(value:

    CharSequence?): String? = value?.toString() } data class ProjectMiniApiEntity( var id: Long, @ColumnInfo(name = "title") var title2: CharSequence, @Ignore var list: List<String> ) TypeConverter͕͋Ε͹ม׵ͯ͘͠ΕΔ @ColumnInfoͰ໊લͷม׵΋Ͱ͖Δ Ignore΋Ͱ͖Δ