Straightforward test changes
[dbsrgits/DBIx-Class.git] / t / 19quotes_newstyle.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 use_ok('DBICTest');
18 use_ok('DBIC::DebugObj');
19
20 my $schema = DBICTest->init_schema();
21
22 #diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
23
24 my $dsn = $schema->storage->_dbi_connect_info->[0];
25 $schema->connection(
26   $dsn,
27   undef,
28   undef,
29   { AutoCommit => 1 },
30   { quote_char => '`', name_sep => '.' },
31 );
32
33 my ($sql, @bind) = ('');
34 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
35 $schema->storage->debug(1);
36
37 my $rs;
38
39 $rs = $schema->resultset('CD')->search(
40            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
41            { join => 'artist' });
42 eval { $rs->count };
43 is_same_sql_bind(
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'"],
48   'got correct SQL for count query with quoting'
49 );
50
51 my $order = 'year DESC';
52 $rs = $schema->resultset('CD')->search({},
53             { 'order_by' => $order });
54 eval { $rs->first };
55 like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
56
57 $rs = $schema->resultset('CD')->search({},
58             { 'order_by' => \$order });
59 eval { $rs->first };
60 like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
61
62 $schema->connection(
63   $dsn,
64   undef,
65   undef,
66   { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
67 );
68
69 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
70 $schema->storage->debug(1);
71
72 $rs = $schema->resultset('CD')->search(
73            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
74            { join => 'artist' });
75 eval { $rs->count };
76 is_same_sql_bind(
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'"],
81   'got correct SQL for count query with bracket quoting'
82 );
83
84 my %data = (
85        name => 'Bill',
86        order => '12'
87 );
88
89 $schema->connection(
90   $dsn,
91   undef,
92   undef,
93   { AutoCommit => 1, quote_char => '`', name_sep => '.' }
94 );
95
96 is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');