| 1 | package Rose::DB::Object::MakeMethods::BigNum; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Carp(); | ||
| 6 | |||
| 7 | our $VERSION = '0.788'; # move up in the file to make CPAN happy | ||
| 8 | |||
| 9 | require Math::BigInt; | ||
| 10 | |||
| 11 | BIGNUM_VERSION_CHECK: | ||
| 12 | { | ||
| 13 | (my $bignum_version = $Math::BigInt::VERSION) =~ s/_//g; | ||
| 14 | |||
| 15 | if($bignum_version >= 1.78) | ||
| 16 | { | ||
| 17 | Math::BigInt->import(try => 'GMP'); | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | use Rose::Object::MakeMethods; | ||
| 22 | our @ISA = qw(Rose::Object::MakeMethods); | ||
| 23 | |||
| 24 | use Rose::DB::Object::Constants | ||
| 25 | qw(STATE_LOADING MODIFIED_COLUMNS MODIFIED_NP_COLUMNS SET_COLUMNS STATE_IN_DB); | ||
| 26 | |||
| 27 | our $Debug = 0; | ||
| 28 | |||
| 29 | sub bigint | ||
| 30 | { | ||
| 31 | my($class, $name, $args) = @_; | ||
| 32 | |||
| 33 | my $key = $args->{'hash_key'} || $name; | ||
| 34 | my $interface = $args->{'interface'} || 'get_set'; | ||
| 35 | my $default = $args->{'default'}; | ||
| 36 | my $check_in = $args->{'check_in'}; | ||
| 37 | my $min = $args->{'min'}; | ||
| 38 | my $max = $args->{'min'}; | ||
| 39 | |||
| 40 | my $column_name = $args->{'column'} ? $args->{'column'}->name : $name; | ||
| 41 | |||
| 42 | my $undef_overrides_default = $args->{'undef_overrides_default'} || 0; | ||
| 43 | |||
| 44 | my $init_method; | ||
| 45 | |||
| 46 | if(exists $args->{'with_init'} || exists $args->{'init_method'}) | ||
| 47 | { | ||
| 48 | $init_method = $args->{'init_method'} || "init_$name"; | ||
| 49 | } | ||
| 50 | |||
| 51 | ## | ||
| 52 | ## Build code snippets | ||
| 53 | ## | ||
| 54 | |||
| 55 | my $qkey = $key; | ||
| 56 | $qkey =~ s/'/\\'/g; | ||
| 57 | my $qname = $name; | ||
| 58 | $qname =~ s/"/\\"/g; | ||
| 59 | |||
| 60 | my $col_name_escaped = $column_name; | ||
| 61 | $col_name_escaped =~ s/'/\\'/g; | ||
| 62 | |||
| 63 | my $mod_columns_key = ($args->{'column'} ? $args->{'column'}->nonpersistent : 0) ? | ||
| 64 | MODIFIED_NP_COLUMNS : MODIFIED_COLUMNS; | ||
| 65 | |||
| 66 | my $dont_use_default_code = !$undef_overrides_default ? qq(defined \$self->{'$qkey'}) : | ||
| 67 | qq(defined \$self->{'$qkey'} || ) . | ||
| 68 | qq((\$self->{STATE_IN_DB()} && !(\$self->{SET_COLUMNS()}{'$col_name_escaped'} || \$self->{'$mod_columns_key'}{'$col_name_escaped'})) || ) . | ||
| 69 | qq(\$self->{SET_COLUMNS()}{'$col_name_escaped'} || ) . | ||
| 70 | qq(\$self->{'$mod_columns_key'}{'$col_name_escaped'}); | ||
| 71 | |||
| 72 | # | ||
| 73 | # check_in code | ||
| 74 | # | ||
| 75 | |||
| 76 | my $check_in_code = ''; | ||
| 77 | my %check; | ||
| 78 | |||
| 79 | if($check_in) | ||
| 80 | { | ||
| 81 | $check_in = [ $check_in ] unless(ref $check_in); | ||
| 82 | |||
| 83 | $check_in_code=<<"EOF"; | ||
| 84 | if(defined \$value) | ||
| 85 | { | ||
| 86 | my \$found = 0; | ||
| 87 | |||
| 88 | foreach my \$check (\@\$check_in) | ||
| 89 | { | ||
| 90 | if(\$value == \$check) | ||
| 91 | { | ||
| 92 | \$found = 1; | ||
| 93 | last; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | Carp::croak "Invalid $name: '\$value'" unless(\$found); | ||
| 98 | } | ||
| 99 | EOF | ||
| 100 | } | ||
| 101 | |||
| 102 | # | ||
| 103 | # min/max code | ||
| 104 | # | ||
| 105 | |||
| 106 | my $min_max_code = ''; | ||
| 107 | |||
| 108 | if($min) | ||
| 109 | { | ||
| 110 | unless($min =~ /^-?\d+$/) | ||
| 111 | { | ||
| 112 | Carp::croak "Invalid minimum value for bigint column $qname: '$min'"; | ||
| 113 | } | ||
| 114 | |||
| 115 | $min_max_code =<<"EOF"; | ||
| 116 | no warnings 'uninitialized'; | ||
| 117 | if(\$value < $min) | ||
| 118 | { | ||
| 119 | Carp::croak ref(\$self), ": Value \$value for $qname() is too small. ", | ||
| 120 | "It must be greater than or equal to $min."; | ||
| 121 | } | ||
| 122 | EOF | ||
| 123 | } | ||
| 124 | |||
| 125 | if($max) | ||
| 126 | { | ||
| 127 | unless($max =~ /^-?\d+$/) | ||
| 128 | { | ||
| 129 | Carp::croak "Invalid maximum value for bigint column $qname: '$max'"; | ||
| 130 | } | ||
| 131 | |||
| 132 | $min_max_code =<<"EOF"; | ||
| 133 | no warnings 'uninitialized'; | ||
| 134 | if(\$value < $min) | ||
| 135 | { | ||
| 136 | Carp::croak ref(\$self), ": Value \$value for $qname() is too large. ", | ||
| 137 | "It must be less than or equal to $max."; | ||
| 138 | } | ||
| 139 | EOF | ||
| 140 | } | ||
| 141 | |||
| 142 | # | ||
| 143 | # set code | ||
| 144 | # | ||
| 145 | |||
| 146 | my $set_code = qq(\$self->{'$qkey'} = defined \$value ? Math::BigInt->new(\$value) : undef;); | ||
| 147 | |||
| 148 | # | ||
| 149 | # column modified code | ||
| 150 | # | ||
| 151 | |||
| 152 | my $column_modified_code = | ||
| 153 | qq(\$self->{'$mod_columns_key'}{'$col_name_escaped'} = 1); | ||
| 154 | |||
| 155 | # | ||
| 156 | # return code | ||
| 157 | # | ||
| 158 | |||
| 159 | my($return_code, $return_code_shift); | ||
| 160 | |||
| 161 | if(defined $default) | ||
| 162 | { | ||
| 163 | $default = defined $default ? Math::BigInt->new($default) : undef; | ||
| 164 | |||
| 165 | $return_code=<<"EOF"; | ||
| 166 | return ($dont_use_default_code) ? \$self->{'$qkey'} : | ||
| 167 | (scalar($column_modified_code, | ||
| 168 | \$self->{'$qkey'} = \$default)); | ||
| 169 | EOF | ||
| 170 | |||
| 171 | } | ||
| 172 | elsif(defined $init_method) | ||
| 173 | { | ||
| 174 | $return_code=<<"EOF"; | ||
| 175 | return \$self->{'$qkey'} if(defined \$self->{'$qkey'}); | ||
| 176 | $column_modified_code; | ||
| 177 | my \$init_value = \$self->$init_method(); | ||
| 178 | return \$self->{'$qkey'} = defined \$init_value ? Math::BigInt->new(\$init_value) : undef; | ||
| 179 | EOF | ||
| 180 | } | ||
| 181 | else | ||
| 182 | { | ||
| 183 | $return_code = qq(return \$self->{'$qkey'};); | ||
| 184 | $return_code_shift = qq(return shift->{'$qkey'};); | ||
| 185 | } | ||
| 186 | |||
| 187 | $return_code_shift ||= $return_code; | ||
| 188 | |||
| 189 | my %methods; | ||
| 190 | |||
| 191 | if($interface eq 'get_set') | ||
| 192 | { | ||
| 193 | my $code; | ||
| 194 | |||
| 195 | # I can't help myself... | ||
| 196 | if(defined $default || defined $init_method) | ||
| 197 | { | ||
| 198 | $code=<<"EOF"; | ||
| 199 | sub | ||
| 200 | { | ||
| 201 | my \$self = shift; | ||
| 202 | |||
| 203 | if(\@_) | ||
| 204 | { | ||
| 205 | my \$value = shift; | ||
| 206 | |||
| 207 | $check_in_code | ||
| 208 | $min_max_code | ||
| 209 | $set_code | ||
| 210 | $column_modified_code unless(\$self->{STATE_LOADING()}); | ||
| 211 | $return_code | ||
| 212 | } | ||
| 213 | |||
| 214 | $return_code | ||
| 215 | }; | ||
| 216 | EOF | ||
| 217 | } | ||
| 218 | else | ||
| 219 | { | ||
| 220 | $code=<<"EOF"; | ||
| 221 | sub | ||
| 222 | { | ||
| 223 | if(\@_ > 1) | ||
| 224 | { | ||
| 225 | my \$self = shift; | ||
| 226 | my \$value = shift; | ||
| 227 | |||
| 228 | $check_in_code | ||
| 229 | $min_max_code | ||
| 230 | $column_modified_code unless(\$self->{STATE_LOADING()}); | ||
| 231 | return $set_code | ||
| 232 | } | ||
| 233 | |||
| 234 | $return_code_shift | ||
| 235 | }; | ||
| 236 | EOF | ||
| 237 | } | ||
| 238 | |||
| 239 | my $error; | ||
| 240 | |||
| 241 | TRY: | ||
| 242 | { | ||
| 243 | local $@; | ||
| 244 | $Debug && warn "sub $name = ", $code; | ||
| 245 | $methods{$name} = eval $code; | ||
| 246 | $error = $@; | ||
| 247 | } | ||
| 248 | |||
| 249 | if($error) | ||
| 250 | { | ||
| 251 | Carp::croak "Error in generated code for method $name - $error\n", | ||
| 252 | "Code was: $code"; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | elsif($interface eq 'get') | ||
| 256 | { | ||
| 257 | my $code; | ||
| 258 | |||
| 259 | # I can't help myself... | ||
| 260 | if(defined $default || defined $init_method) | ||
| 261 | { | ||
| 262 | $code = qq(sub { my \$self = shift; $return_code };); | ||
| 263 | } | ||
| 264 | else | ||
| 265 | { | ||
| 266 | $code = qq(sub { shift->{'$qkey'} }); | ||
| 267 | } | ||
| 268 | |||
| 269 | my $error; | ||
| 270 | |||
| 271 | TRY: | ||
| 272 | { | ||
| 273 | local $@; | ||
| 274 | $Debug && warn "sub $name = ", $code; | ||
| 275 | $methods{$name} = eval $code; | ||
| 276 | $error = $@; | ||
| 277 | } | ||
| 278 | |||
| 279 | if($error) | ||
| 280 | { | ||
| 281 | Carp::croak "Error in generated code for method $name - $error\n", | ||
| 282 | "Code was: $code"; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | elsif($interface eq 'set') | ||
| 286 | { | ||
| 287 | my $arg_check_code = | ||
| 288 | qq(Carp::croak ref(\$_[0]), ": Missing argument in call to $qname" unless(\@_ > 1);); | ||
| 289 | |||
| 290 | my $code=<<"EOF"; | ||
| 291 | sub | ||
| 292 | { | ||
| 293 | $arg_check_code | ||
| 294 | my \$self = shift; | ||
| 295 | my \$value = shift; | ||
| 296 | |||
| 297 | $check_in_code | ||
| 298 | $min_max_code | ||
| 299 | $set_code | ||
| 300 | $column_modified_code unless(\$self->{STATE_LOADING()}); | ||
| 301 | $return_code | ||
| 302 | }; | ||
| 303 | EOF | ||
| 304 | |||
| 305 | my $error; | ||
| 306 | |||
| 307 | TRY: | ||
| 308 | { | ||
| 309 | local $@; | ||
| 310 | $Debug && warn "sub $name = ", $code; | ||
| 311 | $methods{$name} = eval $code; | ||
| 312 | $error = $@; | ||
| 313 | } | ||
| 314 | |||
| 315 | if($error) | ||
| 316 | { | ||
| 317 | Carp::croak "Error in generated code for method $name - $error\n", | ||
| 318 | "Code was: $code"; | ||
| 319 | } | ||
| 320 | } | ||
| 321 | else { Carp::croak "Unknown interface: $interface" } | ||
| 322 | |||
| 323 | return \%methods; | ||
| 324 | } | ||
| 325 | |||
| 326 | 1; | ||
| 327 | |||
| 328 | __END__ |