Given a SelectQuery<? extends Record>, I'm struggling with the effects from 
basically an empty field list.


   1. The query is created with jooq.selectQuery(getTable());
   2. Later, some joins might be added

When fetching from the query, the issue is that multiple tables with 
identical field names lead to wrong data when fetching into a POJO.

How can I change the field list of the SelectQuery? Or would that be part 
of https://github.com/jOOQ/jOOQ/issues/1492 ?

jOOQ 3.10.2

Current workaround (note the placement in the org.jooq.impl package in 
order to access package-private methods):
/*
 * Created on 13 May 2018
 */
package org.jooq.impl;

import java.util.Iterator;
import java.util.Set;

import org.jooq.Field;
import org.jooq.SelectQuery;

import com.google.common.collect.Sets;

public final class SelectQueryProjectionModifier {

    private SelectQueryProjectionModifier() {
        // hide c'tor
    }

    /**
     * jOOQ 3.x doesn't expose a {@link SelectQuery}'s fields in a 
modifyable way
     *
     * @param query
     * @param fields
     *
     * @see https://groups.google.com/forum/#!topic/jooq-user/sZ177NPx_Bc
     * @see https://github.com/jOOQ/jOOQ/issues/1492
     */
    public static void setFields(final SelectQuery<?> query, final 
Field<?>[] fields) {
        if (query instanceof SelectQueryImpl) {
            final Set<Field<?>> selectFields = Sets.newHashSet(fields);
            final SelectFieldList fieldList = ((SelectQueryImpl<?>) 
query).getSelect0();
            if (fieldList.isEmpty()) {
                fieldList.addAll(selectFields);
            } else {
                for (final Iterator<Field<?>> iterator = 
fieldList.listIterator(); iterator.hasNext();) {
                    final Field<?> field = iterator.next();
                    if (!selectFields.contains(field)) {
                        iterator.remove();
                    }
                }
            }
        }
    }
}



-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to