return 'PostgreSQL';
}
+# Pg is not able to MAX(boolean_column), sigh...
+#
+# Generally it would make more sense to have this in the SQLMaker hierarchy,
+# so that eventually { -max => ... } DTRT, but plans going forward are
+# murky at best
+# --ribasushi
+#
+sub _minmax_operator_for_datatype {
+ #my ($self, $datatype, $want_max) = @_;
+
+ return ($_[2] ? 'BOOL_OR' : 'BOOL_AND')
+ if ($_[1] || '') =~ /\Abool(?:ean)?\z/i;
+
+ shift->next::method(@_);
+}
+
sub bind_attribute_by_data_type {
my ($self,$data_type) = @_;
# of the external order and convert them to MIN(X) for ASC or MAX(X)
# for DESC, and group_by the root columns. The end result should be
# exactly what we expect
+ #
- # FIXME - this code is a joke, will need to be completely rewritten in
- # the DQ branch. But I need to push a POC here, otherwise the
- # pesky tests won't pass
- # wrap any part of the order_by that "responds" to an ordering alias
- # into a MIN/MAX
+ # both populated on the first loop over $o_idx
$sql_maker ||= $self->sql_maker;
$order_chunks ||= [
map { ref $_ eq 'ARRAY' ? $_ : [ $_ ] } $sql_maker->_order_by_chunks($attrs->{order_by})
$new_order_by[$o_idx] = \[
sprintf( '%s( %s )%s',
- ($is_desc ? 'MAX' : 'MIN'),
+ $self->_minmax_operator_for_datatype($chunk_ci->{data_type}, $is_desc),
$chunk,
($is_desc ? ' DESC' : ''),
),
);
}
+sub _minmax_operator_for_datatype {
+ #my ($self, $datatype, $want_max) = @_;
+
+ $_[2] ? 'MAX' : 'MIN';
+}
+
sub _resolve_ident_sources {
my ($self, $ident) = @_;
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More;
+
+use lib 't/lib';
+use DBICTest ':DiffSQL';
+
+my $schema = DBICTest->init_schema(
+ no_deploy => 1,
+ quote_names => 1,
+ storage_type => 'DBIx::Class::Storage::DBI::Pg'
+);
+
+my $rs = $schema->resultset('Artist')->search_related('cds_unordered',
+ { "me.rank" => 13 },
+ {
+ prefetch => 'tracks',
+ join => 'genre',
+ order_by => [ 'genre.name', { -desc => \ 'tracks.title' }, { -asc => "me.name" }, { -desc => [qw(year cds_unordered.title)] } ], # me. is the artist, *NOT* the cd
+ rows => 1,
+ },
+);
+
+{
+ # THIS IS AN OFFLINE TEST
+ # We only need this so that the thing can be verified to work without PG_DSN
+ # Executing it while "lying" this way won't work
+ local $rs->result_source->related_source('tracks')->column_info('title')->{data_type} = 'bool';
+ local $rs->result_source->related_source('genre')->column_info('name')->{data_type} = 'BOOLEAN';
+
+ is_same_sql_bind(
+ $rs->as_query,
+ q{(
+ SELECT "cds_unordered"."cdid", "cds_unordered"."artist", "cds_unordered"."title", "cds_unordered"."year", "cds_unordered"."genreid", "cds_unordered"."single_track",
+ "tracks"."trackid", "tracks"."cd", "tracks"."position", "tracks"."title", "tracks"."last_updated_on", "tracks"."last_updated_at"
+ FROM "artist" "me"
+ JOIN (
+ SELECT "cds_unordered"."cdid", "cds_unordered"."artist", "cds_unordered"."title", "cds_unordered"."year", "cds_unordered"."genreid", "cds_unordered"."single_track"
+ FROM "artist" "me"
+ JOIN cd "cds_unordered"
+ ON "cds_unordered"."artist" = "me"."artistid"
+ LEFT JOIN "genre" "genre"
+ ON "genre"."genreid" = "cds_unordered"."genreid"
+ LEFT JOIN "track" "tracks"
+ ON "tracks"."cd" = "cds_unordered"."cdid"
+ WHERE "me"."rank" = ?
+ GROUP BY "cds_unordered"."cdid", "cds_unordered"."artist", "cds_unordered"."title", "cds_unordered"."year", "cds_unordered"."genreid", "cds_unordered"."single_track", "me"."name"
+ ORDER BY BOOL_AND("genre"."name"),
+ BOOL_OR( tracks.title ) DESC,
+ "me"."name" ASC,
+ "year" DESC,
+ "cds_unordered"."title" DESC
+ LIMIT ?
+ ) "cds_unordered"
+ ON "cds_unordered"."artist" = "me"."artistid"
+ LEFT JOIN "genre" "genre"
+ ON "genre"."genreid" = "cds_unordered"."genreid"
+ LEFT JOIN "track" "tracks"
+ ON "tracks"."cd" = "cds_unordered"."cdid"
+ WHERE "me"."rank" = ?
+ ORDER BY "genre"."name",
+ tracks.title DESC,
+ "me"."name" ASC,
+ "year" DESC,
+ "cds_unordered"."title" DESC
+ )},
+ [
+ [ { sqlt_datatype => 'integer', dbic_colname => 'me.rank' } => 13 ],
+ [ DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype => 1 ],
+ [ { sqlt_datatype => 'integer', dbic_colname => 'me.rank' } => 13 ],
+ ],
+ 'correct SQL with aggregate boolean order on Pg',
+ );
+}
+
+done_testing;