Normalization of values from the database

[prev] [thread] [next] [Date index for 2004/07/21]

From: Johan Lindstrom
Subject: Normalization of values from the database
Date: 10:39 on 21 Jul 2004
When I insert and then select values from a column of type "numeric(19,4)", 
I get back numers like 32.5000 instead of 32.5 . I'd like to 
normalize/massage those values before they get to my application.

Class::DBI provides code for a "normalize" phase, but that only applies 
when setting values from code, not when obtaining values from the database 
(or does it?). It would be handy to have something like that on the way up 
as well, but in the mean time I added a "select" trigger for this purpose.

<ecode>
sub numify { my $self = shift;
	my ($name) = @_;
	defined($self->{$name}) and $self->{$name} += 0;
}
__PACKAGE__->add_trigger(select  => sub { my $self = shift;
	$self->numify("account_amount");
});
</ecode>

Note the direct hash key access (bad). But if I used the getter for $name, 
that column would be auto-fleshed out if the value isn't already selected, 
correct?

Is there a better way of doing this?
Is there a better way to figure out if the value is already loaded or not?

Things I just thought about(tm):
* Maybe I should use the setter to assign the value at least.
* exists is probably better than defined, on account of NULL values.


/J
-------- ------ ---- --- -- --  --  -     -     -        -          -
Johan Lindström    Sourcerer @ Boss Casinos   johanl AT DarSerMan.com

Latest bookmark: "TCP Connection Passing"
http://tcpcp.sourceforge.net/
dmoz: /Computers/Programming/Languages/JavaScript/ 12

Normalization of values from the database
Johan Lindstrom 10:39 on 21 Jul 2004

RE: Normalization of values from the database
Addison, Mark 11:56 on 28 Jul 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52