Skip to content

Commit 4af3bd9

Browse files
committed
postfix query.when(condition)
1 parent 660fcd7 commit 4af3bd9

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

mug-guava/src/main/java/com/google/mu/safesql/SafeQuery.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ public static SafeQuery when(
119119
return condition ? of(query, args) : EMPTY;
120120
}
121121

122+
/**
123+
* Returns this SafeQuery if {@code condition} is true; otherwise returns {@link #EMPTY}.
124+
*
125+
* @since 8.4
126+
*/
127+
public SafeQuery when(boolean condition) {
128+
return condition ? this : EMPTY;
129+
}
130+
122131
/**
123132
* An optional query that's only rendered if {@code arg} is present; otherwise returns {@link
124133
* #EMPTY}. It's for use cases where a subquery is only added when present, for example the

mug-guava/src/main/java/com/google/mu/safesql/SafeSql.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,15 @@ public static SafeSql when(
412412
return condition ? of(template, params) : EMPTY;
413413
}
414414

415+
/**
416+
* Returns this SafeSql if {@code condition} is true; otherwise returns {@link #EMPTY}.
417+
*
418+
* @since 8.4
419+
*/
420+
public SafeSql when(boolean condition) {
421+
return condition ? this : EMPTY;
422+
}
423+
415424
/**
416425
* Returns a {@link Template} of {@link SafeSql} based on the {@code template} string.
417426
* Useful for creating a constant to be reused with different parameters.

mug-guava/src/test/java/com/google/mu/safesql/SafeQueryTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,17 @@ public void when_conditionalIsTrue_returnsQuery() {
10121012
.isEqualTo(SafeQuery.of("WHERE id = 1"));
10131013
}
10141014

1015+
@Test
1016+
public void postfixWhen_conditionalIsFalse_returnsEmpty() {
1017+
assertThat(SafeQuery.of("WHERE id = {id}", 1).when(false)).isEqualTo(SafeQuery.EMPTY);
1018+
}
1019+
1020+
@Test
1021+
public void postfixWhen_conditionalIsTrue_returnsQuery() {
1022+
assertThat(SafeQuery.of("WHERE id = {id}", 1).when(true))
1023+
.isEqualTo(SafeQuery.of("WHERE id = 1"));
1024+
}
1025+
10151026
@Test
10161027
public void optionally_optionalArgIsEmpty_returnsEmpty() {
10171028
assertThat(SafeQuery.optionally("WHERE id = {id}", /* id */ Optional.empty()))

mug-guava/src/test/java/com/google/mu/safesql/SafeSqlTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,17 @@ public void when_conditionalIsTrue_returnsQuery() {
664664
.isEqualTo(SafeSql.of("WHERE id = {id}", 1));
665665
}
666666

667+
@Test
668+
public void postfixWhen_conditionalIsFalse_returnsEmpty() {
669+
assertThat(SafeSql.of("WHERE id = {id}", 1).when(false)).isEqualTo(SafeSql.EMPTY);
670+
}
671+
672+
@Test
673+
public void postfixWhen_conditionalIsTrue_returnsQuery() {
674+
assertThat(SafeSql.of("WHERE id = {id}", 1).when(true))
675+
.isEqualTo(SafeSql.of("WHERE id = {id}", 1));
676+
}
677+
667678
@Test
668679
public void optionally_optionalArgIsEmpty_returnsEmpty() {
669680
assertThat(SafeSql.optionally("WHERE id = {id}", /* id */ Optional.empty()))

0 commit comments

Comments
 (0)