* Added the parens for the join condition (SQL::Abstract::Test can't cope with it, and, besides, they help readability there; plus this gives the same output that older SQLA generated).
} else {
push(@sqlf, $self->_make_as($to));
}
- push(@sqlf, ' ON ', $self->_join_condition($on));
+ push(@sqlf, ' ON (', $self->_join_condition($on), ')');
}
return join('', @sqlf);
}
use Test::More;
use IO::File;
+use SQL::Abstract::Test import => ['is_same_sql_bind'];
BEGIN {
eval "use DBD::SQLite";
plan $@
? ( skip_all => 'needs DBD::SQLite for testing' )
- : ( tests => 6 );
+ : ( tests => 7 );
}
use lib qw(t/lib);
use_ok('DBICTest');
+use_ok('DBICTest::DBICDebugObj');
my $schema = DBICTest->init_schema();
-my $orig_debugcb = $schema->storage->debugcb;
-my $orig_debug = $schema->storage->debug;
-
diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
$schema->storage->sql_maker->quote_char('`');
$schema->storage->sql_maker->name_sep('.');
-my $sql = '';
-
-$schema->storage->debugcb(sub { $sql = $_[1] });
+my ($sql, @bind) = ('');
+$schema->storage->debugobj(DBICTest::DBICDebugObj->new(\$sql, \@bind));
$schema->storage->debug(1);
my $rs;
{ 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
{ join => 'artist' });
eval { $rs->count };
-like($sql, qr/\QSELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )\E/, 'got correct SQL for count query with quoting');
+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'
+);
my $order = 'year DESC';
$rs = $schema->resultset('CD')->search({},
{ 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
{ join => 'artist' });
eval { $rs->count };
-like($sql, qr/\QSELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )\E/, 'got correct SQL for count query with bracket quoting');
+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 bracket quoting'
+);
my %data = (
name => 'Bill',
$schema->storage->sql_maker->name_sep('.');
is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
-
-$schema->storage->debugcb($orig_debugcb);
-$schema->storage->debug($orig_debug);
use Test::More;
use IO::File;
+use SQL::Abstract::Test import => ['is_same_sql_bind'];
BEGIN {
eval "use DBD::SQLite";
plan $@
? ( skip_all => 'needs DBD::SQLite for testing' )
- : ( tests => 6 );
+ : ( tests => 7 );
}
use lib qw(t/lib);
use_ok('DBICTest');
-my $schema = DBICTest->init_schema();
+use_ok('DBICTest::DBICDebugObj');
-my $orig_debugcb = $schema->storage->debugcb;
-my $orig_debug = $schema->storage->debug;
+my $schema = DBICTest->init_schema();
diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
{ quote_char => '`', name_sep => '.' },
);
-my $sql = '';
-$schema->storage->debugcb(sub { $sql = $_[1] });
+my ($sql, @bind) = ('');
+$schema->storage->debugobj(DBICTest::DBICDebugObj->new(\$sql, \@bind)),
$schema->storage->debug(1);
my $rs;
{ 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
{ join => 'artist' });
eval { $rs->count };
-like($sql, qr/\QSELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )\E/, 'got correct SQL for count query with quoting');
+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'
+);
my $order = 'year DESC';
$rs = $schema->resultset('CD')->search({},
undef,
{ AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
);
-$schema->storage->debugcb(sub { $sql = $_[1] });
+
+$schema->storage->debugobj(DBICTest::DBICDebugObj->new(\$sql, \@bind)),
$schema->storage->debug(1);
$rs = $schema->resultset('CD')->search(
{ 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
{ join => 'artist' });
eval { $rs->count };
-like($sql, qr/\QSELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )\E/, 'got correct SQL for count query with bracket quoting');
+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 bracket quoting'
+);
my %data = (
name => 'Bill',
);
is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
-
-$schema->storage->debugcb($orig_debugcb);
-$schema->storage->debug($orig_debug);
use Test::More;
#use DBIx::Class::Storage::DBI;
+use SQL::Abstract::Test import => ['is_same_sql_bind'];
use DBIx::Class::Storage::DBI::Oracle::WhereJoins;
plan tests => 4;
# search with undefined or empty $cond
# my ($self, $table, $fields, $where, $order, @rest) = @_;
-is($sa->select([
+my ($sql, @bind) = $sa->select(
+ [
{ me => "cd" },
[
{ "-join_type" => "LEFT", artist => "artist" },
],
[ 'cd.cdid', 'cd.artist', 'cd.title', 'cd.year', 'artist.artistid', 'artist.name' ],
undef,
- undef),
- 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( artist.artistid(+) = me.artist )', 'WhereJoins search with empty where clause');
+ undef
+);
+is_same_sql_bind(
+ $sql, \@bind,
+ 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( artist.artistid(+) = me.artist )', [],
+ 'WhereJoins search with empty where clause'
+);
-is($sa->select([
+($sql, @bind) = $sa->select(
+ [
{ me => "cd" },
[
{ "-join_type" => "", artist => "artist" },
],
[ 'cd.cdid', 'cd.artist', 'cd.title', 'cd.year', 'artist.artistid', 'artist.name' ],
{ 'artist.artistid' => 3 },
- undef),
- 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( ( ( artist.artistid = me.artist ) AND ( artist.artistid = ? ) ) )', 'WhereJoins search with where clause');
+ undef
+);
+is_same_sql_bind(
+ $sql, \@bind,
+ 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( ( ( artist.artistid = me.artist ) AND ( artist.artistid = ? ) ) )', [3],
+ 'WhereJoins search with where clause'
+);
-is($sa->select([
+($sql, @bind) = $sa->select(
+ [
{ me => "cd" },
[
{ "-join_type" => "LEFT", artist => "artist" },
],
[ 'cd.cdid', 'cd.artist', 'cd.title', 'cd.year', 'artist.artistid', 'artist.name' ],
[{ 'artist.artistid' => 3 }, { 'me.cdid' => 5 }],
- undef),
- 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( ( ( artist.artistid(+) = me.artist ) AND ( ( ( artist.artistid = ? ) OR ( me.cdid = ? ) ) ) ) )', 'WhereJoins search with or in where clause');
+ undef
+);
+is_same_sql_bind(
+ $sql, \@bind,
+ 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( ( ( artist.artistid(+) = me.artist ) AND ( ( ( artist.artistid = ? ) OR ( me.cdid = ? ) ) ) ) )', [3, 5],
+ 'WhereJoins search with or in where clause'
+);
use lib qw(t/lib);
use DBICTest;
use Data::Dumper;
+use SQL::Abstract::Test import => ['is_same_sql_bind'];
my $schema = DBICTest->init_schema();
. 'child.father_id ) JOIN person mother ON ( mother.person_id '
. '= child.mother_id )'
;
-is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
+is_same_sql_bind(
+ $sa->_recurse_from(@j), [],
+ $match, [],
+ 'join 1 ok'
+);
my @j2 = (
{ mother => 'person' },
. ' father.person_id = child.father_id )) ON ( mother.person_id = '
. 'child.mother_id )'
;
-is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
+is_same_sql_bind(
+ $sa->_recurse_from(@j2), [],
+ $match, [],
+ 'join 2 ok'
+);
+
my @j3 = (
{ child => 'person' },
. '= child.mother_id )'
;
-is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
+is_same_sql_bind(
+ $sa->_recurse_from(@j3), [],
+ $match, [],
+ 'join 3 (inner join) ok'
+);
my @j4 = (
{ mother => 'person' },
. ' father.person_id = child.father_id )) ON ( mother.person_id = '
. 'child.mother_id )'
;
-is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok');
+is_same_sql_bind(
+ $sa->_recurse_from(@j4), [],
+ $match, [],
+ 'join 4 (nested joins + join types) ok'
+);
my @j5 = (
{ child => 'person' },
. 'child.father_id ) JOIN person mother ON ( mother.person_id '
. '= child.mother_id )'
;
-is( $sa->_recurse_from(@j5), $match, 'join 5 (SCALAR reference for ON statement) ok' );
+is_same_sql_bind(
+ $sa->_recurse_from(@j5), [],
+ $match, [],
+ 'join 5 (SCALAR reference for ON statement) ok'
+);
my @j6 = (
{ child => 'person' },
use Test::More;
use lib qw(t/lib);
use DBICTest;
+use DBICTest::DBICDebugObj;
+use SQL::Abstract::Test import => ['is_same_sql_bind'];
my $schema = DBICTest->init_schema();
-plan tests => 6;
+plan tests => 7;
ok ( $schema->storage->debug(1), 'debug' );
ok ( defined(
# test trace output correctness for bind params
{
- my $sql = '';
+ my ($sql, @bind) = ('');
$schema->storage->debugcb( sub { $sql = $_[1] } );
my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
- like(
- $sql,
- qr/\QSELECT me.cdid, me.artist, me.title, me.year, me.genreid FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'\E/,
- 'got correct SQL with all bind parameters'
+ is_same_sql_bind(
+ $sql, [],
+ "SELECT me.cdid, me.artist, me.title, me.year, me.genreid FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'", [],
+ 'got correct SQL with all bind parameters (debugcb)'
+ );
+
+ $schema->storage->debugcb(undef);
+ $schema->storage->debugobj(DBICTest::DBICDebugObj->new(\$sql, \@bind));
+ @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
+ is_same_sql_bind(
+ $sql, \@bind,
+ "SELECT me.cdid, me.artist, me.title, me.year, me.genreid FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? )", ["'1'", "'1'", "'3'"],
+ 'got correct SQL with all bind parameters (debugobj)'
);
}
use warnings;
use Test::More;
+use SQL::Abstract::Test import => ['is_same_sql_bind'];
BEGIN {
$sql_maker->quote_char('`');
$sql_maker->name_sep('.');
-my ($sql,) = $sql_maker->select(
+my ($sql, @bind) = $sql_maker->select(
[
{
'me' => 'cd'
undef
);
-is($sql,
- q/SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/,
- 'got correct SQL for count query with quoting');
+is_same_sql_bind(
+ $sql, \@bind,
+ q/SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
+ 'got correct SQL and bind parameters for count query with quoting'
+);
-($sql,) = $sql_maker->select(
+($sql, @bind) = $sql_maker->select(
[
{
'me' => 'cd'
TODO: {
local $TODO = "select attr with star needs fixing (mst/nate)";
- ($sql,) = $sql_maker->select(
+ ($sql, @bind) = $sql_maker->select(
[
{
'me' => 'cd'
undef
);
- is($sql, q/SELECT `me`.* FROM `cd` `me`/, 'select attr with me.* is right');
+ is_same_sql_bind(
+ $sql, \@bind,
+ q/SELECT `me`.* FROM `cd` `me`/, [],
+ 'select attr with me.* is right'
+ );
}
-($sql,) = $sql_maker->select(
+($sql, @bind) = $sql_maker->select(
[
{
'me' => 'cd'
undef
);
-is($sql,
- q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/,
- 'did not quote ORDER BY with scalarref');
-
-my %data = (
- name => 'Bill',
- order => 12
+is_same_sql_bind(
+ $sql, \@bind,
+ q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
+ 'did not quote ORDER BY with scalarref'
);
-my @binds;
-
-($sql,@binds) = $sql_maker->update(
+($sql, @bind) = $sql_maker->update(
'group',
{
'order' => '12',
}
);
-is($sql,
- q/UPDATE `group` SET `name` = ?, `order` = ?/,
- 'quoted table names for UPDATE');
+is_same_sql_bind(
+ $sql, \@bind,
+ q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
+ 'quoted table names for UPDATE'
+);
$sql_maker->quote_char([qw/[ ]/]);
-($sql,) = $sql_maker->select(
+($sql, @bind) = $sql_maker->select(
[
{
'me' => 'cd'
undef
);
-is($sql,
- q/SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/,
- 'got correct SQL for count query with bracket quoting');
+is_same_sql_bind(
+ $sql, \@bind,
+ q/SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
+ 'got correct SQL and bind parameters for count query with bracket quoting'
+);
-($sql,@binds) = $sql_maker->update(
+($sql, @bind) = $sql_maker->update(
'group',
{
'order' => '12',
}
);
-is($sql,
- q/UPDATE [group] SET [name] = ?, [order] = ?/,
- 'bracket quoted table names for UPDATE');
+is_same_sql_bind(
+ $sql, \@bind,
+ q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
+ 'bracket quoted table names for UPDATE'
+);
BEGIN {
eval "use DBD::mysql; use SQL::Translator 0.09;";
plan $@
- ? ( skip_all => 'needs SQL::Translator 0.09 for testing' )
+ ? ( skip_all => 'needs DBD::mysql and SQL::Translator 0.09 for testing' )
: ( tests => 102 );
}
--- /dev/null
+package DBICTest::DBICDebugObj;
+
+use strict;
+use warnings;
+
+use Exporter;
+use Class::C3;
+
+use base qw/DBIx::Class::Storage::Statistics/;
+use base qw/Exporter/;
+use base qw/Class::Accessor::Fast/;
+
+__PACKAGE__->mk_accessors( qw/dbictest_sql_ref dbictest_bind_ref/ );
+
+
+=head2 new(PKG, SQL_REF, BIND_REF, ...)
+
+Creates a new instance that on subsequent queries will store
+the generated SQL to the scalar pointed to by SQL_REF and bind
+values to the array pointed to by BIND_REF.
+
+=cut
+
+sub new {
+ my $pkg = shift;
+ my $sql_ref = shift;
+ my $bind_ref = shift;
+
+ my $self = $pkg->SUPER::new(@_);
+
+ $self->debugfh(undef);
+
+ $self->dbictest_sql_ref($sql_ref);
+ $self->dbictest_bind_ref($bind_ref);
+
+ return $self;
+}
+
+sub query_start {
+ my $self = shift;
+
+ (${$self->dbictest_sql_ref}, @{$self->dbictest_bind_ref}) = @_;
+}
+
+sub query_end { }
+
+sub txn_start { }
+
+sub txn_commit { }
+
+sub txn_rollback { }
+
+1;