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