[CDBI] custom accessor problem?

adam adamlounds at gmail.com
Fri Mar 9 16:18:20 GMT 2007


Hi.

We're looking at using Class::DBI on a subset of our sybase tables,
with the view to gradually migrating to it over time.

I've hit a wall and was hoping that someone may be able to help. I
need to have a custom select to retrieve a precision datetime field:

eg
SELECT
 convert (char(26), topic_updated_dt, 109), topic_view_count, topic_reply_count
FROM topics
WHERE topic_id = ?

as the default sybase return from a select only gives the minutes (no
seconds, no milliseconds).

I looked through the docs and found the suggestion of using a
different accessor, so used the code:

my $topic_precise_updated_dt = Class::DBI::Column->new(
   'convert (char(26), topic_updated_dt, 109)' => {
     accessor => 'precise',
   }
);
__PACKAGE__->columns(
    Others => (
      $topic_precise_updated_dt,
      'topic_view_count',
      'topic_reply_count',
    ),
);

Now, when I access $topic->topic_view_count, the sql is executed
correctly, but I can't access the result, as the accessor method is
missing:
(test code)
my $views = $top2->topic_view_count;
warn(Dumper($top2));
  my $precise = $top2->precise;

With $ENV{DBI_TRACE}=1 I get the following:

...
prepare_cached('SELECT convert (char(26), topic_updated_dt, 109),
topic_view_count, topic_reply_count
FROM   topics
WHERE  topic_id=?
')= DBIx::ContextualFetch::st=HASH(0x8fa604c) at DBI.pm line 381
 T  <- FETCH('Taint')= 1 at ContextualFetch.pm line 49
    <- STORE('Taint' 0)= 1 at ContextualFetch.pm line 50
    <- execute('1')= -1 at ContextualFetch.pm line 51
 T  <- STORE('Taint' 1)= 1 at ContextualFetch.pm line 52
 T  <- fetchrow_array= ( 'Mar  9 2007  4:04:52:446PM' 3  1) [3 items]
row1 at ContextualFetch.pm line 87
 T  <- finish= 1 at ContextualFetch.pm line 88
$VAR1 = bless( {
                   'convert (char(26), topic_updated_dt, 109)' => 'Mar
 9 2007  4:04:52:446PM',
                   'topic_id' => '1',
                   'topic_title' => 'my new topic',
  'topic_updated_dt' => 'Mar  9 2007  4:04PM',
                   'topic_view_count' => 3,
                   'topic_reply_count' => 1,
               }, 'xx::Topics' );

died: Can't locate object method "precise" via package "xx::Topics" at
t/xxTopics.t line 108


I tracked this down to line 344 of Class/DBI.pm, in the
_mk_column_accessors code, and I *think* that the line

 %method = ('_' => $acc);         # make the accessor the mutator too

should be

 %method = ('_' => $default_accessor);

instead, as when the code is called the values are

$default_accessor = 'precise'
$acc = '(convert (char(26), topic_updated_dt, 109))'
$mut = 'precise'

If I change the line in Class/DBI.pm then my code works as expected.
However, I am loath to fiddle with Class::DBI itself if it's something
that I'm doing wrong.

Any ideas?

Thanks,
-- 
Adam



More information about the ClassDBI mailing list