File: | blib/lib/MySQL/Util/Lite/Table.pm |
Coverage: | 64.5% |
line | stmt | bran | cond | sub | code |
---|---|---|---|---|---|
1 | package MySQL::Util::Lite::Table; | ||||
2 | |||||
3 | our $VERSION = '0.01'; | ||||
4 | |||||
5 | 1 1 1 | use Modern::Perl; | |||
6 | 1 1 1 | use Moose; | |||
7 | 1 1 1 | use namespace::autoclean; | |||
8 | 1 1 1 | use Method::Signatures; | |||
9 | 1 1 1 | use Data::Printer alias => 'pdump'; | |||
10 | 1 1 1 | use MySQL::Util::Lite::ForeignKey; | |||
11 | 1 1 1 | use MySQL::Util::Lite::Column; | |||
12 | |||||
13 | has name => ( | ||||
14 | is => 'ro', | ||||
15 | isa => 'Str', | ||||
16 | required => 1, | ||||
17 | ); | ||||
18 | |||||
19 | has schema_name => ( | ||||
20 | is => 'ro', | ||||
21 | isa => 'Str', | ||||
22 | required => 1, | ||||
23 | ); | ||||
24 | |||||
25 | has _util => ( | ||||
26 | is => 'ro', | ||||
27 | isa => 'MySQL::Util', | ||||
28 | required => 1, | ||||
29 | ); | ||||
30 | |||||
31 | 1 9 9 | method get_parent_tables { | |||
32 | |||||
33 | 9 | my %seen; | |||
34 | 9 | my @ret; | |||
35 | 9 | my @fks = $self->get_foreign_keys; | |||
36 | |||||
37 | 9 | foreach my $fk (@fks) { | |||
38 | 5 | foreach my $col ( $fk->get_columns ) { | |||
39 | |||||
40 | 5 | my $fq_table_name = sprintf( "%s.%s", | |||
41 | $col->parent_schema_name, $col->parent_table_name ); | ||||
42 | |||||
43 | 5 | if ( !$seen{$fq_table_name} ) { | |||
44 | 5 | push @ret, | |||
45 | MySQL::Util::Lite::Table->new( | ||||
46 | name => $col->parent_table_name, | ||||
47 | schema_name => $col->parent_schema_name, | ||||
48 | _util => $self->_util | ||||
49 | ); | ||||
50 | } | ||||
51 | |||||
52 | 5 | $seen{$fq_table_name}++; | |||
53 | } | ||||
54 | } | ||||
55 | |||||
56 | 9 | return @ret; | |||
57 | } | ||||
58 | |||||
59 | 1 18 18 | method get_foreign_keys { | |||
60 | |||||
61 | 18 | my $fks_href = $self->_util->get_fk_constraints( $self->name ); | |||
62 | 18 | my @fks; | |||
63 | |||||
64 | 18 | foreach my $fk_name ( keys %$fks_href ) { | |||
65 | 10 | push @fks, | |||
66 | MySQL::Util::Lite::ForeignKey->new( | ||||
67 | name => $fk_name, | ||||
68 | table_name => $self->name, | ||||
69 | schema_name => $self->schema_name, | ||||
70 | _util => $self->_util | ||||
71 | ); | ||||
72 | } | ||||
73 | |||||
74 | 18 | return @fks; | |||
75 | } | ||||
76 | |||||
77 | 1 0 0 | method has_parents { | |||
78 | |||||
79 | 0 | my @parents = $self->get_parent_tables; | |||
80 | 0 | if (@parents) { | |||
81 | 0 | return 1; | |||
82 | } | ||||
83 | |||||
84 | 0 | return 0; | |||
85 | } | ||||
86 | |||||
87 | 1 0 0 0 0 0 0 0 | method get_column (Str :$name) { | |||
88 | |||||
89 | 0 | my @cols = $self->get_columns; | |||
90 | 0 | foreach my $col (@cols) { | |||
91 | 0 | if ( $col->name eq $name ) { | |||
92 | 0 | return $col; | |||
93 | } | ||||
94 | } | ||||
95 | } | ||||
96 | |||||
97 | 1 9 9 9 9 9 9 9 | method get_columns (Bool :$exclude_autoinc = 0) { | |||
98 | |||||
99 | 9 | my @cols; | |||
100 | 9 | my $aref = $self->_util->describe_table( $self->name ); | |||
101 | 9 | foreach my $col (@$aref) { | |||
102 | |||||
103 | 18 | my $new = MySQL::Util::Lite::Column->new( | |||
104 | name => $col->{FIELD}, | ||||
105 | table_name => $self->name, | ||||
106 | schema_name => $self->schema_name, | ||||
107 | key => $col->{KEY}, | ||||
108 | default => $col->{DEFAULT}, | ||||
109 | type => $col->{TYPE}, | ||||
110 | is_null => $col->{NULL} =~ /^yes$/i ? 1 : 0, | ||||
111 | is_autoinc => $col->{EXTRA} =~ /auto_increment/i ? 1 : 0, | ||||
112 | ); | ||||
113 | |||||
114 | 18 | if ($exclude_autoinc and $new->is_autoinc) { | |||
115 | 0 | next; | |||
116 | } | ||||
117 | |||||
118 | 18 | push @cols, $new; | |||
119 | } | ||||
120 | |||||
121 | 9 | return @cols; | |||
122 | } | ||||
123 | |||||
124 | 1; |