Fey::Role::SQL::HasLimitClause - A role for queries which can include a LIMIT clause


Fey documentation Contained in the Fey distribution.

Index


Code Index:

NAME

Top

Fey::Role::SQL::HasLimitClause - A role for queries which can include a LIMIT clause

VERSION

Top

version 0.40

SYNOPSIS

Top

  use Moose;

  with 'Fey::Role::SQL::HasLimitClause';

DESCRIPTION

Top

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

METHODS

Top

This role provides the following methods:

$query->limit()

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

$query->limit_clause()

Returns the LIMIT 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::HasLimitClause;
BEGIN {
  $Fey::Role::SQL::HasLimitClause::VERSION = '0.40';
}

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

use Scalar::Util qw( blessed );

use Fey::Types qw( PosInteger PosOrZeroInteger Undef );

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

has '_limit' => (
    is        => 'rw',
    writer    => '_set_limit',
    predicate => '_has_limit',
);

has '_offset' => (
    is        => 'rw',
    writer    => '_set_offset',
    predicate => '_has_offset',
);

sub limit {
    my $self = shift;
    my ( $limit, $offset ) = pos_validated_list(
        \@_,
        { isa => (PosInteger | Undef) },
        {
            isa      => PosOrZeroInteger,
            optional => 1,
        },
    );

    $self->_set_limit($limit)
        if defined $limit;
    $self->_set_offset($offset)
        if defined $offset;

    return $self;
}

sub limit_clause {
    my $self = shift;

    return unless $self->_has_limit() || $self->_has_offset();

    my $sql = '';

    $sql .= 'LIMIT ' . $self->_limit()
        if $self->_has_limit();
    $sql .= q{ }
        if $self->_has_limit() && $self->_has_offset();
    $sql .= 'OFFSET ' . $self->_offset()
        if $self->_has_offset();

    return $sql;
}

1;

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




__END__