File tree Expand file tree Collapse file tree 4 files changed +40
-0
lines changed
main/java/com/google/mu/safesql
test/java/com/google/mu/safesql Expand file tree Collapse file tree 4 files changed +40
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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 ()))
Original file line number Diff line number Diff 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 ()))
You can’t perform that action at this time.
0 commit comments