Rename SQLAHacks to SQLMaker, shuffle around files, add extensive
[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
4cf8bfe6 14#diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
5a1f2d73 15
c216324a 16$schema->storage->sql_maker->quote_char('`');
17$schema->storage->sql_maker->name_sep('.');
2a816814 18
af6aac2d 19my ($sql, @bind);
67e1ac6d 20$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
c216324a 21$schema->storage->debug(1);
5a1f2d73 22
23my $rs;
24
c216324a 25$rs = $schema->resultset('CD')->search(
54540863 26 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
2a816814 27 { join => 'artist' });
5a1f2d73 28eval { $rs->count };
9b459129 29is_same_sql_bind(
a258ee5d 30 $sql, \@bind,
cebb7cce 31 "SELECT COUNT( * ) FROM cd `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
a258ee5d 32 'got correct SQL for count query with quoting'
9b459129 33);
2a816814 34
5a1f2d73 35my $order = 'year DESC';
c216324a 36$rs = $schema->resultset('CD')->search({},
5a1f2d73 37 { 'order_by' => $order });
38eval { $rs->first };
39like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
e535069e 40
c216324a 41$rs = $schema->resultset('CD')->search({},
e535069e 42 { 'order_by' => \$order });
5a1f2d73 43eval { $rs->first };
44like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
e535069e 45
c216324a 46$schema->storage->sql_maker->quote_char([qw/[ ]/]);
47$schema->storage->sql_maker->name_sep('.');
2437a1e3 48
c216324a 49$rs = $schema->resultset('CD')->search(
2437a1e3 50 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
51 { join => 'artist' });
5a1f2d73 52eval { $rs->count };
9b459129 53is_same_sql_bind(
a258ee5d 54 $sql, \@bind,
cebb7cce 55 "SELECT COUNT( * ) FROM cd [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
9b459129 56 'got correct SQL for count query with bracket quoting'
57);
2437a1e3 58
6346a152 59my %data = (
60 name => 'Bill',
61 order => '12'
62);
2437a1e3 63
c216324a 64$schema->storage->sql_maker->quote_char('`');
65$schema->storage->sql_maker->name_sep('.');
2437a1e3 66
c216324a 67is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
56166f36 68
69done_testing;