maskarade/Android-Orma

Best practice to search the values in direct associations

phicdy opened this issue · 3 comments

For example, when I'd like to search the books that publisher's name is something, what is the best practice ?

@Table
class Publisher {
  @PrimaryKey
  val id: Long

  @Column
  val name: String
}

@Table
class Book {

    @PrimaryKey
    val id: Long

    @column
    val title: String

    @Column
    val publisher: Publisher
}

My way is here.

val searchWord = "hogehoge"
orma.selectFromBook()
    .where(Publisher_Schema.INSTANCE.name.name + """ like "$searchWord" """)
    .executeAsObservable()
    .toList()
    .subscribeOn(Schedulers.io())

If an entity have a direct association, Orma generates a query builder for the association.

In the case of your example, try the following code.

val searchWord = "hogehoge"
orma.selectFromBook()
    .publisher { it.nameLike(searchWord) }
    .executeAsObservable()
    .toList()
    .subscribeOn(Schedulers.io())

This feature is available since Orma v5.0.0.

@k-kagurazaka Great. Thank you for the code.

After updated to v5.1.2, I couldn't use this way, but I found the solution. The target column requires @Column(index = true).

@Table
class Publisher {
  @PrimaryKey
  val id: Long

  @Column(index = true)
  val name: String
}

@Table
class Book {

    @PrimaryKey
    val id: Long

    @column
    val title: String

    @Column(index = true)
    val publisher: Publisher
}

Then, we can use the codes below.

val searchWord = "hogehoge"
orma.selectFromBook()
    .publisher { it.nameLike(searchWord) }
    .executeAsObservable()
    .toList()
    .subscribeOn(Schedulers.io())