Skip to content

refactor(jdbc): extract MetadataSql query builders - #228

Merged
magnusp merged 1 commit into
mainfrom
refactor/i207-metadata-sql
Oct 24, 2026
Merged

magnusp merged 1 commit into
mainfrom
refactor/i207-metadata-sql

Conversation

@magnusp

@magnusp magnusp commented Oct 24, 2026

Copy link
Copy Markdown
Owner

Fixes #207

What changed

Wave 5a of the LibsqlDatabaseMetaData split. New package-private MetadataSql holds literal(String) and one builder per metadata query: tables, columns, imported keys, exported keys, primary keys, table types, schemas, catalogs, and index info. Argument normalization (null/empty pattern to %) and result shaping stay in LibsqlDatabaseMetaData.

No SQL text changed, no test edited, pom.xml untouched.

Preserved deliberately:

  • AND TABLE_TYPE IN ('TABLE','VIEW') (no space after the comma).
  • types[i].toUpperCase() without a locale.
  • getColumns still passes a null originSql.
  • Empty types still behaves like null.

Proof: generated SQL is byte-identical

Same throwaway probe (MetadataSqlBytesProbe, untracked, deleted before commit) compiled in a worktree of origin/main and in this branch. It stubs LibsqlConnection.executeStatement to record Stmt.sql() for every builder over 8 patterns (null, ``, %, `t`, `O'Brien`, `t'x`, `TABLE`, `sqlite_%`) x 5 type sets (`null`, `{}`, `{"TABLE"}`, `{"table","View"}`, `{"it's","SYSTEM TABLE"}`), both `getIndexInfo` unique modes, and cross-reference with null/child/parent tables.

$ diff -u wt-main/libsql-jdbc/target/metadata-sql-probe.txt wt-207/libsql-jdbc/target/metadata-sql-probe.txt
IDENTICAL
$ sha256sum .../metadata-sql-probe.txt   # both files
83b42bd7c72acdc23593e110c6da7c0948406c77b7f7331ae3bb4807e54be580

106,822 bytes of generated SQL per side, equal byte for byte. The existing assertions (TABLE_NAME LIKE 'O''Brien', UPPER(TYPE) IN ('TABLE', 'VIEW'), AND TABLE_TYPE IN ('TABLE','VIEW'), 'sqlite_schema' AS TABLE_NAME, ORDER BY t.TABLE_RANK, t.TABLE_NAME, p.cid) and the stub dispatch on JOIN pragma_foreign_key_list / FROM pragma_foreign_key_list / FROM pragma_table_info also pass unchanged.

Probe: a changed SQL byte fails a test

Mutated the types separator to sql.append(", ") in MetadataSql.tables:

[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0 ... in ...LibsqlDatabaseMetaDataTest
java.lang.AssertionError:
Expecting actual:
  "... AND TABLE_TYPE IN ('TABLE', 'VIEW') ORDER BY TABLE_TYPE, TABLE_NAME"
to contain:
  "AND TABLE_TYPE IN ('TABLE','VIEW')"
	at ...LibsqlDatabaseMetaDataTest.getTablesWithMultipleTypesUppercasesThem(LibsqlDatabaseMetaDataTest.java:224)

Reverted; the lane below is on the reverted head.

Line counts

  • LibsqlDatabaseMetaData.java: 1057 -> 949 (-108; acceptance: under 950).
  • MetadataSql.java: 133 new.

Lanes

  • ./mvnw -pl libsql-jdbc -am test: Tests run: 396, Failures: 0, Errors: 0, Skipped: 0 — BUILD SUCCESS.
  • ./mvnw spotless:apply then ./mvnw spotless:check: BUILD SUCCESS.
  • ./mvnw -pl libsql-conformance -am verify: Tests run: 272, Failures: 0, Errors: 0, Skipped: 7 — BUILD SUCCESS.

Move the metadata query builders and literal escaping out of
LibsqlDatabaseMetaData into a package-private MetadataSql. Argument
normalization and result shaping stay in the metadata class.

Every SQL string is byte-identical: the builders keep the original
construction, including the comma without a space in
IN ('TABLE','VIEW'), toUpperCase() without a locale, a null originSql
for getColumns, and empty types behaving like null.

LibsqlDatabaseMetaData 1057 -> 949 lines.

Fixes #207
@magnusp
magnusp merged commit e19a419 into main Oct 24, 2026
6 checks passed
@magnusp
magnusp deleted the refactor/i207-metadata-sql branch October 24, 2026 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract MetadataSql query builders from LibsqlDatabaseMetaData

1 participant