Port 89dbicadmin.t from trunk - now runs on MSWin32 too
[dbsrgits/DBIx-Class.git] / t / 19quotes_newstyle.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use IO::File;
6
7 BEGIN {
8     eval "use DBD::SQLite";
9     plan $@
10         ? ( skip_all => 'needs DBD::SQLite for testing' )
11         : ( tests => 6 );
12 }
13
14 use lib qw(t/lib);
15
16 use DBIC::SqlMakerTest;
17 use DBIC::DebugObj;
18
19 use_ok('DBICTest');
20 my $schema = DBICTest->init_schema();
21
22 diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
23
24 my $dsn = $schema->storage->connect_info->[0];
25 $schema->connection(
26   $dsn,
27   undef,
28   undef,
29   { AutoCommit => 1 },
30   { quote_char => '`', name_sep => '.' },
31 );
32
33 my $sql;
34 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql));
35 $schema->storage->debug (1);
36
37 my $rs = $schema->resultset('CD')->search(
38            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
39            { join => 'artist' });
40 eval { $rs->count };
41 ok (eq_sql
42   (
43     $sql,
44     q/SELECT COUNT( * ) FROM `cd` `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/,
45   ),
46   'got correct SQL for count query with quoting'
47 );
48 my $order = 'year DESC';
49 $rs = $schema->resultset('CD')->search({},
50             { 'order_by' => $order });
51 eval { $rs->first };
52 ok (eq_sql
53   (
54     $sql,
55     qq/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `${order}`/,
56   ),
57   'quoted ORDER BY with DESC (should use a scalarref anyway)'
58 );
59
60 $rs = $schema->resultset('CD')->search({},
61             { 'order_by' => \$order });
62 eval { $rs->first };
63 ok (eq_sql
64   (
65     $sql,
66     qq/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY ${order}/,
67   ),
68   'did not quote ORDER BY with scalarref'
69 );
70
71 $schema->connection(
72   $dsn,
73   undef,
74   undef,
75   { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
76 );
77 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql));
78 $schema->storage->debug(1);
79
80 $rs = $schema->resultset('CD')->search(
81            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
82            { join => 'artist' });
83 eval { $rs->count };
84 ok (eq_sql
85   (
86     $sql,
87     q/SELECT COUNT( * ) FROM [cd] [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/,
88   ),
89   'got correct SQL for count query with bracket quoting'
90 );
91
92 my %data = (
93        name => 'Bill',
94        order => '12'
95 );
96
97 $schema->connection(
98   $dsn,
99   undef,
100   undef,
101   { AutoCommit => 1, quote_char => '`', name_sep => '.' }
102 );
103
104 is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');