Merge 'informix' into 'trunk'
[dbsrgits/DBIx-Class.git] / t / sqlahacks / quotes / quotes_newstyle.t
CommitLineData
2cc3a7be 1use strict;
2use warnings;
3
4use Test::More;
c61a0748 5
6use lib qw(t/lib);
949172b0 7use DBIC::SqlMakerTest;
2cc3a7be 8
9BEGIN {
10 eval "use DBD::SQLite";
11 plan $@
12 ? ( skip_all => 'needs DBD::SQLite for testing' )
688e73cb 13 : ( tests => 7 );
2cc3a7be 14}
15
2cc3a7be 16use_ok('DBICTest');
67e1ac6d 17use_ok('DBIC::DebugObj');
2cc3a7be 18
9b459129 19my $schema = DBICTest->init_schema();
5a1f2d73 20
4cf8bfe6 21#diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
2cc3a7be 22
ee3bc4ea 23my $dsn = $schema->storage->_dbi_connect_info->[0];
77d76d0f 24$schema->connection(
25 $dsn,
26 undef,
27 undef,
28 { AutoCommit => 1 },
29 { quote_char => '`', name_sep => '.' },
30);
2cc3a7be 31
af6aac2d 32my ($sql, @bind);
67e1ac6d 33$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
c216324a 34$schema->storage->debug(1);
5a1f2d73 35
36my $rs;
37
c216324a 38$rs = $schema->resultset('CD')->search(
2cc3a7be 39 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
40 { join => 'artist' });
5a1f2d73 41eval { $rs->count };
9b459129 42is_same_sql_bind(
a258ee5d 43 $sql, \@bind,
cebb7cce 44 "SELECT COUNT( * ) FROM cd `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
9b459129 45 'got correct SQL for count query with quoting'
46);
2cc3a7be 47
5a1f2d73 48my $order = 'year DESC';
c216324a 49$rs = $schema->resultset('CD')->search({},
5a1f2d73 50 { 'order_by' => $order });
51eval { $rs->first };
52like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
2cc3a7be 53
c216324a 54$rs = $schema->resultset('CD')->search({},
2cc3a7be 55 { 'order_by' => \$order });
5a1f2d73 56eval { $rs->first };
57like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
2cc3a7be 58
77d76d0f 59$schema->connection(
60 $dsn,
61 undef,
62 undef,
63 { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
64);
9b459129 65
67e1ac6d 66$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
c216324a 67$schema->storage->debug(1);
2cc3a7be 68
c216324a 69$rs = $schema->resultset('CD')->search(
2cc3a7be 70 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
71 { join => 'artist' });
5a1f2d73 72eval { $rs->count };
9b459129 73is_same_sql_bind(
a258ee5d 74 $sql, \@bind,
cebb7cce 75 "SELECT COUNT( * ) FROM cd [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
9b459129 76 'got correct SQL for count query with bracket quoting'
77);
2cc3a7be 78
79my %data = (
80 name => 'Bill',
81 order => '12'
82);
83
77d76d0f 84$schema->connection(
85 $dsn,
86 undef,
87 undef,
88 { AutoCommit => 1, quote_char => '`', name_sep => '.' }
89);
2cc3a7be 90
c216324a 91is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');