add test for distinct result of sql function
[dbsrgits/DBIx-Class.git] / t / 19quotes_newstyle.t
CommitLineData
2cc3a7be 1use strict;
2use warnings;
3
4use Test::More;
5use IO::File;
c61a0748 6
7use lib qw(t/lib);
949172b0 8use DBIC::SqlMakerTest;
2cc3a7be 9
10BEGIN {
11 eval "use DBD::SQLite";
12 plan $@
13 ? ( skip_all => 'needs DBD::SQLite for testing' )
9b459129 14 : ( tests => 7 );
2cc3a7be 15}
16
2cc3a7be 17use_ok('DBICTest');
67e1ac6d 18use_ok('DBIC::DebugObj');
2cc3a7be 19
9b459129 20my $schema = DBICTest->init_schema();
5a1f2d73 21
4cf8bfe6 22#diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
2cc3a7be 23
ee3bc4ea 24my $dsn = $schema->storage->_dbi_connect_info->[0];
77d76d0f 25$schema->connection(
26 $dsn,
27 undef,
28 undef,
29 { AutoCommit => 1 },
30 { quote_char => '`', name_sep => '.' },
31);
2cc3a7be 32
9b459129 33my ($sql, @bind) = ('');
67e1ac6d 34$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
c216324a 35$schema->storage->debug(1);
5a1f2d73 36
37my $rs;
38
c216324a 39$rs = $schema->resultset('CD')->search(
2cc3a7be 40 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
41 { join => 'artist' });
5a1f2d73 42eval { $rs->count };
9b459129 43is_same_sql_bind(
d07ee31a 44 $sql,
45 \@bind,
46 "SELECT COUNT( * ) FROM (SELECT `me`.`cdid` FROM `cd` `me` JOIN `artist` `artist` ON `artist`.`artistid` = `me`.`artist` WHERE ( ( `artist`.`name` = ? AND `me`.`year` = ? ) ) GROUP BY `me`.`cdid`) `count_subq`",
47 ["'Caterwauler McCrae'", "'2001'"],
9b459129 48 'got correct SQL for count query with quoting'
49);
2cc3a7be 50
5a1f2d73 51my $order = 'year DESC';
c216324a 52$rs = $schema->resultset('CD')->search({},
5a1f2d73 53 { 'order_by' => $order });
54eval { $rs->first };
55like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
2cc3a7be 56
c216324a 57$rs = $schema->resultset('CD')->search({},
2cc3a7be 58 { 'order_by' => \$order });
5a1f2d73 59eval { $rs->first };
60like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
2cc3a7be 61
77d76d0f 62$schema->connection(
63 $dsn,
64 undef,
65 undef,
66 { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
67);
9b459129 68
67e1ac6d 69$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
c216324a 70$schema->storage->debug(1);
2cc3a7be 71
c216324a 72$rs = $schema->resultset('CD')->search(
2cc3a7be 73 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
74 { join => 'artist' });
5a1f2d73 75eval { $rs->count };
9b459129 76is_same_sql_bind(
d07ee31a 77 $sql,
78 \@bind,
79 "SELECT COUNT( * ) FROM (SELECT [me].[cdid] FROM [cd] [me] JOIN [artist] [artist] ON [artist].[artistid] = [me].[artist] WHERE ( ( [artist].[name] = ? AND [me].[year] = ? ) ) GROUP BY [me].[cdid]) [count_subq]",
80 ["'Caterwauler McCrae'", "'2001'"],
9b459129 81 'got correct SQL for count query with bracket quoting'
82);
2cc3a7be 83
84my %data = (
85 name => 'Bill',
86 order => '12'
87);
88
77d76d0f 89$schema->connection(
90 $dsn,
91 undef,
92 undef,
93 { AutoCommit => 1, quote_char => '`', name_sep => '.' }
94);
2cc3a7be 95
c216324a 96is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');