398aa043b2a4ccd0664018fb687b28915afae3fc
[dbsrgits/DBIx-Class.git] / t / sqlahacks / quotes / quotes.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use lib qw(t/lib);
7 use DBIC::SqlMakerTest;
8
9 BEGIN {
10     eval "use DBD::SQLite";
11     plan $@
12         ? ( skip_all => 'needs DBD::SQLite for testing' )
13         : ( tests => 7 );
14 }
15
16
17 use_ok('DBICTest');
18 use_ok('DBIC::DebugObj');
19 my $schema = DBICTest->init_schema();
20
21 #diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
22
23 $schema->storage->sql_maker->quote_char('`');
24 $schema->storage->sql_maker->name_sep('.');
25
26 my ($sql, @bind);
27 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
28 $schema->storage->debug(1);
29
30 my $rs;
31
32 $rs = $schema->resultset('CD')->search(
33            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
34            { join => 'artist' });
35 eval { $rs->count };
36 is_same_sql_bind(
37   $sql, \@bind,
38   "SELECT COUNT( * ) FROM cd `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
39   'got correct SQL for count query with quoting'
40 );
41
42 my $order = 'year DESC';
43 $rs = $schema->resultset('CD')->search({},
44             { 'order_by' => $order });
45 eval { $rs->first };
46 like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
47
48 $rs = $schema->resultset('CD')->search({},
49             { 'order_by' => \$order });
50 eval { $rs->first };
51 like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
52
53 $schema->storage->sql_maker->quote_char([qw/[ ]/]);
54 $schema->storage->sql_maker->name_sep('.');
55
56 $rs = $schema->resultset('CD')->search(
57            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
58            { join => 'artist' });
59 eval { $rs->count };
60 is_same_sql_bind(
61   $sql, \@bind,
62   "SELECT COUNT( * ) FROM cd [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
63   'got correct SQL for count query with bracket quoting'
64 );
65
66 my %data = (
67        name => 'Bill',
68        order => '12'
69 );
70
71 $schema->storage->sql_maker->quote_char('`');
72 $schema->storage->sql_maker->name_sep('.');
73
74 is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');