RE: Normalization of values from the database

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

From: Addison, Mark
Subject: RE: Normalization of values from the database
Date: 11:56 on 28 Jul 2004
From: Johan Lindstrom Sent: 21 July 2004 11:40
>
>When I insert and then select values from a column of type
"numeric(19,4)",=20
>I get back numers like 32.5000 instead of 32.5 . I'd like to=20
>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=20
>(or does it?). It would be handy to have something like that on the way
up=20
>as well, but in the mean time I added a "select" trigger for this
purpose.
>
> <ecode>
> sub numify { my $self =3D shift;
> 	my ($name) =3D @_;
>	defined($self->{$name}) and $self->{$name} +=3D 0;
> }
> __PACKAGE__->add_trigger(select  =3D> sub { my $self =3D shift;
> 	$self->numify("account_amount");
> });
> </ecode>
>
>Note the direct hash key access (bad). But if I used the getter for
$name,=20
>that column would be auto-fleshed out if the value isn't already
selected,=20
>correct?

If you want to access the data without triggering all the db stuff
there are a set of methods, see LOW-LEVEL DATA ACCESS in the docs.
e.g. for your code...

 <ecode>
 sub numify { my $self =3D shift;
 	my ($name) =3D @_;
=09
 	defined($self->_attrs($name))=20
	and $self->_attribute_set($self->_attrs($name)+0);
 }
 </ecode>

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

 $self->_attribute_exists($name)

>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


mark
--
This email (and any attachments) is intended solely for the individual(s) t=
o whom addressed. It may contain confidential and/or legally privileged inf=
ormation. Any statement or opinions therein are not necessarily those of IT=
N unless specifically stated. Any unauthorised use, disclosure or copying i=
s prohibited. If you have received this email in error, please notify the s=
ender and delete it from your system. Security and reliability of the e-mai=
l and attachments are not guaranteed. You must take full responsibility for=
 virus checking.



Independent Television News Limited,=20

Registered No. 548648 England,

VAT Reg. No: GB 756 2995 81,=20

200 Gray's Inn Road, London WC1X 8XZ,

Telephone: 020 7833 3000.

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