#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::DB::Item::BISACSubject;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Assert;
use base 'Common::DB::Item';

use constant kTable => 'bisac_subject';
use constant kDB    => Common::DB::Item::kCommonDB;

sub Search {
    my ( $class, %args ) = @_;

    my $searchTerm = $args{searchTerm};
    my $limit      = $args{limit};

    my $where;

    if ($searchTerm) {
        my $where1 = $class->SUPER::SearchSyntax( field => 'bisac_subject_code', value => $searchTerm );

        # !!! Can I also search within the literal?  I'll need to index it, probably...
        my $where2 = $class->SUPER::SearchSyntax( field => 'literal', value => $searchTerm );
        $where = "(($where1) OR ($where2))";
    }

    my $sql = 'SELECT * FROM ' . kTable;
    if ($where) {
        $sql .= " WHERE $where";
    }

    $sql .= ' ORDER BY bisac_subject_code';

    if ($limit) {
        $sql .= " LIMIT $limit";
    }

    Common::Log::Debug("SEARCH: $sql");

    return $class->SUPER::GetAll($sql);
}

1;
