Re: Removing auto-generated methods (set_sql)

[prev] [thread] [next] [Date index for 2004/06/15]

From: Tony Bowden
Subject: Re: Removing auto-generated methods (set_sql)
Date: 19:59 on 15 Jun 2004
On Tue, Jun 15, 2004 at 01:53:30PM -0400, William McKee wrote:
> OK, this makes sense. I didn't see anything in the CDBI docs about
> running a single SQL statement except the retrieve_from_sql which cannot
> support joins. Perrin's reply to drop down to retrieving the dbh and
> calling DBI directly seems the best approach. Perhaps that should be
> documented and/or added to the wiki?

Feel free to add it to the wiki. If people come up with a good wording
I'll certainly look at adding it to the main docs.

> So could I do something like the following:
>     $class->set_sql(my_query => <<"");
>     SELECT *
>     FROM   __TABLE(Class::One=c1)__,
>            __TABLE(Class::Two=c2)__
>     WHERE  __JOIN(c1.id c2.user_id)__
>     AND    %s

Not quite. You literally just do the __JOIN(c1 c2)__. It'll know what
columns to join on based on the relationships you've already set up
between them. And you probably don't want a SELECT * - you want it to be
the ESSENTIAL columns of $class: c1.id is often sufficient.

>     my @recs = $class->my_query({c1.field1 => $val1, c2.field3 => $val3});
> Is there a way to control how the fields and values that are bound to %s
> get compared? I'm guessing that the default is '='. I need to be able to
> do LIKE and ILIKE comparisons at the very least.

It's up to you to define this in the method that calls sql_my_query.

So, if you want a LIKE query, you'll need to do something like
(untested):

	sub my_join_search { 
		my ($class, $args) = @_;
		my $where_clause = join " AND ", map "$_ LIKE ?", keys %$args;
		return $class->sth_to_objects(
			$class->sql_my_query($where_clause), values %args
		);
	}

To be really robust you'll need to cope with overriden accessor names
etc, and you can abstract away that LIKE to be more generic so you don't
have to repeat yourself for ILIKE or EQUALS or whatever. But that should
give the gist. It's how search() works in Class::DBI itself, and you can
always look at it for some more details on how to abstract this further.

And, as I said earlier, I should really abstract more of this out
further so that writing these yourself is even easier...

> BTW, where is the __JOIN__ substitution defined? I do not see any
> references to it in the docs.

It's defined in transform_sql(). 

It's not actually documented, yet :)

Tony

(message missing)

Removing auto-generated methods (set_sql)
William McKee 16:27 on 14 Jun 2004

Re: Removing auto-generated methods (set_sql)
Tony Bowden 16:33 on 14 Jun 2004

Re: Removing auto-generated methods (set_sql)
William McKee 16:57 on 14 Jun 2004

Re: Removing auto-generated methods (set_sql)
Perrin Harkins 17:30 on 14 Jun 2004

Re: Removing auto-generated methods (set_sql)
William McKee 20:51 on 14 Jun 2004

Re: Removing auto-generated methods (set_sql)
Perrin Harkins 20:56 on 14 Jun 2004

Re: Removing auto-generated methods (set_sql)
William McKee 21:48 on 14 Jun 2004

Re: Removing auto-generated methods (set_sql)
Tony Bowden 21:59 on 14 Jun 2004

Re: Removing auto-generated methods (set_sql)
Tony Bowden 08:53 on 15 Jun 2004

Re: Removing auto-generated methods (set_sql)
William McKee 17:53 on 15 Jun 2004

Re: Removing auto-generated methods (set_sql)
Tony Bowden 19:59 on 15 Jun 2004

Re: Removing auto-generated methods (set_sql)
Perrin Harkins 00:04 on 16 Jun 2004

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