« blib/lib/UR/BoolExpr/Template/PropertyComparison/True.pm
↑ Summary
blib/lib/UR/DataSource/RDBMS/TableColumn.pm »
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package UR::DataSource::RDBMS::Table; | ||
| 5 | |||
| 6 | use UR; | ||
| 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 | my($self,$subject) = @_; | ||
| 32 | |||
| 33 | 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 | my $pos = index($class, '::Table'); | ||
| 38 | substr($class, $pos + 2, 5, $subject); # +2 to keep the "::" | ||
| 39 | |||
| 40 | return $class; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _fk_constraint_class { | ||
| 44 | return shift->_related_class_name('FkConstraint'); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _pk_constraint_class { | ||
| 48 | return shift->_related_class_name('PkConstraintColumn'); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _unique_constraint_class { | ||
| 52 | return shift->_related_class_name('UniqueConstraintColumn'); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _table_column_class { | ||
| 56 | return shift->_related_class_name('TableColumn'); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _bitmap_index_class { | ||
| 60 | return shift->_related_class_name('BitmapIndex'); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub columns { | ||
| 64 | my $self = shift; | ||
| 65 | |||
| 66 | my $col_class = $self->_table_column_class; | ||
| 67 | return $col_class->get(data_source => $self->data_source, table_name => $self->table_name); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub column_names { | ||
| 71 | return map { $_->column_name } shift->columns; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub primary_key_constraint_columns { | ||
| 75 | my $self = shift; | ||
| 76 | |||
| 77 | my $pk_class = $self->_pk_constraint_class; | ||
| 78 | my @pks = $pk_class->get(data_source => $self->data_source, table_name => $self->table_name); | ||
| 79 | my @pks_with_rank = map { [ $_->rank, $_ ] } @pks; | ||
| 80 | return map { $_->[1] } | ||
| 81 | sort { $a->[0] <=> $b->[0] } | ||
| 82 | @pks_with_rank; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | sub primary_key_constraint_column_names { | ||
| 87 | return map { $_->column_name } shift->primary_key_constraint_columns; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | sub fk_constraints { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | my $fk_class = $self->_fk_constraint_class; | ||
| 95 | my @fks = $fk_class->get(data_source => $self->data_source, table_name => $self->table_name); | ||
| 96 | return @fks; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub fk_constraint_names { | ||
| 100 | return map { $_->fk_constraint_name } shift->fk_constraints; | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | sub ref_fk_constraints { | ||
| 105 | my $self = shift; | ||
| 106 | |||
| 107 | my $fk_class = $self->_fk_constraint_class; | ||
| 108 | my @fks = $fk_class->get(data_source => $self->data_source, r_table_name => $self->table_name); | ||
| 109 | return @fks; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub ref_fk_constraint_names { | ||
| 113 | return map { $_->fk_constraint_name } shift->ref_fk_constraints; | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | sub unique_constraint_column_names { | ||
| 118 | my($self,$constraint) = @_; | ||
| 119 | |||
| 120 | my @c; | ||
| 121 | if ($constraint) { | ||
| 122 | @c = $self->unique_constraints(constraint_name => $constraint); | ||
| 123 | } else { | ||
| 124 | @c = $self->unique_constraints(); | ||
| 125 | } | ||
| 126 | my %names = map {$_->column_name => 1 } @c; | ||
| 127 | return keys %names; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub unique_constraint_names { | ||
| 131 | my $self = shift; | ||
| 132 | |||
| 133 | my %names = map { $_->constraint_name => 1 } $self->unique_constraints; | ||
| 134 | return keys %names; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub unique_constraints { | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | my $uc_class = $self->_unique_constraint_class; | ||
| 141 | my @c = $uc_class->get( data_source => $self->data_source, table_name => $self->table_name, @_); | ||
| 142 | |||
| 143 | return @c; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub bitmap_indexes { | ||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my $bi_class = $self->_bitmap_index_class; | ||
| 150 | my @bi = $bi_class->get(data_source => $self->data_source, table_name => $self->table_name); | ||
| 151 | return @bi; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | sub bitmap_index_names { | ||
| 156 | return map { $_->bitmap_index_name } shift->bitmap_indexes; | ||
| 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 | my $self = shift; | ||
| 163 | |||
| 164 | return UR::Object::Type->get(table_name => $self->table_name, @_); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub handler_class_name { | ||
| 168 | my $self = shift; | ||
| 169 | |||
| 170 | return $self->handler_class(@_)->class_name; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub delete { | ||
| 174 | my $self = shift; | ||
| 175 | |||
| 176 | my @deleteme = ( $self->fk_constraints, | ||
| 177 | $self->bitmap_indexes, | ||
| 178 | $self->primary_key_constraint_columns, | ||
| 179 | $self->columns, | ||
| 180 | ); | ||
| 181 | for my $obj ( @deleteme ) { | ||
| 182 | $obj->delete; | ||
| 183 | unless ($obj->isa('UR::DeletedRef')) { | ||
| 184 | Carp::confess("Failed to delete $obj ".$obj->{'id'}); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | $self->SUPER::delete(); | ||
| 188 | 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<ur update classes>, but can be retrieved and used in any application. | ||
| 214 | Their instances come from from the MetaDB (L<UR::DataSource::Meta>) 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 |