line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::Class::EasyConf::YAML; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
28851
|
use 5.008008; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
46
|
|
4
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
453
|
use YAML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
8
|
|
|
|
|
|
|
our $VERBOSE = undef; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub configure { |
11
|
|
|
|
|
|
|
my ($package, $fh) = @_; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my %args = map { lc $_ => 1 } @ARGV; |
14
|
|
|
|
|
|
|
$VERBOSE = $args{verbose} ? 1 : undef; |
15
|
|
|
|
|
|
|
my $config = $args{configuration} ? 1 : undef; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
{ |
18
|
|
|
|
|
|
|
no strict 'refs'; |
19
|
|
|
|
|
|
|
$fh ||= *{"${package}::DATA"}; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $yaml = do { local $/; <$fh> }; |
23
|
|
|
|
|
|
|
my $data = YAML::Load($yaml); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
_set_table ($package, $data); |
26
|
|
|
|
|
|
|
_set_columns ($package, $data); |
27
|
|
|
|
|
|
|
_set_pk ($package, $data); |
28
|
|
|
|
|
|
|
_set_unique ($package, $data); |
29
|
|
|
|
|
|
|
_set_relationships ($package, $data); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
print_configuration($package) if $config; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _set_table { |
36
|
|
|
|
|
|
|
my ($package, $data) = @_; |
37
|
|
|
|
|
|
|
my $table = $data->{table} |
38
|
|
|
|
|
|
|
or die "Cannot configure $package: table not specified.\n"; |
39
|
|
|
|
|
|
|
$package->table($table); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub _set_pk { |
43
|
|
|
|
|
|
|
my ($package, $data) = @_; |
44
|
|
|
|
|
|
|
my $pk = $data->{primary_key} |
45
|
|
|
|
|
|
|
or die "Cannot configure $package: primary key not specified.\n"; |
46
|
|
|
|
|
|
|
$package->set_primary_key(ref $pk eq 'ARRAY' ? @$pk : $pk); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _set_columns { |
50
|
|
|
|
|
|
|
my ($package, $data) = @_; |
51
|
|
|
|
|
|
|
die "Cannot configure $package: columns not properly specified.\n" |
52
|
|
|
|
|
|
|
unless ref $data->{columns} eq 'HASH'; |
53
|
|
|
|
|
|
|
my $columns = $data->{columns}; |
54
|
|
|
|
|
|
|
$package->add_columns(%{ $columns }); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _set_unique { |
58
|
|
|
|
|
|
|
my ($package, $data) = @_; |
59
|
|
|
|
|
|
|
return unless ref $data->{unique} eq 'HASH'; |
60
|
|
|
|
|
|
|
for my $name (keys %{ $data->{unique} }) { |
61
|
|
|
|
|
|
|
my $spec = $data->{unique}->{$name}; |
62
|
|
|
|
|
|
|
my $cols = ref $spec eq 'ARRAY' ? $spec : [ $spec ]; |
63
|
|
|
|
|
|
|
$package->add_unique_constraint($name => $cols); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _set_relationships { |
68
|
|
|
|
|
|
|
my ($package, $data) = @_; |
69
|
|
|
|
|
|
|
return unless ref $data->{relationships} eq 'ARRAY'; |
70
|
|
|
|
|
|
|
for my $relation (@{ $data->{relationships} }) { |
71
|
|
|
|
|
|
|
my ($relation, $value) = each %{ $relation }; |
72
|
|
|
|
|
|
|
my ($type, $class, $condition, $attrs) = @{ $value }; |
73
|
|
|
|
|
|
|
$class = resolve_relative_class($class, $package) |
74
|
|
|
|
|
|
|
unless $type eq 'many_to_many'; |
75
|
|
|
|
|
|
|
$package->$type($relation, $class, $condition, $attrs); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
if ($VERBOSE) { |
78
|
|
|
|
|
|
|
my %relationships = map { $_ => 1 } $package->relationships; |
79
|
|
|
|
|
|
|
my $ok = exists $relationships{$relation} ? 1 : undef; |
80
|
|
|
|
|
|
|
my $space_one = " " x (50 - length $package); |
81
|
|
|
|
|
|
|
my $space_two = " " x (20 - length $relation); |
82
|
|
|
|
|
|
|
print STDERR |
83
|
|
|
|
|
|
|
($ok ? "RELATION OK:" : "RELATION FAIL: "), |
84
|
|
|
|
|
|
|
"$package $space_one $relation $space_two $type\n", |
85
|
|
|
|
|
|
|
"\t$package->$type($relation, $class, $condition)\n\n"; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub resolve_relative_class { |
91
|
|
|
|
|
|
|
my ($class, $package) = @_; |
92
|
|
|
|
|
|
|
return $class if $class =~ /::/; # fully qualified already. |
93
|
|
|
|
|
|
|
(my $new_package = $package) =~ s/::[^:]+$/::$class/; |
94
|
|
|
|
|
|
|
return $new_package; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub print_configuration { |
98
|
|
|
|
|
|
|
my $package = shift; |
99
|
|
|
|
|
|
|
local $\ = "\n"; |
100
|
|
|
|
|
|
|
print "Table: ", $package->table; |
101
|
|
|
|
|
|
|
print "Primary Key: ", join ", " => $package->primary_columns; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
print "Columns: "; |
104
|
|
|
|
|
|
|
for my $col ($package->columns) { |
105
|
|
|
|
|
|
|
my %info = %{ $package->column_info($col) }; |
106
|
|
|
|
|
|
|
print "\t$col:"; |
107
|
|
|
|
|
|
|
print "\t\t$_ => $info{$_}" for sort keys %info; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
print "Relationships:"; |
111
|
|
|
|
|
|
|
for my $rel ($package->relationships) { |
112
|
|
|
|
|
|
|
my %info = %{ $package->relationship_info($rel) }; |
113
|
|
|
|
|
|
|
print "\t$rel:"; |
114
|
|
|
|
|
|
|
for my $key (sort keys %info) { |
115
|
|
|
|
|
|
|
if (ref $info{$key}) { |
116
|
|
|
|
|
|
|
print "\t\t$key:"; |
117
|
|
|
|
|
|
|
print "\t\t\t$_ => $info{$key}->{$_}" for sort keys %{ $info{$key} }; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
else { |
120
|
|
|
|
|
|
|
print "\t\t$key => $info{$key}"; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
print "Constraints: "; |
126
|
|
|
|
|
|
|
my %uniq = $package->unique_constraints; |
127
|
|
|
|
|
|
|
map { print "\t$_ => ", join ", " => @{ $uniq{$_} } } keys %uniq; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
1; |
132
|
|
|
|
|
|
|
__END__ |