3

I am trying to replace my database with Room persistence I've a method which accept a Custom Object and return the id of that row is in database

   /**
     * This method return -1 if there is not any classInfo other wise return
     * the id of the classInfo
     * 
     * @param ClassInfo
     * @return
     */
    public int getClassIdByInfo(ClassInfo classInfo) {
        Cursor c = db.query(DB_CLASS_INFO, new String[]{CL_ID}, CL_BRANCH
                + "=? AND " + CL_SEM + "=? AND " + CL_SECTION + "=?",
                new String[]{classInfo.branch, classInfo.sem, classInfo.section}, null, null, null);
        if (c.getCount() > 0) {
            c.moveToFirst();
            return c.getInt(0);
        } else {

            return -1;
        }  

I want to replace this method with Room persistence DAO method

@Dao
public interface StudentClassDao {

    @Query("SELECT id FROM class_info....")  //what will be the query?
    int getClassIdByInfo(ClassInfo classInfo);
}

What will be the query for that scenario?

Sunny
  • 14,522
  • 15
  • 84
  • 129
  • "What will be the query for that scenario?" -- the first step is to replace `int getClassIdByInfo(ClassInfo classInfo)` with `int getClassIdByInfo(String branch, String sem, String section)` (replacing `String` with whatever the proper types are for those parameters). Then, your query is whatever `DB_CLASS_INFO` is, except that you use `:branch`, `:sem`, and `:section` in place of the `?` values. – CommonsWare Jun 18 '17 at 15:36
  • Can't we use something like `:classInfo.branch` it'll be much easier when parameters grow? – Sunny Jun 18 '17 at 15:43
  • AFAIK, no. AFAIK, Room does not employ a full expression language, the way the data binding framework does. It would not surprise me if they add that someday, given that they already did a lot of the work with data binding. But I have not seen any signs that it is there now. – CommonsWare Jun 18 '17 at 16:02
  • @CommonsWare. Thanks for the reply. – Sunny Jun 18 '17 at 16:21
  • that sounds like a great idea actually. I don't think we can do it before 1.0 but please file a bug and we'll consider it post 1.0 . – yigit Jun 18 '17 at 18:14
  • @yigit: OK, [here you go](https://issuetracker.google.com/issues/62760145). – CommonsWare Jun 18 '17 at 18:48

0 Answers0