File Coverage

lib/UR/DataSource/RDBMS/Table.pm
Criterion Covered Total %
statement 44 80 55.0
branch 0 4 0.0
condition n/a
subroutine 16 25 64.0
pod 11 16 68.7
total 71 125 56.8


line stmt bran cond sub pod time code
1 35     35   1384 use strict;
  35         50  
  35         4333  
2 35     35   168 use warnings;
  35         42  
  35         1465  
3              
4             package UR::DataSource::RDBMS::Table;
5              
6 35     35   129 use UR;
  35         45  
  35         207  
7             our $VERSION = "0.46"; # UR $VERSION;
8              
9             UR::Object::Type->define(
10             class_name => 'UR::DataSource::RDBMS::Table',
11             is => ['UR::DataSource::RDBMS::Entity'],
12             dsmap => 'dd_table',
13             id_properties => [qw/data_source table_name/],
14             properties => [
15             data_source => { type => 'varchar', len => undef, sql => 'data_source' },
16             data_source_obj => { type => 'UR::DataSource', id_by => 'data_source'},
17             namespace => { calculate_from => [ 'data_source'],
18             calculate => q( (split(/::/,$data_source))[0] ) },
19             owner => { type => 'varchar', len => undef, is_optional => 1, sql => 'owner' },
20             table_name => { type => 'varchar', len => undef, sql => 'table_name' },
21             er_type => { type => 'varchar', len => undef, sql => 'er_type', is_optional => 1 },
22             last_ddl_time => { type => 'timestamp', len => undef, sql => 'last_ddl_time', is_optional => 1 },
23             last_object_revision => { type => 'timestamp', len => undef, sql => 'last_object_revision' },
24             remarks => { type => 'varchar', len => undef, is_optional => 1, sql => 'remarks' },
25             table_type => { type => 'varchar', len => undef, sql => 'table_type' },
26             ],
27             data_source => 'UR::DataSource::Meta',
28             );
29              
30             sub _related_class_name {
31 685     685   1035 my($self,$subject) = @_;
32              
33 685         947 my $class = ref($self);
34            
35             # FIXME This seems kinda braindead, but is probably faster than using s///
36             # Is it really the right thing?
37 685         1286 my $pos = index($class, '::Table');
38 685         1752 substr($class, $pos + 2, 5, $subject); # +2 to keep the "::"
39              
40 685         1268 return $class;
41             }
42            
43             sub _fk_constraint_class {
44 181     181   458 return shift->_related_class_name('FkConstraint');
45             }
46              
47             sub _pk_constraint_class {
48 302     302   645 return shift->_related_class_name('PkConstraintColumn');
49             }
50            
51             sub _unique_constraint_class {
52 0     0   0 return shift->_related_class_name('UniqueConstraintColumn');
53             }
54              
55             sub _table_column_class {
56 141     141   1903 return shift->_related_class_name('TableColumn');
57             }
58              
59             sub _bitmap_index_class {
60 61     61   182 return shift->_related_class_name('BitmapIndex');
61             }
62              
63             sub columns {
64 141     141 1 242 my $self = shift;
65              
66 141         662 my $col_class = $self->_table_column_class;
67 141         493 return $col_class->get(data_source => $self->data_source, table_name => $self->table_name);
68             }
69              
70             sub column_names {
71 28     28 1 118 return map { $_->column_name } shift->columns;
  66         144  
72             }
73              
74             sub primary_key_constraint_columns {
75 302     302 1 382 my $self = shift;
76              
77 302         723 my $pk_class = $self->_pk_constraint_class;
78 302         951 my @pks = $pk_class->get(data_source => $self->data_source, table_name => $self->table_name);
79 302         586 my @pks_with_rank = map { [ $_->rank, $_ ] } @pks;
  402         1341  
80 402         957 return map { $_->[1] }
81 302         736 sort { $a->[0] <=> $b->[0] }
  131         234  
82             @pks_with_rank;
83             }
84              
85              
86             sub primary_key_constraint_column_names {
87 210     210 1 586 return map { $_->column_name } shift->primary_key_constraint_columns;
  283         645  
88             }
89              
90              
91             sub fk_constraints {
92 181     181 1 261 my $self = shift;
93              
94 181         874 my $fk_class = $self->_fk_constraint_class;
95 181         657 my @fks = $fk_class->get(data_source => $self->data_source, table_name => $self->table_name);
96 181         663 return @fks;
97             }
98              
99             sub fk_constraint_names {
100 28     28 1 107 return map { $_->fk_constraint_name } shift->fk_constraints;
  8         26  
101             }
102              
103              
104             sub ref_fk_constraints {
105 0     0 1 0 my $self = shift;
106              
107 0         0 my $fk_class = $self->_fk_constraint_class;
108 0         0 my @fks = $fk_class->get(data_source => $self->data_source, r_table_name => $self->table_name);
109 0         0 return @fks;
110             }
111              
112             sub ref_fk_constraint_names {
113 0     0 1 0 return map { $_->fk_constraint_name } shift->ref_fk_constraints;
  0         0  
114             }
115              
116              
117             sub unique_constraint_column_names {
118 0     0 0 0 my($self,$constraint) = @_;
119              
120 0         0 my @c;
121 0 0       0 if ($constraint) {
122 0         0 @c = $self->unique_constraints(constraint_name => $constraint);
123             } else {
124 0         0 @c = $self->unique_constraints();
125             }
126 0         0 my %names = map {$_->column_name => 1 } @c;
  0         0  
127 0         0 return keys %names;
128             }
129              
130             sub unique_constraint_names {
131 0     0 0 0 my $self = shift;
132              
133 0         0 my %names = map { $_->constraint_name => 1 } $self->unique_constraints;
  0         0  
134 0         0 return keys %names;
135             }
136              
137             sub unique_constraints {
138 0     0 0 0 my $self = shift;
139              
140 0         0 my $uc_class = $self->_unique_constraint_class;
141 0         0 my @c = $uc_class->get( data_source => $self->data_source, table_name => $self->table_name, @_);
142              
143 0         0 return @c;
144             }
145              
146             sub bitmap_indexes {
147 61     61 1 112 my $self = shift;
148              
149 61         217 my $bi_class = $self->_bitmap_index_class;
150 61         249 my @bi = $bi_class->get(data_source => $self->data_source, table_name => $self->table_name);
151 61         458 return @bi;
152             }
153              
154              
155             sub bitmap_index_names {
156 61     61 1 220 return map { $_->bitmap_index_name } shift->bitmap_indexes;
  0            
157             }
158              
159             # FIXME Due to a "bug" in getting class objects, you need to pass in namespace => 'name' as
160             # arguments to get this to work.
161             sub handler_class {
162 0     0 0   my $self = shift;
163              
164 0           return UR::Object::Type->get(table_name => $self->table_name, @_);
165             }
166              
167             sub handler_class_name {
168 0     0 0   my $self = shift;
169              
170 0           return $self->handler_class(@_)->class_name;
171             }
172              
173             sub delete {
174 0     0 1   my $self = shift;
175              
176 0           my @deleteme = ( $self->fk_constraints,
177             $self->bitmap_indexes,
178             $self->primary_key_constraint_columns,
179             $self->columns,
180             );
181 0           for my $obj ( @deleteme ) {
182 0           $obj->delete;
183 0 0         unless ($obj->isa('UR::DeletedRef')) {
184 0           Carp::confess("Failed to delete $obj ".$obj->{'id'});
185             }
186             }
187 0           $self->SUPER::delete();
188 0           return $self;
189             }
190            
191              
192              
193             1;
194              
195             =pod
196              
197             =head1 NAME
198              
199             UR::DataSource::Meta::RDBMS::Table - Object-oriented class for RDBMS table objects.
200              
201             =head1 SYNOPSIS
202              
203             $t = UR::DataSource::Meta::RDBMS::Table->get(
204             data_source => 'Namespace::DataSource::Name',
205             table_name => 'MY_TABLE_NAME');
206             @c = $t->column_names;
207             @f = $t->fk_constraint_names;
208              
209             =head1 DESCRIPTION
210              
211             Objects of this class represent a table in a database. They are
212             primarily used by the class updating logic in the command line tool
213             C, but can be retrieved and used in any application.
214             Their instances come from from the MetaDB (L) which
215             is partitioned and has one physical database per Namespace.
216              
217             =head2 Related Metadata Methods
218              
219             =over 4
220              
221             =item @col_objs = $t->columns();
222              
223             =item @col_names = $t->column_names();
224              
225             =item @fk_objs = $t->fk_constraints();
226              
227             =item @fk_names = $t->fk_constraint_names();
228              
229             =item @ref_fk_objs = $t->ref_fk_constraints();
230              
231             =item @ref_fk_names = $t->ref_fk_constraint_names();
232              
233             =item @pk_objs = $t->primary_key_constraint_columns();
234              
235             =item @pk_col_names = $t->primary_key_constraint_column_names();
236              
237             =item @bi_objs = $t->bitmap_indexes();
238              
239             =item @bi_names = $t->bitmap_index_names();
240              
241             Return related metadata objects (or names) for the given table object.
242              
243             =back
244              
245             =cut
246