« blib/lib/blx/xsdsql/xsd_parser/path_map.pm
↑ Summary
blib/lib/blx/xsdsql/generator/sql/generic/handle/create_view.pm »
| 1 | package blx::xsdsql::schema_repository::sql::generic::limits; | ||
| 2 | use strict; # use strict is for PBP | ||
| 3 | use Filter::Include; | ||
| 4 | include blx::xsdsql::include; | ||
| 5 | #line 6 | ||
| 6 | use Storable; | ||
| 7 | |||
| 8 | use blx::xsdsql::ios::debuglogger; | ||
| 9 | use blx::xsdsql::ut::ut qw(nvl); | ||
| 10 | |||
| 11 | use base qw(Exporter); | ||
| 12 | |||
| 13 | our %EXPORT_TAGS = ( 'all' => [ qw( get_xsd_based_types get_normalized_type ) ] ); | ||
| 14 | |||
| 15 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 16 | |||
| 17 | our @EXPORT = qw( ); | ||
| 18 | |||
| 19 | |||
| 20 | my %LIMITS=( #default limits if key is not spec | ||
| 21 | string => { SIZE => -1 } # -1 => unlimited | ||
| 22 | ,fixedstring => { FIXSIZE => -1 } | ||
| 23 | ,boolean => { SIZE => 5 } | ||
| 24 | ,string50 => { SIZE => 50 } | ||
| 25 | ,sizestring => { SIZE => -1 } # size string have the size in bytes not in characters | ||
| 26 | ,sizestring50 => { SIZE => 50 } | ||
| 27 | ,number3 => { INT => 3 ,DEC => 0 } | ||
| 28 | ,number5 => { INT => 5 ,DEC => 0 } | ||
| 29 | ,number10 => { INT => 10 ,DEC => 0 } | ||
| 30 | ,number20 => { INT => 20 ,DEC => 0 } | ||
| 31 | ,unumber3 => { INT => 3 ,DEC => 0 } | ||
| 32 | ,unumber5 => { INT => 5 ,DEC => 0 } | ||
| 33 | ,unumber10 => { INT => 10 ,DEC => 0 } | ||
| 34 | ,unumber20 => { INT => 20 ,DEC => 0 } | ||
| 35 | ,decimal => { INT => 36 ,DEC => 18} | ||
| 36 | ,number => { INT => 36 ,DEC => 18} | ||
| 37 | ,integer => { INT => 36 ,DEC => 0} | ||
| 38 | ); | ||
| 39 | |||
| 40 | my %MAXLIMITS=( # max limits of types | ||
| 41 | ); | ||
| 42 | |||
| 43 | |||
| 44 | my %SQL_TRANSLATE=( | ||
| 45 | string => 'varchar(%d)' | ||
| 46 | ,fixedstring => 'char(%d)' | ||
| 47 | ,string50 => 'varchar(%d)' | ||
| 48 | ,sizestring => 'varchar(%d)' | ||
| 49 | ,sizestring50 => 'varchar(%d)' | ||
| 50 | ,decimal => 'decimal(%i,%d)' | ||
| 51 | ); | ||
| 52 | |||
| 53 | use constant { | ||
| 54 | NORMALIZED_TYPES => { | ||
| 55 | anyURI => 'string' | ||
| 56 | ,base64Binary => 'sizestring' | ||
| 57 | ,boolean => 'boolean' | ||
| 58 | ,byte => 'number3' | ||
| 59 | ,dateTime => 'sizestring50' | ||
| 60 | ,date => 'sizestring50' | ||
| 61 | ,decimal => 'decimal' | ||
| 62 | ,double => 'sizestring' | ||
| 63 | ,duration => 'string' | ||
| 64 | ,ENTITIES => 'string' | ||
| 65 | ,ENTITY => 'string' | ||
| 66 | ,float => 'sizestring' | ||
| 67 | ,gDay => 'sizestring50' | ||
| 68 | ,gMonth => 'sizestring50' | ||
| 69 | ,gMonthDay => 'sizestring50' | ||
| 70 | ,gYearMonth => 'sizestring50' | ||
| 71 | ,gYear => 'sizestring50' | ||
| 72 | ,hexBinary => 'sizestring' | ||
| 73 | ,IDREFS => 'string' | ||
| 74 | ,IDREF => 'string' | ||
| 75 | ,ID => 'string' | ||
| 76 | ,integer => 'integer' | ||
| 77 | ,int => 'number10' | ||
| 78 | ,language => 'string' | ||
| 79 | ,long => 'number20' | ||
| 80 | ,Name => 'string' | ||
| 81 | ,NCName => 'string' | ||
| 82 | ,negativeInteger => 'integer' | ||
| 83 | ,NMTOKENS => 'string' | ||
| 84 | ,NMTOKEN => 'string' | ||
| 85 | ,nonNegativeInteger => 'number10' | ||
| 86 | ,nonPositiveInteger => 'number10' | ||
| 87 | ,normalizedString => 'string' | ||
| 88 | ,NOTATION => 'string' | ||
| 89 | ,positiveInteger => 'number10' | ||
| 90 | ,QName => 'string' | ||
| 91 | ,short => 'number5' | ||
| 92 | ,string => 'string' | ||
| 93 | ,time => 'string50' | ||
| 94 | ,token => 'string' | ||
| 95 | ,unsignedByte => 'unumber3' | ||
| 96 | ,unsignedInt => 'unumber10' | ||
| 97 | ,unsignedLong => 'unumber20' | ||
| 98 | ,unsignedShort => 'unumber5' | ||
| 99 | } | ||
| 100 | }; | ||
| 101 | |||
| 102 | |||
| 103 | use constant { | ||
| 104 | A_BASED_TYPES => [sort keys %{&NORMALIZED_TYPES}] | ||
| 105 | }; | ||
| 106 | |||
| 107 | use constant { | ||
| 108 | H_BASED_TYPES => {map { ($_,undef) } @{&A_BASED_TYPES} } | ||
| 109 | }; | ||
| 110 | |||
| 111 | my %CONSTRUCTOR_PARAMS=map { ($_,undef) } qw(LOG DEBUG); | ||
| 112 | |||
| 113 | sub _check_params { | ||
| 114 | my (%params)=@_; | ||
| 115 | return grep(!exists $CONSTRUCTOR_PARAMS{$_},keys %params); | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub _get_sql_translate { croak "abstract method\n"; } | ||
| 120 | |||
| 121 | sub _get_limits { croak "abstract method\n"; } | ||
| 122 | |||
| 123 | sub _get_maxlimits { croak "abstract method\n"; } | ||
| 124 | |||
| 125 | sub _new { | ||
| 126 | my ($class,%params)=@_; | ||
| 127 | affirm { scalar(_check_params(%params)) == 0 } join(' ',_check_params(%params)).": this params are not permitted"; | ||
| 128 | $params{LOG}=blx::xsdsql::ios::debuglogger->new(DEBUG => $params{DEBUG}) unless defined $params{LOG}; | ||
| 129 | return bless \%params,$class; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub new { croak "abstract method"; } | ||
| 133 | |||
| 134 | sub get_xsd_based_types { | ||
| 135 | my %params=@_; | ||
| 136 | $params{FORMAT}='ARRAY' unless defined $params{FORMAT}; | ||
| 137 | for ($params{FORMAT}) { | ||
| 138 | /^HASH$/ && return wantarray ? %{&H_BASED_TYPES} : H_BASED_TYPES; | ||
| 139 | /^ARRAY$/ && return wantarray ? @{&A_BASED_TYPES} : A_BASED_TYPES; | ||
| 140 | affirm { 0 } $params{FORMAT}.q(: value param FORMAT must be the string 'HASH' or 'ARRAY') | ||
| 141 | } | ||
| 142 | undef; | ||
| 143 | } | ||
| 144 | |||
| 145 | { | ||
| 146 | my %norm_type=(); | ||
| 147 | sub get_normalized_type { | ||
| 148 | my ($xsd_type,%params)=@_; | ||
| 149 | affirm { defined $xsd_type } "1^ param not set"; | ||
| 150 | affirm { defined NORMALIZED_TYPES->{$xsd_type} } "$xsd_type: is not a simple base type"; | ||
| 151 | unless (scalar(%norm_type)) { | ||
| 152 | for my $k(keys %{&NORMALIZED_TYPES}) { | ||
| 153 | local $_; | ||
| 154 | for (NORMALIZED_TYPES->{$k}) { | ||
| 155 | /string/ && do { $norm_type{$k}='string'; last; }; | ||
| 156 | /boolean/ && do { $norm_type{$k}='boolean'; last; }; | ||
| 157 | /int|number/ && do { $norm_type{$k}='integer';last; }; | ||
| 158 | /decimal/ && do { $norm_type{$k}='decimal';last; }; | ||
| 159 | affirm { 0 } "$k: not normalized"; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | affirm { defined $norm_type{$xsd_type} } "$xsd_type: not normalized"; | ||
| 164 | return $norm_type{$xsd_type}; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | sub get_translated_type { | ||
| 169 | my ($self,$xsd_type,%params)=@_; | ||
| 170 | affirm { ref($self) ne __PACKAGE__ } "this method must be called from a subclass"; | ||
| 171 | my $tag=ref($params{COLUMN})=~/::column$/ | ||
| 172 | ? nvl($params{COLUMN}->get_path,$params{COLUMN}->get_name) | ||
| 173 | : undef; | ||
| 174 | $tag=defined $tag ? " column '$tag': " :''; | ||
| 175 | { | ||
| 176 | my ($nt,$lm,$sqlt,$ml,$t)=(); | ||
| 177 | unless (defined $self->{_DATA_CONVERSION}) { | ||
| 178 | $lm=Storable::dclone(\%LIMITS); | ||
| 179 | $t=$self->_get_limits; | ||
| 180 | for my $k(keys %$t) { | ||
| 181 | $lm->{$k}=$t->{$k}; | ||
| 182 | } | ||
| 183 | $sqlt=Storable::dclone(\%SQL_TRANSLATE); | ||
| 184 | $t=$self->_get_sql_translate; | ||
| 185 | for my $k(keys %$t) { | ||
| 186 | $sqlt->{$k}=$t->{$k}; | ||
| 187 | } | ||
| 188 | $ml=Storable::dclone(\%MAXLIMITS); | ||
| 189 | $t=$self->_get_maxlimits; | ||
| 190 | for my $k(keys %$t) { | ||
| 191 | $ml->{$k}=$t->{$k}; | ||
| 192 | } | ||
| 193 | $self->{_DATA_CONVERSION}->{LIMITS}=$lm; | ||
| 194 | $self->{_DATA_CONVERSION}->{SQL_TRANSLATE}=$sqlt; | ||
| 195 | $self->{_DATA_CONVERSION}->{MAXLIMITS}=$ml; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | affirm { defined $xsd_type } "$tag 1^ param not set"; | ||
| 199 | affirm { exists H_BASED_TYPES->{$xsd_type} } "$xsd_type: is not a based type"; | ||
| 200 | |||
| 201 | my $user_limits=nvl($params{LIMITS},{}); | ||
| 202 | affirm { ref($user_limits) eq 'HASH' } $tag.ref($user_limits).': param LIMITS must be an hash'; | ||
| 203 | affirm {!exists $user_limits->{SQL_SIZE}} "SQL_SIZE is obsolete"; | ||
| 204 | my $norm=NORMALIZED_TYPES->{$xsd_type}; | ||
| 205 | affirm { defined $norm } "$tag$xsd_type: unknow xsd type"; | ||
| 206 | $norm='fixedstring' if defined $user_limits->{FIXSIZE} and get_normalized_type($xsd_type) eq 'string'; | ||
| 207 | my $lm=defined $self->{_DATA_CONVERSION}->{LIMITS}->{$norm} ? Storable::dclone($self->{_DATA_CONVERSION}->{LIMITS}->{$norm}) : undef; | ||
| 208 | affirm { defined $lm } "$tag.$norm: this internal type has not entry in limits"; | ||
| 209 | my $maxlimits=$self->{_DATA_CONVERSION}->{MAXLIMITS}->{$norm}; | ||
| 210 | $maxlimits=Storable::dclone($lm) unless defined $maxlimits; | ||
| 211 | affirm { defined $maxlimits } "$tag no max limit for internal type '$norm'"; | ||
| 212 | for my $k(keys %$user_limits) { | ||
| 213 | next unless defined $user_limits->{$k}; | ||
| 214 | affirm { $user_limits->{$k}=~/^\d+$/ } $tag.$user_limits->{$k}.": the value of attribute '$k' of param LIMITS must a absolute number"; | ||
| 215 | affirm { $k eq 'DEC' || $user_limits->{$k} > 0} $tag.$user_limits->{$k}.": the value of attribute '$k' of param LIMITS must be positive number"; | ||
| 216 | my $ml=$maxlimits->{$k}; | ||
| 217 | affirm { defined $ml } "$tag: no MAXLIMIT for key '$k' in type '$norm'"; | ||
| 218 | affirm { $ml == -1 || $user_limits->{$k} <= $ml } $tag.$user_limits->{$k}.": the value of attribute '$k' of param LIMITS exceded the max value of ".$ml; | ||
| 219 | affirm { $lm->{$k} == -1 || $user_limits->{$k} <= $lm->{$k} } $tag.$user_limits->{$k}.": the value of attribute '$k' of param LIMITS exceded the max value of ".$lm->{$k}; | ||
| 220 | $lm->{$k}=$user_limits->{$k}; | ||
| 221 | } | ||
| 222 | my $sqltype=$self->{_DATA_CONVERSION}->{SQL_TRANSLATE}->{$norm}; | ||
| 223 | affirm { defined $sqltype } "$tag$norm: this internal type is not know for conversion into sqltype"; | ||
| 224 | local $_; | ||
| 225 | for ($norm) { | ||
| 226 | /fixedstring/ && do { | ||
| 227 | affirm { defined $maxlimits->{FIXSIZE} } "$tag$norm: attribute FIXSIZE of MAXLIMITS is not set"; | ||
| 228 | if ($maxlimits->{FIXSIZE} != -1) { | ||
| 229 | my $sz=defined $lm->{FIXSIZE} ? $lm->{FIXSIZE} : $maxlimits->{FIXSIZE}; | ||
| 230 | affirm { defined $sz } "$tag$norm: attribute FIXSIZE of MAXLIMITS is not set"; | ||
| 231 | $sqltype=$self->{_DATA_CONVERSION}->{SQL_TRANSLATE}->{fixedstring}; | ||
| 232 | affirm { defined $sqltype } "sql_translate: fixedstring not found"; | ||
| 233 | $sqltype=~s/\%d/$sz/g; | ||
| 234 | } | ||
| 235 | last; | ||
| 236 | }; | ||
| 237 | /string|boolean/ && do { | ||
| 238 | affirm { defined $maxlimits->{SIZE} } "$tag$norm: attribute SIZE of MAXLIMITS is not set"; | ||
| 239 | if ($maxlimits->{SIZE} != -1) { | ||
| 240 | my $sz=$lm->{SIZE} == -1 ? $maxlimits->{SIZE} : $lm->{SIZE}; | ||
| 241 | affirm { defined $sz } "$tag$norm: attribute SIZE/FIXSIZE of MAXLIMITS is not set"; | ||
| 242 | $sqltype=$self->{_DATA_CONVERSION}->{SQL_TRANSLATE}->{fixedstring} | ||
| 243 | if defined $user_limits->{FIXSIZE} | ||
| 244 | ; | ||
| 245 | affirm { defined $sqltype } "sql_translate: fixedstring not found"; | ||
| 246 | $sqltype=~s/\%d/$sz/g; | ||
| 247 | } | ||
| 248 | last; | ||
| 249 | }; | ||
| 250 | /decimal/ && do { | ||
| 251 | for my $k(qw(INT DEC)) { | ||
| 252 | affirm { defined $maxlimits->{$k} } "$tag$norm: attribute $k of MAXLIMITS is not set"; | ||
| 253 | if ($maxlimits->{$k} != -1) { | ||
| 254 | affirm { defined $lm->{$k} } "$tag$norm: attribute $k of MAXLIMITS is not set"; | ||
| 255 | my $n=$lm->{$k} == -1 ? $maxlimits->{$k} : $lm->{$k}; | ||
| 256 | affirm { defined $n } "$tag$norm: attribute $k of MAXLIMITS is not set"; | ||
| 257 | local $_; | ||
| 258 | SW: | ||
| 259 | for($k) { | ||
| 260 | /^INT$/ && do { $sqltype=~s/\%i/$n/g;last SW; }; | ||
| 261 | /^DEC$/ && do { $sqltype=~s/\%d/$n/g;last SW; }; | ||
| 262 | affirm { 0 } "$tag$norm: who is '$k' ?"; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | last; | ||
| 267 | }; | ||
| 268 | /integer|number/ && do { | ||
| 269 | for my $k(qw(INT)) { | ||
| 270 | affirm { defined $maxlimits->{$k} } "$tag$norm: attribute $k of MAXLIMITS is not set"; | ||
| 271 | if ($maxlimits->{$k} != -1) { | ||
| 272 | affirm { defined $lm->{$k} } "$tag$norm: attribute $k of MAXLIMITS is not set"; | ||
| 273 | my $n=$lm->{$k} == -1 ? $maxlimits->{$k} : $lm->{$k}; | ||
| 274 | affirm { defined $n } "$tag$norm: attribute $k of MAXLIMITS is not set"; | ||
| 275 | local $_; | ||
| 276 | SW: | ||
| 277 | for($k) { | ||
| 278 | /^INT$/ && do { $sqltype=~s/\%i/$n/g;last SW; }; | ||
| 279 | /^DEC$/ && do { $sqltype=~s/\%d/$n/g;last SW; }; | ||
| 280 | affirm { 0 } "$tag$norm: who is '$k' ?"; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | } | ||
| 284 | last; | ||
| 285 | }; | ||
| 286 | affirm { 0 } "$tag$norm: the conversion is not implemented"; | ||
| 287 | } | ||
| 288 | return $sqltype; | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | 1; | ||
| 296 | |||
| 297 | |||
| 298 | __END__ |