moved tests to compose_namespace instead of compose_connection, marked compose_connec...
[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];
25$schema->connection($dsn, { quote_char => '`', name_sep => '.' });
2cc3a7be 26
5a1f2d73 27my $sql = '';
c216324a 28$schema->storage->debugcb(sub { $sql = $_[1] });
29$schema->storage->debug(1);
5a1f2d73 30
31my $rs;
32
c216324a 33$rs = $schema->resultset('CD')->search(
2cc3a7be 34 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
35 { join => 'artist' });
5a1f2d73 36eval { $rs->count };
37like($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 38
5a1f2d73 39my $order = 'year DESC';
c216324a 40$rs = $schema->resultset('CD')->search({},
5a1f2d73 41 { 'order_by' => $order });
42eval { $rs->first };
43like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
2cc3a7be 44
c216324a 45$rs = $schema->resultset('CD')->search({},
2cc3a7be 46 { 'order_by' => \$order });
5a1f2d73 47eval { $rs->first };
48like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
2cc3a7be 49
c216324a 50$schema->connection($dsn, { quote_char => [qw/[ ]/], name_sep => '.' });
51$schema->storage->debugcb(sub { $sql = $_[1] });
52$schema->storage->debug(1);
2cc3a7be 53
c216324a 54$rs = $schema->resultset('CD')->search(
2cc3a7be 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');
2cc3a7be 59
60my %data = (
61 name => 'Bill',
62 order => '12'
63);
64
c216324a 65$schema->connection($dsn, { quote_char => '`', name_sep => '.' });
2cc3a7be 66
c216324a 67is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
2cc3a7be 68
c216324a 69$schema->storage->debugcb($orig_debugcb);
70$schema->storage->debug($orig_debug);