[Dbix-class] Get generated SQL

John Goulah jgoulah at gmail.com
Mon Mar 10 15:09:55 GMT 2008

On Sat, Mar 8, 2008 at 12:53 PM, Matt S Trout <dbix-class at trout.me.uk>

> 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 =3D $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:


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 =3D "";
while (<STDIN>) {
    my $line =3D $_;
    if ($line eq "TERM\n") {
    } else {
        $query .=3D $_;

$query =3D~ s/\n/ /g;
$query =3D~ s/\: (( ?'[^']+'[^']*)+)$//m;
my $end_args =3D $1;
$end_args =3D~ s/^\s+//;
$end_args =3D~ s/\s+$//;
my @end_args =3D split(",", $end_args);

print "\n\nResult...\n\n";

for my $current_arg (@end_args) {
    $current_arg =3D~ s/^\s+//;
    $current_arg =3D~ s/\s+$//;
    $query =3D~ s/\?/$current_arg/;
print "$query\n\n";
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20080310/a0b=

More information about the DBIx-Class mailing list