« blib/lib/blx/xsdsql/ios/istream.pm
↑ Summary
blib/lib/blx/xsdsql/schema_repository/sql/generic/table.pm »
| 1 | package blx::xsdsql::xsd_parser::type::simple; | ||
| 2 | use strict; # use strict is for PBP | ||
| 3 | use Filter::Include; | ||
| 4 | include blx::xsdsql::include; | ||
| 5 | #line 6 | ||
| 6 | |||
| 7 | use blx::xsdsql::ut::ut qw(nvl); | ||
| 8 | use blx::xsdsql::schema_repository::sql::generic::limits qw(get_xsd_based_types get_normalized_type); | ||
| 9 | |||
| 10 | use base qw(blx::xsdsql::xsd_parser::type::base); | ||
| 11 | |||
| 12 | |||
| 13 | sub _minus { | ||
| 14 | my ($n1,$n2)=@_; | ||
| 15 | return $n2 unless defined $n1; | ||
| 16 | return $n1 < $n2 ? $n1 : $n2; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub _merge { | ||
| 20 | my ($h1,$h2)=@_; | ||
| 21 | for my $k(keys %$h2) { $h1->{$k}=$h2->{$k} } | ||
| 22 | undef; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my ($class,%params)=@_; | ||
| 27 | affirm { defined $params{NAME} != defined $params{XSD_TYPE} } 'param NAME or XSD_TYPE must be set'; | ||
| 28 | affirm { ref($params{NAME}) eq '' || ref($params{NAME}) eq 'HASH' } 'param NAME must be scalar or HASH'; | ||
| 29 | affirm { defined $params{NAME} && !defined $params{LIMITS} || defined $params{XSD_TYPE}} 'param LIMITS is valid with param XSD_TYPE'; | ||
| 30 | affirm { ref($params{XSD_TYPE}) eq '' } 'param XSD_TYPE must be a scalar'; | ||
| 31 | affirm { !defined $params{LIMITS} || ref($params{LIMITS}) eq 'HASH' } 'param LIMITS must be HASH'; | ||
| 32 | bless \%params,$class; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | sub get_sql_type { | ||
| 38 | my ($self,%params)=@_; | ||
| 39 | return $self->{SQL_T_} if defined $self->{SQL_T_}; | ||
| 40 | if (defined (my $xsd_type=$self->{XSD_TYPE})) { | ||
| 41 | affirm { exists get_xsd_based_types(FORMAT => 'HASH')->{$xsd_type} } "$xsd_type: is not a simple base type"; | ||
| 42 | return $self->{SQL_T_}={ | ||
| 43 | %{nvl($self->{LIMITS},{})} | ||
| 44 | ,BASE => $xsd_type | ||
| 45 | ,SQL_TYPE => $xsd_type | ||
| 46 | } | ||
| 47 | } | ||
| 48 | $self->{NAME}={ base => $self->{NAME}} unless ref($self->{NAME}); | ||
| 49 | my $name=$self->{NAME}; | ||
| 50 | my $base=$name->{base}; | ||
| 51 | affirm { defined $base } "base not set"; | ||
| 52 | $self->{SQL_T_}={ BASE => $base }; | ||
| 53 | |||
| 54 | my $normalized_type=get_normalized_type($base); | ||
| 55 | affirm { defined $normalized_type } "$base: not normalized"; | ||
| 56 | if ($normalized_type eq 'string') { | ||
| 57 | if (defined (my $l=$name->{length})) { | ||
| 58 | $l+=0; | ||
| 59 | _merge($self->{SQL_T_},{ SQL_TYPE => $base,FIXSIZE => $l }); | ||
| 60 | } | ||
| 61 | elsif (defined (my $l1=$name->{maxLength})) { | ||
| 62 | $l1+= 0; | ||
| 63 | _merge($self->{SQL_T_},{ SQL_TYPE => $base,SIZE => $l1 }); | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | _merge($self->{SQL_T_},{ SQL_TYPE => $base }); | ||
| 67 | } | ||
| 68 | if (defined (my $e=$name->{enumeration})) { | ||
| 69 | affirm { ref($e) eq 'ARRAY' } ref($e).": not an ARRAY"; | ||
| 70 | my $n=nvl($self->{SQL_T_}->{SIZE},nvl($self->{SQL_T_}->{FIXSIZE},0)); | ||
| 71 | for my $v(@$e) { | ||
| 72 | my $lv=length($v); | ||
| 73 | $n=$lv if $lv > $n; | ||
| 74 | } | ||
| 75 | if (defined $self->{SQL_T_}->{FIXSIZE}) { | ||
| 76 | $self->{SQL_T_}->{FIXSIZE}=$n; | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | $self->{SQL_T_}->{SIZE}=$n; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | elsif ($normalized_type eq 'decimal') { | ||
| 84 | _merge($self->{SQL_T_},{ SQL_TYPE => 'decimal' }); | ||
| 85 | if (defined (my $l=$name->{totalDigits})) { | ||
| 86 | $l+=0; | ||
| 87 | $self->{SQL_T_}->{INT}=$l; | ||
| 88 | } | ||
| 89 | if (defined (my $l=$name->{fractionDigits})) { | ||
| 90 | $l+=0; | ||
| 91 | $self->{SQL_T_}->{DEC}=$l; | ||
| 92 | } | ||
| 93 | if (defined (my $l=$name->{maxInclusive})) { | ||
| 94 | delete $self->{SQL_T_}->{INT}; #unlimited | ||
| 95 | delete $self->{SQL_T_}->{DEC}; #unlimited | ||
| 96 | } | ||
| 97 | } | ||
| 98 | elsif ($normalized_type eq 'integer') { | ||
| 99 | _merge($self->{SQL_T_},{ SQL_TYPE => $base }); | ||
| 100 | if (defined (my $l=$name->{totalDigits})) { | ||
| 101 | $l+=0; | ||
| 102 | $self->{SQL_T_}->{INT}=$l; | ||
| 103 | } | ||
| 104 | if (defined (my $l=$name->{maxInclusive})) { | ||
| 105 | $l=~s/^0+//; | ||
| 106 | $self->{SQL_T_}->{INT}=_minus($self->{SQL_T_}->{INT},length($l.'')); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | elsif ($normalized_type eq 'boolean') { | ||
| 110 | _merge($self->{SQL_T_},{ SQL_TYPE => 'boolean' }); | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | affirm {0} "$normalized_type: not manipulated"; | ||
| 114 | } | ||
| 115 | return $self->{SQL_T_}; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub resolve_type { return $_[0]; } | ||
| 119 | |||
| 120 | sub link_to_column { | ||
| 121 | my ($self,$c,%params)=@_; | ||
| 122 | if ($c->get_max_occurs > 1) { | ||
| 123 | return $self if defined $c->get_table_reference; | ||
| 124 | my $parent_table=$params{TABLE}; | ||
| 125 | my $schema=$params{SCHEMA}; | ||
| 126 | my $table = $schema->get_attrs_value(qw(TABLE_CLASS))->new( | ||
| 127 | PATH => $c->get_path | ||
| 128 | ,INTERNAL_REFERENCE => 1 | ||
| 129 | ,DEBUG => $self->get_attrs_value(qw(DEBUG)) | ||
| 130 | ); | ||
| 131 | $schema->set_table_names($table); | ||
| 132 | |||
| 133 | $table->add_columns( | ||
| 134 | $schema->get_attrs_value(qw(EXTRA_TABLES))->factory_column(qw(ID)) | ||
| 135 | ,$schema->get_attrs_value(qw(EXTRA_TABLES))->factory_column(qw(SEQ)) | ||
| 136 | ,$schema->get_attrs_value(qw(EXTRA_TABLES))->factory_column(qw(VALUE_COL)) | ||
| 137 | ->set_attrs_value(TYPE => $self,PATH => $c->get_path) | ||
| 138 | ); | ||
| 139 | $c->set_attrs_value( | ||
| 140 | PATH_REFERENCE => $table->get_path | ||
| 141 | ,INTERNAL_REFERENCE => 1 | ||
| 142 | ,TYPE => $schema->get_attrs_value(qw(ID_SQL_TYPE)) | ||
| 143 | ,TABLE_REFERENCE => $table | ||
| 144 | ); | ||
| 145 | $parent_table->add_child_tables($table); | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | $c->set_attrs_value(TYPE => $self); | ||
| 149 | } | ||
| 150 | return $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | 1; | ||
| 154 | |||
| 155 | |||
| 156 | __END__ |