another test
[dbsrgits/DBIx-Class-Historic.git] / t / 19quotes_newstyle.t
CommitLineData
2cc3a7be 1use strict;
2use warnings;
3
4use Test::More;
5use IO::File;
c61a0748 6
7use lib qw(t/lib);
949172b0 8use DBIC::SqlMakerTest;
2cc3a7be 9
10BEGIN {
11 eval "use DBD::SQLite";
12 plan $@
13 ? ( skip_all => 'needs DBD::SQLite for testing' )
b3c4ad6b 14 : ( tests => 9 );
2cc3a7be 15}
16
2cc3a7be 17use_ok('DBICTest');
67e1ac6d 18use_ok('DBIC::DebugObj');
2cc3a7be 19
9b459129 20my $schema = DBICTest->init_schema();
5a1f2d73 21
4cf8bfe6 22#diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
2cc3a7be 23
ee3bc4ea 24my $dsn = $schema->storage->_dbi_connect_info->[0];
77d76d0f 25$schema->connection(
26 $dsn,
27 undef,
28 undef,
29 { AutoCommit => 1 },
30 { quote_char => '`', name_sep => '.' },
31);
2cc3a7be 32
af6aac2d 33my ($sql, @bind);
67e1ac6d 34$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
c216324a 35$schema->storage->debug(1);
5a1f2d73 36
37my $rs;
38
c216324a 39$rs = $schema->resultset('CD')->search(
2cc3a7be 40 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
41 { join => 'artist' });
5a1f2d73 42eval { $rs->count };
9b459129 43is_same_sql_bind(
a258ee5d 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'"],
9b459129 46 'got correct SQL for count query with quoting'
47);
2cc3a7be 48
c2b7c5dc 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' });
53eval { $rs->count };
54is_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
b3c4ad6b 60# check that the table works
61eval {
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};
68ok !$@, 'operations on scalarref table name work';
69
5a1f2d73 70my $order = 'year DESC';
c216324a 71$rs = $schema->resultset('CD')->search({},
5a1f2d73 72 { 'order_by' => $order });
73eval { $rs->first };
74like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
2cc3a7be 75
c216324a 76$rs = $schema->resultset('CD')->search({},
2cc3a7be 77 { 'order_by' => \$order });
5a1f2d73 78eval { $rs->first };
79like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
2cc3a7be 80
77d76d0f 81$schema->connection(
82 $dsn,
83 undef,
84 undef,
85 { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
86);
9b459129 87
67e1ac6d 88$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
c216324a 89$schema->storage->debug(1);
2cc3a7be 90
c216324a 91$rs = $schema->resultset('CD')->search(
2cc3a7be 92 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
93 { join => 'artist' });
5a1f2d73 94eval { $rs->count };
9b459129 95is_same_sql_bind(
a258ee5d 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'"],
9b459129 98 'got correct SQL for count query with bracket quoting'
99);
2cc3a7be 100
101my %data = (
102 name => 'Bill',
103 order => '12'
104);
105
77d76d0f 106$schema->connection(
107 $dsn,
108 undef,
109 undef,
110 { AutoCommit => 1, quote_char => '`', name_sep => '.' }
111);
2cc3a7be 112
c216324a 113is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');