NAME

DBIO::DB2::DDL - Generate DB2 DDL from DBIO Result classes

VERSION

version 0.900007

DESCRIPTION

DBIO::DB2::DDL generates a DB2 DDL script from a DBIO::Schema class hierarchy. It is the desired-state side of the test-deploy-and- compare strategy used by DBIO::DB2::Deploy.

my $ddl = DBIO::DB2::DDL->install_ddl($schema_class_or_instance);

The output is plain SQL, suitable for executing one statement at a time against a fresh DB2 database. Emits CREATE TABLE (inline columns, primary key, named UNIQUE and named FOREIGN KEY constraints) and CREATE INDEX.

The unique_constraints of a source are emitted as CONSTRAINT "name" UNIQUE ("col", ...) inside CREATE TABLE, under the name the source gives them (ADR 0009). A constraint over a nullable column is refused with a croak naming table and column: Db2 answers it with SQL0542N.

Foreign keys are emitted as named inline constraints (CONSTRAINT fk_<table>_<cols> FOREIGN KEY ...) with a deterministic name derived from the relationship. DB2 enforces referential integrity, so the install DDL must carry the FKs; the deterministic name gives the live database and the throwaway compare schema the same stable constraint identity, which is what makes the name-based FK diff in DBIO::DB2::Diff::ForeignKey sound (see ADR 0005). New-table FKs are also rendered inline by DBIO::DB2::Diff::Table on the diff path.

METHODS

install_ddl

my $ddl = DBIO::DB2::DDL->install_ddl($schema);

Returns the full installation DDL as a single string.

An optional second argument names the schema the views are created in, as the catalog holds it (DB2INST1). DBIO::DB2::Deploy passes the connection's CURRENT SCHEMA. Without it the schema is the CURRENT SCHEMA of the connection of $schema (a schema object, whose storage connects if it has not yet; "current_schema"), the install's target schema. A view that reads a view of the set through a qualifier equal to it (DB2INST1.v) reads that view, as it would unqualified; through another qualifier it reads a view of another schema and depends on nothing. Where the schema is not known (a class, no connection, no answer) such a qualified read of a view of the set is refused: the order cannot be decided ("order_views").

current_schema

my $schema_name = DBIO::DB2::DDL::current_schema($dbh);

The CURRENT SCHEMA of the handle, right-trimmed (the register is CHAR(128), space-padded; the catalog holds names unpadded). The one place that reads it: DBIO::DB2::Deploy and "install_ddl" both ask here.

qualified_tables

my @pairs = DBIO::DB2::DDL->qualified_tables($schema);   # [ 'other.t', 'other' ], ...

The tables of the schema named schema.table (not the views), with their schema, sorted by name. DBIO::DB2::Deploy reads these schemas, the declared tables of them only, besides the current one.

view_reads

my @names = DBIO::DB2::DDL::view_reads($view_definition);
my @names = DBIO::DB2::DDL::view_reads($view_definition, $schema);

The names a view definition reads as a table or view: every name that stands where a FROM or a JOIN (or a comma in a FROM list) expects a table, in a subquery, a derived table or a join group as much as at the top, in order of appearance, without repeats.

A name is returned as the catalog holds it: a quoted name ("x") as written, an unquoted one upper-case, which is what Db2 folds it to. The text is split into names, words, strings and comments first, so a name inside a string literal or a comment reads nothing; a quoted identifier is never a keyword; a name followed by ( is a call (TABLE(f(1))), not a table; the FROM of TRIM, EXTRACT and SUBSTRING and of IS DISTINCT FROM opens no table list. It reads text, not the server, because "install_ddl" has no database to ask, and it is the one detection for the install and the diff.

Common table expressions. The names a WITH clause defines (WITH a AS (...), b (x, y) AS (...) SELECT ...) are not read as views: a FROM a that means such a name is no dependency, whether or not a view a exists. A name is a CTE from its own definition on (so a recursive one reads itself as the CTE), in the select the WITH belongs to and in everything nested in it; a body that reads a name defined only after it, or a qualified name, reads a table or view. The reads inside the bodies count as any other. WITH UR and WITH CHECK OPTION are no CTE. A WITH that starts like a CTE but is not one that can be read (a missing AS, a missing body) throws a DBIO::Exception: the order it would give cannot be trusted.

A schema qualifier (s.name). Without $schema every read is returned by its name, the qualifier dropped: the list is then what the definition may depend on, and it is "order_views" that refuses what it cannot decide (below). With $schema, the name of the schema the views are created in as the catalog holds it (DB2INST1), a read qualified with another schema is not returned: it is no view of this set. A qualifier equal to $schema counts as no qualifier. The qualifier is compared as the catalog holds it, like the names: an unquoted one upper-case, a quoted one as written.

Still not recognised, so that the server may refuse the statement: a view read through a synonym or an alias, a view named in a dynamic way.

view_dependencies

my @names = DBIO::DB2::DDL::view_dependencies(\@set, $name, $definition, $schema);

The views of @set (names) the view $name with $definition reads, sorted, never the view itself: what "view_reads" returns, matched against the set (a name as written, else without regard to case when exactly one view of the set has that name that way -- an unquoted read is the upper-case name, the views of this driver are written in quotes). It is the one decision shared by "order_views" and by DBIO::DB2::Diff::View.

A read qualified with a schema is a dependency when $schema is given and the qualifier is that schema, and none when it is another schema (the view of another schema is not one of the set). Without $schema the qualifier cannot be judged: if the name of such a read is a view of the set it throws a DBIO::Exception naming the view and the read, before any statement has been written, because dropping the qualifier would order by a view that may be another one, and keeping it may leave out a real dependency. A qualified read of a name that is no view of the set is no dependency either way.

order_views

my @names = DBIO::DB2::DDL::order_views(\%definitions);
my @names = DBIO::DB2::DDL::order_views(\%definitions, $schema);

The order in which a set of views is created, shared by the install ("install_ddl") and by the diff (DBIO::DB2::Diff::View). %definitions maps a view name to its definition (SQL text, or undef for a view that reads nothing). A view goes after the views of the set it reads ("view_dependencies"); of the views free to go the first by name is next, so views free of each other stay in name order. A read of the view itself, of a name that is no view of the set, of a CTE of that name ("view_reads") or of a view of another schema is no dependency. The drop order is this one reversed: a view goes before the views it reads.

$schema is the schema the views are created in (see "view_dependencies"). The install does not know it and the diff takes it from the live model; without it, a qualified read of a name that is a view of the set is refused with a DBIO::Exception instead of being guessed at (ADR 0011 addendum).

When the views read each other in a cycle no order creates them, and the call throws a DBIO::Exception naming the views on the cycle, before any statement has been written.

AUTHOR

DBIO & DBIx::Class Authors

COPYRIGHT AND LICENSE

Copyright (C) 2026 DBIO Authors Portions Copyright (C) 2005-2025 DBIx::Class Authors Based on DBIx::Class, heavily modified.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.