On Sat, Mar 8, 2008 at 12:53 PM, Matt S Trout <[EMAIL PROTECTED]>
wrote:
> On Tue, Feb 26, 2008 at 12:58:20PM +0000, Matt Lawrence wrote:
> > Emmanuel Quevillon wrote:
> > >Hi,
> > >
> > >Is there a simple way to get the automatic generated SQL from a
> > >ResultSet? As for example $query = $rs->sql()?
> > >Thanks
> > >
> > I wrote a patch to achieve this a little while ago, but I didn't get
> > around to writing good enough tests. Since someone else seems to want
> > the feature I may as well share it.
> >
> > It's a patch against CPAN version 0.08008, which I haven't checked
> > against later versions. It doesn't cause any test failures on my system,
> > although I didn't run all the optional tests.
> >
> > It provides a method called as_sql() on the cursor, which is proxied
> > from the resultset.
>
> Interesting.
>
> Have you considered writing some code to interpolate the bind parameters?
We have some code for this written by Jeff Lanza, I'm not sure its flawless
but its a start :)
Here you go, patches welcome, and if this is something we can put somewhere
useful let me know and I'll bundle it up:
#!/usr/bin/perl
use warnings;
use strict;
# TRANSLATE DBIx query into SQL
print "
Paste your query below,
then type 'TERM' on a line by itself to process arguments:
";
my $query = "";
while (<STDIN>) {
my $line = $_;
if ($line eq "TERM\n") {
last;
} else {
$query .= $_;
}
}
$query =~ s/\n/ /g;
$query =~ s/\: (( ?'[^']+'[^']*)+)$//m;
my $end_args = $1;
$end_args =~ s/^\s+//;
$end_args =~ s/\s+$//;
my @end_args = split(",", $end_args);
print "\n\nResult...\n\n";
for my $current_arg (@end_args) {
$current_arg =~ s/^\s+//;
$current_arg =~ s/\s+$//;
$query =~ s/\?/$current_arg/;
}
print "$query\n\n";
_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/[EMAIL PROTECTED]