| 1 | package blx::xsdsql::xsd_parser::node::element; | ||
| 2 | |||
| 3 | use strict; # use strict is for PBP | ||
| 4 | use Filter::Include; | ||
| 5 | include blx::xsdsql::include; | ||
| 6 | #line 7 | ||
| 7 | use File::Basename; | ||
| 8 | |||
| 9 | use blx::xsdsql::ut::ut qw(nvl); | ||
| 10 | use blx::xsdsql::xsd_parser::type; | ||
| 11 | use blx::xsdsql::xsd_parser::type::simple; | ||
| 12 | use base qw(blx::xsdsql::xsd_parser::node); | ||
| 13 | |||
| 14 | sub trigger_at_start_node { | ||
| 15 | my ($self,%params)=@_; | ||
| 16 | my $parent_table=$self->_get_parent_table; | ||
| 17 | my $isparent_choice=$parent_table->get_attrs_value(qw(CHOICE)); | ||
| 18 | my ($maxoccurs,$minoccurs) = ($self->_resolve_maxOccurs,$self->_resolve_minOccurs(CHOICE => $isparent_choice)); | ||
| 19 | my $is_mixed=$self->_is_into_a_mixed; | ||
| 20 | if (defined (my $name=$self->get_attrs_value(qw(name)))) { | ||
| 21 | my $path=$self->_construct_path($name,%params,PARENT => undef); | ||
| 22 | if (defined (my $xsd_type=$self->get_attrs_value(qw(type)))) { | ||
| 23 | my ($schema,$debug)=($self->get_attrs_value(qw(STACK))->[1],$self->get_attrs_value(qw(DEBUG))); | ||
| 24 | if ($is_mixed) { | ||
| 25 | my $column = $self->get_attrs_value(qw(COLUMN_CLASS))->new( | ||
| 26 | MINOCCURS => 1 | ||
| 27 | ,MAXOCCURS => 1 | ||
| 28 | ,CHOICE => $isparent_choice | ||
| 29 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 30 | ,MIXED => 1 | ||
| 31 | ,TYPE => blx::xsdsql::xsd_parser::type::simple->new(NAME => 'string') | ||
| 32 | ); | ||
| 33 | if (defined $parent_table->get_xsd_seq) { #the table is a sequence or choice | ||
| 34 | $column->set_attrs_value(XSD_SEQ => $parent_table->get_xsd_seq); | ||
| 35 | $parent_table->inc_xsd_seq unless $isparent_choice; #the columns of a choice have the same xsd_seq | ||
| 36 | } | ||
| 37 | $parent_table->add_columns($column); | ||
| 38 | } | ||
| 39 | my $ty_obj=blx::xsdsql::xsd_parser::type::factory( | ||
| 40 | $xsd_type | ||
| 41 | ,SCHEMA => $schema | ||
| 42 | ,DEBUG => $debug | ||
| 43 | ); | ||
| 44 | |||
| 45 | if ($maxoccurs > 1 && ref($ty_obj)=~/::simple$/) { | ||
| 46 | $self->_debug(undef,$xsd_type,': simple type with maxOccurs > 1'); | ||
| 47 | my $column = $self->get_attrs_value(qw(COLUMN_CLASS))->new( | ||
| 48 | PATH => $path | ||
| 49 | ,TYPE => $schema->get_attrs_value(qw(ID_SQL_TYPE)) | ||
| 50 | ,MINOCCURS => $minoccurs | ||
| 51 | ,MAXOCCURS => $maxoccurs | ||
| 52 | ,INTERNAL_REFERENCE => 1 | ||
| 53 | ,CHOICE => $isparent_choice | ||
| 54 | ,ELEMENT_FORM => $self->_resolve_form | ||
| 55 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 56 | ); | ||
| 57 | if (defined $parent_table->get_xsd_seq) { #the table is a sequence or choice | ||
| 58 | $column->set_attrs_value(XSD_SEQ => $parent_table->get_xsd_seq); | ||
| 59 | $parent_table->inc_xsd_seq unless $isparent_choice; #the columns of a choice have the same xsd_seq | ||
| 60 | } | ||
| 61 | $parent_table->add_columns($column); | ||
| 62 | my $table=$self->get_attrs_value(qw(TABLE_CLASS))->new( | ||
| 63 | PATH => $path | ||
| 64 | ,INTERNAL_REFERENCE => 1 | ||
| 65 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 66 | ,MIXED => $is_mixed | ||
| 67 | ); | ||
| 68 | $schema->set_table_names($table); | ||
| 69 | $table->add_columns( | ||
| 70 | $schema->get_attrs_value(qw(EXTRA_TABLES))->factory_column(qw(ID)) | ||
| 71 | ,$schema->get_attrs_value(qw(EXTRA_TABLES))->factory_column(qw(SEQ)) | ||
| 72 | ); | ||
| 73 | my $value_col=$schema->get_attrs_value(qw(EXTRA_TABLES))->factory_column(qw(VALUE_COL)) | ||
| 74 | ->set_attrs_value(TYPE => $ty_obj,PATH => $path,CHOICE => $isparent_choice); | ||
| 75 | |||
| 76 | if ($is_mixed) { | ||
| 77 | my $col = $self->get_attrs_value(qw(COLUMN_CLASS))->new( | ||
| 78 | MINOCCURS => 1 | ||
| 79 | ,MAXOCCURS => 1 | ||
| 80 | ,CHOICE => $isparent_choice | ||
| 81 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 82 | ,MIXED => 1 | ||
| 83 | ,TYPE => blx::xsdsql::xsd_parser::type::simple->new(NAME => 'string') | ||
| 84 | ); | ||
| 85 | $table->add_columns($col); | ||
| 86 | } | ||
| 87 | $table->add_columns($value_col); | ||
| 88 | $column->set_attrs_value(TABLE_REFERENCE => $table,PATH_REFERENCE => $table->get_path); | ||
| 89 | $parent_table->add_child_tables($table); | ||
| 90 | $self->set_attrs_value(TABLE => $table); | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | $self->_debug(undef,$path,': type with maxOccurs <= 1 or type is not simple'); | ||
| 94 | my $column = $self->get_attrs_value(qw(COLUMN_CLASS))->new( | ||
| 95 | PATH => $path | ||
| 96 | ,TYPE => $ty_obj | ||
| 97 | ,MINOCCURS => $minoccurs | ||
| 98 | ,MAXOCCURS => $maxoccurs | ||
| 99 | ,CHOICE => $isparent_choice | ||
| 100 | ,ELEMENT_FORM => $self->_resolve_form | ||
| 101 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 102 | ); | ||
| 103 | if (defined $parent_table->get_xsd_seq) { #the table is a sequence or choice | ||
| 104 | $column->set_attrs_value(XSD_SEQ => $parent_table->get_xsd_seq); | ||
| 105 | $parent_table->inc_xsd_seq unless $isparent_choice; #the columns of a choice have the same xsd_seq | ||
| 106 | } | ||
| 107 | $parent_table->add_columns($column); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | else { #anonymous type - converted into a table | ||
| 111 | $self->_debug(__LINE__,$path,': anonymous type - converted into table'); | ||
| 112 | my $schema=$self->get_attrs_value(qw(STACK))->[1]; | ||
| 113 | my $table = $self->get_attrs_value(qw(TABLE_CLASS))->new( | ||
| 114 | PATH => $path | ||
| 115 | ,ANONYMOUS_TYPE => 1 | ||
| 116 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 117 | ); | ||
| 118 | $schema->set_table_names($table); | ||
| 119 | my $maxocc=nvl($params{MAXOCCURS},1); | ||
| 120 | $table->set_attrs_value(MAXOCCURS => $maxocc) if $maxocc > 1; | ||
| 121 | $table->set_attrs_value(MAXOCCURS => $maxoccurs) if $maxoccurs > 1; | ||
| 122 | $table->add_columns($schema->get_attrs_value(qw(EXTRA_TABLES))->factory_column(qw(ID))); | ||
| 123 | $parent_table->add_child_tables($table); | ||
| 124 | my $form=$self->_resolve_form; | ||
| 125 | my $column = $schema->get_attrs_value(qw(COLUMN_CLASS))->new( #hoock to the parent the column | ||
| 126 | NAME => $name | ||
| 127 | ,PATH => undef | ||
| 128 | ,TYPE => $schema->get_attrs_value(qw(ID_SQL_TYPE)) | ||
| 129 | ,MINOCCURS => $minoccurs | ||
| 130 | ,MAXOCCURS => $maxoccurs | ||
| 131 | ,PATH_REFERENCE => $path | ||
| 132 | ,TABLE_REFERENCE => $table | ||
| 133 | ,CHOICE => $isparent_choice | ||
| 134 | ,ELEMENT_FORM => $form | ||
| 135 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 136 | ); | ||
| 137 | |||
| 138 | if (defined $parent_table->get_xsd_seq) { #the table is a xs:sequence or a xs:choice | ||
| 139 | $column->set_attrs_value(XSD_SEQ => $parent_table->get_xsd_seq); | ||
| 140 | $parent_table->inc_xsd_seq unless $isparent_choice; | ||
| 141 | } | ||
| 142 | $parent_table->add_columns($column); | ||
| 143 | my $cols=$parent_table->get_columns; | ||
| 144 | my $child_tables=$parent_table->get_child_tables; | ||
| 145 | $self->set_attrs_value( | ||
| 146 | TABLE => $table | ||
| 147 | ,TABLE_INDEX => scalar(@$child_tables) - 1 | ||
| 148 | ,PARENT_TABLE => $parent_table | ||
| 149 | ,COLUMN_INDEX => scalar(@$cols) - 1 | ||
| 150 | ); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | elsif (defined (my $ref=$self->get_attrs_value(qw(ref)))) { | ||
| 154 | $self->_debug(__LINE__,$ref,': element without name and with ref'); | ||
| 155 | my $schema=$self->get_attrs_value(qw(STACK))->[1]; | ||
| 156 | my ($ns,$base)=$ref=~/^([^:]+):(.*)$/; | ||
| 157 | if (defined $ns) { #search the namespace naame | ||
| 158 | $ref=$base; | ||
| 159 | my $namespace=$schema->find_namespace_from_abbr($ns); | ||
| 160 | affirm { defined $namespace } "$ns: not find URI from this namespace abbr"; | ||
| 161 | $ns=$namespace; | ||
| 162 | } | ||
| 163 | my $path=$self->_construct_path($ref,%params,PARENT => undef); | ||
| 164 | my $column = $schema->get_attrs_value(qw(COLUMN_CLASS))->new( | ||
| 165 | REF => 1 | ||
| 166 | ,PATH => $path | ||
| 167 | ,MINOCCURS => $minoccurs | ||
| 168 | ,MAXOCCURS => $maxoccurs | ||
| 169 | ,CHOICE => $isparent_choice | ||
| 170 | ,ELEMENT_FORM => $self->_resolve_form | ||
| 171 | ,URI => $ns | ||
| 172 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 173 | ); | ||
| 174 | if (defined $parent_table->get_xsd_seq) { #the table is a sequence or choice | ||
| 175 | $column->set_attrs_value(XSD_SEQ => $parent_table->get_xsd_seq); | ||
| 176 | $parent_table->inc_xsd_seq unless $isparent_choice; #the columns of a choice have the same xsd_seq | ||
| 177 | } | ||
| 178 | $parent_table->add_columns($column); | ||
| 179 | $self->set_attrs_value(TABLE => $parent_table); | ||
| 180 | } | ||
| 181 | else { | ||
| 182 | croak "node without name or ref is not supported\n"; | ||
| 183 | } | ||
| 184 | |||
| 185 | return $self; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub _get_type { | ||
| 190 | my ($self,%params)=@_; | ||
| 191 | my $childs=delete $params{CHILD}; | ||
| 192 | $childs=$self->get_attrs_value(qw(CHILD)) unless defined $childs; | ||
| 193 | affirm { defined $childs } "CHILD not set"; | ||
| 194 | affirm { ref($childs) eq 'ARRAY' } "CHILD not array"; | ||
| 195 | affirm { scalar(@$childs) == 1 } scalar(@$childs).": CHILDS element must be 1"; | ||
| 196 | my $child=$childs->[0]; | ||
| 197 | my $out={}; | ||
| 198 | my ($schema,$debug)=($self->get_attrs_value(qw(STACK))->[1],$self->get_attrs_value(qw(DEBUG))); | ||
| 199 | $self->_resolve_simple_type($child,undef,$out,%params,SCHEMA => $schema); | ||
| 200 | my $ty_obj=ref($out->{base}) eq '' | ||
| 201 | ? blx::xsdsql::xsd_parser::type::simple->new(NAME => $out,SCHEMA => $schema,DEBUG => $debug) | ||
| 202 | : $out->{base}; | ||
| 203 | return $ty_obj; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub _get_last_node_column { | ||
| 207 | my ($self,$table,%params)=@_; | ||
| 208 | my @cols=$table->get_columns; | ||
| 209 | my $col=undef; | ||
| 210 | while (1) { | ||
| 211 | $col=pop @cols; | ||
| 212 | last unless defined $col; | ||
| 213 | last unless $col->is_sys_attributes or $col->is_attribute or $col->is_pk; | ||
| 214 | } | ||
| 215 | return $col; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub trigger_at_end_node { | ||
| 219 | my ($self,%params)=@_; | ||
| 220 | my $table=$self->get_attrs_value(qw(TABLE)); | ||
| 221 | |||
| 222 | if (defined $table && $table->get_attrs_value(qw(ANONYMOUS_TYPE))) { | ||
| 223 | if (defined (my $childs=$self->get_attrs_value(qw(CHILD)))) { | ||
| 224 | my $parent_table=$self->get_attrs_value(qw(PARENT_TABLE)); | ||
| 225 | my $col=$self->_get_last_node_column($parent_table); | ||
| 226 | affirm { defined $col } "link column not found for table ".$table->get_sql_name; | ||
| 227 | my ($schema,$debug)=($self->get_attrs_value(qw(STACK))->[1],$self->get_attrs_value(qw(DEBUG))); | ||
| 228 | my $ty_obj=$self->_get_type(CHILD => $childs); | ||
| 229 | if ($col->get_max_occurs <= 1) { | ||
| 230 | $col->set_attrs_value(TYPE => $ty_obj,TABLE_REFERENCE => undef,PATH_REFERENCE => undef,PATH => $table->get_path); | ||
| 231 | $parent_table->delete_child_tables($self->get_attrs_value(qw(TABLE_INDEX))); | ||
| 232 | } | ||
| 233 | else { | ||
| 234 | my $path=$table->get_path; | ||
| 235 | $col->set_attrs_value(INTERNAL_REFERENCE => 1,PATH => $path); | ||
| 236 | my $column = $schema->get_attrs_value(qw(COLUMN_CLASS))->new( | ||
| 237 | NAME => basename($path) | ||
| 238 | ,PATH => $path | ||
| 239 | ,CHOICE => $table->is_choice | ||
| 240 | ,TYPE => $ty_obj | ||
| 241 | ,ELEMENT_FORM => $self->_resolve_form | ||
| 242 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 243 | ,VALUE_COL => 1 | ||
| 244 | ); | ||
| 245 | $table->set_attrs_value(INTERNAL_REFERENCE => 1); | ||
| 246 | $table->add_columns( | ||
| 247 | $schema->get_attrs_value(qw(EXTRA_TABLES))->factory_column(qw(SEQ)) | ||
| 248 | ,$column | ||
| 249 | ); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | return $self; | ||
| 254 | } | ||
| 255 | |||
| 256 | 1; | ||
| 257 | |||
| 258 | |||
| 259 | __END__ |