add test for distinct result of sql function
[dbsrgits/DBIx-Class.git] / t / 19quotes.t
CommitLineData
2a816814 1use strict;
58d387fe 2use warnings;
3
2a816814 4use Test::More;
5use IO::File;
c61a0748 6
7use lib qw(t/lib);
949172b0 8use DBIC::SqlMakerTest;
2a816814 9
10BEGIN {
11 eval "use DBD::SQLite";
12 plan $@
13 ? ( skip_all => 'needs DBD::SQLite for testing' )
9b459129 14 : ( tests => 7 );
2a816814 15}
16
2a816814 17
18use_ok('DBICTest');
67e1ac6d 19use_ok('DBIC::DebugObj');
c216324a 20my $schema = DBICTest->init_schema();
22b15c96 21
4cf8bfe6 22#diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
5a1f2d73 23
c216324a 24$schema->storage->sql_maker->quote_char('`');
25$schema->storage->sql_maker->name_sep('.');
2a816814 26
9b459129 27my ($sql, @bind) = ('');
67e1ac6d 28$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
c216324a 29$schema->storage->debug(1);
5a1f2d73 30
31my $rs;
32
c216324a 33$rs = $schema->resultset('CD')->search(
54540863 34 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
2a816814 35 { join => 'artist' });
5a1f2d73 36eval { $rs->count };
9b459129 37is_same_sql_bind(
d07ee31a 38 $sql,
39 \@bind,
40 "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`",
41 ["'Caterwauler McCrae'", "'2001'"],
42
43 'got correct SQL for joined count query with quoting'
9b459129 44);
2a816814 45
5a1f2d73 46my $order = 'year DESC';
c216324a 47$rs = $schema->resultset('CD')->search({},
5a1f2d73 48 { 'order_by' => $order });
49eval { $rs->first };
50like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
e535069e 51
c216324a 52$rs = $schema->resultset('CD')->search({},
e535069e 53 { 'order_by' => \$order });
5a1f2d73 54eval { $rs->first };
55like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
e535069e 56
c216324a 57$schema->storage->sql_maker->quote_char([qw/[ ]/]);
58$schema->storage->sql_maker->name_sep('.');
2437a1e3 59
c216324a 60$rs = $schema->resultset('CD')->search(
2437a1e3 61 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
62 { join => 'artist' });
5a1f2d73 63eval { $rs->count };
9b459129 64is_same_sql_bind(
d07ee31a 65 $sql,
66 \@bind,
67 "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]",
68 ["'Caterwauler McCrae'", "'2001'"],
9b459129 69 'got correct SQL for count query with bracket quoting'
70);
2437a1e3 71
6346a152 72my %data = (
73 name => 'Bill',
74 order => '12'
75);
2437a1e3 76
c216324a 77$schema->storage->sql_maker->quote_char('`');
78$schema->storage->sql_maker->name_sep('.');
2437a1e3 79
c216324a 80is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');