Minor documentation patch by Flavio Poletti - remove useless prefetch from examples
[dbsrgits/DBIx-Class.git] / t / 19quotes.t
CommitLineData
2a816814 1use strict;
58d387fe 2use warnings;
3
2a816814 4use Test::More;
5use IO::File;
6
7BEGIN {
8 eval "use DBD::SQLite";
9 plan $@
10 ? ( skip_all => 'needs DBD::SQLite for testing' )
58d387fe 11 : ( tests => 6 );
2a816814 12}
13
14use lib qw(t/lib);
15
16use_ok('DBICTest');
c216324a 17my $schema = DBICTest->init_schema();
22b15c96 18
c216324a 19my $orig_debugcb = $schema->storage->debugcb;
20my $orig_debug = $schema->storage->debug;
5a1f2d73 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
5a1f2d73 27my $sql = '';
28
c216324a 29$schema->storage->debugcb(sub { $sql = $_[1] });
30$schema->storage->debug(1);
5a1f2d73 31
32my $rs;
33
c216324a 34$rs = $schema->resultset('CD')->search(
54540863 35 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
2a816814 36 { join => 'artist' });
5a1f2d73 37eval { $rs->count };
38like($sql, qr/\QSELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )\E/, 'got correct SQL for count query with quoting');
2a816814 39
5a1f2d73 40my $order = 'year DESC';
c216324a 41$rs = $schema->resultset('CD')->search({},
5a1f2d73 42 { 'order_by' => $order });
43eval { $rs->first };
44like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
e535069e 45
c216324a 46$rs = $schema->resultset('CD')->search({},
e535069e 47 { 'order_by' => \$order });
5a1f2d73 48eval { $rs->first };
49like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
e535069e 50
c216324a 51$schema->storage->sql_maker->quote_char([qw/[ ]/]);
52$schema->storage->sql_maker->name_sep('.');
2437a1e3 53
c216324a 54$rs = $schema->resultset('CD')->search(
2437a1e3 55 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
56 { join => 'artist' });
5a1f2d73 57eval { $rs->count };
58like($sql, qr/\QSELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )\E/, 'got correct SQL for count query with bracket quoting');
2437a1e3 59
6346a152 60my %data = (
61 name => 'Bill',
62 order => '12'
63);
2437a1e3 64
c216324a 65$schema->storage->sql_maker->quote_char('`');
66$schema->storage->sql_maker->name_sep('.');
2437a1e3 67
c216324a 68is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
2437a1e3 69
c216324a 70$schema->storage->debugcb($orig_debugcb);
71$schema->storage->debug($orig_debug);