ParameterEntryDAO { public List<ParameterEntry> list(QueryRunner queryRunner, JdbcParameter parameter, ParameterEntriesFilter filters) { SelectQuery query = QueryFactory.selectAll().from(configuration.parameterEntryEntityName()) .where("fk_parameter = :parameterId") .withArgument("parameterId", parameter.getId()); int maxDistinctLevels = configuration.levelColumnCount(); int levelIndex = 0; for (Level level : parameter.getLevels()) { if (levelIndex >= maxDistinctLevels) { break; } if (filters.hasFilter(level.getName())) { LevelFilter filter = filters.levelFilter(level.getName()); query.append(" and (upper(" + level(levelIndex) + ")").append(levelSearchCondition(filter, levelIndex)).append(")"); query.withArgument(level(levelIndex), FilterConverter.parseAntMatcher(filter.value())); } levelIndex++; } Map<String, Integer> levelIndexMap = createLevelIndex(parameter); for (LevelSorting levelSorting : filters.sorting()) { if (levelIndexMap.containsKey(levelSorting.level())) { levelIndex = levelIndexMap.get(levelSorting.level()); if(levelIndex < maxDistinctLevels) { query.orderBy(level(levelIndex), parseSortOrder(levelSorting.direction())); } } } if (filters.applyPaging()) { query.limit(filters.pageSize(), filters.offset()); } else if (filters.applyLimits()) { query.limit(filters.pageSize()); } return queryRunner.queryList(query, new ParameterEntryMapper(configuration)); } ParameterEntryDAO(DefaultJdbcConfig configuration); long insert(QueryRunner queryRunner, ParameterEntry parameterEntry, long parameterId); List<Long> insert(QueryRunner queryRunner, Iterable<ParameterEntry> parameterEntries, long parameterId); Set<ParameterEntry> getParameterEntries(QueryRunner queryRunner, String parameterName); List<ParameterEntry> getParameterEntries(QueryRunner queryRunner, List<Long> ids); Set<JdbcParameterEntry> getJdbcParameterEntries(QueryRunner queryRunner, String parameterName); List<ParameterEntry> getParameterEntriesBatch(QueryRunner queryRunner, String parameterName, long lastEntryId, int batchSize); void deleteParameterEntries(QueryRunner queryRunner, String parameterName); void delete(QueryRunner queryRunner, long entryId); void delete(QueryRunner queryRunner, Iterable<Long> entriesIds); void update(QueryRunner queryRunner, long entryId, ParameterEntry entry); List<ParameterEntry> list(QueryRunner queryRunner, JdbcParameter parameter, ParameterEntriesFilter filters);  }
@Test public void shouldLimitNumberOfEntriesReturned() { database().withParameter("parameter").withParameterEntries("parameter", 10).build(); ParameterEntryDAO parameterEntryDAO = get(ParameterEntryDAO.class); QueryRunner runner = queryRunner(); JdbcParameter parameter = get(ParameterDAO.class).getParameter(runner, "parameter"); List<ParameterEntry> entries = parameterEntryDAO.list(runner, parameter, new ParameterEntriesFilter(0, 5)); runner.close(); assertThat(entries).hasSize(5); }
@Test public void shouldSelectEntriesWithGivenOffset() { database().withParameter("parameter").withParameterEntries("parameter", 9).build(); ParameterEntryDAO parameterEntryDAO = get(ParameterEntryDAO.class); QueryRunner runner = queryRunner(); JdbcParameter parameter = get(ParameterDAO.class).getParameter(runner, "parameter"); List<ParameterEntry> entries = parameterEntryDAO.list(runner, parameter, new ParameterEntriesFilter(1, 5)); runner.close(); assertThat(entries).hasSize(4); }