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