Fey::Role::SQL::HasOrderByClause - A role for queries which can include a ORDER BY clause


Fey documentation Contained in the Fey distribution.

Index


Code Index:

NAME

Top

Fey::Role::SQL::HasOrderByClause - A role for queries which can include a ORDER BY clause

VERSION

Top

version 0.40

SYNOPSIS

Top

  use Moose;

  with 'Fey::Role::SQL::HasOrderByClause';

DESCRIPTION

Top

Classes which do this role represent a query which can include a ORDER BY clause.

METHODS

Top

This role provides the following methods:

$query->order_by()

See the Fey::SQL section on ORDER BY Clauses for more details.

$query->order_by_clause()

Returns the ORDER BY clause portion of the SQL statement as a string.

BUGS

Top

See Fey for details on how to report bugs.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Fey documentation Contained in the Fey distribution.

package Fey::Role::SQL::HasOrderByClause;
BEGIN {
  $Fey::Role::SQL::HasOrderByClause::VERSION = '0.40';
}

use strict;
use warnings;
use namespace::autoclean;

use Fey::Types qw( ArrayRef OrderByElement );
use Scalar::Util qw( blessed );

use Moose::Role;
use MooseX::Params::Validate qw( pos_validated_list );

has '_order_by' => (
    traits  => ['Array'],
    is      => 'bare',
    isa     => ArrayRef,
    default => sub { [] },
    handles => {
        _add_order_by_elements => 'push',
        _has_order_by_elements => 'count',
        _order_by              => 'elements',
    },
    init_arg => undef,
);

sub order_by {
    my $self = shift;

    my $count = @_ ? @_ : 1;
    my (@by) = pos_validated_list(
        \@_,
        ( ( { isa => OrderByElement } ) x $count ),
        MX_PARAMS_VALIDATE_NO_CACHE => 1,
    );

    $self->_add_order_by_elements(@by);

    return $self;
}

sub order_by_clause {
    my $self = shift;
    my $dbh  = shift;

    return unless $self->_has_order_by_elements();

    my $sql = 'ORDER BY ';

    my @elt = $self->_order_by();

    for my $elt (@elt) {
        if ( !blessed $elt ) {
            $sql .= q{ } . uc $elt;
        }
        else {
            $sql .= ', ' if $elt != $elt[0];
            $sql .= $elt->sql_or_alias($dbh);
        }
    }

    return $sql;
}

1;

# ABSTRACT: A role for queries which can include a ORDER BY clause




__END__