This test was essentially c/p-ed in 2cc3a7be3, consolidate
[dbsrgits/DBIx-Class.git] / t / sqlmaker / quotes.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use lib qw(t/lib);
7 use DBICTest;
8 use DBIC::SqlMakerTest;
9 use DBIC::DebugObj;
10
11 my $schema = DBICTest->init_schema();
12
13 $schema->connection(
14   @{ $schema->storage->_dbi_connect_info },
15   { AutoCommit => 1, quote_char => [qw/[ ]/] }
16 );
17
18 my ($sql, @bind);
19 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
20 $schema->storage->debug(1);
21
22 my $rs = $schema->resultset('CD')->search(
23            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
24            { join => 'artist' });
25 my $expected_bind =   ["'Caterwauler McCrae'", "'2001'"];
26 eval { $rs->count };
27 is_same_sql_bind(
28   $sql, \@bind,
29   "SELECT COUNT( * ) FROM cd [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )",
30   $expected_bind,
31   'got correct SQL for count query with bracket quoting'
32 );
33
34 $schema->storage->sql_maker->quote_char('`');
35 $schema->storage->sql_maker->name_sep('.');
36
37 eval { $rs->count };
38 is_same_sql_bind(
39   $sql, \@bind,
40   "SELECT COUNT( * ) FROM cd `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )",
41   $expected_bind,
42   'got correct SQL for count query with quoting'
43 );
44
45 my $order = 'year DESC';
46 $rs = $schema->resultset('CD')->search({},
47             { 'order_by' => $order });
48 eval { $rs->first };
49 like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
50
51 $rs = $schema->resultset('CD')->search({},
52             { 'order_by' => \$order });
53 eval { $rs->first };
54 like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
55
56 is(
57   $schema->storage->sql_maker->update('group', { name => 'Bill', order => 12 }),
58   'UPDATE `group` SET `name` = ?, `order` = ?',
59   'quoted table names for UPDATE' );
60
61 done_testing;