greenrobot / greenDAO

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.
http://greenrobot.org/greendao/
12.63k stars 2.89k forks source link

Is it possible to retrieve a single column from database using greenDAO? #662

Closed Bringoff closed 7 years ago

Bringoff commented 7 years ago

When I used OrmLite I have been able to query for single column (like id) to use list of ids in other query (for example, for IN statement). OrmLite has RawRowMapper for this and I could make something like this:

private List<Long> getSomeIds() throws SQLException {
        String query = "___some query here___";
        final RawRowMapper<Long> mapper = new RawRowMapper<Long>() {
            @Override
            public Long mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
                return Long.parseLong(resultColumns[0]);
            }
        };
        GenericRawResults<Long> results = mDao.queryRaw(query, mapper);
        return results.getResults();
    }

and had only ids. With GreenDAO I should write following:

private List<Long> getSomeIdss() throws SQLException {
        List<Item> items = internalDao.queryRaw(
                "___ almost the same query here___"
        );

        List<Long> ids = new ArrayList<>(items.size());
        for (Item item : items) {
            ids.add(item.getNonNullId());
        }
        return ids;
    }

I didn't make speed comparison, but I can suggest that redundant iteration has some impact. So if there is no feature like this to query for a single column, this can be feature request. But maybe, I miss something.

greenrobot-team commented 7 years ago

This was answered in #380. To sum it up: not possible, but greenDAO is fast. Just get the ids from the result objects, as in your example. -ut