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