Skip to content
Permalink
Browse files
make SimpleQueryResult a container for client warnings, and expose th…
…ose warnings via QueryResult
  • Loading branch information
maedhroz authored and ifesdjeen committed Jul 10, 2020
1 parent fb4cc70 commit 327c5980b21b1d7c47e0935ef5cb0c0c4c90d3ed
Showing 4 changed files with 78 additions and 2 deletions.
@@ -58,6 +58,8 @@
public interface QueryResult extends Iterator<Row>
{
List<String> names();

List<String> warnings();

default QueryResult filter(Predicate<Row> fn)
{
@@ -80,7 +80,8 @@ public static Builder builder()

private int numColumns = UNSET;
private String[] names;
private List<Object[]> results = new ArrayList<>();
private final List<Object[]> results = new ArrayList<>();
private final List<String> warnings = new ArrayList<>();

public Builder columns(String... columns)
{
@@ -110,6 +111,12 @@ public Builder row(Object... values)
return this;
}

public Builder warning(String message)
{
warnings.add(message);
return this;
}

public SimpleQueryResult build()
{
if (names == null)
@@ -120,7 +127,8 @@ public SimpleQueryResult build()
for (int i = 0; i < numColumns; i++)
names[i] = "unknown";
}
return new SimpleQueryResult(names, results.stream().toArray(Object[][]::new));

return new SimpleQueryResult(names, results.toArray(new Object[0][]), warnings);
}
}

@@ -146,6 +154,12 @@ public List<String> names()
return names;
}

@Override
public List<String> warnings()
{
throw new UnsupportedOperationException("Warnings are not yet supported for " + getClass().getSimpleName());
}

@Override
public boolean hasNext()
{
@@ -176,6 +190,12 @@ public List<String> names()
{
return delegate.names();
}

@Override
public List<String> warnings()
{
return delegate.warnings();
}

@Override
public boolean hasNext()
@@ -63,14 +63,21 @@ public class SimpleQueryResult implements QueryResult
{
private final String[] names;
private final Object[][] results;
private final List<String> warnings;
private final Predicate<Row> filter;
private final Row row;
private int offset = -1;

public SimpleQueryResult(String[] names, Object[][] results)
{
this(names, results, Collections.emptyList());
}

public SimpleQueryResult(String[] names, Object[][] results, List<String> warnings)
{
this.names = Objects.requireNonNull(names, "names");
this.results = results;
this.warnings = Objects.requireNonNull(warnings, "warnings");
this.row = new Row(names);
this.filter = ignore -> true;
}
@@ -79,6 +86,7 @@ private SimpleQueryResult(String[] names, Object[][] results, Predicate<Row> fil
{
this.names = names;
this.results = results;
this.warnings = Collections.emptyList();
this.filter = filter;
this.offset = offset;
this.row = new Row(names);
@@ -89,6 +97,12 @@ public List<String> names()
return Collections.unmodifiableList(Arrays.asList(names));
}

@Override
public List<String> warnings()
{
return Collections.unmodifiableList(warnings);
}

public SimpleQueryResult filter(Predicate<Row> fn)
{
return new SimpleQueryResult(names, results, filter.and(fn), offset);
@@ -23,9 +23,11 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -38,6 +40,7 @@ public void empty()
QueryResult result = QueryResults.empty();

assertThat(result.names()).isEmpty();
assertThat(result.warnings()).isEmpty();
assertThat(result.toString()).isEqualTo("[]");

assertThat(result.hasNext()).isFalse();
@@ -52,6 +55,43 @@ public void empty()
assertThatThrownBy(it::next).isInstanceOf(NoSuchElementException.class);
}

@Test
public void warnings()
{
String[] names = { "fname", "lname"};
Object[][] rows = { new Object[] { "david", "capwell"} };
SimpleQueryResult result = new SimpleQueryResult(names, rows, Collections.singletonList("test warning"));
assertThat(result.warnings()).isEqualTo(Collections.singletonList("test warning"));
}

@Test
public void delegatedWarnings()
{
String[] names = { "fname", "lname"};
Object[][] rows = { new Object[] { "david", "capwell"} };
SimpleQueryResult delegate = new SimpleQueryResult(names, rows, Collections.singletonList("test warning"));
QueryResult filteredResult = QueryResults.filter(delegate, Objects::nonNull);
assertThat(filteredResult.warnings()).isEqualTo(Collections.singletonList("test warning"));
}

@Test
public void warningsFromBuilder()
{
String[] names = { "fname", "lname"};
Object[][] rows = { new Object[] { "david", "capwell"} };
String warning = "test warning";
SimpleQueryResult expected = new SimpleQueryResult(names, rows, Collections.singletonList(warning));

SimpleQueryResult fromBuilder = QueryResults.builder()
.columns(names)
.row(rows[0])
.warning(warning)
.build();

assertThat(fromBuilder.warnings()).isEqualTo(Collections.singletonList(warning));
assertThat(fromBuilder.warnings()).isEqualTo(expected.warnings());
}

@Test
public void equals()
{

0 comments on commit 327c598

Please sign in to comment.