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