[CDBI] Use Of Placeholders In set_sql() Example of Beginners Guide

Matt S Trout dbix-class at trout.me.uk
Sun Mar 19 19:48:31 GMT 2006


hkclark at gmail.com wrote:
> On 3/19/06, Sam Tregar <sam at tregar.com <mailto:sam at tregar.com>> wrote:
>  > On Thu, 16 Mar 2006 hkclark at gmail.com <mailto:hkclark at gmail.com> wrote:
>  >
>  > > However, I can only get any form of substitution to work if use "%s"
>  > > in the set_sql() and then do something like this to call it:
>  > >    my $sth = Music::Artist->sql_persons("F");
>  > >    $sth->execute;
>  > >    my @persons = Music::Artist->sth_to_objects($sth);
>  >
>  > If you use placeholders then you must pass the param to execute()
>  > instead:
>  >
>  >      my $sth = Music::Artist->sql_persons();
>  >      $sth->execute("F");
>  >      my @persons = Music::Artist->sth_to_objects($sth);
>  >
>  > -sam
>  >
> 
> Thanks for the reply, Sam.  Yeah, it seems to work when I call 
> execute(), but the example in the Beginners Guide seems to suggest it 
> should work without that ( 
> http://wiki.class-dbi.com/wiki/Beginners_guide#set_sql):
> 
> Like in most of CDBI you can also use placeholders or parameters in your 
> "set_sql" commands to accect runtime arguments. Like:
> 
> __PACKAGE__->set_sql(persons => qq {
> select artist.artistid
> from artist, person
> where artist.artistid = person.artistid
> and person.gender = ?});
> 
> Then in your code you'd call it like:
> 
> my @persons = Music::Artist->search_persons("F");

Which would work. Whereas in your code above you call

my $sth = Music::Artist->sql_persons("F");

sql_persons != search_persons :)

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +




More information about the ClassDBI mailing list