Remove all "magic number" DBI get_info calls from the codebase
[dbsrgits/DBIx-Class.git] / t / sqlmaker / 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
2cc3a7be 9use_ok('DBICTest');
67e1ac6d 10use_ok('DBIC::DebugObj');
2cc3a7be 11
9b459129 12my $schema = DBICTest->init_schema();
5a1f2d73 13
ee3bc4ea 14my $dsn = $schema->storage->_dbi_connect_info->[0];
77d76d0f 15$schema->connection(
16 $dsn,
17 undef,
18 undef,
19 { AutoCommit => 1 },
20 { quote_char => '`', name_sep => '.' },
21);
2cc3a7be 22
af6aac2d 23my ($sql, @bind);
67e1ac6d 24$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
c216324a 25$schema->storage->debug(1);
5a1f2d73 26
27my $rs;
28
c216324a 29$rs = $schema->resultset('CD')->search(
2cc3a7be 30 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
31 { join => 'artist' });
5a1f2d73 32eval { $rs->count };
9b459129 33is_same_sql_bind(
a258ee5d 34 $sql, \@bind,
cebb7cce 35 "SELECT COUNT( * ) FROM cd `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
9b459129 36 'got correct SQL for count query with quoting'
37);
2cc3a7be 38
5a1f2d73 39my $order = 'year DESC';
c216324a 40$rs = $schema->resultset('CD')->search({},
5a1f2d73 41 { 'order_by' => $order });
42eval { $rs->first };
43like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
2cc3a7be 44
c216324a 45$rs = $schema->resultset('CD')->search({},
2cc3a7be 46 { 'order_by' => \$order });
5a1f2d73 47eval { $rs->first };
48like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
2cc3a7be 49
77d76d0f 50$schema->connection(
51 $dsn,
52 undef,
53 undef,
54 { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' }
55);
9b459129 56
67e1ac6d 57$schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
c216324a 58$schema->storage->debug(1);
2cc3a7be 59
c216324a 60$rs = $schema->resultset('CD')->search(
2cc3a7be 61 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
62 { join => 'artist' });
5a1f2d73 63eval { $rs->count };
9b459129 64is_same_sql_bind(
a258ee5d 65 $sql, \@bind,
cebb7cce 66 "SELECT COUNT( * ) FROM cd [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
9b459129 67 'got correct SQL for count query with bracket quoting'
68);
2cc3a7be 69
70my %data = (
71 name => 'Bill',
72 order => '12'
73);
74
77d76d0f 75$schema->connection(
76 $dsn,
77 undef,
78 undef,
79 { AutoCommit => 1, quote_char => '`', name_sep => '.' }
80);
2cc3a7be 81
c216324a 82is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
56166f36 83
84done_testing;