Re: Trouble making set_sql work...

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

From: colm-cdbi
Subject: Re: Trouble making set_sql work...
Date: 18:38 on 13 Jul 2004
On Tue, Jul 13, 2004 at 01:32:59PM -0400, Andy Harrison wrote:
> On Tue, 13 Jul 2004 11:50:04 -0500, Stuart Johnston
> <sjohnston@xxxxxxxxxx.xxx> wrote:
> >
> > You other option would be to use %s, sprintf style substitution:
> > 
> > http://groups.kasei.com/mail/arc/cdbi-talk/2004-06/msg00068.html
> > 
> 
> I just went and did it like this and it works fine.
> 
>         my $sql = qq {
>             SELECT option_data, COUNT(*) 
>             FROM entry_options 
>             WHERE option = ? 
>             GROUP BY option_data 
>             ORDER BY count DESC
>             LIMIT $main::opt_top_limit
>         };

I wouldn't recommend doing it that way.

In the spirit of Class::DBI IMO it would be best to wrap a method around
the auto-generated sql_show_top.

Your SQL implies you want raw data and not objects.  If so I haven't
tried it but this should be mostly right :

 __PACKAGE__->set_sql( show_top => q{
             SELECT option_data, COUNT(*)
             FROM entry_options
             WHERE option = ?
             GROUP BY option_data
             ORDER BY count DESC
             LIMIT %s
         });

  sub get_top_matches {
    my ($self, $option, $limit) = @_;
    my $sth = $self->sql_show_top($limit);
    $sth->execute($option);
    return $sth->fetchall_arrayref();
  }

  # in a nearby script :

  my $top_matches = Your::Class->get_top_matches($option, $limit);
  foreach my $result ( @$top_matches ) {
    my ($option_data, $count) = @$result;
    printf("%5d => %s\n", $count, $option_data);
  }

#--

Colm

(message missing)

Trouble making set_sql work...
Andy Harrison 14:28 on 13 Jul 2004

Re: Trouble making set_sql work...
colm-cdbi 14:54 on 13 Jul 2004

Re: Trouble making set_sql work...
colm-cdbi 15:10 on 13 Jul 2004

Re: Trouble making set_sql work...
Andy Harrison 14:58 on 13 Jul 2004

Re: Trouble making set_sql work...
Jesse Sheidlower 15:04 on 13 Jul 2004

Re: Trouble making set_sql work...
Andy Harrison 16:36 on 13 Jul 2004

Re: Trouble making set_sql work...
John Beppu 16:46 on 13 Jul 2004

Re: Trouble making set_sql work...
Stuart Johnston 16:50 on 13 Jul 2004

Re: Trouble making set_sql work...
Andy Harrison 17:32 on 13 Jul 2004

Re: Trouble making set_sql work...
colm-cdbi 18:38 on 13 Jul 2004

Re: Trouble making set_sql work...
Andy Harrison 19:33 on 13 Jul 2004

Re: Trouble making set_sql work...
Drew Taylor 19:44 on 13 Jul 2004

Re: Trouble making set_sql work...
colm-cdbi 20:08 on 13 Jul 2004

Re: Trouble making set_sql work...
Andy Harrison 20:52 on 13 Jul 2004

Re: Trouble making set_sql work...
colm-cdbi 18:57 on 13 Jul 2004

Re: Trouble making set_sql work...
Jay Strauss 15:10 on 13 Jul 2004

Re: Trouble making set_sql work...
Perrin Harkins 17:51 on 13 Jul 2004

Re: Trouble making set_sql work...
Andy Harrison 18:23 on 13 Jul 2004

Re: Trouble making set_sql work...
Stuart Johnston 19:44 on 13 Jul 2004

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