Repository navigation
DDL and metadata: deferrable keys, view comments, routines, roles, defaults - #92
Merged
Merged
Conversation
A foreign key checked at the end of the transaction could not be written, as the dialect API had no DEFERRABLE, and the PostgreSQL and Oracle providers reported every key as checked at once. DdlGenerator gains the enum Deferrability (NOT_DEFERRABLE, INITIALLY_IMMEDIATE, INITIALLY_DEFERRED), foreignKeyClause(..., deferrability) and addForeignKeyConstraint(..., deferrability), which write the dialect's clause and DEFERRABLE INITIALLY IMMEDIATE or DEFERRED after it, and supportsDeferrableForeignKeys(), false by default. A key that is not deferrable is the plain clause of every dialect; a deferrable one is empty where the database checks every key at once. PostgreSQL, Oracle and SQLite write it, and Derby from 10.11. SQL Server, MySQL, MariaDB, H2 (which rejects the clause), DuckDB, ClickHouse, Hive, Impala and the ANSI dialect have none. The PostgreSQL provider now reads pg_constraint.condeferrable and condeferred, the Oracle provider ALL_CONSTRAINTS.DEFERRABLE and DEFERRED. The consistency tests check the clauses of every dialect. On PostgreSQL and Oracle a key added as initially deferred lets a child row come before its parent within a transaction and is read back as initially deferred; on SQLite the key goes into the table's definition, as SQLite adds no constraint to an existing table. The metadata contract creates such a key where the database has them and expects every other key to be reported as checked at once. Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
A comment on a view was written with COMMENT ON TABLE by every dialect that takes the standard form, which PostgreSQL rejects for a view, and MySQL, MariaDB, ClickHouse and Hive wrote statements for a view that their databases do not take. commentOnTable now writes COMMENT ON VIEW for a view, and DdlGenerator gains supportsCommentOnView(), true by default. PostgreSQL, H2, DuckDB and the ANSI dialect write COMMENT ON VIEW and COMMENT ON COLUMN, Oracle COMMENT ON TABLE and COLUMN, which it takes for a view, and SQL Server the MS_Description of the view and its column. MySQL and MariaDB keep no comment on a view, ClickHouse and Hive set a comment as a property of a table, and Impala takes Hive's spelling: for a view and its columns these write nothing and say so, as do SQLite and Derby, which keep no comments at all. The consistency tests check what every dialect writes for a view and its column. On PostgreSQL, Oracle and SQL Server, and on H2, the comments the dialect writes on a view and its column are read back as the snapshot reads them. The metadata contract puts a comment on a view's column too; every provider already reported it. Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
A model could hold a function or a procedure, with its parameters and its body, but the dialect API could only write the grants on it, so a routine could not be written into a database. The providers read a routine's body but not the language it is written in. DdlGenerator gains the records RoutineParameter(name, mode, type), RoutineDefinition(schema, name, parameters, returnType, language, body) and RoutineSignature(schema, name, parameterTypes), the enum ParameterMode, routineLanguages(), createFunction(definition, orReplace), createProcedure(definition, orReplace), dropFunction(signature, ifExists) and dropProcedure(signature, ifExists). The body is the database's own and is written unchanged; each statement is one statement for JDBC, without a client's delimiter. A dialect writes nothing for a body in a language it does not take, or for a parameter mode its database has not. - PostgreSQL takes sql and plpgsql: the body in a dollar quote that does not occur in it, OR REPLACE, IN, OUT and INOUT parameters, and DROP with the parameter types, which names one of several overloads. - Oracle takes plsql: RETURN type AS body, IN, OUT and IN OUT, OR REPLACE, IF EXISTS from 23; a schema holds one routine of a name, so DROP names it alone. - SQL Server takes tsql: parameters as @variables, OUTPUT for OUT and INOUT in a procedure, IN only in a function, OR ALTER and DROP ... IF EXISTS from 2016. - MySQL and MariaDB take sql: the body starts with the routine's characteristics where it has them, as the database writes them; IN only in a function, OR REPLACE on MariaDB. - H2's routines are Java methods whose signature is in their source, DuckDB's macros have no types, and SQLite, Derby, ClickHouse, Hive, Impala and the ANSI dialect write no routines. Function and Procedure gain language(), and their records take it as a further component and keep their constructors without it. PostgreSQL reads pg_language, MySQL and MariaDB ROUTINE_BODY, H2 EXTERNAL_LANGUAGE; Oracle's functions and procedures are plsql and SQL Server's tsql where the module holds their text. The consistency tests check the statements of every dialect. On PostgreSQL, Oracle, SQL Server, MySQL and MariaDB a function and a procedure are created, created again where the dialect replaces them, the function is called, both are read back with their parameters, language and body, and dropped by their signature; on PostgreSQL one of two overloads is dropped by its parameter types. The metadata contract expects the language of a function's body. Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
DatabaseServiceImpl reads the procedures and functions of a plain JDBC connection with getProcedures and getFunctions and their parameters with getProcedureColumns and getFunctionColumns by name. Where a schema holds several routines of one name, the driver reports the parameters of all of them, and every routine got them all. A parameter row now belongs to the routine its SPECIFIC_NAME names, read last in the row as JDBC lists it. A row or a routine without a specific name, or a driver without the column, leaves the row to every routine of the name, as before. H2 makes an alias of Integer.toString two procedures, toString(int) and toString(int, int); read through the driver, each now has its own parameters. Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
createRole and dropRole wrote CREATE ROLE and DROP ROLE for every dialect, also where the database has no roles, and could not say IF NOT EXISTS or IF EXISTS, so a script that drops its roles failed where one was missing. DdlGenerator gains createRole(name, ifNotExists) and dropRole(name, ifExists), which are empty where the database has no roles, and supportsRoles(), true by default; the methods without the flag stay as they were. With the flag, an existing or missing role is no error where the dialect can say so; elsewhere the clause is left out. - PostgreSQL has no CREATE ROLE IF NOT EXISTS: a DO block takes an existing role as done, the one the user statement uses; DROP ROLE IF EXISTS. - Oracle: IF [NOT] EXISTS from 23. - SQL Server: a guard on sys.database_principals, as for a user. - MySQL from 8.0, MariaDB, H2, ClickHouse and the ANSI dialect: CREATE ROLE IF NOT EXISTS and DROP ROLE IF EXISTS. - Derby, Hive and Impala have roles without IF [NOT] EXISTS. - DuckDB, SQLite and MySQL before 8.0 have no roles. The consistency tests check the statements of every dialect. On PostgreSQL, SQL Server, Oracle, MySQL and MariaDB a role is created, created again where the dialect can say IF NOT EXISTS, read back by the metadata provider as a role and dropped twice; on H2 the statements run. Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
JDBC asks for a column's default as the database writes it in SQL, a
string as a quoted literal. MySQL keeps a literal default unquoted in
information_schema.COLUMNS, and Connector/J reports it so: DEFAULT
'NEW' came back as NEW, which a reader cannot tell from an expression.
MariaDB quotes a string itself, but reports the text NULL for a
nullable column without a default.
The MySQL provider now reads the columns itself: as the driver reports
them, a schema being a catalog, with the defaults of the schema read
from information_schema.COLUMNS in one query. An expression default,
which EXTRA marks DEFAULT_GENERATED, is given as MySQL keeps it, with
the quotes and backslashes it escapes there unescaped
(lower(_utf8mb4'ABC')); a literal of a number or bit type as it is, and
a hexadecimal one; any other literal, of a text, date, time or
enumeration, quoted, a quote doubled and a backslash escaped. The
MariaDB provider reads the columns the same way and gives the text NULL,
which MariaDB reports for no default and for DEFAULT NULL alike, as no
default.
The metadata contract creates a table with a string, a number and an
expression default and a column without one, and expects on every
database a quoted literal, which the database may decorate
('NEW'::character varying on PostgreSQL, ('NEW') on SQL Server), a
number, an expression that is no literal, and no default. PostgreSQL,
Oracle, SQL Server, H2 and DuckDB gave that already.
Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
The snapshot read the bulk metadata of the schemas its tables lie in and of the current one. A schema that holds functions, procedures, sequences or user-defined types but no table came back without them. Sequences, user-defined types, procedures and functions are now read for every schema the database reports as well, and for a database without schemas (MySQL, MariaDB) every catalog, as a catalog is taken for the schema there. What lies in a table's schema only, keys, indexes, constraints, triggers, partitions and privileges, is read for the schemas of the tables as before. On Oracle XE the database reports 30 users; 17 of them, Oracle's own users without a table such as DIP or GGSYS, are read now, in about 0.6 s once warm, beside about 5.5 s for the routines, sequences and types of the 13 schemas that hold tables, and they add 33 objects. PostgreSQL reports no further system schema; a user schema without a table costs a query or two. A schema with one function and no table is now in the snapshot on H2, PostgreSQL, Oracle, SQL Server, MySQL and MariaDB, with a sequence too on H2. Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
This was referenced Nov 13, 2026
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.
Seven additions to what the dialects write and what the metadata providers read back, one commit each. They close the gaps a consumer meets when it writes a model into a database and reads it again.
Dialect
Deferrability,foreignKeyClause(…, Deferrability),addForeignKeyConstraint(…, Deferrability),supportsDeferrableForeignKeys(). PostgreSQL, Oracle, SQLite and Derby writeDEFERRABLE INITIALLY IMMEDIATE|DEFERRED; the PostgreSQL and Oracle providers now read it back instead of reporting every key as not deferrable.supportsCommentOnView().commentOnTablewritesCOMMENT ON VIEWfor a view where the database has it,COMMENT ON TABLEon Oracle,MS_Descriptionwith the object typeVIEWon SQL Server. MySQL and MariaDB keep no view comments and say so.RoutineParameter,RoutineDefinition,RoutineSignature,routineLanguages(),createFunction,createProcedure,dropFunction,dropProcedure. Written by PostgreSQL (sql,plpgsql), Oracle (plsql), SQL Server (tsql), MySQL and MariaDB (sql); each statement is one JDBC statement without a client delimiter.FunctionandProceduregainlanguage(), read by the providers.createRole(name, ifNotExists),dropRole(name, ifExists),supportsRoles(). Databases without the clause get a guard (PostgreSQL aDOblock, SQL Server a check onsys.database_principals); DuckDB and SQLite have no roles.Metadata
SPECIFIC_NAME, so overloads no longer share each other's parameters.Left out
RETURNS TABLE/SETOF, characteristics as fields, Oracle packages, dropping one overload on any database but PostgreSQL, any routine DDL for H2 and DuckDB.