Merge 'trunk' into 'replication_dedux'
[dbsrgits/DBIx-Class.git] / t / 19quotes_newstyle.t
CommitLineData
2cc3a7be 1use strict;
2use warnings;
3
4use Test::More;
5use IO::File;
6
7BEGIN {
8 eval "use DBD::SQLite";
9 plan $@
10 ? ( skip_all => 'needs DBD::SQLite for testing' )
11 : ( tests => 6 );
12}
13
14use lib qw(t/lib);
15
16use_ok('DBICTest');
c216324a 17my $schema = DBICTest->init_schema();
2cc3a7be 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/));
2cc3a7be 23
c216324a 24my $dsn = $schema->storage->connect_info->[0];
77d76d0f 25$schema->connection(
26 $dsn,
27 undef,
28 undef,
29 { AutoCommit => 1 },
30 { quote_char => '`', name_sep => '.' },
31);
2cc3a7be 32
5a1f2d73 33my $sql = '';
c216324a 34$schema->storage->debugcb(sub { $sql = $_[1] });
35$schema->storage->debug(1);
5a1f2d73 36
37my $rs;
38
c216324a 39$rs = $schema->resultset('CD')->search(
2cc3a7be 40 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
41 { join => 'artist' });
5a1f2d73 42eval { $rs->count };
43like($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');
2cc3a7be 44
5a1f2d73 45my $order = 'year DESC';
c216324a 46$rs = $schema->resultset('CD')->search({},
5a1f2d73 47 { 'order_by' => $order });
48eval { $rs->first };
49like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
2cc3a7be 50
c216324a 51$rs = $schema->resultset('CD')->search({},
2cc3a7be 52 { 'order_by' => \$order });
5a1f2d73 53eval { $rs->first };
54like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
2cc3a7be 55
77d76d0f 56$schema->connection(
57 $dsn,
58 undef,
59 undef,
60 { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
61);
c216324a 62$schema->storage->debugcb(sub { $sql = $_[1] });
63$schema->storage->debug(1);
2cc3a7be 64
c216324a 65$rs = $schema->resultset('CD')->search(
2cc3a7be 66 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
67 { join => 'artist' });
5a1f2d73 68eval { $rs->count };
69like($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');
2cc3a7be 70
71my %data = (
72 name => 'Bill',
73 order => '12'
74);
75
77d76d0f 76$schema->connection(
77 $dsn,
78 undef,
79 undef,
80 { AutoCommit => 1, quote_char => '`', name_sep => '.' }
81);
2cc3a7be 82
c216324a 83is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
2cc3a7be 84
c216324a 85$schema->storage->debugcb($orig_debugcb);
86$schema->storage->debug($orig_debug);