"Dry-run" for SQL ?

5 messages Options
Embed this post
Permalink
Robert Heinzmann

"Dry-run" for SQL ?

Reply Threaded More More options
Print post
Permalink
Hello,

Is it possible to have a "dry run" mode in DBIx-Class ?

I just want to see the SQL Statements DBIx would execute, but not want
DBIx to execute them.

This way I can verify the statements before executing.

Is this possible ?

---
Robert Heinzmann

_______________________________________________
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/dbix-class@...
Rob Kinyon

Re: "Dry-run" for SQL ?

Reply Threaded More More options
Print post
Permalink
DBD::Mock was built specifically for this.

Rob

On Tue, Jun 30, 2009 at 12:35, Robert
Heinzmann<[hidden email]> wrote:

> Hello,
>
> Is it possible to have a "dry run" mode in DBIx-Class ?
>
> I just want to see the SQL Statements DBIx would execute, but not want
> DBIx to execute them.
>
> This way I can verify the statements before executing.
>
> Is this possible ?
>
> ---
> Robert Heinzmann
>
> _______________________________________________
> 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/dbix-class@...
>



--
Thanks,
Rob Kinyon

_______________________________________________
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/dbix-class@...
Adam Sjøgren-2

Re: "Dry-run" for SQL ?

Reply Threaded More More options
Print post
Permalink
In reply to this post by Robert Heinzmann
On Tue, 30 Jun 2009 18:35:29 +0200, Robert wrote:

> Is it possible to have a "dry run" mode in DBIx-Class ?

> I just want to see the SQL Statements DBIx would execute, but not want
> DBIx to execute them.

> This way I can verify the statements before executing.

I simply wrap the statements in a transaction and then roll it back at
the end.

Running the code with DBIC_TRACE=1 allows me to see the SQL executed,
and the database isn't changed.

I usually do something like this:

  my $transaction=sub {
      # Manipulate database...
      die "dry run";
  };
  $schema->txn_do($transaction);

Remembering to put the die in there is of course important :-)


  Best regards,

    Adam

--
                                                          Adam Sjøgren
                                                    [hidden email]

_______________________________________________
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/dbix-class@...
Robert Heinzmann

AW: Re: "Dry-run" for SQL ?

Reply Threaded More More options
Print post
Permalink
Hello Adam,

thanks for the Tip !

This works great. I get "Real World Testing" and all application logic works.

With DBD::Mock I have to make "syntetic tests" for all parts of the code that depends on previous inserts or on select results.

DBIC_TRACE is great :)

Regards,
Robert

 

> -----Ursprüngliche Nachricht-----
> Von: Adam Sjøgren [mailto:[hidden email]]
> Gesendet: Donnerstag, 2. Juli 2009 16:29
> An: [hidden email]
> Betreff: [Dbix-class] Re: "Dry-run" for SQL ?
>
> On Tue, 30 Jun 2009 18:35:29 +0200, Robert wrote:
>
> > Is it possible to have a "dry run" mode in DBIx-Class ?
>
> > I just want to see the SQL Statements DBIx would execute,
> but not want
> > DBIx to execute them.
>
> > This way I can verify the statements before executing.
>
> I simply wrap the statements in a transaction and then roll
> it back at the end.
>
> Running the code with DBIC_TRACE=1 allows me to see the SQL
> executed, and the database isn't changed.
>
> I usually do something like this:
>
>   my $transaction=sub {
>       # Manipulate database...
>       die "dry run";
>   };
>   $schema->txn_do($transaction);
>
> Remembering to put the die in there is of course important :-)
>
>
>   Best regards,
>
>     Adam
>

_______________________________________________
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/dbix-class@...
fREW Schmidt

Re: Re: "Dry-run" for SQL ?

Reply Threaded More More options
Print post
Permalink

> I usually do something like this:
>
>   my $transaction=sub {
>       # Manipulate database...
>       die "dry run";
>   };
>   $schema->txn_do($transaction);
>
> Remembering to put the die in there is of course important :-)
>
>
>   Best regards,
>
>     Adam
>

A more automated way to do this would be to do what is done in the DBIC test suite:

use_ok('DBICTest');
use_ok('DBIC::DebugObj');
my $schema = DBICTest->init_schema();

$schema->storage->sql_maker->quote_char('`');
$schema->storage->sql_maker->name_sep('.');

my ($sql, @bind);
$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
$schema->storage->debug(1);

my $rs;

$rs = $schema->resultset('CD')->search(
           { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
           { join => 'artist' });
eval { $rs->count };
is_same_sql_bind(
  $sql, \@bind,
  "SELECT COUNT( * ) FROM `cd` `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
  'got correct SQL for count query with quoting'
);


--
fREW Schmidt
http://blog.afoolishmanifesto.com

_______________________________________________
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/dbix-class@...