package LATMOS::Accounts::Bases::Sql::Onlyaddress;

use strict;
use warnings;

use base qw(LATMOS::Accounts::Bases::Sql::Address);

our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];

=head1 NAME

LATMOS::Accounts::Bases::Sql::Onlyaddress - Secondary office Address support SQL base

=head1 DESCRIPTION

This module handle object for office address, allowing to support multiple
address per people.

=cut

sub list {
    my ($class, $base) = @_;

    my $sth = $base->db->prepare_cached(
        sprintf(
            q{select %s.%s as k from %s
              join "user" on %s."user" = "user".name %s
              where %s.ikey not in (select okey from %s where attr = 'isMainAddress') %s
              order by %s.%s
            },
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_key_field),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_object_table),
            ($base->{wexported} ? '' : 'and "user".exported = true'),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_object_table. '_attributes'), # attr
            ($base->{wexported} ? '' : 'and ' .
                $base->db->quote_identifier($class->_object_table) . '.exported = true'),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_key_field),
        )
    );
    $sth->execute;
    my @keys;
    while(my $res = $sth->fetchrow_hashref) {
        push(@keys, $res->{k});
    }
    @keys
}

sub list_from_rev {
    my ($class, $base, $rev) = @_;

    my $sth = $base->db->prepare_cached(
        sprintf(
            q{select %s as k from %s where ikey not in (select okey from %s
            where attr = 'isMainAddress') and rev > ? %s order by %s},
            $base->db->quote_identifier($class->_key_field),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_object_table. '_attributes'), # attr
            ($base->{wexported} ? '' : 'and exported = true'),
            $base->db->quote_identifier($class->_key_field),
        )
    );
    $sth->execute($rev);
    my @keys;
    while(my $res = $sth->fetchrow_hashref) {
        push(@keys, $res->{k});
    }
    @keys
}

sub new {
    my ($class, $base, $id) = @_;
    my $sth = $base->db->prepare_cached(
        sprintf(q{select 1 from %s where %s = ? %s and ikey not in (select okey from %s where attr = 'isMainAddress')},
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_key_field),
            ($base->{wexported} ? '' : 'and exported = true'),
            $base->db->quote_identifier($class->_object_table. '_attributes'),
        ),
    );
    my $count = $sth->execute($id);
    $sth->finish;
    $count == 1 or return;
    $class->SUPER::new($base, $id);
}

1;

__END__

=head1 SEE ALSO

L<LATMOS::Accounts::Bases::Sql>

L<LATMOS::Accounts::Bases::Sql::Address>

=head1 AUTHOR

Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.

=cut
