* Documented using PostgreSQL arrays in Cookbook.
[dbsrgits/DBIx-Class.git] / t / 19quotes.t
CommitLineData
2a816814 1use strict;
58d387fe 2use warnings;
3
2a816814 4use Test::More;
5use IO::File;
9b459129 6use SQL::Abstract::Test import => ['is_same_sql_bind'];
2a816814 7
8BEGIN {
9 eval "use DBD::SQLite";
10 plan $@
11 ? ( skip_all => 'needs DBD::SQLite for testing' )
9b459129 12 : ( tests => 7 );
2a816814 13}
14
15use lib qw(t/lib);
16
17use_ok('DBICTest');
9b459129 18use_ok('DBICTest::DBICDebugObj');
c216324a 19my $schema = DBICTest->init_schema();
22b15c96 20
c216324a 21diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
5a1f2d73 22
c216324a 23$schema->storage->sql_maker->quote_char('`');
24$schema->storage->sql_maker->name_sep('.');
2a816814 25
9b459129 26my ($sql, @bind) = ('');
27$schema->storage->debugobj(DBICTest::DBICDebugObj->new(\$sql, \@bind));
c216324a 28$schema->storage->debug(1);
5a1f2d73 29
30my $rs;
31
c216324a 32$rs = $schema->resultset('CD')->search(
54540863 33 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
2a816814 34 { join => 'artist' });
5a1f2d73 35eval { $rs->count };
9b459129 36is_same_sql_bind(
37 $sql, \@bind,
38 "SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
39 'got correct SQL for count query with quoting'
40);
2a816814 41
5a1f2d73 42my $order = 'year DESC';
c216324a 43$rs = $schema->resultset('CD')->search({},
5a1f2d73 44 { 'order_by' => $order });
45eval { $rs->first };
46like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
e535069e 47
c216324a 48$rs = $schema->resultset('CD')->search({},
e535069e 49 { 'order_by' => \$order });
5a1f2d73 50eval { $rs->first };
51like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
e535069e 52
c216324a 53$schema->storage->sql_maker->quote_char([qw/[ ]/]);
54$schema->storage->sql_maker->name_sep('.');
2437a1e3 55
c216324a 56$rs = $schema->resultset('CD')->search(
2437a1e3 57 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
58 { join => 'artist' });
5a1f2d73 59eval { $rs->count };
9b459129 60is_same_sql_bind(
61 $sql, \@bind,
62 "SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
63 'got correct SQL for count query with bracket quoting'
64);
2437a1e3 65
6346a152 66my %data = (
67 name => 'Bill',
68 order => '12'
69);
2437a1e3 70
c216324a 71$schema->storage->sql_maker->quote_char('`');
72$schema->storage->sql_maker->name_sep('.');
2437a1e3 73
c216324a 74is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');