Greendao sort by field in related table

Is there a way to sort by field in a linked table with greenDao? For instance. I have a car desk and a driver desk. Each car has a driver. Now I want to query (e.g. blue) cars and sort by driver name

+6
source share
4 answers

QueryBuilder has methods for specifying the sort order. Look for methods starting with "order ...", for example. orderAsc (Property).

+5
source

I also communicate with GreenDao and hope that my small addition to the comment in the first answer and the description in the request part of the greenDao documentation .

( :)):

Query query = carsDao.queryRawCreate(   ", driver D WHERE T.COLOR='blue' AND T.DRIVER_ID=D._ID ORDER BY D.NAME ASC");

SQL, :

SELECT T.'id', T.'name', T.'color', T.'driver_id'
FROM cars T, driver D
WHERE T.COLOR='blue'
AND T.DRIVER_ID=D._ID
ORDER BY D.NAME ASC

queryRawCreate, - sql, queryRawCreate.

. , , JOIN.

+5

QueryBuilders Dao, greendao ORM.

,

 ProductDao productDao;
 DaoSession daoSession;

DaoMaster DaoSession . onCreate() , Application.

DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getApplicationContext(), "app-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
daoSession = new DaoMaster(db).newSession();

,

 daoSession = ((MyApplication) getApplication()).getDaoSession();
 productDao = daoSession.getProductDao();

, , .

private void refreshProducts() {
        switch (sorted_by){
            case SORT_BY_DATE:
                cardItems = productDao.queryBuilder().orderAsc(ProductDao.Properties.Id).list();
                setupRecyclerView();
                break;

            case SORT_BY_PRICE:
                cardItems = productDao.queryBuilder().orderDesc(ProductDao.Properties.Price).list();
                setupRecyclerView();
                break;

            case SORT_BY_POPULARITY:
                cardItems = productDao.queryBuilder().orderDesc(ProductDao.Properties.Name).list();
                setupRecyclerView();
                break;
        }
    }
+1

QueryBuilder.orderRaw() Join.getTablePrefix()

:

QueryBuilder.LOG_SQL = true;//Enable to see SQL result

  QueryBuilder<Item> query = daoSession.queryBuilder(Item.class);
  Join itemWithCollection = query.join(JoinItemWithCollection.class,
      JoinItemWithCollectionDao.Properties.ItemId);
  String joinedTable = itemWithCollection.getTablePrefix();
  Join collection = query.join(itemWithCollection, JoinItemWithCollectionDao.Properties.CollectionId,
      Collection.class, CollectionDao.Properties.Id);
  String orderCol = JoinItemWithCollectionDao.Properties.SomeOrderCol.columnName;
  collection.where(CollectionDao.Properties.Key.eq(collectionId));
  query.orderRaw(joinedTable+".\""+orderCol+"\" DESC");
  query.limit(limit);
  query.offset(from);

  List<Item> results = query.list();
0

All Articles