Repository navigation
feat(jdbc): implement getGeneratedKeys for inserts - #82
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #39. Part of #54.
What changed
LibsqlStatement.getGeneratedKeys()now returns the generated key after anINSERTorREPLACE(includingWITH ... INSERTandINSERT ... RETURNING). It returns a fresh empty one-column result when no insert has run.LibsqlPreparedStatementproduces the same generated-key result for prepared inserts.LibsqlDatabaseMetaData.supportsGetGeneratedKeys()now returnstrue, matching the implemented behavior.LibsqlGeneratedKeysTest(unit) andGeneratedKeysIT(live sqld).Contract decision
Matches xerial 3.53.4.0
CoreStatement.java:181-210:last_insert_rowid()and one row containing the key.getGeneratedKeys()returns one empty column instead of throwing.INSERT/REPLACE-with-optional-CTE forms as xerial, using a linear scanner instead of xerial's backtracking regex.executeQuerydoes not replace an earlier generated key, matching xerial'supdateGeneratedKeyscall sites.The key comes from the Hrana execute result's
last_insert_rowidfield instead of a follow-upSELECT last_insert_rowid(). Auto-commit statements run on a one-shot Hrana stream, so a follow-up query observes a different session. Live sqld confirms the insert result carries the key while a separate pipeline'sselect last_insert_rowid()returns0. This is not an observable deviation; it is how the same-session value is obtained before the stream closes.Evidence
Failing before the fix (
./mvnw -pl libsql-jdbc -am test -Dtest=LibsqlGeneratedKeysTest -Dsurefire.failIfNoSpecifiedTests=false):Passing after the fix:
./mvnw -pl libsql-jdbc -am test—Tests run: 148, Failures: 0, Errors: 0, Skipped: 0,BUILD SUCCESS.DOCKER_CONFIG=/tmp/docker-config-empty ./mvnw -pl libsql-itests -am verify—Tests run: 21, Failures: 0, Errors: 0, Skipped: 0,BUILD SUCCESS../mvnw -pl libsql-conformance -am verify—Tests run: 115, Failures: 0, Errors: 0, Skipped: 1,BUILD SUCCESS../mvnw spotless:applythen./mvnw spotless:check—BUILD SUCCESS.Docker config workaround used: nono denies reads of
~/.docker/config.json, so the integration run usedDOCKER_CONFIG=/tmp/docker-config-emptywith/tmp/docker-config-empty/config.jsoncontaining{"auths":{}}. Theghcr.io/tursodatabase/libsql-server:latestimage is public, so no credentials were needed.The live test covers statement inserts, prepared inserts, inserts inside a transaction, empty results for non-inserts, and two concurrent connections each getting their own key. It also shows the session-state risk directly: a separate one-shot
select last_insert_rowid()returns0, so generated keys must come from the insert's own result.Deviations
None.