Can jooq recognize optional parameters for routines and create overloads accordingly?

fhueser :

we use jooq in a Kotlin/Java environment to access a timescaledb. Now, oftentimes when we regenerate the jooq objects we get errors if there was a change in the signature of a routine on the db even though it just added optional parameters. Can I somehow make jooq generate overloads of the respective methods to avoid having to manually fill in the default values of optional parameters?

Thanks!

Lukas Eder :

You could extend the org.jooq.meta.Database class that you're using to generate code in order to produce additional RoutineDefinition instances from each code generation run. All the information you need for this is there, you just have to do something like this (I haven't tried it, there might be bugs, feel free to edit):

public class MyDatabase extends PostgresDatabase {
    @Override
    protected List<RoutineDefinition> getRoutines0() throws SQLException {
        List<RoutineDefinition> routines = super.getRoutines0();
        List<RoutineDefinition> result = new ArrayList<>(routines);

        routineLoop:
        for (RoutineDefinition routine : routines) {
            if (routine.isSQLUsable()) {
                List<ParameterDefinition> in = new ArrayList<>(routine.getInParameters());
                Iterator<ParameterDefinition> it = in.iterator();
                boolean hasDefaulted = false;

                while (it.hasNext())
                    if (it.next().isDefaulted()) {
                        hasDefaulted = true;
                        it.remove();
                    }

                    // This approach only works if the trailing params are defaulted
                    else if (hasDefaulted)
                        continue routineLoop;

                if (hasDefaulted)
                    result.add(new AbstractRoutineDefinition(
                        routine.getSchema(), null, routine.getName(), 
                        routine.getComment(), "OverloadIndex" // Replace this
                    ) {
                        @Override
                        protected void init0() {
                            addParameter(InOutDefinition.RETURN, routine.getReturnValue());

                            for (ParameterDefinition p : in)
                                addParameter(InOutDefinition.IN, p);
                        }
                    });
            }
        }

        return result;
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=415272&siteId=1