| 1 | package DBIO::Oracle::DDL; | ||
| 2 | # ABSTRACT: Generate Oracle DDL from DBIO Result classes | ||
| 3 | |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use DBIO::SQL::Util qw(_quote_ident); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use DBIO::Oracle::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use DBIO::Oracle::Identifier; | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | sub install_ddl { | ||
| 14 | my ($class, $schema) = @_; | ||
| 15 | my @stmts; | ||
| 16 | my @view_stmts; | ||
| 17 | my %seen_table; | ||
| 18 | my %seen_view; | ||
| 19 | |||
| 20 | for my $source_name (sort $schema->sources) { | ||
| 21 | my $source = $schema->source($source_name); | ||
| 22 | my $table_name = $source->name; | ||
| 23 | |||
| 24 | # Views: emit CREATE VIEW after all tables; skip virtual views. | ||
| 25 | if ($source->isa('DBIO::ResultSource::View')) { | ||
| 26 | next if $source->can('is_virtual') && $source->is_virtual; | ||
| 27 | my $vname = ref $table_name eq 'SCALAR' ? $$table_name : $table_name; | ||
| 28 | next if ref $vname || $seen_view{$vname}++; | ||
| 29 | my $def = $source->can('view_definition') ? $source->view_definition : undef; | ||
| 30 | next unless defined $def && length $def; | ||
| 31 | push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', _quote_ident($vname), $def; | ||
| 32 | next; | ||
| 33 | } | ||
| 34 | |||
| 35 | # A scalar-ref name is either a literal table name (\'cd', emitted | ||
| 36 | # unquoted) or an inline subquery source; deref the former, skip the latter. | ||
| 37 | if (ref $table_name eq 'SCALAR') { | ||
| 38 | $table_name = $$table_name; | ||
| 39 | next if $table_name =~ /\s|\(/; | ||
| 40 | } | ||
| 41 | next if ref $table_name; | ||
| 42 | |||
| 43 | # Several result sources can map to one physical table; emit it once. | ||
| 44 | next if $seen_table{$table_name}++; | ||
| 45 | |||
| 46 | # Detect autoincrement columns | ||
| 47 | my @autoinc_cols; | ||
| 48 | for my $col_name ($source->columns) { | ||
| 49 | my $info = $source->column_info($col_name); | ||
| 50 | push @autoinc_cols, $col_name if $info->{is_auto_increment}; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Column definitions | ||
| 54 | my @col_defs; | ||
| 55 | my %is_pk; | ||
| 56 | my @pk_cols = $source->primary_columns; | ||
| 57 | @is_pk{@pk_cols} = (1) x @pk_cols; | ||
| 58 | |||
| 59 | for my $col_name ($source->columns) { | ||
| 60 | my $info = $source->column_info($col_name); | ||
| 61 | my $type = DBIO::Oracle::Type::map_dbio_type_to_oracle($info->{data_type}, size => $info->{size}); | ||
| 62 | my $def = sprintf ' %s %s', _quote_ident($col_name), $type; | ||
| 63 | |||
| 64 | if ($info->{is_auto_increment}) { | ||
| 65 | # SERIAL/BIGSERIAL mapped to NUMBER + sequence (type already set) | ||
| 66 | } | ||
| 67 | |||
| 68 | $def .= ' NOT NULL' if defined $info->{is_nullable} && !$info->{is_nullable}; | ||
| 69 | |||
| 70 | if (defined $info->{default_value} && !$info->{is_auto_increment}) { | ||
| 71 | my $dv = $info->{default_value}; | ||
| 72 | if (ref $dv eq 'SCALAR') { | ||
| 73 | $def .= " DEFAULT $$dv"; | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | $def .= " DEFAULT '$dv'"; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | push @col_defs, $def; | ||
| 81 | } | ||
| 82 | |||
| 83 | # Primary key constraint | ||
| 84 | if (@pk_cols) { | ||
| 85 | push @col_defs, sprintf ' PRIMARY KEY (%s)', | ||
| 86 | join(', ', map { _quote_ident($_) } @pk_cols); | ||
| 87 | } | ||
| 88 | |||
| 89 | my $qualified = _quote_ident($table_name); | ||
| 90 | my $sql = sprintf "CREATE TABLE %s (\n%s\n);", $qualified, join(",\n", @col_defs); | ||
| 91 | push @stmts, $sql; | ||
| 92 | |||
| 93 | # Sequences for autoincrement columns | ||
| 94 | for my $col_name (@autoinc_cols) { | ||
| 95 | my $seq_name = DBIO::Oracle::Identifier::shorten("${table_name}_${col_name}_seq"); | ||
| 96 | push @stmts, sprintf "CREATE SEQUENCE %s;", _quote_ident($seq_name); | ||
| 97 | } | ||
| 98 | |||
| 99 | # Indexes for unique constraints | ||
| 100 | for my $col_name ($source->columns) { | ||
| 101 | my $info = $source->column_info($col_name); | ||
| 102 | if ($info->{is_unique} || $info->{is_single_unique_key}) { | ||
| 103 | push @stmts, sprintf 'CREATE UNIQUE INDEX %s ON %s (%s);', | ||
| 104 | _quote_ident(DBIO::Oracle::Identifier::shorten("${table_name}_${col_name}_idx")), | ||
| 105 | $qualified, | ||
| 106 | _quote_ident($col_name); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | return join " | ||
| 112 | |||
| 113 | ", @stmts, @view_stmts; | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ | ||
| 120 | |||
| 121 | =pod | ||
| 122 | |||
| 123 | =encoding UTF-8 | ||
| 124 | |||
| 125 | =head1 NAME | ||
| 126 | |||
| 127 | DBIO::Oracle::DDL - Generate Oracle DDL from DBIO Result classes | ||
| 128 | |||
| 129 | =head1 VERSION | ||
| 130 | |||
| 131 | version 0.900001 | ||
| 132 | |||
| 133 | =head1 DESCRIPTION | ||
| 134 | |||
| 135 | C<DBIO::Oracle::DDL> generates Oracle DDL from the DBIO schema class hierarchy. | ||
| 136 | It is the desired-state side of the test-deploy-and-compare strategy used by | ||
| 137 | L<DBIO::Oracle::Deploy>. | ||
| 138 | |||
| 139 | my $ddl = DBIO::Oracle::DDL->install_ddl($schema); | ||
| 140 | # CREATE TABLE ...; CREATE SEQUENCE ...; CREATE INDEX ...; | ||
| 141 | |||
| 142 | =head1 METHODS | ||
| 143 | |||
| 144 | =head2 install_ddl | ||
| 145 | |||
| 146 | my $ddl = DBIO::Oracle::DDL->install_ddl($schema); | ||
| 147 | |||
| 148 | Returns the full installation DDL as a single string. C<$schema> may be | ||
| 149 | a connected schema instance or a schema class name. | ||
| 150 | |||
| 151 | =seealso | ||
| 152 | |||
| 153 | =over | ||
| 154 | |||
| 155 | =item * L<DBIO::Oracle> - schema component | ||
| 156 | |||
| 157 | =item * L<DBIO::Oracle::Deploy> - uses this to generate DDL for deployment | ||
| 158 | |||
| 159 | =item * L<DBIO::PostgreSQL::DDL> - reference implementation | ||
| 160 | |||
| 161 | =back | ||
| 162 | |||
| 163 | =head1 AUTHOR | ||
| 164 | |||
| 165 | DBIO & DBIx::Class Authors | ||
| 166 | |||
| 167 | =head1 COPYRIGHT AND LICENSE | ||
| 168 | |||
| 169 | Copyright (C) 2026 DBIO Authors | ||
| 170 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||
| 171 | Based on DBIx::Class, heavily modified. | ||
| 172 | |||
| 173 | This is free software; you can redistribute it and/or modify it under | ||
| 174 | the same terms as the Perl 5 programming language system itself. | ||
| 175 | |||
| 176 | =cut | ||