Rename SQLAHacks to SQLMaker, shuffle around files, add extensive
[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 DBIC::SqlMakerTest;
8
9
10 use_ok('DBICTest');
11 use_ok('DBIC::DebugObj');
12 my $schema = DBICTest->init_schema();
13
14 #diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
15
16 $schema->storage->sql_maker->quote_char('`');
17 $schema->storage->sql_maker->name_sep('.');
18
19 my ($sql, @bind);
20 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
21 $schema->storage->debug(1);
22
23 my $rs;
24
25 $rs = $schema->resultset('CD')->search(
26            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
27            { join => 'artist' });
28 eval { $rs->count };
29 is_same_sql_bind(
30   $sql, \@bind,
31   "SELECT COUNT( * ) FROM cd `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
32   'got correct SQL for count query with quoting'
33 );
34
35 my $order = 'year DESC';
36 $rs = $schema->resultset('CD')->search({},
37             { 'order_by' => $order });
38 eval { $rs->first };
39 like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
40
41 $rs = $schema->resultset('CD')->search({},
42             { 'order_by' => \$order });
43 eval { $rs->first };
44 like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
45
46 $schema->storage->sql_maker->quote_char([qw/[ ]/]);
47 $schema->storage->sql_maker->name_sep('.');
48
49 $rs = $schema->resultset('CD')->search(
50            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
51            { join => 'artist' });
52 eval { $rs->count };
53 is_same_sql_bind(
54   $sql, \@bind,
55   "SELECT COUNT( * ) FROM cd [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
56   'got correct SQL for count query with bracket quoting'
57 );
58
59 my %data = (
60        name => 'Bill',
61        order => '12'
62 );
63
64 $schema->storage->sql_maker->quote_char('`');
65 $schema->storage->sql_maker->name_sep('.');
66
67 is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
68
69 done_testing;