Merge branch 'master' into topic/constructor_rewrite
[dbsrgits/DBIx-Class.git] / t / sqlmaker / quotes / quotes_newstyle.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 use_ok('DBICTest');
10 use_ok('DBIC::DebugObj');
11
12 my $schema = DBICTest->init_schema();
13
14 my $dsn = $schema->storage->_dbi_connect_info->[0];
15 $schema->connection(
16   $dsn,
17   undef,
18   undef,
19   { AutoCommit => 1 },
20   { quote_char => '`', name_sep => '.' },
21 );
22
23 my ($sql, @bind);
24 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
25 $schema->storage->debug(1);
26
27 my $rs;
28
29 $rs = $schema->resultset('CD')->search(
30            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
31            { join => 'artist' });
32 eval { $rs->count };
33 is_same_sql_bind(
34   $sql, \@bind,
35   "SELECT COUNT( * ) FROM cd `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
36   'got correct SQL for count query with quoting'
37 );
38
39 my $order = 'year DESC';
40 $rs = $schema->resultset('CD')->search({},
41             { 'order_by' => $order });
42 eval { $rs->first };
43 like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
44
45 $rs = $schema->resultset('CD')->search({},
46             { 'order_by' => \$order });
47 eval { $rs->first };
48 like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
49
50 $schema->connection(
51   $dsn,
52   undef,
53   undef,
54   { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
55 );
56
57 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
58 $schema->storage->debug(1);
59
60 $rs = $schema->resultset('CD')->search(
61            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
62            { join => 'artist' });
63 eval { $rs->count };
64 is_same_sql_bind(
65   $sql, \@bind,
66   "SELECT COUNT( * ) FROM cd [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
67   'got correct SQL for count query with bracket quoting'
68 );
69
70 my %data = (
71        name => 'Bill',
72        order => '12'
73 );
74
75 $schema->connection(
76   $dsn,
77   undef,
78   undef,
79   { AutoCommit => 1, quote_char => '`', name_sep => '.' }
80 );
81
82 is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
83
84 done_testing;