Merge branch 'master' into topic/constructor_rewrite
[dbsrgits/DBIx-Class.git] / t / sqlmaker / quotes / quotes.t
CommitLineData
2a816814 1use strict;
58d387fe 2use warnings;
3
2a816814 4use Test::More;
c61a0748 5
6use lib qw(t/lib);
949172b0 7use DBIC::SqlMakerTest;
2a816814 8
2a816814 9
10use_ok('DBICTest');
67e1ac6d 11use_ok('DBIC::DebugObj');
c216324a 12my $schema = DBICTest->init_schema();
22b15c96 13
c216324a 14$schema->storage->sql_maker->quote_char('`');
15$schema->storage->sql_maker->name_sep('.');
2a816814 16
af6aac2d 17my ($sql, @bind);
67e1ac6d 18$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
c216324a 19$schema->storage->debug(1);
5a1f2d73 20
21my $rs;
22
c216324a 23$rs = $schema->resultset('CD')->search(
54540863 24 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
2a816814 25 { join => 'artist' });
5a1f2d73 26eval { $rs->count };
9b459129 27is_same_sql_bind(
a258ee5d 28 $sql, \@bind,
cebb7cce 29 "SELECT COUNT( * ) FROM cd `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
a258ee5d 30 'got correct SQL for count query with quoting'
9b459129 31);
2a816814 32
5a1f2d73 33my $order = 'year DESC';
c216324a 34$rs = $schema->resultset('CD')->search({},
5a1f2d73 35 { 'order_by' => $order });
36eval { $rs->first };
37like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
e535069e 38
c216324a 39$rs = $schema->resultset('CD')->search({},
e535069e 40 { 'order_by' => \$order });
5a1f2d73 41eval { $rs->first };
42like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
e535069e 43
c216324a 44$schema->storage->sql_maker->quote_char([qw/[ ]/]);
45$schema->storage->sql_maker->name_sep('.');
2437a1e3 46
c216324a 47$rs = $schema->resultset('CD')->search(
2437a1e3 48 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
49 { join => 'artist' });
5a1f2d73 50eval { $rs->count };
9b459129 51is_same_sql_bind(
a258ee5d 52 $sql, \@bind,
cebb7cce 53 "SELECT COUNT( * ) FROM cd [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
9b459129 54 'got correct SQL for count query with bracket quoting'
55);
2437a1e3 56
6346a152 57my %data = (
58 name => 'Bill',
59 order => '12'
60);
2437a1e3 61
c216324a 62$schema->storage->sql_maker->quote_char('`');
63$schema->storage->sql_maker->name_sep('.');
2437a1e3 64
c216324a 65is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
56166f36 66
67done_testing;