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