Skip to content

Commit

Permalink
Join sql fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Nov 27, 2024
1 parent 8e087b0 commit 4800d8a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Includes
<dependency>
<groupId>dev.mccue</groupId>
<artifactId>jdbc</artifactId>
<version>2024.11.25</version>
<version>2024.11.27</version>
</dependency>
```

Expand All @@ -25,7 +25,7 @@ Includes

```groovy
dependencies {
implementation("dev.mccue:jdbc:2024.11.25")
implementation("dev.mccue:jdbc:2024.11.27")
}
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.mccue</groupId>
<artifactId>jdbc</artifactId>
<version>2024.11.25</version>
<version>2024.11.27</version>
<packaging>jar</packaging>

<properties>
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/dev/mccue/jdbc/SQLFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -16,15 +18,15 @@ public final class SQLFragment {

private SQLFragment(String sql, List<?> parameters) {
this.sql = sql;
this.parameters = List.copyOf(parameters);
this.parameters = Collections.unmodifiableList(new ArrayList<>(parameters));
}

public static SQLFragment of(String sql, List<?> parameters) {
return new SQLFragment(sql, parameters);
}

public static SQLFragment of(String sql) {
return new SQLFragment(sql, List.of());
return new SQLFragment(sql, Collections.unmodifiableList(new ArrayList<>()));
}

public String sql() {
Expand Down Expand Up @@ -58,6 +60,22 @@ public SQLFragment concat(SQLFragment other) {
return SQLFragment.of(sql + other.sql, params);
}

public static SQLFragment join(String separator, Collection<SQLFragment> fragments) {
var params = new ArrayList<>();
var fragmentStr = new StringBuilder();

var iter = fragments.iterator();
while (iter.hasNext()) {
var fragment = iter.next();
params.addAll(fragment.parameters);
fragmentStr.append(fragment.sql);
if (iter.hasNext()) {
fragmentStr.append(separator);
}
}
return SQLFragment.of(fragmentStr.toString(), params);
}

public PreparedStatement prepareStatement(Connection connection) throws SQLException {
var stmt = connection.prepareStatement(sql);
int i = 1;
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/dev/mccue/jdbc/test/SQLFragmentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.mccue.jdbc.test;

import dev.mccue.jdbc.SQLFragment;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SQLFragmentTest {
@Test
public void allowsNullParams() {
var fragment = SQLFragment.of("SELECT ?", Arrays.asList(1, null, "a", null));
assertEquals(fragment.parameters(), Arrays.asList(1, null, "a", null));
}

@Test
public void concatsInOrder() {
var fragment = SQLFragment.of("SELECT ?", Arrays.asList(1, null, "a", null))
.concat(SQLFragment.of("FROM ?", Arrays.asList(5, 6)));
assertEquals(fragment.parameters(), Arrays.asList(1, null, "a", null, 5, 6));
}
}

0 comments on commit 4800d8a

Please sign in to comment.