Share
Embed
Room 2.2.0-alpha01
Masatoshi Kubode
August 01, 2019
Transcript
28 slides
-
©2019 Wantedly, Inc. ٱอग़խढ़ 8BOUFEMZ 8BOUFEMZ7JTJUͷ"OESPJEΞϓϦΛ։ൃ͍ͯ͠·͢ɻ ,PUMJO .BHJDUIF(BUIFSJOH 1PLFNPO(P ࣗݾհ
5XJUUFS !TXJ[@BSE (JU)VC !LVCPEF -
©2019 Wantedly, Inc. 1SFQBDLBHFE%BUBCBTF 4DIFNB%FGBVMU7BMVFT .BOZUP.BOZ3FMBUJPOT
0OFUP0OF3FMBUJPOT 5BSHFU&OUJUZ (SBEMF*ODSFNFOUBM"OOPUBUJPO1SPDFTTPS ৽ػೳ -
©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 ϥϯλΠϜΤϥʔʹͳΔ -
©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ʹ͑ͳ͍ -
©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 ) -
©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ࢦఆͰ͖ͳ͍ -
©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 ) -
©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දݱͰ͖Δ -
©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ඞਢ -
©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` = ? -
©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Ͱ͖Δ