package Utilities::Database::Predicate;
use strict;
use overload q("") => \&as_string;
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);

use Exporter;

@ISA         = qw(Exporter);
@EXPORT_OK   = qw(AND OR NOT);
%EXPORT_TAGS = (logical => [qw(AND OR NOT)]);

sub AND {
    my $self = {OP       => 'AND',
                OPERANDS => [@_]};
    bless $self, __PACKAGE__;
}

sub OR {
    my $self = {OP       => 'OR',
                OPERANDS => [@_]};
    bless $self, __PACKAGE__;
}

sub NOT {
    my $self = {OP      => 'NOT',
                OPERAND => shift};
    bless $self, __PACKAGE__;
}

sub as_string {
    my $self = shift;

    for ($self->{OP}) {
        $_ eq 'AND'  && do { return join ' AND ' => map {"($_)"} @{$self->{OPERANDS}} };
        $_ eq 'OR'   && do { return join ' OR '  => map {"($_)"} @{$self->{OPERANDS}} };
        $_ eq 'NOT'  && do { return "NOT ($self->{OPERAND})" };
    }
}

1;
