Skip to content

jdbc, metadata: a database read as a whole or as its user sees it - #112

Merged
stbischof merged 1 commit into
mainfrom
feat/metadata-scope
Dec 2, 2026
Merged

stbischof merged 1 commit into
mainfrom
feat/metadata-scope

Conversation

@stbischof

@stbischof stbischof commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

A database is read from one of two points of view, and the caller says which:

  • DATABASE – the whole database as its administrator sees it: every schema and object, every user and role, every membership and grant.
  • USER – what the connected user may see: the objects it holds a right on (also in schemas of others), the roles it holds, its memberships and the grants that concern it. What it may not see is left out, without an error.
MetadataProvider provider = MetadataProviders.forConnection(connection, MetadataScope.USER).orElseThrow();
MetaInfo info = new DatabaseServiceImpl().createMetaInfo(connection, provider, /* strict */ false);
info.structureInfo().requestedScope();
info.structureInfo().scope();

This change holds the API, the snapshot, Oracle and PostgreSQL. The providers of the other databases are unchanged: they read one way only and report DATABASE, as the defaults of the interface say.

API

  • MetadataScope { DATABASE, USER }. The scope is a property of the MetadataProvider, not of each method: withScope, scope(), effectiveScope(connection), scopeRequirement(). MetadataProviders and MetadataProviderFactory create a provider for a scope; the methods without one read in DATABASE as before.
  • A snapshot is read in one scope, as a whole. Where the connection may not read in DATABASE, the whole snapshot is read in USER – never part in one and part in the other – and a warning names the missing right. With strict, a MetadataScopeException names the product, both scopes and the right, and nothing is read.
  • StructureInfo.requestedScope() and scope() say what was asked for and what was read. StructureInfoRecord keeps its former constructor.

Oracle

  • DATABASE reads the DBA_ views and needs SELECT_CATALOG_ROLE or SELECT ANY DICTIONARY; USER reads the ALL_ views.
  • The tables come from the objects view of the scope, no longer through the driver: a DATABASE snapshot no longer lists the PUBLIC synonyms as tables.
  • The column privileges are read again: the query named a column ALL_COL_PRIVS does not have, failed, and every table reported none.

PostgreSQL

  • Every user may read the system catalog, so DATABASE needs no right; USER reads the same catalog under the conditions information_schema applies.
  • Grants come from the access control lists, no longer from information_schema, which showed only the grants to or by a role of the reader, repeated a table grant for every column and listed the implicit rights of an owner.

Tests

A contract reads the same objects on Oracle and PostgreSQL in a container, in both scopes: the administrator reads everything; a plain user reads in USER what it may see and, asked for DATABASE, gets the same snapshot or, strictly, the exception naming the right it lacks. The Oracle dialect tests that read with a plain user ask for USER.

A database is read from one of two points of view. DATABASE is the
whole database as its administrator sees it: every schema and object,
every user and role, every membership and grant. USER is what the
connected user may see: the objects it holds a right on, the roles it
holds, its memberships and the grants that concern it.

The scope belongs to the MetadataProvider, not to each method:
withScope gives the provider for a scope, scope() names it,
effectiveScope(connection) says whether the connection may read in it,
and scopeRequirement() names the right DATABASE needs, in the words of
the database. getAllSchemas lists the schemas of the scope. A provider
that reads one way only keeps the defaults, DATABASE and no right
needed. MetadataProviders and MetadataProviderFactory create a provider
for a scope; the methods without one read in DATABASE as before.

A snapshot is read in one scope, as a whole. createMetaInfo asks the
provider once, before reading, whether the connection may read in the
scope asked for. Where it may not, the whole snapshot is read in the
scope it may read in, never part in one and part in the other, and a
warning names the missing right; with strict, MetadataScopeException
names the product, both scopes and the right, and nothing is read.
StructureInfo reports requestedScope() and scope(), both DATABASE where
no scope was asked for; StructureInfoRecord keeps its former
constructor. Where the provider lists the schemas of its scope, they
replace those JDBC lists, and the tables are read schema by schema from
the provider, not from the driver, which reads as the connected user
sees the database. The snapshot without a provider is unchanged.

Oracle reads DATABASE from the DBA_ views, which SELECT_CATALOG_ROLE or
SELECT ANY DICTIONARY opens, and USER from the ALL_ views, which show
such a reader nothing of the schemas of others. The tables come from
the objects view of the scope, no longer through the driver, which
listed the PUBLIC synonyms as tables; the schemas are the users of the
scope.

PostgreSQL shows every user the whole system catalog, so DATABASE needs
no right. The grants are read from the access control lists of tables,
columns, routines, sequences and the database, no longer from
information_schema, which showed only the grants to or by a role of the
reader, repeated a table grant for every column and listed the implicit
rights of an owner; the sequences from pg_sequence. USER reads the same
catalog under the conditions information_schema applies.

The column privileges of Oracle are read by the owner of the table:
ALL_COL_PRIVS names it TABLE_SCHEMA, as ALL_TAB_PRIVS does, only
DBA_COL_PRIVS calls it OWNER. The query named a column ALL_COL_PRIVS
does not have, so it failed and the column privileges of every table
were reported as unreadable.

A contract reads the same objects on each database in a container, in
both scopes: the administrator reads everything, a plain user reads in
USER what it may see and, asked for DATABASE, gets the same snapshot
or, strictly, an exception that names the right it lacks. The tests of
Oracle and PostgreSQL add what each shows differently. Unit tests cover
the defaults of the scope and a snapshot of H2 read in one scope. The
Oracle dialect tests that read with a plain user ask for USER.

Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
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.

1 participant