| 1 | package DBIO::Firebird::DDL; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Generate Firebird 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::Firebird::Type qw(ddl_type_from_info); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub install_ddl { | ||||||||||||||||||||||||||
|
Subroutine
install_ddl: not called CC=24 SCAR=64.0
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 0 | my ($class, $schema) = @_; | |||||||||||||||||||||||||||
| 13 | 0 | my @stmts; | |||||||||||||||||||||||||||
| 14 | my @view_stmts; | ||||||||||||||||||||||||||||
| 15 | 0 | my %seen_table; | |||||||||||||||||||||||||||
| 16 | 0 | my %seen_view; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | for my $source_name (sort $schema->sources) { | |||||||||||||||||||||||||||
| 19 | 0 | my $source = $schema->source($source_name); | |||||||||||||||||||||||||||
| 20 | 0 | my $table_name = $source->name; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Views: emit CREATE VIEW after all tables; skip virtual views. | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | if ($source->isa('DBIO::ResultSource::View')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | next if $source->can('is_virtual') && $source->is_virtual; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$source->can("is_virtual") and $source->is_virtual
MC/DC0%0/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual")
$source->is_virtual
Truth table0%0/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my $vname = ref $table_name eq 'SCALAR' ? $$table_name : $table_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | next if ref $vname || $seen_view{$vname}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $vname or $seen_view{$vname}++
MC/DC0%0/2
ref $vname or $seen_view{$vname}++
ref $vname
$seen_view{$vname}++
Truth table0%0/3
A or B
A= ref $vnameB= $seen_view{$vname}++
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $def = $source->can('view_definition') ? $source->view_definition : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | next unless defined $def && length $def; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $def and length $def
MC/DC0%0/2
defined $def and length $def
defined $def
length $def
Truth table0%0/3
A and B
A= defined $defB= length $def
| |||||||||||||||||||||||||||||
| 29 | 0 | push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', _quote_ident($vname), $def; | |||||||||||||||||||||||||||
| 30 | 0 | next; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # A scalar-ref name is either a literal table name (\'cd', emitted | ||||||||||||||||||||||||||||
| 34 | # unquoted) or an inline subquery source; deref the former, skip the latter. | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if (ref $table_name eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $table_name = $$table_name; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | next if $table_name =~ /\s|\(/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | next if ref $table_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Several result sources can map to one physical table; emit it once. | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | next if $seen_table{$table_name}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | my @col_defs; | |||||||||||||||||||||||||||
| 45 | my %is_pk; | ||||||||||||||||||||||||||||
| 46 | 0 | my @pk_cols = $source->primary_columns; | |||||||||||||||||||||||||||
| 47 | 0 | @is_pk{@pk_cols} = (1) x @pk_cols; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | for my $col_name ($source->columns) { | |||||||||||||||||||||||||||
| 50 | 0 | my $info = $source->column_info($col_name); | |||||||||||||||||||||||||||
| 51 | 0 | my $type = ddl_type_from_info($info); | |||||||||||||||||||||||||||
| 52 | 0 | my $def = sprintf ' %s %s', _quote_ident($col_name), $type; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | $def .= ' NOT NULL' if defined $info->{is_nullable} && !$info->{is_nullable}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $info->{"is_nullable"} and not $info->{"is_nullable"}
MC/DC0%0/2
defined $info->{"is_nullable"} and not $info->{"is_nullable"}
defined $info->{"is_nullable"}
not $info->{"is_nullable"}
Truth table0%0/3
A and not(B)
A= defined $info->{"is_nullable"}B= not $info->{"is_nullable"}
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if (defined $info->{default_value}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | my $dv = $info->{default_value}; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | $def .= ref $dv eq 'SCALAR' ? " DEFAULT $$dv" : " DEFAULT '$dv'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 0 | push @col_defs, $def; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (@pk_cols) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | push @col_defs, sprintf ' PRIMARY KEY (%s)', | ||||||||||||||||||||||||||||
| 63 | 0 | join(', ', map { _quote_ident($_) } @pk_cols); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $qualified = _quote_ident($table_name); | |||||||||||||||||||||||||||
| 67 | 0 | push @stmts, sprintf "CREATE TABLE %s (\n%s\n);", $qualified, join(",\n", @col_defs); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # Unique indexes | ||||||||||||||||||||||||||||
| 70 | 0 | for my $col_name ($source->columns) { | |||||||||||||||||||||||||||
| 71 | 0 | my $info = $source->column_info($col_name); | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($info->{is_unique} || $info->{is_single_unique_key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$info->{"is_unique"} or $info->{"is_single_unique_key"}
MC/DC0%0/2
$info->{"is_unique"} or $info->{"is_single_unique_key"}
$info->{"is_unique"}
$info->{"is_single_unique_key"}
Truth table0%0/3
A or B
A= $info->{"is_unique"}B= $info->{"is_single_unique_key"}
| |||||||||||||||||||||||||||||
| 73 | 0 | push @stmts, sprintf 'CREATE UNIQUE INDEX %s ON %s (%s);', | |||||||||||||||||||||||||||
| 74 | _quote_ident("${table_name}_${col_name}_idx"), | ||||||||||||||||||||||||||||
| 75 | $qualified, | ||||||||||||||||||||||||||||
| 76 | _quote_ident($col_name); | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | return join " | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | ", @stmts, @view_stmts; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | 1; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =pod | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head1 NAME | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | DBIO::Firebird::DDL - Generate Firebird DDL from DBIO Result classes | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 VERSION | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | version 0.900001 | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 METHODS | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 install_ddl | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | my $ddl = DBIO::Firebird::DDL->install_ddl($schema); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Returns the full installation DDL as a single string. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 117 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 118 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 121 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||||