| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package FabForce::DBDesigner4::Table; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 22047 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 35 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 5 | 1 |  |  | 1 |  | 4 | use Exporter; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 91 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our @ISA = qw(Exporter); | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our @EXPORT_OK = qw( | 
| 10 |  |  |  |  |  |  | IDENTIFYING_1_TO_1 | 
| 11 |  |  |  |  |  |  | IDENTIFYING_1_TO_N | 
| 12 |  |  |  |  |  |  | NON_IDENTIFYING_1_TO_N | 
| 13 |  |  |  |  |  |  | NON_IDENTIFYING_1_TO_1 | 
| 14 |  |  |  |  |  |  | ); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our %EXPORT_TAGS = ( | 
| 17 |  |  |  |  |  |  | 'const' => [@EXPORT_OK] | 
| 18 |  |  |  |  |  |  | ); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | use constant { | 
| 21 | 1 |  |  |  |  | 1528 | IDENTIFYING_1_TO_1     => 0, | 
| 22 |  |  |  |  |  |  | IDENTIFYING_1_TO_N     => 1, | 
| 23 |  |  |  |  |  |  | NON_IDENTIFYING_1_TO_N => 2, | 
| 24 |  |  |  |  |  |  | NON_IDENTIFYING_1_TO_1 => 5, | 
| 25 | 1 |  |  | 1 |  | 4 | }; | 
|  | 1 |  |  |  |  | 1 |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | our $VERSION     = '0.07'; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub new{ | 
| 30 | 0 |  |  | 0 | 1 |  | my ($class,%args) = @_; | 
| 31 | 0 |  |  |  |  |  | my $self = {}; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 0 |  |  |  |  |  | bless $self,$class; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 0 |  |  |  |  |  | $self->{COORDS}          = []; | 
| 36 | 0 |  |  |  |  |  | $self->{COLUMNS}         = []; | 
| 37 | 0 |  |  |  |  |  | $self->{COLUMNS_DETAILS} = []; | 
| 38 | 0 |  |  |  |  |  | $self->{NAME}            = ''; | 
| 39 | 0 |  |  |  |  |  | $self->{RELATIONS}       = []; | 
| 40 | 0 |  |  |  |  |  | $self->{KEY}             = []; | 
| 41 | 0 |  |  |  |  |  | $self->{ATTRIBUTE}       = {}; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 | 0 |  |  |  |  | $self->{COORDS}          = $args{-coords}         if(_checkArg('coords'        , $args{-coords}        )); | 
| 44 | 0 | 0 |  |  |  |  | $self->{COLUMNS}         = $args{-columns}        if(_checkArg('columns'       , $args{-columns}       )); | 
| 45 | 0 | 0 |  |  |  |  | $self->{COLUMNS_DETAILS} = $args{-column_details} if(_checkArg('columnsdetails', $args{-columnsdetails})); | 
| 46 | 0 | 0 |  |  |  |  | $self->{NAME}            = $args{-name}           if(_checkArg('name'          , $args{-name}          )); | 
| 47 | 0 | 0 |  |  |  |  | $self->{RELATIONS}       = $args{-relations}      if(_checkArg('relations'     , $args{-relations}     )); | 
| 48 | 0 | 0 |  |  |  |  | $self->{KEY}             = $args{-key}            if(_checkArg('key'           , $args{-key}           )); | 
| 49 | 0 | 0 |  |  |  |  | $self->{INDEX}           = $args{-index}          if(_checkArg('index'         , $args{-index}         )); | 
| 50 | 0 | 0 |  |  |  |  | $self->{ATTRIBUTE}       = $args{-attr}           if(_checkArg('attribute'     , $args{-attr}          )); | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 |  |  |  |  |  | return $self; | 
| 53 |  |  |  |  |  |  | }# new | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub columns{ | 
| 56 | 0 |  |  | 0 | 1 |  | my ($self,$ar) = @_; | 
| 57 | 0 | 0 | 0 |  |  |  | unless($ar && _checkArg('columns',$ar)){ | 
| 58 | 0 |  |  |  |  |  | my @columns; | 
| 59 | 0 |  |  |  |  |  | for my $col(@{$self->{COLUMNS}}){ | 
|  | 0 |  |  |  |  |  |  | 
| 60 | 0 |  |  |  |  |  | my $string = join('',keys(%$col)); | 
| 61 | 0 |  |  |  |  |  | for my $val(values(%$col)){ | 
| 62 | 0 |  |  |  |  |  | for my $elem(@$val){ | 
| 63 | 0 | 0 |  |  |  |  | if( defined $elem ){ | 
| 64 | 0 | 0 |  |  |  |  | $elem = 'VARCHAR(255)'              if $elem =~ /^varchar$/i; | 
| 65 | 0 | 0 |  |  |  |  | $elem = "ENUM('1','0') DEFAULT '0'" if $elem =~ /^enum$/i; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 0 |  |  |  |  |  | $string .= " ".$elem; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | } | 
| 71 | 0 |  |  |  |  |  | push(@columns,$string); | 
| 72 |  |  |  |  |  |  | } | 
| 73 | 0 |  |  |  |  |  | return @columns; | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 0 |  |  |  |  |  | $self->{COLUMNS} = $ar; | 
| 76 | 0 |  |  |  |  |  | return 1; | 
| 77 |  |  |  |  |  |  | }# columns | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub column_details { | 
| 80 | 0 |  |  | 0 | 1 |  | my ($self, $details) = @_; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 | 0 | 0 |  |  |  | if ( $details && _checkArg('columndetails', $details) ) { | 
| 83 | 0 |  |  |  |  |  | $self->{COLUMNS_DETAILS} = $details; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | return $self->{COLUMNS_DETAILS}; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub column_names{ | 
| 90 | 0 |  |  | 0 | 1 |  | my ($self) = @_; | 
| 91 | 0 |  |  |  |  |  | my @names; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 0 |  |  |  |  |  | for my $col ( @{ $self->{COLUMNS} } ){ | 
|  | 0 |  |  |  |  |  |  | 
| 94 | 0 |  |  |  |  |  | push @names, join '', keys %$col; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 0 |  |  |  |  |  | return @names; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub columnType{ | 
| 101 | 0 |  |  | 0 | 1 |  | my ($self,$name) = @_; | 
| 102 | 0 | 0 |  |  |  |  | return unless($name); | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | my $type = ''; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 0 |  |  |  |  |  | for(0..scalar(@{$self->{COLUMNS}})-1){ | 
|  | 0 |  |  |  |  |  |  | 
| 107 | 0 |  |  |  |  |  | my ($key) = keys(%{$self->{COLUMNS}->[$_]}); | 
|  | 0 |  |  |  |  |  |  | 
| 108 | 0 | 0 |  |  |  |  | if($key eq $name){ | 
| 109 | 0 |  |  |  |  |  | $type = $self->{COLUMNS}->[$_]->{$key}->[0]; | 
| 110 | 0 |  |  |  |  |  | last; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  | } | 
| 113 | 0 |  |  |  |  |  | return $type; | 
| 114 |  |  |  |  |  |  | }# columnType | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub columnInfo{ | 
| 117 | 0 |  |  | 0 | 1 |  | my ($self,$nr) = @_; | 
| 118 | 0 |  |  |  |  |  | return $self->{COLUMNS}->[$nr]; | 
| 119 |  |  |  |  |  |  | }# columnInfo | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | sub addColumn{ | 
| 122 | 0 |  |  | 0 | 1 |  | my ($self,$ar) = @_; | 
| 123 | 0 | 0 | 0 |  |  |  | return unless($ar && ref($ar) eq 'ARRAY'); | 
| 124 | 0 |  |  |  |  |  | push(@{$self->{COLUMNS}},{$ar->[0] => [@{$ar}[1,2]]}); | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 125 | 0 |  |  |  |  |  | return 1; | 
| 126 |  |  |  |  |  |  | }# addColumn | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | sub stringsToTableCols{ | 
| 129 | 0 |  |  | 0 | 1 |  | my ($self,@array) = @_; | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 |  |  |  |  |  | my @returnArray; | 
| 132 | 0 |  |  |  |  |  | for my $col(@array){ | 
| 133 | 0 |  |  |  |  |  | $col =~ s!,\s*?$!!; | 
| 134 | 0 |  |  |  |  |  | $col =~ s!^\s*!!; | 
| 135 | 0 | 0 | 0 |  |  |  | next if((not defined $col) or $col eq ''); | 
| 136 | 0 |  |  |  |  |  | my ($name,$type,$info) = split(/\s+/,$col,3); | 
| 137 | 0 |  |  |  |  |  | push(@returnArray,{$name => [$type,$info]}); | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 0 |  |  |  |  |  | return @returnArray; | 
| 141 |  |  |  |  |  |  | }# arrayToTableCols | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | sub coords{ | 
| 144 | 0 |  |  | 0 | 1 |  | my ($self,$ar) = @_; | 
| 145 | 0 | 0 | 0 |  |  |  | return @{$self->{COORDS}} unless($ar && _checkArg('coords',$ar)); | 
|  | 0 |  |  |  |  |  |  | 
| 146 | 0 |  |  |  |  |  | $self->{COORDS} = $ar; | 
| 147 | 0 |  |  |  |  |  | return 1; | 
| 148 |  |  |  |  |  |  | }# start | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | sub name{ | 
| 151 | 0 |  |  | 0 | 1 |  | my ($self,$value) = @_; | 
| 152 | 0 | 0 | 0 |  |  |  | return $self->{NAME} unless($value && _checkArg('name',$value)); | 
| 153 | 0 |  |  |  |  |  | $self->{NAME} = $value; | 
| 154 | 0 |  |  |  |  |  | return 1; | 
| 155 |  |  |  |  |  |  | }# name | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | sub relations{ | 
| 158 | 0 |  |  | 0 | 1 |  | my ($self,$value) = @_; | 
| 159 | 0 | 0 | 0 |  |  |  | return @{$self->{RELATIONS}} unless($value && _checkArg('relations',$value)); | 
|  | 0 |  |  |  |  |  |  | 
| 160 | 0 |  |  |  |  |  | $self->{RELATIONS} = $value; | 
| 161 | 0 |  |  |  |  |  | return 1; | 
| 162 |  |  |  |  |  |  | }# relations | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | sub addRelation{ | 
| 165 | 0 |  |  | 0 | 1 |  | my ($self,$value) = @_; | 
| 166 | 0 | 0 | 0 |  |  |  | return unless($value && ref($value) eq 'ARRAY' && scalar(@$value) == 4); | 
|  |  |  | 0 |  |  |  |  | 
| 167 | 0 |  |  |  |  |  | push(@{$self->{RELATIONS}},$value); | 
|  | 0 |  |  |  |  |  |  | 
| 168 | 0 |  |  |  |  |  | return 1; | 
| 169 |  |  |  |  |  |  | }# addRelation | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | sub removeRelation{ | 
| 172 | 0 |  |  | 0 | 1 |  | my ($self,$index) = @_; | 
| 173 | 0 | 0 | 0 |  |  |  | return unless(defined $index or $index > (scalar(@{$self->{RELATIONS}})-1)); | 
|  | 0 |  |  |  |  |  |  | 
| 174 | 0 |  |  |  |  |  | splice(@{$self->{RELATIONS}},$index,1); | 
|  | 0 |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | }# removeRelation | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | sub changeRelation{ | 
| 178 | 0 |  |  | 0 | 1 |  | my ($self,$index,$value) = @_; | 
| 179 | 0 | 0 | 0 |  |  |  | return unless(defined $index and defined $value); | 
| 180 | 0 |  |  |  |  |  | $self->{RELATIONS}->[$index]->[0] = $value; | 
| 181 |  |  |  |  |  |  | }# changeRelation | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | sub key{ | 
| 184 | 0 |  |  | 0 | 1 |  | my ($self,$value) = @_; | 
| 185 | 0 | 0 | 0 |  |  |  | return @{$self->{KEY}} unless($value && _checkArg('key',$value)); | 
|  | 0 |  |  |  |  |  |  | 
| 186 | 0 |  |  |  |  |  | $self->{KEY} = $value; | 
| 187 | 0 |  |  |  |  |  | return 1; | 
| 188 |  |  |  |  |  |  | }# key | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | sub tableIndex{ | 
| 191 | 0 |  |  | 0 | 1 |  | my ($self,$value) = @_; | 
| 192 | 0 | 0 | 0 |  |  |  | return @{$self->{INDEX}} unless($value && _checkArg('index',$value)); | 
|  | 0 |  |  |  |  |  |  | 
| 193 | 0 |  |  |  |  |  | $self->{INDEX} = $value; | 
| 194 | 0 |  |  |  |  |  | return 1; | 
| 195 |  |  |  |  |  |  | }# tableIndex | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | sub attribute{ | 
| 198 | 0 |  |  | 0 | 1 |  | my ($self,$value) = @_; | 
| 199 | 0 | 0 | 0 |  |  |  | return @{$self->{ATTRIBUTE}} unless($value && _checkArg('attribute',$value)); | 
|  | 0 |  |  |  |  |  |  | 
| 200 | 0 |  |  |  |  |  | $self->{ATTRIBUTE} = $value; | 
| 201 | 0 |  |  |  |  |  | return 1; | 
| 202 |  |  |  |  |  |  | }# attribute | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | sub get_foreign_keys{ | 
| 205 | 0 |  |  | 0 | 1 |  | my ($table) = @_; | 
| 206 |  |  |  |  |  |  |  | 
| 207 | 0 | 0 |  |  |  |  | unless( defined $table->{_foreign_relations} ){ | 
| 208 | 0 |  |  |  |  |  | my $tablename = $table->name(); | 
| 209 | 0 |  |  |  |  |  | my @relations = grep{$_->[1] =~ /^$tablename\./}$table->relations(); | 
|  | 0 |  |  |  |  |  |  | 
| 210 | 0 |  |  |  |  |  | $table->{_foreign_relations} = { _getForeignKeys(@relations) }; | 
| 211 |  |  |  |  |  |  | } | 
| 212 | 0 |  |  |  |  |  | return $table->{_foreign_relations}; | 
| 213 |  |  |  |  |  |  | } | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | sub _checkArg{ | 
| 216 | 0 |  |  | 0 |  |  | my ($type,$value) = @_; | 
| 217 | 0 |  |  |  |  |  | my $return = 0; | 
| 218 | 0 | 0 |  |  |  |  | if($value){ | 
| 219 | 0 |  |  |  |  |  | $return = 1 if($type eq 'coords' | 
| 220 |  |  |  |  |  |  | && ref($value) eq 'ARRAY' | 
| 221 |  |  |  |  |  |  | && scalar(@$value) == 4 | 
| 222 | 0 | 0 | 0 |  |  |  | && !grep{/\D/}@$value); | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 223 |  |  |  |  |  |  |  | 
| 224 | 0 |  |  |  |  |  | $return = 1 if($type eq 'columns' | 
| 225 |  |  |  |  |  |  | && ref($value) eq 'ARRAY' | 
| 226 | 0 | 0 | 0 |  |  |  | && !(!grep{ref($_) eq 'HASH'}@$value)); | 
|  |  |  | 0 |  |  |  |  | 
| 227 |  |  |  |  |  |  |  | 
| 228 | 0 | 0 | 0 |  |  |  | $return = 1 if( $type eq 'columndetails' && ref($value) eq 'ARRAY' ); | 
| 229 |  |  |  |  |  |  |  | 
| 230 | 0 | 0 | 0 |  |  |  | $return = 1 if($type eq 'name' | 
| 231 |  |  |  |  |  |  | && ref(\$value) eq 'SCALAR'); | 
| 232 |  |  |  |  |  |  |  | 
| 233 | 0 |  |  |  |  |  | $return = 1 if($type eq 'relations' | 
| 234 |  |  |  |  |  |  | && ref($value) eq 'ARRAY' | 
| 235 | 0 | 0 | 0 |  |  |  | && !(!grep{ref($_) eq 'ARRAY'}@$value)); | 
|  |  |  | 0 |  |  |  |  | 
| 236 |  |  |  |  |  |  |  | 
| 237 | 0 |  |  |  |  |  | $return = 1 if($type eq 'key' | 
| 238 |  |  |  |  |  |  | && ref($value) eq 'ARRAY' | 
| 239 | 0 | 0 | 0 |  |  |  | && !(!grep{ref(\$_) eq 'SCALAR'}@$value)); | 
|  |  |  | 0 |  |  |  |  | 
| 240 |  |  |  |  |  |  |  | 
| 241 | 0 |  |  |  |  |  | $return = 1 if($type eq 'index' | 
| 242 |  |  |  |  |  |  | && ref($value) eq 'ARRAY' | 
| 243 | 0 | 0 | 0 |  |  |  | && !(!grep{ref(\$_) eq 'SCALAR'}@$value)); | 
|  |  |  | 0 |  |  |  |  | 
| 244 |  |  |  |  |  |  |  | 
| 245 | 0 | 0 | 0 |  |  |  | $return = 1 if($type eq 'attribute' | 
| 246 |  |  |  |  |  |  | && ref($value) eq 'HASH'); | 
| 247 |  |  |  |  |  |  | } | 
| 248 |  |  |  |  |  |  |  | 
| 249 | 0 |  |  |  |  |  | return $return; | 
| 250 |  |  |  |  |  |  | }# checkArg | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | sub _getForeignKeys{ | 
| 255 | 0 |  |  | 0 |  |  | my @rels = @_; | 
| 256 | 0 |  |  |  |  |  | my %relations; | 
| 257 | 0 |  |  |  |  |  | for my $rel(@rels){ | 
| 258 | 0 | 0 |  |  |  |  | next unless $rel; | 
| 259 | 0 |  |  |  |  |  | my $start           = (split(/\./,$rel->[1]))[1]; | 
| 260 | 0 |  |  |  |  |  | my ($table,$target) =  split(/\./,$rel->[2]); | 
| 261 | 0 |  |  |  |  |  | push(@{$relations{$table}},[$start,$target]); | 
|  | 0 |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | } | 
| 263 | 0 |  |  |  |  |  | return %relations; | 
| 264 |  |  |  |  |  |  | }# getForeignKeys | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  | 1; | 
| 267 |  |  |  |  |  |  | __END__ |