« blib/lib/blx/xsdsql/generator.pm
↑ Summary
blib/lib/blx/xsdsql/schema_repository/sql/generic/limits.pm »
| 1 | package blx::xsdsql::xsd_parser::path_map; | ||
| 2 | |||
| 3 | use strict; # use strict is for PBP | ||
| 4 | use Filter::Include; | ||
| 5 | include blx::xsdsql::include; | ||
| 6 | #line 7 | ||
| 7 | |||
| 8 | use blx::xsdsql::ut::ut qw(nvl); | ||
| 9 | |||
| 10 | |||
| 11 | use base(qw(blx::xsdsql::ut::common_interfaces blx::xsdsql::ios::debuglogger)); | ||
| 12 | |||
| 13 | use constant { | ||
| 14 | STD_NAMESPACE => 'http://www.w3.org/2001/XMLSchema-instance' | ||
| 15 | }; | ||
| 16 | |||
| 17 | |||
| 18 | our %_ATTRS_R:Constant(()); | ||
| 19 | our %_ATTRS_W:Constant( | ||
| 20 | map { my $a=$_;($a,sub { croak $a.": this attribute is not writeable"}) } | ||
| 21 | qw( | ||
| 22 | LINK_TABLES | ||
| 23 | ATTRS | ||
| 24 | TC | ||
| 25 | ) | ||
| 26 | ); | ||
| 27 | |||
| 28 | sub _get_attrs_r { return \%_ATTRS_R; } | ||
| 29 | sub _get_attrs_w { return \%_ATTRS_W; } | ||
| 30 | |||
| 31 | |||
| 32 | sub _register_attribute { | ||
| 33 | my ($self,%params)=@_; | ||
| 34 | my $tag=$params{TAG}; | ||
| 35 | $self->_debug($tag,'register column attribute ',$params{C}->get_name,' with (',$params{C}->get_full_name,')'); | ||
| 36 | $params{ATTRIBUTES}->{$params{T}->get_sql_name}->{$params{C}->get_name}=$params{C}; | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _link_tables { # link column $t1.$c1 => $t2.id | ||
| 41 | my ($self,$t1,$c1,$t2,%params)=@_; | ||
| 42 | $self->{LINK_TABLES}->{$t1->get_sql_name}->{$t2->get_sql_name}=$c1; | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _register_path { | ||
| 47 | my ($self,%params)=@_; | ||
| 48 | my $tag=$params{TAG}; | ||
| 49 | my $path=defined $params{C} | ||
| 50 | ? $params{C}->get_attrs_value(qw(PATH)) | ||
| 51 | : $params{T}->get_attrs_value(qw(PATH)); | ||
| 52 | |||
| 53 | if ($params{ORIG_PATH_NAME}) { | ||
| 54 | $path=$self->_resolve_relative_path($params{ORIG_PATH_NAME},$params{T},$path,%params); | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | affirm { !$params{T}->is_type } "$tag: ORIG_PATH_NAME not set for type table"; | ||
| 58 | } | ||
| 59 | affirm { defined $path && length($path) } "path not set"; | ||
| 60 | |||
| 61 | my $ret=sub { | ||
| 62 | if (defined $params{C}) { #map path into a column | ||
| 63 | my $h={ T => $params{T},C => $params{C}}; | ||
| 64 | my @stack=@{$params{STACK}}; | ||
| 65 | $h->{STACK}=\@stack if $params{T}->is_unpath || $params{T}->is_group_type; | ||
| 66 | $self->_debug($tag,'register column path ',$path,' with (',$params{C}->get_full_name,')'); | ||
| 67 | return $h; | ||
| 68 | } | ||
| 69 | else { #map path into a tables stack | ||
| 70 | $params{STACK}=[] unless defined $params{STACK}; | ||
| 71 | push @{$params{STACK}},{ T => $params{T} }; | ||
| 72 | $self->_debug($tag,'register table path ',$path,' with ('. | ||
| 73 | join(',',map { $_->{T}->get_sql_name.(defined $_->{C} ? '.'.$_->{C}->get_sql_name : '') } @{$params{STACK}}) | ||
| 74 | .')'); | ||
| 75 | return $params{STACK}; | ||
| 76 | } | ||
| 77 | affirm { 0 } "dead code"; | ||
| 78 | undef; | ||
| 79 | }->(); | ||
| 80 | |||
| 81 | if (my $tc=$params{PATH}->{$path}) { #path is already register | ||
| 82 | if ($self->{DEBUG}) { #check the consistence $tc and $ret | ||
| 83 | $self->_debug($tag,"$path: path already register"); | ||
| 84 | if (ref($tc) eq 'ARRAY' && ref($ret) ne 'ARRAY') { | ||
| 85 | $self->_debug(__LINE__,$tc->[-1]->{T}->get_sql_name,$ret->{C}->get_full_name); | ||
| 86 | confess "check consistence 1 failed\n"; | ||
| 87 | } | ||
| 88 | confess "check consistence 2 failed " if ref($tc) eq 'HASH' && ref($ret) ne 'HASH'; | ||
| 89 | if (ref($tc) eq 'ARRAY' && $tc->[-1]->{T}->get_sql_name ne $ret->[-1]->{T}->get_sql_name) { | ||
| 90 | $self->_debug(__LINE__,$tc->[-1]->{T}->get_sql_name); | ||
| 91 | $self->_debug(__LINE__,$tc->[-1]->{T}->get_path); | ||
| 92 | $self->_debug(__LINE__,$ret->[-1]->{T}->get_sql_name); | ||
| 93 | $self->_debug(__LINE__,$ret->[-1]->{T}->get_path); | ||
| 94 | } | ||
| 95 | |||
| 96 | confess "check consistence 3 failed " if ref($tc) eq 'ARRAY' && $tc->[-1]->{T}->get_sql_name ne $ret->[-1]->{T}->get_sql_name; | ||
| 97 | |||
| 98 | if (ref($tc) eq 'HASH' && $tc->{C}->get_full_name ne $ret->{C}->get_full_name) { | ||
| 99 | $self->_debug(__LINE__,$tc->{C}->get_full_name,$ret->{C}->get_full_name); | ||
| 100 | $self->_debug(__LINE__,$ret->{T}->get_sql_name,$ret->{T}->is_group_type); | ||
| 101 | $self->_debug(__LINE__,$tc->{T}->get_sql_name,$tc->{T}->is_group_type); | ||
| 102 | $self->_debug(__LINE__," consistence 4 failed"); | ||
| 103 | } | ||
| 104 | |||
| 105 | confess "check consistence 4 failed " if ref($tc) eq 'HASH' && $tc->{C}->get_full_name ne $ret->{C}->get_full_name; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | else { | ||
| 109 | my $p=$params{PATH}; | ||
| 110 | $p->{$path}=$ret; | ||
| 111 | if (my $ns=$params{XML_NAMESPACES}) { | ||
| 112 | for my $n(@$ns) { | ||
| 113 | next if $n eq 'xs'; | ||
| 114 | my $newpath=join('/',map { length($_) ? $n.':'.$_ : $_; } split('/',$path)); | ||
| 115 | $p->{$newpath}=$ret; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | return $path; | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | sub _resolve_path_ref { | ||
| 125 | my ($self,$table,$col,$path_ref,%params)=@_; | ||
| 126 | $self->_debug($params{TAG},'col',"'".$col->get_full_name."'","ref path '$path_ref'"); | ||
| 127 | my $tab_ref=$params{TYPE_PATHS}->{$path_ref}; | ||
| 128 | return $tab_ref if defined $tab_ref; | ||
| 129 | for my $child($table->get_child_tables) { | ||
| 130 | return $child if nvl($child->get_attrs_value('PATH'),'') eq $path_ref; | ||
| 131 | } | ||
| 132 | for my $t($params{ROOT_TABLE}->get_child_tables) { | ||
| 133 | return $t if nvl($t->get_path,'') eq $path_ref; | ||
| 134 | } | ||
| 135 | undef; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _resolve_table_path { | ||
| 139 | my ($self,$t,%params)=@_; | ||
| 140 | my $x=-1; | ||
| 141 | while(!$t->get_attrs_value(qw(PATH))) { | ||
| 142 | $t=$params{STACK}->[$x--]->{T}; | ||
| 143 | } | ||
| 144 | return $t; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub _resolve_relative_path { | ||
| 148 | my ($self,$startpath,$table,$relative_path,%params)=@_; | ||
| 149 | affirm { defined $startpath } "1 param not set"; | ||
| 150 | affirm { defined $table } "2 param not set"; | ||
| 151 | affirm { defined $relative_path } "3 param not set"; | ||
| 152 | my $path=$startpath.substr($relative_path,length($self->_resolve_table_path($table,%params)->get_attrs_value(qw(PATH)))); | ||
| 153 | return $path; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _mapping_path { | ||
| 157 | my ($self,$table,%params)=@_; | ||
| 158 | my $tag=$params{TAG}; | ||
| 159 | if ($table->get_path && !$table->is_group_type) { | ||
| 160 | $self->_register_path(%params,T => $table,C => undef,TAG => __LINE__); | ||
| 161 | $params{STACK}=[]; | ||
| 162 | } | ||
| 163 | |||
| 164 | for my $col($table->get_columns) { | ||
| 165 | next if $col->is_pk || $col->is_sys_attributes; | ||
| 166 | if ($col->is_attribute) { | ||
| 167 | $self->_register_attribute(%params,T => $table,C => $col,TAG => __LINE__); | ||
| 168 | next; | ||
| 169 | } | ||
| 170 | my ($path_ref,$table_ref)=$col->get_attrs_value(qw(PATH_REFERENCE TABLE_REFERENCE)); | ||
| 171 | if (defined $path_ref || defined $table_ref) { | ||
| 172 | if (defined $table_ref) { #the column ref a table | ||
| 173 | if ($col->is_internal_reference) { | ||
| 174 | $self->_register_path(%params,T => $table,C => $col,TAG => __LINE__); | ||
| 175 | if ($table_ref->is_simple_content_type) { | ||
| 176 | for my $col($table_ref->get_columns) { | ||
| 177 | next unless $col->is_attribute; | ||
| 178 | $self->_register_attribute(%params,T => $table_ref,C => $col,TAG => __LINE__); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | else { | ||
| 183 | $self->_link_tables($table,$col,$table_ref); | ||
| 184 | my $orig_path_name=$params{ORIG_PATH_NAME}; | ||
| 185 | if (my $path=$col->get_path) { | ||
| 186 | |||
| 187 | $orig_path_name=$self->_resolve_relative_path( | ||
| 188 | nvl($params{ORIG_PATH_NAME},$self->_resolve_table_path($table,%params)->get_path) | ||
| 189 | ,$table | ||
| 190 | ,$path | ||
| 191 | ,%params | ||
| 192 | ,TAG => __LINE__ | ||
| 193 | ); | ||
| 194 | |||
| 195 | } | ||
| 196 | my @stack=({ T => $table,C => $col }); | ||
| 197 | @stack=(@{$params{STACK}},@stack) if ! $table->get_path || $table->is_group_type; | ||
| 198 | $self->_mapping_path($table_ref,%params,STACK => \@stack,ORIG_PATH_NAME => $orig_path_name); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | else { # the column ref a path of an unknow table | ||
| 202 | affirm { !$col->is_internal_reference } nvl($tag).": the column has internal reference and table_ref is not a table: '".$col->get_full_name."'"; | ||
| 203 | my $t=$self->_resolve_path_ref($table,$col,$path_ref,%params,TAG => __LINE__); | ||
| 204 | affirm { defined $t } "$path_ref: path not resolved from '".$col->get_full_name."'"; | ||
| 205 | $self->_link_tables($table,$col,$t); | ||
| 206 | $col->set_attrs_value(TABLE_REFERENCE => $t); | ||
| 207 | my $orig_path_name=$params{ORIG_PATH_NAME}; | ||
| 208 | if (my $path=$col->get_path) { | ||
| 209 | $orig_path_name=$self->_resolve_relative_path( | ||
| 210 | nvl($params{ORIG_PATH_NAME},$table->get_attrs_value(qw(PATH))) | ||
| 211 | ,$table | ||
| 212 | ,$path | ||
| 213 | ,%params | ||
| 214 | ,TAG => __LINE__ | ||
| 215 | ); | ||
| 216 | } | ||
| 217 | my @stack=({ T => $table,C => $col }); | ||
| 218 | @stack=(@{$params{STACK}},@stack) if ! $table->get_path || $table->is_group_type; | ||
| 219 | $self->_mapping_path($t,%params,STACK => \@stack,ORIG_PATH_NAME => $orig_path_name); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | else { | ||
| 223 | $self->_register_path(%params,T => $table,C => $col,TAG => __LINE__) if defined $col->get_path; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | undef; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub mapping_paths { | ||
| 230 | my ($self,$root_table,$type_paths,%params)=@_; | ||
| 231 | affirm { ref($root_table) =~/::table$/ } ": 1^ param must be a table"; | ||
| 232 | affirm { ref($type_paths) eq 'HASH' } ": 2^ param must be hash"; | ||
| 233 | my %path_translate=(); | ||
| 234 | my %attr_translate=(); | ||
| 235 | $self->_mapping_path( | ||
| 236 | $root_table | ||
| 237 | ,PATH => \%path_translate | ||
| 238 | ,TYPE_PATHS => $type_paths | ||
| 239 | ,STACK => [] | ||
| 240 | ,ATTRIBUTES => \%attr_translate | ||
| 241 | ,ROOT_TABLE => $root_table | ||
| 242 | ); | ||
| 243 | $self->{TC}=\%path_translate; | ||
| 244 | $self->{ATTRS}=\%attr_translate; | ||
| 245 | $self; | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | sub _manip_path { | ||
| 251 | my ($self,$path,%params)=@_; | ||
| 252 | return $path unless $path=~/:/; # no namespace specificied | ||
| 253 | my @p=map { | ||
| 254 | my $out=$_; | ||
| 255 | $out=$2 if /^([^:]+):(.*)$/; | ||
| 256 | $out; | ||
| 257 | } grep(length($_),split('/',$path)); | ||
| 258 | return '/'.join('/',@p); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub resolve_path { #return an array if resolve into tables otherwise an hash | ||
| 262 | my ($self,$path,%params)=@_; | ||
| 263 | affirm { defined $path } "1^ arg not set"; | ||
| 264 | my $p=$self->_manip_path($path,%params); | ||
| 265 | affirm { defined $p} "_manip_path return undef"; | ||
| 266 | my $a=$self->{TC}->{$p}; | ||
| 267 | $self->_debug($params{TAG},"$p: path not resolved - orig path is '$path'") unless defined $a; | ||
| 268 | $a; | ||
| 269 | } | ||
| 270 | |||
| 271 | |||
| 272 | sub resolve_attributes { | ||
| 273 | my ($self,$table,$nsprefixes,@attrnames)=@_; | ||
| 274 | affirm { defined $table } "1^ param not set"; | ||
| 275 | affirm { ref($nsprefixes) eq 'HASH' } "2^ param is not HASH"; | ||
| 276 | my $table_name=$table->get_sql_name; | ||
| 277 | $self->_debug(undef,'attributes: ',keys %{$self->{ATTRS}->{$table_name}}); | ||
| 278 | my @cols=map { | ||
| 279 | my $fullname=$_; | ||
| 280 | my $out=undef; | ||
| 281 | if ($fullname!~/^xmlns:/ && $fullname ne 'xmlns') { | ||
| 282 | my ($ns,$name)=$fullname=~/^([^:]+):([^:]+)$/; | ||
| 283 | if (defined $ns) { | ||
| 284 | affirm { defined $nsprefixes->{$ns} } "$ns: prefix not know"; | ||
| 285 | if ($nsprefixes->{$ns} ne STD_NAMESPACE) { | ||
| 286 | $out=$self->{ATTRS}->{$table_name}->{$name}; | ||
| 287 | affirm {defined $out } "$name: not found column for this attribute in table '$table_name'"; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | else { | ||
| 291 | $out=$self->{ATTRS}->{$table_name}->{$fullname}; | ||
| 292 | affirm {defined $out } "$fullname: not found column for this attribute in table '$table_name'"; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | $out; | ||
| 296 | } @attrnames; | ||
| 297 | return @cols if wantarray; | ||
| 298 | return scalar(@cols) <= 1 ? $cols[0] : \@cols; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub resolve_column_link { | ||
| 302 | my ($self,$t1,$t2,%params)=@_; | ||
| 303 | my ($n1,$n2)=($t1->get_sql_name,$t2->get_sql_name); | ||
| 304 | my $col=$self->{LINK_TABLES}->{$n1}->{$n2}; | ||
| 305 | affirm { defined $col } "$n1 => $n2: link not resolved"; | ||
| 306 | $col; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub new { | ||
| 310 | my ($classname,%params)=@_; | ||
| 311 | my $self=bless {},$classname; | ||
| 312 | $self->set_attrs_value(%params); | ||
| 313 | $self; | ||
| 314 | } | ||
| 315 | |||
| 316 | |||
| 317 | 1; | ||
| 318 | |||
| 319 | __END__ |