Skip to content

Commit

Permalink
moved the parsing logic into the get method based on advice from @cretz
Browse files Browse the repository at this point in the history
… in #10
  • Loading branch information
alexanderkjall committed May 1, 2018
1 parent b74be5c commit 9bf382d
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 72 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/postgresql/sql2/PGSubmission.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public Object finish() {
}

public void addRow(DataRow row) {
collector.accumulator().accept(collectorHolder, row);
try {
collector.accumulator().accept(collectorHolder, row);
} catch (Throwable e) {
publicStage.completeExceptionally(e);
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/org/postgresql/sql2/communication/TableCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.postgresql.sql2.communication;

import org.postgresql.sql2.communication.packets.parts.ColumnDescription;

public class TableCell {
private byte[] bytes;
private int start;
private int stop;
private ColumnDescription columnDescription;

public TableCell(byte[] bytes, int start, int stop, ColumnDescription columnDescription) {
this.bytes = bytes;
this.start = start;
this.stop = stop;
this.columnDescription = columnDescription;
}

public byte[] getBytes() {
return bytes;
}

public int getStart() {
return start;
}

public int getStop() {
return stop;
}

public ColumnDescription getColumnDescription() {
return columnDescription;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.postgresql.sql2.communication.packets;

import jdk.incubator.sql2.Result;
import org.postgresql.sql2.communication.TableCell;
import org.postgresql.sql2.communication.packets.parts.ColumnDescription;
import org.postgresql.sql2.util.BinaryHelper;

Expand All @@ -9,7 +10,7 @@
import java.util.Map;

public class DataRow implements Result.Row {
private Map<String, Object> columns;
private Map<String, TableCell> columns;
private long rowNumber;

public DataRow(byte[] bytes, ColumnDescription[] description, long rowNumber) {
Expand All @@ -21,23 +22,29 @@ public DataRow(byte[] bytes, ColumnDescription[] description, long rowNumber) {
for(int i = 0; i < numOfColumns; i++) {
int length = BinaryHelper.readInt(bytes[pos], bytes[pos + 1], bytes[pos + 2], bytes[pos + 3]);
pos += 4;
switch (description[i].getFormatCode()){

case TEXT:
String data = new String(BinaryHelper.subBytes(bytes, pos, pos + length), StandardCharsets.UTF_8);
columns.put(description[i].getName(), description[i].getColumnType().getTextParser().apply(data));
break;
case BINARY:
columns.put(description[i].getName(), description[i].getColumnType().getBinaryParser().apply(bytes, pos, pos + length));
break;
}
columns.put(description[i].getName().toLowerCase(), new TableCell(bytes, pos, pos + length, description[i]));
pos += length;
}
}

@Override
public <T> T get(String id, Class<T> type) {
return (T) columns.get(id);
TableCell tc = columns.get(id.toLowerCase());

if(tc == null) {
throw new IllegalArgumentException("no column with id " + id);
}

switch (tc.getColumnDescription().getFormatCode()){

case TEXT:
String data = new String(BinaryHelper.subBytes(tc.getBytes(), tc.getStart(), tc.getStop()), StandardCharsets.UTF_8);
return (T)tc.getColumnDescription().getColumnType().getTextParser().apply(data);
case BINARY:
return (T)tc.getColumnDescription().getColumnType().getBinaryParser().apply(tc.getBytes(), tc.getStart(), tc.getStop());
}

return null;
}

@Override
Expand Down
60 changes: 59 additions & 1 deletion src/test/java/org/postgresql/sql2/ErrorStatesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collector;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class ErrorStatesTest {
@ClassRule
Expand All @@ -20,7 +22,7 @@ public class ErrorStatesTest {
private static DataSource ds;

@BeforeClass
public static void setUp() throws Exception {
public static void setUp() {
ds = TestUtil.openDB(postgres);

TestUtil.createTable(ds, "tab",
Expand All @@ -44,4 +46,60 @@ public void testSqlError() throws InterruptedException {
assertEquals("select select", ex.getSqlString());
}
}

@Test
public void testGetNameThatIsntUsed() throws InterruptedException {
try (Connection conn = ds.getConnection()) {
CompletionStage<Integer> idF = conn.<Integer>rowOperation("select 100 as t")
.collect(Collector.of(
() -> new int[1],
(a, r) -> a[0] = r.get("notused", Integer.class),
(l, r) -> null,
a -> a[0])
)
.submit()
.getCompletionStage();

idF.toCompletableFuture().get();
fail("the column 'notused' doesn't exist in the result.row and should result in an IllegalArgumentException");
} catch (ExecutionException e) {
IllegalArgumentException ex = (IllegalArgumentException)e.getCause();

assertEquals("no column with id notused", ex.getMessage());
}
}

@Test
public void testGetCaseInsensitive1() throws ExecutionException, InterruptedException {
try (Connection conn = ds.getConnection()) {
CompletionStage<Integer> idF = conn.<Integer>rowOperation("select 100 as t")
.collect(Collector.of(
() -> new int[1],
(a, r) -> a[0] = r.get("T", Integer.class),
(l, r) -> null,
a -> a[0])
)
.submit()
.getCompletionStage();

assertEquals(Integer.valueOf(100), idF.toCompletableFuture().get());
}
}

@Test
public void testGetCaseInsensitive2() throws ExecutionException, InterruptedException {
try (Connection conn = ds.getConnection()) {
CompletionStage<Integer> idF = conn.<Integer>rowOperation("select 100 as T")
.collect(Collector.of(
() -> new int[1],
(a, r) -> a[0] = r.get("t", Integer.class),
(l, r) -> null,
a -> a[0])
)
.submit()
.getCompletionStage();

assertEquals(Integer.valueOf(100), idF.toCompletableFuture().get());
}
}
}
Loading

0 comments on commit 9bf382d

Please sign in to comment.