| 1 | package Fey::Loader::DBI; | ||||||||||||||||||||
| 2 | { | ||||||||||||||||||||
| 3 | 2 | $Fey::Loader::DBI::VERSION = '0.13'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | ▶ | use MooseX::Params::Validate qw( validated_hash ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | has 'dbh' => ( | |||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||
| 14 | isa => 'DBI::db', | ||||||||||||||||||||
| 15 | required => 1, | ||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 2 | has 'schema_class' => ( | |||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||
| 20 | isa => 'ClassName', | ||||||||||||||||||||
| 21 | default => 'Fey::Schema', | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 2 | has 'table_class' => ( | |||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||
| 26 | isa => 'ClassName', | ||||||||||||||||||||
| 27 | default => 'Fey::Table', | ||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 2 | has 'column_class' => ( | |||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||
| 32 | isa => 'ClassName', | ||||||||||||||||||||
| 33 | default => 'Fey::Column', | ||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 2 | has 'fk_class' => ( | |||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||
| 38 | isa => 'ClassName', | ||||||||||||||||||||
| 39 | default => 'Fey::FK', | ||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 2 | has '_dbh_name' => ( | |||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||
| 44 | isa => 'Str', | ||||||||||||||||||||
| 45 | lazy => 1, | ||||||||||||||||||||
| 46 | builder => '_build_dbh_name', | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 2 | ▶ | use Fey::Column; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 50 | 2 | ▶ | use Fey::FK; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 51 | 2 | ▶ | use Fey::Schema; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | 2 | ▶ | use Fey::Table; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 2 | ▶ | use Scalar::Util qw( looks_like_number ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 2 | ▶ | sub make_schema { | ||||||||||||||||||
|
Subroutine
make_schema: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 57 | 2 | my $self = shift; | |||||||||||||||||||
| 58 | 2 | my %p = validated_hash( \@_, name => { isa => 'Str', optional => 1 } ); | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 2 | ▶ | my $name = delete $p{name} || $self->_dbh_name(); | ||||||||||||||||||
|
Condition33%1/3
delete $p{'name'} || $self->_dbh_name
MC/DC0%0/2
delete $p{'name'} || $self->_dbh_name
delete $p{'name'}
$self->_dbh_name
Truth table33%1/3
A || B
A= delete $p{'name'}B= $self->_dbh_name
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 2 | my $schema = $self->schema_class()->new( name => $name ); | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 2 | $self->_add_tables($schema); | |||||||||||||||||||
| 65 | 2 | $self->_add_foreign_keys($schema); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 2 | return $schema; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 5 | ▶ | sub _build_dbh_name { | ||||||||||||||||||
|
Subroutine
_build_dbh_name: called CC=3 SCAR=11.3
| |||||||||||||||||||||
| 71 | 5 | my $self = shift; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 5 | my $dsn_ish = $self->dbh()->{Name}; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 5 | ▶ | return $dsn_ish unless $dsn_ish =~ /\W/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 3 | ▶ | return $1 if $dsn_ish =~ /(?:database|dbname)=([^;]+?)(?:;|\z)/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 0 | die "Cannot figure out the database name from the DSN - $dsn_ish\n"; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 2 | ▶ | sub _add_tables { | ||||||||||||||||||
|
Subroutine
_add_tables: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 83 | 2 | my $self = shift; | |||||||||||||||||||
| 84 | 2 | my $schema = shift; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | my $sth = $self->dbh()->table_info( | |||||||||||||||||||
| 87 | $self->_catalog_name(), $self->_schema_name(), | ||||||||||||||||||||
| 88 | '%', 'TABLE,VIEW' | ||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 2 | ▶ | while ( my $table_info = $sth->fetchrow_hashref() ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 10 | $self->_add_table( $schema, $table_info ); | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 52 | ▶ | sub _catalog_name {undef} | ||||||||||||||||||
|
Subroutine
_catalog_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 52 | ▶ | sub _schema_name {undef} | ||||||||||||||||||
|
Subroutine
_schema_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 98 | ▶ | sub _unquote_identifier { | ||||||||||||||||||
|
Subroutine
_unquote_identifier: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 101 | 98 | my $self = shift; | |||||||||||||||||||
| 102 | 98 | my $ident = shift; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 98 | ▶ | my $quote = $self->dbh()->get_info(29) || q{"}; | ||||||||||||||||||
|
Condition50%1/2
$self->dbh->get_info(29) || '"'
MC/DC0%0/1
$self->dbh->get_info(29) || '"'
$self->dbh->get_info(29)
Truth table50%1/2
A
A= $self->dbh->get_info(29)
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 98 | $ident =~ s/^\Q$quote\E|\Q$quote\E$//g; | |||||||||||||||||||
| 107 | 98 | $ident =~ s/\Q$quote$quote\E/$quote/g; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 98 | return $ident; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 10 | ▶ | sub _add_table { | ||||||||||||||||||
|
Subroutine
_add_table: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 113 | 10 | my $self = shift; | |||||||||||||||||||
| 114 | 10 | my $schema = shift; | |||||||||||||||||||
| 115 | 10 | my $table_info = shift; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 10 | my $name = $self->_unquote_identifier( $table_info->{TABLE_NAME} ); | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 10 | my $table = $self->table_class()->new( | |||||||||||||||||||
| 120 | name => $name, | ||||||||||||||||||||
| 121 | is_view => $self->_is_view($table_info), | ||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 10 | $self->_add_columns($table); | |||||||||||||||||||
| 125 | 10 | $self->_set_primary_key($table); | |||||||||||||||||||
| 126 | 10 | $self->_set_other_keys($table); | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 10 | $schema->add_table($table); | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 10 | ▶ | sub _is_view { $_[1]->{TABLE_TYPE} eq 'VIEW' ? 1 : 0 } | ||||||||||||||||||
|
Subroutine
_is_view: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 10 | ▶ | sub _add_columns { | ||||||||||||||||||
|
Subroutine
_add_columns: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 134 | 10 | my $self = shift; | |||||||||||||||||||
| 135 | 10 | my $table = shift; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 10 | my $sth = $self->dbh()->column_info( | |||||||||||||||||||
| 138 | $self->_catalog_name(), | ||||||||||||||||||||
| 139 | $self->_schema_name(), | ||||||||||||||||||||
| 140 | $table->name(), | ||||||||||||||||||||
| 141 | '%' | ||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 10 | ▶ | while ( my $col_info = $sth->fetchrow_hashref() ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | 28 | my %col = $self->_column_params( $table, $col_info ); | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 28 | my $col = $self->column_class()->new(%col); | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 28 | $table->add_column($col); | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 28 | ▶ | sub _column_params { | ||||||||||||||||||
|
Subroutine
_column_params: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 154 | 28 | my $self = shift; | |||||||||||||||||||
| 155 | 28 | my $table = shift; | |||||||||||||||||||
| 156 | 28 | my $col_info = shift; | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 28 | my $name = $self->_unquote_identifier( $col_info->{COLUMN_NAME} ); | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | my %col = ( | ||||||||||||||||||||
| 161 | name => $name, | ||||||||||||||||||||
| 162 | type => $col_info->{TYPE_NAME}, | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | # NULLABLE could be 2, which indicates unknown | ||||||||||||||||||||
| 165 | 28 | ▶ | is_nullable => ( $col_info->{NULLABLE} == 1 ? 1 : 0 ), | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | $col{length} = $col_info->{COLUMN_SIZE} | ||||||||||||||||||||
| 169 | 28 | ▶ | if defined $col_info->{COLUMN_SIZE}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | $col{precision} = $col_info->{DECIMAL_DIGITS} | ||||||||||||||||||||
| 172 | 28 | ▶ | if defined $col_info->{DECIMAL_DIGITS}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 28 | ▶ | if ( defined $col_info->{COLUMN_DEF} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 175 | 6 | my $default = $self->_default( $col_info->{COLUMN_DEF}, $col_info ); | |||||||||||||||||||
| 176 | 6 | ▶ | $col{default} = $default | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | if defined $default; | ||||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 28 | $col{is_auto_increment} = $self->_is_auto_increment( $table, $col_info ); | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 28 | return %col; | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 12 | ▶ | sub _default { | ||||||||||||||||||
|
Subroutine
_default: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 186 | 12 | my $self = shift; | |||||||||||||||||||
| 187 | 12 | my $default = shift; | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 12 | ▶ | if ( $default =~ /^NULL$/i ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 190 | 1 | return Fey::Literal::Null->new(); | |||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | elsif ( $default =~ s/^(["'])(.*)\1$/$2/ ) { | ||||||||||||||||||||
| 193 | 4 | my $quote = $1; | |||||||||||||||||||
| 194 | 4 | $default =~ s/\Q$quote$quote/$quote/g; | |||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 4 | return Fey::Literal::String->new($default); | |||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | elsif ( looks_like_number($default) ) { | ||||||||||||||||||||
| 199 | 4 | return Fey::Literal::Number->new($default); | |||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | else { | ||||||||||||||||||||
| 202 | 3 | return Fey::Literal::Term->new($default); | |||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 28 | ▶ | sub _is_auto_increment { | ||||||||||||||||||
|
Subroutine
_is_auto_increment: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 207 | 28 | return 0; | |||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 10 | ▶ | sub _set_primary_key { | ||||||||||||||||||
|
Subroutine
_set_primary_key: called CC=4 SCAR=14.0
| |||||||||||||||||||||
| 211 | 10 | my $self = shift; | |||||||||||||||||||
| 212 | 10 | my $table = shift; | |||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 10 | my $pk_info = $self->dbh()->primary_key_info( | |||||||||||||||||||
| 215 | $self->_catalog_name(), | ||||||||||||||||||||
| 216 | $self->_schema_name(), | ||||||||||||||||||||
| 217 | $table->name() | ||||||||||||||||||||
| 218 | ); | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | 10 | ▶ | return unless $pk_info; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 10 | my %pk; | |||||||||||||||||||
| 223 | 10 | ▶ | while ( my $pk_col = $pk_info->fetchrow_hashref() ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | # KEY_SEQ refers to the "position" of the column in the table, | ||||||||||||||||||||
| 226 | # not in the key, and so may start at any random number. | ||||||||||||||||||||
| 227 | $pk{ $pk_col->{KEY_SEQ} } | ||||||||||||||||||||
| 228 | 12 | = $self->_unquote_identifier( $pk_col->{COLUMN_NAME} ); | |||||||||||||||||||
| 229 | } | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 10 | my @pk = @pk{ sort keys %pk }; | |||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 10 | ▶ | $table->add_candidate_key(@pk) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 234 | if @pk; | ||||||||||||||||||||
| 235 | } | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | 10 | ▶ | sub _set_other_keys { | ||||||||||||||||||
|
Subroutine
_set_other_keys: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 238 | 10 | my $self = shift; | |||||||||||||||||||
| 239 | 10 | my $table = shift; | |||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 10 | my $key_info = $self->dbh()->statistics_info( | |||||||||||||||||||
| 242 | $self->_catalog_name(), | ||||||||||||||||||||
| 243 | $self->_schema_name(), | ||||||||||||||||||||
| 244 | $table->name(), | ||||||||||||||||||||
| 245 | 'unique only', | ||||||||||||||||||||
| 246 | 'quick' | ||||||||||||||||||||
| 247 | ); | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | 10 | ▶ | return unless $key_info; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | 10 | my %ck; | |||||||||||||||||||
| 252 | 10 | ▶ | while ( my $ck_col = $key_info->fetchrow_hashref() ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 253 | 16 | ▶ | $ck{ $ck_col->{INDEX_NAME} } ||= []; | ||||||||||||||||||
|
Condition100%2/2
$ck{$ck_col->{"INDEX_NAME"}} ||= []
MC/DC100%1/1
$ck{$ck_col->{"INDEX_NAME"}} ||= []
$ck{$ck_col->{"INDEX_NAME"}}
Truth table100%2/2
A
A= $ck{$ck_col->{"INDEX_NAME"}}
| |||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | $ck{ $ck_col->{INDEX_NAME} }[ $ck_col->{ORDINAL_POSITION} - 1 ] | ||||||||||||||||||||
| 256 | 16 | = $self->_unquote_identifier( $ck_col->{COLUMN_NAME} ); | |||||||||||||||||||
| 257 | } | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | 10 | for my $key ( values %ck ) { | |||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | # The defined check is another Pg workaround. ORDINAL_POSITION | ||||||||||||||||||||
| 262 | # ends up sequential across all keys, which is wack. | ||||||||||||||||||||
| 263 | 14 | $table->add_candidate_key( grep {defined} @{$key} ); | |||||||||||||||||||
| 264 | } | ||||||||||||||||||||
| 265 | } | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | 2 | ▶ | sub _add_foreign_keys { | ||||||||||||||||||
|
Subroutine
_add_foreign_keys: called CC=7 SCAR=19.5
| |||||||||||||||||||||
| 268 | 2 | my $self = shift; | |||||||||||||||||||
| 269 | 2 | my $schema = shift; | |||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | my @keys | ||||||||||||||||||||
| 272 | 2 | = qw( UK_TABLE_NAME UK_COLUMN_NAME FK_TABLE_NAME FK_COLUMN_NAME ); | |||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | 2 | for my $table ( $schema->tables() ) { | |||||||||||||||||||
| 275 | 10 | my $sth = $self->_fk_info_sth( $table->name() ); | |||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | 10 | ▶ | next unless $sth; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | 10 | my %fk; | |||||||||||||||||||
| 280 | 10 | ▶ | while ( my $fk_info = $sth->fetchrow_hashref() ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 281 | 8 | $self->_translate_fk_info($fk_info); | |||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | 8 | for my $k (@keys) { | |||||||||||||||||||
| 284 | $fk_info->{$k} = $self->_unquote_identifier( $fk_info->{$k} ) | ||||||||||||||||||||
| 285 | 32 | ▶ | if defined $fk_info->{$k}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 286 | } | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | # The FK_NAME might not be unique (two tables can use the | ||||||||||||||||||||
| 289 | # same FK name). | ||||||||||||||||||||
| 290 | my $key = join q{-}, | ||||||||||||||||||||
| 291 | 8 | @{$fk_info}{qw( FK_NAME FK_TABLE_NAME UK_TABLE_NAME )}; | |||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | 8 | push @{ $fk{$key}{source_columns} }, | |||||||||||||||||||
| 294 | $schema->table( $fk_info->{FK_TABLE_NAME} ) | ||||||||||||||||||||
| 295 | 8 | ->column( $fk_info->{FK_COLUMN_NAME} ); | |||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | 8 | push @{ $fk{$key}{target_columns} }, | |||||||||||||||||||
| 298 | $schema->table( $fk_info->{UK_TABLE_NAME} ) | ||||||||||||||||||||
| 299 | 8 | ->column( $fk_info->{UK_COLUMN_NAME} ); | |||||||||||||||||||
| 300 | } | ||||||||||||||||||||
| 301 | |||||||||||||||||||||
| 302 | 10 | for my $fk_cols ( values %fk ) { | |||||||||||||||||||
| 303 | 8 | my $fk = $self->fk_class()->new( %{$fk_cols} ); | |||||||||||||||||||
| 304 | |||||||||||||||||||||
| 305 | 8 | $schema->add_foreign_key($fk); | |||||||||||||||||||
| 306 | } | ||||||||||||||||||||
| 307 | } | ||||||||||||||||||||
| 308 | } | ||||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | { | ||||||||||||||||||||
| 311 | 2 | my %ODBCToSQL = ( | |||||||||||||||||||
| 312 | PKTABLE_NAME => 'UK_TABLE_NAME', | ||||||||||||||||||||
| 313 | PKCOLUMN_NAME => 'UK_COLUMN_NAME', | ||||||||||||||||||||
| 314 | FKTABLE_NAME => 'FK_TABLE_NAME', | ||||||||||||||||||||
| 315 | FKCOLUMN_NAME => 'FK_COLUMN_NAME', | ||||||||||||||||||||
| 316 | KEY_SEQ => 'ORDINAL_POSITION', | ||||||||||||||||||||
| 317 | ); | ||||||||||||||||||||
| 318 | |||||||||||||||||||||
| 319 | 8 | ▶ | sub _translate_fk_info { | ||||||||||||||||||
|
Subroutine
_translate_fk_info: called CC=3 SCAR=15.1
| |||||||||||||||||||||
| 320 | 8 | my $self = shift; | |||||||||||||||||||
| 321 | 8 | my $info = shift; | |||||||||||||||||||
| 322 | |||||||||||||||||||||
| 323 | 8 | ▶ | return if $info->{UK_TABLE_NAME}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 324 | |||||||||||||||||||||
| 325 | 0 | ▶ | while ( my ( $from, $to ) = each %ODBCToSQL ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 326 | 0 | $info->{$to} = delete $info->{$from}; | |||||||||||||||||||
| 327 | } | ||||||||||||||||||||
| 328 | } | ||||||||||||||||||||
| 329 | } | ||||||||||||||||||||
| 330 | |||||||||||||||||||||
| 331 | 2 | ▶ | sub _fk_info_sth { | ||||||||||||||||||
|
Subroutine
_fk_info_sth: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 332 | 10 | my $self = shift; | |||||||||||||||||||
| 333 | 10 | my $name = shift; | |||||||||||||||||||
| 334 | |||||||||||||||||||||
| 335 | 10 | return $self->dbh()->foreign_key_info( | |||||||||||||||||||
| 336 | $self->_catalog_name, | ||||||||||||||||||||
| 337 | $self->_schema_name, | ||||||||||||||||||||
| 338 | $name, | ||||||||||||||||||||
| 339 | $self->_catalog_name, | ||||||||||||||||||||
| 340 | $self->_schema_name, | ||||||||||||||||||||
| 341 | undef, | ||||||||||||||||||||
| 342 | ); | ||||||||||||||||||||
| 343 | } | ||||||||||||||||||||
| 344 | |||||||||||||||||||||
| 345 | 2 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||||||||
| 346 | |||||||||||||||||||||
| 347 | 2 | 1; | |||||||||||||||||||
| 348 | |||||||||||||||||||||
| 349 | # ABSTRACT: Base class (and fallback) for loading a schema | ||||||||||||||||||||
| 350 | |||||||||||||||||||||
| 351 | |||||||||||||||||||||
| 352 | |||||||||||||||||||||
| 353 | =pod | ||||||||||||||||||||
| 354 | |||||||||||||||||||||
| 355 | =head1 NAME | ||||||||||||||||||||
| 356 | |||||||||||||||||||||
| 357 | Fey::Loader::DBI - Base class (and fallback) for loading a schema | ||||||||||||||||||||
| 358 | |||||||||||||||||||||
| 359 | =head1 VERSION | ||||||||||||||||||||
| 360 | |||||||||||||||||||||
| 361 | version 0.13 | ||||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | =head1 SYNOPSIS | ||||||||||||||||||||
| 364 | |||||||||||||||||||||
| 365 | my $loader = Fey::Loader->new( dbh => $dbh ); | ||||||||||||||||||||
| 366 | |||||||||||||||||||||
| 367 | my $schema = $loader->make_schema( name => $name ); | ||||||||||||||||||||
| 368 | |||||||||||||||||||||
| 369 | =head1 DESCRIPTION | ||||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | C<Fey::Loader::DBI> will create a schema by using the various DBI info | ||||||||||||||||||||
| 372 | methods. It is a complete implementation of a loader, but it only | ||||||||||||||||||||
| 373 | works if the driver in question fully supports the info methods | ||||||||||||||||||||
| 374 | needed, which many don't. In addition, some information simply isn't | ||||||||||||||||||||
| 375 | available via those methods, like whether a column is auto-incremented. | ||||||||||||||||||||
| 376 | |||||||||||||||||||||
| 377 | For that reason, you probably won't get good results for your schema | ||||||||||||||||||||
| 378 | unless there is a driver-specific loader subclass for your DBMS. | ||||||||||||||||||||
| 379 | |||||||||||||||||||||
| 380 | =head1 METHODS | ||||||||||||||||||||
| 381 | |||||||||||||||||||||
| 382 | This class provides the following methods: | ||||||||||||||||||||
| 383 | |||||||||||||||||||||
| 384 | =head2 Fey::Loader::DBI->new( dbh => $dbh ) | ||||||||||||||||||||
| 385 | |||||||||||||||||||||
| 386 | Given a database handle, returns a new C<Fey::Loader::DBI> object. You | ||||||||||||||||||||
| 387 | probably want to call C<Fey::Loader->new()> instead, though. | ||||||||||||||||||||
| 388 | |||||||||||||||||||||
| 389 | To change the classes used to build up the schema and its related | ||||||||||||||||||||
| 390 | objects, you may provide C<schema_class>, C<table_class>, | ||||||||||||||||||||
| 391 | C<column_class>, and C<fk_class> parameters; they default to | ||||||||||||||||||||
| 392 | C<Fey::Schema>, C<Fey::Table>, C<Fey::Column>, and C<Fey::FK> | ||||||||||||||||||||
| 393 | respectively. | ||||||||||||||||||||
| 394 | |||||||||||||||||||||
| 395 | =head2 $loader->make_schema( name => $name ) | ||||||||||||||||||||
| 396 | |||||||||||||||||||||
| 397 | This method returns a new, fully-populated C<Fey::Schema> object. The | ||||||||||||||||||||
| 398 | name parameter is optional, and if given will be used as the name of | ||||||||||||||||||||
| 399 | the new schema. Otherwise the name will be found through the C<DBI> | ||||||||||||||||||||
| 400 | handle. | ||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | =head1 BUGS | ||||||||||||||||||||
| 403 | |||||||||||||||||||||
| 404 | Please report any bugs or feature requests to | ||||||||||||||||||||
| 405 | C<bug-fey-loader@rt.cpan.org>, or through the web interface at | ||||||||||||||||||||
| 406 | L<http://rt.cpan.org>. I will be notified, and then you'll | ||||||||||||||||||||
| 407 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||
| 408 | |||||||||||||||||||||
| 409 | =head1 AUTHOR | ||||||||||||||||||||
| 410 | |||||||||||||||||||||
| 411 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||
| 412 | |||||||||||||||||||||
| 413 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 414 | |||||||||||||||||||||
| 415 | This software is Copyright (c) 2011 by Dave Rolsky. | ||||||||||||||||||||
| 416 | |||||||||||||||||||||
| 417 | This is free software, licensed under: | ||||||||||||||||||||
| 418 | |||||||||||||||||||||
| 419 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||
| 420 | |||||||||||||||||||||
| 421 | =cut | ||||||||||||||||||||
| 422 | |||||||||||||||||||||
| 423 | |||||||||||||||||||||
| 424 | __END__ | ||||||||||||||||||||