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