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