7c2d3f20788df0721facb34278046a3553126a6b
[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 use lib qw(t/lib);
8 use DBIC::SqlMakerTest;
9
10 BEGIN {
11     eval "use DBD::SQLite";
12     plan $@
13         ? ( skip_all => 'needs DBD::SQLite for testing' )
14         : ( tests => 9 );
15 }
16
17 use_ok('DBICTest');
18 use_ok('DBIC::DebugObj');
19
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->_dbi_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, @bind);
34 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
35 $schema->storage->debug(1);
36
37 my $rs;
38
39 $rs = $schema->resultset('CD')->search(
40            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
41            { join => 'artist' });
42 eval { $rs->count };
43 is_same_sql_bind(
44   $sql, \@bind,
45   "SELECT COUNT( * ) FROM `cd` `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
46   'got correct SQL for count query with quoting'
47 );
48
49 # try with ->table(\'cd') should NOT be quoted
50 $rs = $schema->resultset('CDTableRef')->search(
51            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
52            { join => 'artist' });
53 eval { $rs->count };
54 is_same_sql_bind(
55   $sql, \@bind,
56   "SELECT COUNT( * ) FROM cd `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
57   'got correct SQL for count query with quoting'
58 );
59
60 # check that the table works
61 eval {
62   my $rs = $schema->resultset('CDTableRef');
63   $rs->create({ cdid => 6, artist => 3, title => 'mtfnpy', year => 2009 });
64   my $row = $rs->find(6);
65   $row->update({ title => 'bleh' });
66   $row->delete;
67 };
68 ok !$@, 'operations on scalarref table name work';
69
70 my $order = 'year DESC';
71 $rs = $schema->resultset('CD')->search({},
72             { 'order_by' => $order });
73 eval { $rs->first };
74 like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
75
76 $rs = $schema->resultset('CD')->search({},
77             { 'order_by' => \$order });
78 eval { $rs->first };
79 like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
80
81 $schema->connection(
82   $dsn,
83   undef,
84   undef,
85   { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
86 );
87
88 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
89 $schema->storage->debug(1);
90
91 $rs = $schema->resultset('CD')->search(
92            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
93            { join => 'artist' });
94 eval { $rs->count };
95 is_same_sql_bind(
96   $sql, \@bind,
97   "SELECT COUNT( * ) FROM [cd] [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
98   'got correct SQL for count query with bracket quoting'
99 );
100
101 my %data = (
102        name => 'Bill',
103        order => '12'
104 );
105
106 $schema->connection(
107   $dsn,
108   undef,
109   undef,
110   { AutoCommit => 1, quote_char => '`', name_sep => '.' }
111 );
112
113 is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');