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 @@ -35,7 +35,8 @@ public LibsqlDatabaseMetaData(LibsqlConnection connection) {

private String resolveDatabaseProductVersion() throws SQLException {
if (cachedDatabaseProductVersion == null) {
try (var rs = connection.createStatement().executeQuery("SELECT sqlite_version()")) {
try (var statement = connection.createStatement();
var rs = statement.executeQuery("SELECT sqlite_version()")) {
cachedDatabaseProductVersion = rs.next() ? rs.getString(1) : "3.0";
} catch (SQLException e) {
cachedDatabaseProductVersion = "3.0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -36,6 +37,33 @@ void getTablesClosesResultSetWhenStatementCloses() throws SQLException {
assertThat(resultSet.isClosed()).isTrue();
}

@Test
void getDatabaseProductVersionClosesItsStatement() throws SQLException {
RecordingConnection connection = new RecordingConnection(versionResult("3.45.1"));

new LibsqlDatabaseMetaData(connection).getDatabaseProductVersion();

assertThat(connection.statements)
.as("the product-version lookup must open exactly one statement")
.hasSize(1);
assertThat(connection.statements.get(0).isClosed())
.as("the product-version lookup must close its statement")
.isTrue();
}

@Test
void getDatabaseProductVersionOpensOneStatementAcrossTwoCalls() throws SQLException {
RecordingConnection connection = new RecordingConnection(versionResult("3.45.1"));
LibsqlDatabaseMetaData metadata = new LibsqlDatabaseMetaData(connection);

assertThat(metadata.getDatabaseProductVersion()).isEqualTo("3.45.1");
assertThat(metadata.getDatabaseProductVersion()).isEqualTo("3.45.1");

assertThat(connection.statements)
.as("the second product-version lookup must hit the cache and open nothing")
.hasSize(1);
}

@Test
void escapesSingleQuoteInTableNamePattern() throws SQLException {
RecordingConnection connection = new RecordingConnection(emptyResult());
Expand Down Expand Up @@ -280,6 +308,14 @@ private static StmtResult emptyResult() {
return new StmtResult(List.of(new Col("TABLE_NAME", "TEXT")), List.of(), 0, null);
}

private static StmtResult versionResult(String version) {
return new StmtResult(
List.of(new Col("sqlite_version()", "TEXT")),
List.of(new Row(List.of(Value.of(version)))),
0,
null);
}

private static final class StmtResultConnection extends LibsqlConnection {
private final StmtResult result;

Expand Down Expand Up @@ -391,13 +427,21 @@ private static String literalAfter(String sql, String prefix, String suffix) thr

private static final class RecordingConnection extends LibsqlConnection {
private final StmtResult result;
private final List<LibsqlStatement> statements = new ArrayList<>();
private String lastSql;

private RecordingConnection(StmtResult result) {
super(null, null);
this.result = result;
}

@Override
public Statement createStatement() throws SQLException {
LibsqlStatement statement = (LibsqlStatement) super.createStatement();
statements.add(statement);
return statement;
}

@Override
public StmtResult executeStatement(Stmt stmt) throws SQLException {
lastSql = stmt.sql();
Expand Down
Loading