import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
+import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.String.format;
}
};
}
+
+ /**
+ * @throws IllegalArgumentException if the collection is not null and has strictly more
+ * than {@link #PARTITION_SIZE_FOR_ORACLE} values.
+ */
+ public static void checkThatNotTooManyConditions(@Nullable Collection<?> values, String message) {
+ if (values != null) {
+ checkArgument(values.size() <= PARTITION_SIZE_FOR_ORACLE, message);
+ }
+ }
}
assertThat(DatabaseUtils.tableExists("foo", connection)).isFalse();
}
}
+
+ @Test
+ public void checkThatNotTooManyConditions_does_not_fail_if_less_than_1000_conditions() {
+ DatabaseUtils.checkThatNotTooManyConditions(null, "unused");
+ DatabaseUtils.checkThatNotTooManyConditions(Collections.emptySet(), "unused");
+ DatabaseUtils.checkThatNotTooManyConditions(Collections.nCopies(10, "foo"), "unused");
+ DatabaseUtils.checkThatNotTooManyConditions(Collections.nCopies(1_000, "foo"), "unused");
+ }
+
+ @Test
+ public void checkThatNotTooManyConditions_throws_IAE_if_strictly_more_than_1000_conditions() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("the message");
+
+ DatabaseUtils.checkThatNotTooManyConditions(Collections.nCopies(1_001, "foo"), "the message");
+ }
}
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
+import static org.sonar.db.DatabaseUtils.checkThatNotTooManyConditions;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;
if (query.hasEmptySetOfComponents()) {
return emptyList();
}
+ checkThatNotTooManyComponents(query);
return mapper(session).selectByQuery(organizationUuid, query, new RowBounds(offset, limit));
}
if (query.hasEmptySetOfComponents()) {
return 0;
}
-
+ checkThatNotTooManyComponents(query);
return mapper(session).countByQuery(organizationUuid, query);
}
return componentDto.get();
}
+ /**
+ * Same as {@link #selectByQuery(DbSession, String, ComponentQuery, int, int)} except
+ * that the filter on organization is disabled.
+ */
public List<ComponentDto> selectByQuery(DbSession session, ComponentQuery query, int offset, int limit) {
return selectByQueryImpl(session, null, query, offset, limit);
}
+ /**
+ * @throws IllegalArgumentException if parameter query#getComponentIds() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
+ * @throws IllegalArgumentException if parameter query#getComponentKeys() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
+ * @throws IllegalArgumentException if parameter query#getComponentUuids() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
+ */
public List<ComponentDto> selectByQuery(DbSession dbSession, String organizationUuid, ComponentQuery query, int offset, int limit) {
requireNonNull(organizationUuid, "organizationUuid can't be null");
return selectByQueryImpl(dbSession, organizationUuid, query, offset, limit);
}
+ /**
+ * Same as {@link #countByQuery(DbSession, String, ComponentQuery)} except
+ * that the filter on organization is disabled.
+ */
public int countByQuery(DbSession session, ComponentQuery query) {
return countByQueryImpl(session, null, query);
}
+ /**
+ * @throws IllegalArgumentException if parameter query#getComponentIds() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
+ * @throws IllegalArgumentException if parameter query#getComponentKeys() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
+ * @throws IllegalArgumentException if parameter query#getComponentUuids() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
+ */
public int countByQuery(DbSession session, String organizationUuid, ComponentQuery query) {
requireNonNull(organizationUuid, "organizationUuid can't be null");
return countByQueryImpl(session, organizationUuid, query);
public List<KeyWithUuidDto> selectComponentKeysHavingIssuesToMerge(DbSession dbSession, String mergeBranchUuid) {
return mapper(dbSession).selectComponentKeysHavingIssuesToMerge(mergeBranchUuid);
}
+
+ private static void checkThatNotTooManyComponents(ComponentQuery query) {
+ checkThatNotTooManyConditions(query.getComponentIds(), "Too many component ids in query");
+ checkThatNotTooManyConditions(query.getComponentKeys(), "Too many component keys in query");
+ checkThatNotTooManyConditions(query.getComponentUuids(), "Too many component UUIDs in query");
+ }
+
}
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
import javax.annotation.Nullable;
import org.assertj.core.api.ListAssert;
import org.junit.Rule;
}
@Test
- public void select_provisioned() {
+ public void selectByQuery_provisioned() {
OrganizationDto organization = db.organizations().insert();
ComponentDto provisionedProject = db.components()
.insertComponent(newPrivateProjectDto(organization).setDbKey("provisioned.project").setName("Provisioned Project"));
assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW).build())).isEqualTo(1);
}
+ @Test
+ public void countByQuery_with_organization_throws_NPE_of_organizationUuid_is_null() {
+ expectedException.expect(NullPointerException.class);
+ expectedException.expectMessage("organizationUuid can't be null");
+
+ underTest.countByQuery(dbSession, null, ALL_PROJECTS_COMPONENT_QUERY);
+ }
+
+ @Test
+ public void countByQuery_throws_IAE_if_too_many_component_ids() {
+ Set<Long> ids = LongStream.range(0L, 1_010L).boxed().collect(Collectors.toSet());
+ ComponentQuery.Builder query = ComponentQuery.builder()
+ .setQualifiers(Qualifiers.PROJECT)
+ .setComponentIds(ids);
+
+ assertThatCountByQueryThrowsIAE(query, "Too many component ids in query");
+ }
+
+ @Test
+ public void countByQuery_throws_IAE_if_too_many_component_keys() {
+ Set<String> keys = IntStream.range(0, 1_010).mapToObj(String::valueOf).collect(Collectors.toSet());
+ ComponentQuery.Builder query = ComponentQuery.builder()
+ .setQualifiers(Qualifiers.PROJECT)
+ .setComponentKeys(keys);
+
+ assertThatCountByQueryThrowsIAE(query, "Too many component keys in query");
+ }
+
+ @Test
+ public void countByQuery_throws_IAE_if_too_many_component_uuids() {
+ Set<String> uuids = IntStream.range(0, 1_010).mapToObj(String::valueOf).collect(Collectors.toSet());
+ ComponentQuery.Builder query = ComponentQuery.builder()
+ .setQualifiers(Qualifiers.PROJECT)
+ .setComponentUuids(uuids);
+
+ assertThatCountByQueryThrowsIAE(query, "Too many component UUIDs in query");
+ }
+
+ private void assertThatCountByQueryThrowsIAE(ComponentQuery.Builder query, String expectedMessage) {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage(expectedMessage);
+
+ underTest.countByQuery(dbSession, query.build());
+ }
+
@Test
public void select_ghost_projects() {
OrganizationDto organization = db.organizations().insert();
}
@Test
- public void countByQuery_with_organization_throws_NPE_of_organizationUuid_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("organizationUuid can't be null");
+ public void selectByQuery_throws_IAE_if_too_many_component_ids() {
+ Set<Long> ids = LongStream.range(0L, 1_010L).boxed().collect(Collectors.toSet());
+ ComponentQuery.Builder query = ComponentQuery.builder()
+ .setQualifiers(Qualifiers.PROJECT)
+ .setComponentIds(ids);
- underTest.countByQuery(dbSession, null, ALL_PROJECTS_COMPONENT_QUERY);
+ assertThatSelectByQueryThrowsIAE(query, "Too many component ids in query");
+ }
+
+ @Test
+ public void selectByQuery_throws_IAE_if_too_many_component_keys() {
+ Set<String> keys = IntStream.range(0, 1_010).mapToObj(String::valueOf).collect(Collectors.toSet());
+ ComponentQuery.Builder query = ComponentQuery.builder()
+ .setQualifiers(Qualifiers.PROJECT)
+ .setComponentKeys(keys);
+
+ assertThatSelectByQueryThrowsIAE(query, "Too many component keys in query");
+ }
+
+ @Test
+ public void selectByQuery_throws_IAE_if_too_many_component_uuids() {
+ Set<String> uuids = IntStream.range(0, 1_010).mapToObj(String::valueOf).collect(Collectors.toSet());
+ ComponentQuery.Builder query = ComponentQuery.builder()
+ .setQualifiers(Qualifiers.PROJECT)
+ .setComponentUuids(uuids);
+
+ assertThatSelectByQueryThrowsIAE(query, "Too many component UUIDs in query");
+ }
+
+ private void assertThatSelectByQueryThrowsIAE(ComponentQuery.Builder query, String expectedMessage) {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage(expectedMessage);
+
+ underTest.selectByQuery(dbSession, query.build(), 0, Integer.MAX_VALUE);
}
@Test