Skip to content

Commit

Permalink
fix(tgsql): binary for repl output
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Jul 25, 2024
1 parent 7b270f6 commit 26b53f3
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.tsurugidb.tgsql.cli.repl;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -37,6 +45,12 @@ public ReplResultProcessor(@Nonnull TgsqlConfig config, @Nonnull ReplReporter re
this.reporter = reporter;
}

// for test
ReplResultProcessor() {
this.config = null;
this.reporter = null;
}

@Override
public long process(ResultSet target) throws ServerException, IOException, InterruptedException {
dumpMetadata(target.getMetadata());
Expand All @@ -50,6 +64,7 @@ public long process(ResultSet target) throws ServerException, IOException, Inter
boolean over = false;

var columnList = new ArrayList<Object>();
var sb = new StringBuilder();
int rowSize = 0;
while (ResultSetUtil.fetchNextRow(target, target.getMetadata(), columnList::add)) {
if (maxLines >= 0) {
Expand All @@ -59,7 +74,8 @@ public long process(ResultSet target) throws ServerException, IOException, Inter
}
}

reporter.reportResultSetRow(columnList.toString());
appendTo(sb, columnList);
reporter.reportResultSetRow(sb.toString());
rowSize++;
columnList.clear();

Expand Down Expand Up @@ -110,6 +126,70 @@ public String toString() {
}
}

private void appendTo(StringBuilder sb, List<Object> columnList) {
sb.setLength(0);
sb.append('[');

int i = 0;
for (Object value : columnList) {
if (i++ != 0) {
sb.append(", ");
}
appendTo(sb, value);
}

sb.append(']');
}

private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSS");
private static final DateTimeFormatter OFFSET_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSSXXX");
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSSSSS");
private static final DateTimeFormatter OFFSET_TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSSSSSXXX");

void appendTo(StringBuilder sb, Object value) {
if (value instanceof BigDecimal) {
var v = (BigDecimal) value;
sb.append(v.toPlainString());
return;
}

if (value instanceof LocalDate) {
var v = (LocalDate) value;
sb.append(DATE_FORMATTER.format(v));
return;
}
if (value instanceof LocalDateTime) {
var v = (LocalDateTime) value;
sb.append(DATETIME_FORMATTER.format(v));
return;
}
if (value instanceof OffsetDateTime) {
var v = (OffsetDateTime) value;
sb.append(OFFSET_DATETIME_FORMATTER.format(v));
return;
}
if (value instanceof LocalTime) {
var v = (LocalTime) value;
sb.append(TIME_FORMATTER.format(v));
return;
}
if (value instanceof OffsetTime) {
var v = (OffsetTime) value;
sb.append(OFFSET_TIME_FORMATTER.format(v));
return;
}

if (value instanceof byte[]) {
for (byte v : (byte[]) value) {
sb.append(String.format("%02x", v));
}
return;
}

sb.append(value);
}

@Override
public void close() throws IOException {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.tsurugidb.tgsql.cli.repl;

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

import java.io.IOException;
import java.io.UncheckedIOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;

import org.junit.jupiter.api.Test;

class ReplResultProcessorTest {

@Test
void appendTo() {
test("abc", "abc");
test("123", 123);
test("123", BigDecimal.valueOf(123));

test("2024-07-25", LocalDate.of(2024, 7, 25));
test("2024-07-25 01:02:03.123456789", LocalDateTime.of(2024, 7, 25, 1, 2, 3, 123456789));
test("2024-07-25 23:59:59.000000000", LocalDateTime.of(2024, 7, 25, 23, 59, 59, 0));
test("2024-07-25 01:02:03.123456789+09:00", OffsetDateTime.of(2024, 7, 25, 1, 2, 3, 123456789, ZoneOffset.ofHours(9)));
test("2024-07-25 23:59:59.000000000+09:00", OffsetDateTime.of(2024, 7, 25, 23, 59, 59, 0, ZoneOffset.ofHours(9)));
test("2024-07-25 23:59:59.000000000Z", OffsetDateTime.of(2024, 7, 25, 23, 59, 59, 0, ZoneOffset.UTC));
test("01:02:03.123456789", LocalTime.of(1, 2, 3, 123456789));
test("23:59:59.000000000", LocalTime.of(23, 59, 59, 0));
test("01:02:03.123456789+09:00", OffsetTime.of(1, 2, 3, 123456789, ZoneOffset.ofHours(9)));
test("23:59:59.000000000+09:00", OffsetTime.of(23, 59, 59, 0, ZoneOffset.ofHours(9)));
test("23:59:59.000000000Z", OffsetTime.of(23, 59, 59, 0, ZoneOffset.UTC));

test("0123456789abcdef", new byte[] { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef });
}

private void test(String expected, Object value) {
var sb = new StringBuilder();

try (var target = new ReplResultProcessor()) {
target.appendTo(sb, value);
} catch (IOException e) {
throw new UncheckedIOException(e);
}

assertEquals(expected, sb.toString());
}
}

0 comments on commit 26b53f3

Please sign in to comment.