Skip to content

Commit

Permalink
Merge pull request #48 from xingyutangyuan/master
Browse files Browse the repository at this point in the history
Add examples of SafeQuery
  • Loading branch information
fluentfuture authored Nov 29, 2023
2 parents de74060 + 1c0845f commit c8d6ac5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 3 deletions.
23 changes: 23 additions & 0 deletions mug-errorprone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Compile Time Plugin for `StringFormat`

The `com.google.mu.util.StringFormat` class in Mug and `com.google.mu.safesql.SafeQuery` class provide parsing and formatting functionality based on a string template. This artifact provides compile-time checks to help using these classes safely.

If you use bazel, the `mug:format` and `mug-guava:safe_sql` build targets export the plugin out of the box.

If you use Maven, add the following POM snippet to your `maven-compiler-plugin`:

```
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.23.0</version>
</path>
<path>
<groupId>com.google.mug</groupId>
<artifactId>mug-errorprone</artifactId>
<version>7.0</version>
</path>
</configuration>
```
1 change: 1 addition & 0 deletions mug-examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ junit_tests(
"//mug:base",
"//mug:format",
"//mug-guava",
"//mug-guava:safe_sql",
"@maven//:com_google_guava_guava",
"@maven//:com_google_truth_truth",
"@maven//:com_google_truth_extensions_truth_java8_extension",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.google.mu.examples;

import static com.google.common.truth.Truth8.assertThat;
import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import com.google.mu.safesql.SafeQuery;
import com.google.mu.util.StringFormat;

@RunWith(JUnit4.class)
Expand All @@ -32,6 +34,13 @@ public class HowToUseStringFormatTest {
.containsExactly("front desk", "12:00");
}

@Test public void safeQueryExample() {
String id = "foo";
StringFormat.To<SafeQuery> whereClause = SafeQuery.template("WHERE id = '{id}'");
assertThat(whereClause.with(id))
.isEqualTo(SafeQuery.of("WHERE id = 'foo'"));
}

@SuppressWarnings("StringUnformatArgsCheck")
String failsBecauseTwoLambdaParametersAreExpected() {
return new StringFormat("{key}:{value}").parseOrThrow("k:v", key -> key);
Expand All @@ -46,4 +55,9 @@ String failsBecauseLambdaParameterNamesAreOutOfOrder() {
String failsDueToBadPlaceholderName() {
return new StringFormat("{?}:{-}").parseOrThrow("k:v", (k, v) -> k);
}

@SuppressWarnings("StringFormatArgsCheck")
SafeQuery mismatchingPlaceholderInSafeQueryTemplate(String name) {
return SafeQuery.template("WHERE id = '{id}'").with(name);
}
}
18 changes: 17 additions & 1 deletion mug-guava/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
SAFE_SQL_SRCS = glob(["src/main/java/com/google/mu/safesql/*.java"])

java_library(
name = "mug-guava",
srcs = glob(["src/main/java/**/*.java"]),
srcs = glob(["src/main/java/**/*.java"], exclude = SAFE_SQL_SRCS),
visibility = ["//visibility:public"],
deps = [
"@@maven//:com_google_guava_guava",
Expand All @@ -10,6 +12,18 @@ java_library(
],
)

java_library(
name = "safe_sql",
srcs = SAFE_SQL_SRCS,
visibility = ["//visibility:public"],
deps = [
"@@maven//:com_google_guava_guava",
"@maven//:com_google_errorprone_error_prone_annotations",
"//mug:base",
"//mug:format",
],
)


load("@com_googlesource_gerrit_bazlets//tools:junit.bzl", "junit_tests")

Expand All @@ -19,7 +33,9 @@ junit_tests(
srcs = glob(["src/test/java/**/*Test.java"]),
deps = [
":mug-guava",
":safe_sql",
"//mug:base",
"//mug:format",
"@maven//:com_google_guava_guava",
"@maven//:com_google_guava_guava_testlib",
"@maven//:com_google_truth_truth",
Expand Down
9 changes: 9 additions & 0 deletions mug-guava/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Using Mug with Guava

This artifact provides utilities with both Mug and Guava dependencies.

* `SafeQuery` builds SQL queries using the same template syntax as `StringFormat`, while preventing SQL injection.
* `GuavaCollectors` provide extra `Collector` and `BiCollector`s.
* `CaseBreaker` helps breaking apart camelCase, snake_case, UPPER_SNAKE_CASE and dash-case words.
* `BinarySearch` is a generic binary search algorithm that can be used for use cases beyond `List` and arrays.
* `Immutables` has more convenient factory methods for Guava immutable collections.
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,15 @@ public void badPlaceholderName() {
.isEqualTo(SafeQuery.of("SELECT * FROM jobs WHERE id = x"));
}

@SuppressWarnings("LabsStringFormatArgsCheck")
@SuppressWarnings("StringFormatArgsCheck")
@Test
public void placeholderNameDoesNotMatch() {
StringFormat.To<SafeQuery> template = template("SELECT * FROM {table} WHERE id = {id}");
assertThat(template.with(/* table */ SafeQuery.of("jobs"), /* id */ SafeQuery.of("x")))
.isEqualTo(SafeQuery.of("SELECT * FROM jobs WHERE id = x"));
}

@SuppressWarnings("LabsStringFormatArgsCheck")
@SuppressWarnings("StringFormatArgsCheck")
@Test
public void wrongNumberOfArgs() {
assertThrows(
Expand Down
1 change: 1 addition & 0 deletions mug-protobuf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Extra protobuf utilities

0 comments on commit c8d6ac5

Please sign in to comment.