Merge 'trunk' into 'replication_dedux'
[dbsrgits/DBIx-Class.git] / t / 19quotes.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 $schema->storage->sql_maker->quote_char('`');
25 $schema->storage->sql_maker->name_sep('.');
26
27 my $sql = '';
28
29 $schema->storage->debugcb(sub { $sql = $_[1] });
30 $schema->storage->debug(1);
31
32 my $rs;
33
34 $rs = $schema->resultset('CD')->search(
35            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
36            { join => 'artist' });
37 eval { $rs->count };
38 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');
39
40 my $order = 'year DESC';
41 $rs = $schema->resultset('CD')->search({},
42             { 'order_by' => $order });
43 eval { $rs->first };
44 like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
45
46 $rs = $schema->resultset('CD')->search({},
47             { 'order_by' => \$order });
48 eval { $rs->first };
49 like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
50
51 $schema->storage->sql_maker->quote_char([qw/[ ]/]);
52 $schema->storage->sql_maker->name_sep('.');
53
54 $rs = $schema->resultset('CD')->search(
55            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
56            { join => 'artist' });
57 eval { $rs->count };
58 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');
59
60 my %data = (
61        name => 'Bill',
62        order => '12'
63 );
64
65 $schema->storage->sql_maker->quote_char('`');
66 $schema->storage->sql_maker->name_sep('.');
67
68 is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
69
70 $schema->storage->debugcb($orig_debugcb);
71 $schema->storage->debug($orig_debug);