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