File Coverage

lib/SQL/Admin/Catalog.pm
Criterion Covered Total %
statement 51 74 68.9
branch 9 30 30.0
condition 8 19 42.1
subroutine 14 20 70.0
pod 0 9 0.0
total 82 152 53.9


line stmt bran cond sub pod time code
1              
2             package SQL::Admin::Catalog;
3              
4 1     1   1521 use strict;
  1         2  
  1         39  
5 1     1   4 use warnings;
  1         2  
  1         43  
6              
7             our $VERSION = v0.5.0;
8              
9             ######################################################################
10              
11 1     1   468 use SQL::Admin::Catalog::Schema;
  1         3  
  1         26  
12 1     1   603 use SQL::Admin::Catalog::Sequence;
  1         2  
  1         30  
13 1     1   539 use SQL::Admin::Catalog::Index;
  1         2  
  1         22  
14 1     1   550 use SQL::Admin::Catalog::Table;
  1         2  
  1         20  
15 1     1   6215 use SQL::Admin::Catalog::Table::Column;
  1         4  
  1         38  
16 1     1   1859 use SQL::Admin::Catalog::Table::PrimaryKey;
  1         3  
  1         76  
17 1     1   1637 use SQL::Admin::Catalog::Table::Unique;
  1         2  
  1         27  
18 1     1   617 use SQL::Admin::Catalog::Table::ForeignKey;
  1         2  
  1         28  
19              
20             ######################################################################
21              
22 1     1   7 use List::Util;
  1         3  
  1         919  
23              
24             our $NAME_TRANSFORM = {
25             NAME => sub { $_[0] },
26             NAME_lc => sub { lc $_[0] },
27             NAME_uc => sub { uc $_[0] },
28             };
29              
30             ######################################################################
31              
32             my $class_map = {
33             schema => 'SQL::Admin::Catalog::Schema',
34             sequence => 'SQL::Admin::Catalog::Sequence',
35             table => 'SQL::Admin::Catalog::Table',
36             index => 'SQL::Admin::Catalog::Index',
37             column => 'SQL::Admin::Catalog::Table::Column',
38             primary_key => 'SQL::Admin::Catalog::Table::PrimaryKey',
39             unique => 'SQL::Admin::Catalog::Table::Unique',
40             foreign_key => 'SQL::Admin::Catalog::Table::ForeignKey',
41             };
42              
43             ######################################################################
44             ######################################################################
45             sub new { # ;
46 8     8 0 5446 my ($class, %param) = @_;
47              
48 8 50       188 $param{FetchHashKeyName} = 'NAME_uc'
49             unless $param{FetchHashKeyName};
50              
51 8 50       40 $param{FetchHashKeyName} = 'NAME'
52             unless exists $NAME_TRANSFORM->{ $param{FetchHashKeyName} };
53              
54             ##################################################################
55              
56 8   33     211 bless {
57             name_transform => $NAME_TRANSFORM->{ $param{FetchHashKeyName} },
58             allow_redeclare => 1,
59              
60             (map +($_ => {}), keys %$class_map),
61             }, ref $class || $class;
62             }
63              
64              
65             ######################################################################
66             ######################################################################
67             sub catalog { # ;
68 0     0 0 0 $_[0];
69             }
70              
71              
72             ######################################################################
73             ######################################################################
74             sub add { # ;
75 20     20 0 99 my ($self, $type, %param) = @_;
76              
77 20 50       54 return unless exists $class_map->{$type};
78              
79 20   50     77 my $typemap = $self->{$type} ||= {};
80              
81             ##################################################################
82              
83 20 50 66     83 $param{schema} = $self->{default_schema}
84             if ! $param{schema} and $self->{default_schema};
85              
86 20         80 my $obj = $self->get ($type, %param, catalog => $self);
87              
88             ##################################################################
89              
90 20 100       102 if (my $schema = $obj->schema) {
91 11   66     71 $self->{schema}{$schema} ||= $class_map->{schema}->new (name => $schema);
92             }
93              
94             ##################################################################
95              
96 20   66     60 $typemap->{ $obj->fullname } ||= $obj;
97             }
98              
99              
100             ######################################################################
101             ######################################################################
102             sub get { # ;
103 23     23 0 87 my ($self, $type, %param) = @_;
104              
105             return
106 23 50       63 unless exists $class_map->{$type};
107              
108             ##################################################################
109              
110             # my $obj = $class_map->{$type}->new (%param, catalog => $self);
111 23         419 my $obj = $class_map->{$type}->new (%param);
112 23         112 my $key = $obj->fullname;
113              
114 23 100       80 $obj = $self->{$type}{ $key }
115             if exists $self->{$type}{ $key };
116              
117             ##################################################################
118              
119 23         75 return $obj;
120             }
121              
122              
123             ######################################################################
124             ######################################################################
125             sub exists { # ;
126 0     0 0   my ($self, $type, @list) = @_;
127              
128             return
129 0 0         unless exists $class_map->{$type};
130              
131 0           my $key;
132 0 0         $key = 1 == @list ? $list[0] : $class_map->{$type}->new (@list);
133 0 0         $key = $key->fullname if ref $key;
134              
135 0   0       return exists $self->{$type}{ $key } && $key;
136             }
137              
138              
139             ######################################################################
140             ######################################################################
141             sub list { # ;
142 0     0 0   my ($self, $type) = @_;
143              
144 0 0         +{ %{ $self->{$type} || {} } };
  0            
145             }
146              
147              
148             ######################################################################
149             ######################################################################
150             sub add_statement { # ;
151 0     0 0   my $self = shift;
152 0   0       my $list = $self->{statements} ||= [];
153              
154 0           push @$list, @_;
155             }
156              
157              
158             ######################################################################
159             ######################################################################
160             sub load { # ;
161 0     0 0   my ($self, $driver, @params) = @_;
162              
163 0 0         $driver = SQL::Admin->get_driver ($driver, @{ shift @params || [] } )
  0 0          
164             unless ref $driver;
165              
166 0           $driver->load ($self, @params);
167              
168 0           $self;
169             }
170              
171              
172             ######################################################################
173             ######################################################################
174             sub save { # ;
175 0     0 0   my ($self, $driver, @params) = @_;
176              
177 0 0         $driver = SQL::Admin->get_driver ($driver, @{ shift @params || [] } )
  0 0          
178             unless ref $driver;
179              
180 0           $driver->save ($self, @params);
181              
182 0           $self;
183             }
184              
185              
186             ######################################################################
187             ######################################################################
188              
189             package SQL::Admin::Catalog;
190              
191             1;
192              
193             __END__