Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,15 @@ public String getString(int columnIndex) throws SQLException {
};
}

/**
* Converts every storage class through {@link #getInt(int)}, matching sqlite-jdbc. NULL and values
* whose integer coercion is zero are false: non-numeric text such as {@code "true"}, REAL values
* between -1 and 1, and empty or non-numeric blobs. Integers, REAL values truncated to a nonzero
* integer, and text or blob numeric prefixes such as {@code "2"} or {@code x'313233'} are true.
*/
@Override
public boolean getBoolean(int columnIndex) throws SQLException {
Value val = getCurrentValue(columnIndex);
return switch (val) {
case Value.Null n -> false;
case Value.Integer i -> i.value() != 0;
case Value.Float f -> f.value() != 0.0;
case Value.Text t -> "true".equalsIgnoreCase(t.value()) || "1".equals(t.value());
case Value.Blob b -> b.bytes().length > 0 && b.bytes()[0] != 0;
};
return getInt(columnIndex) != 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.magnusp.libsql.jdbc;

import com.github.magnusp.libsql.hrana.codec.Col;
import com.github.magnusp.libsql.hrana.codec.Row;
import com.github.magnusp.libsql.hrana.codec.StmtResult;
import com.github.magnusp.libsql.hrana.codec.Value;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

class LibsqlResultSetGetBooleanTest {

static Stream<Arguments> storageClasses() {
return Stream.of(
Arguments.of("text 'true'", new Value.Text("true"), false),
Arguments.of("text '0'", new Value.Text("0"), false),
Arguments.of("text '2'", new Value.Text("2"), true),
Arguments.of("real 0.5", new Value.Float(0.5), false),
Arguments.of("real 2.0", new Value.Float(2.0), true),
Arguments.of(
"blob x'313233'",
new Value.Blob("123".getBytes(StandardCharsets.UTF_8)),
true),
Arguments.of("null", new Value.Null(), false));
}

@ParameterizedTest(name = "{0}")
@MethodSource("storageClasses")
void getBooleanConvertsThroughIntegerLikeXerial(String name, Value value, boolean expected)
throws SQLException {
LibsqlResultSet rs = resultSet(value);
assertThat(rs.next()).isTrue();

assertThat(rs.getBoolean(1)).isEqualTo(expected);
assertThat(rs.getBoolean(1)).isEqualTo(rs.getInt(1) != 0);
}

@Test
void reproductionValuesMatchSqliteJdbc() throws SQLException {
LibsqlResultSet rs = resultSet(
new Value.Text("true"),
new Value.Text("2"),
new Value.Float(0.5));

assertThat(rs.next()).isTrue();

assertThat(rs.getBoolean(1)).isFalse();
assertThat(rs.getBoolean(2)).isTrue();
assertThat(rs.getBoolean(3)).isFalse();
}

private static LibsqlResultSet resultSet(Value... values) {
List<Col> cols = Stream.generate(() -> new Col("c", null)).limit(values.length).toList();
StmtResult result = new StmtResult(cols, List.of(new Row(List.of(values))), 1, null);
return new LibsqlResultSet(null, result);
}
}
Loading