Skip to content

Commit 46a039a

Browse files
authored
feat(jdbc): report live SQLite version from sqld via sqlite_version() (#21)
## Summary Previously `getDatabaseProductVersion()`, `getDatabaseMajorVersion()`, and `getDatabaseMinorVersion()` in `LibsqlDatabaseMetaData` returned hardcoded values (`"3.0"`, `3`, `0`), regardless of the actual SQLite version running inside the connected sqld instance. ## Changes - Added a private `resolveDatabaseProductVersion()` helper that lazily executes `SELECT sqlite_version()` against the server on first call and caches the result on the metadata object. - `getDatabaseProductVersion()` now returns the real version string (e.g. `"3.46.1"`). - `getDatabaseMajorVersion()` / `getDatabaseMinorVersion()` parse their respective components from that string. - A safe fallback to `"3.0"` / `3` / `0` is retained if the query fails for any reason. ## Testing - `spotless:apply` applied; `spotless:check` passes. - `mvnw test -pl libsql-jdbc` passes.
1 parent 65854b6 commit 46a039a

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

‎libsql-jdbc/src/main/java/com/github/magnusp/libsql/jdbc/LibsqlDatabaseMetaData.java‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@
99
public class LibsqlDatabaseMetaData implements DatabaseMetaData {
1010

1111
private final LibsqlConnection connection;
12+
private String cachedDatabaseProductVersion;
1213

1314
public LibsqlDatabaseMetaData(LibsqlConnection connection) {
1415
this.connection = connection;
1516
}
1617

18+
private String resolveDatabaseProductVersion() throws SQLException {
19+
if (cachedDatabaseProductVersion == null) {
20+
try (var rs = connection.createStatement().executeQuery("SELECT sqlite_version()")) {
21+
cachedDatabaseProductVersion = rs.next() ? rs.getString(1) : "3.0";
22+
} catch (SQLException e) {
23+
cachedDatabaseProductVersion = "3.0";
24+
}
25+
}
26+
return cachedDatabaseProductVersion;
27+
}
28+
1729
@Override
1830
public boolean allProceduresAreCallable() throws SQLException { return false; }
1931
@Override
@@ -37,7 +49,7 @@ public String getURL() throws SQLException {
3749
@Override
3850
public String getDatabaseProductName() throws SQLException { return "SQLite (libSQL)"; }
3951
@Override
40-
public String getDatabaseProductVersion() throws SQLException { return "3.0"; }
52+
public String getDatabaseProductVersion() throws SQLException { return resolveDatabaseProductVersion(); }
4153
@Override
4254
public String getDriverName() throws SQLException { return "libSQL JDBC Driver"; }
4355
@Override
@@ -381,8 +393,14 @@ public Connection getConnection() throws SQLException {
381393
@Override public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) throws SQLException { throw new UnsupportedOperationException(); }
382394
@Override public boolean supportsResultSetHoldability(int holdability) throws SQLException { return holdability == ResultSet.HOLD_CURSORS_OVER_COMMIT; }
383395
@Override public int getResultSetHoldability() throws SQLException { return ResultSet.HOLD_CURSORS_OVER_COMMIT; }
384-
@Override public int getDatabaseMajorVersion() throws SQLException { return 3; }
385-
@Override public int getDatabaseMinorVersion() throws SQLException { return 0; }
396+
@Override public int getDatabaseMajorVersion() throws SQLException {
397+
String[] parts = resolveDatabaseProductVersion().split("\\.");
398+
try { return Integer.parseInt(parts[0]); } catch (NumberFormatException e) { return 3; }
399+
}
400+
@Override public int getDatabaseMinorVersion() throws SQLException {
401+
String[] parts = resolveDatabaseProductVersion().split("\\.");
402+
try { return parts.length > 1 ? Integer.parseInt(parts[1]) : 0; } catch (NumberFormatException e) { return 0; }
403+
}
386404
@Override public int getJDBCMajorVersion() throws SQLException { return 4; }
387405
@Override public int getJDBCMinorVersion() throws SQLException { return 3; }
388406
@Override public int getSQLStateType() throws SQLException { return sqlStateSQL; }

0 commit comments

Comments
 (0)