#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::DB::Item::Song;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Util qw(escape_mysql_regexp escape_mysql_like);
use Common::DB::ItemCollection;

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

use constant kTable => 'song';
use constant kDB    => Common::DB::Item::kClientDB();

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

    my $order;
    if ( $args{order} ) {
        $order = $args{order};
    } else {
        $order = "song.title, composer.name";
    }

    my $sql = "SELECT song.* FROM song LEFT JOIN composer ON composer.composer_id=song.song_id ORDER BY $order";
    return $class->SUPER::GetAll($sql);
}

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

    return undef if ( $searchTerm eq '' );

    my $dbo = Common::RSApp::GetClientDB();
    my $where;

    $where =
        $class->SearchSyntax( field => "song.title",    value => $searchTerm ) . " OR "
      . $class->SearchSyntax( field => "song.iswc",     value => $searchTerm ) . " OR "
      . $class->SearchSyntax( field => "composer.name", value => $searchTerm );

    my $sql = "SELECT song.* FROM song LEFT JOIN composer ON composer.composer_id=song.composer_id WHERE $where ORDER BY song.title";
    return $class->SUPER::GetAll($sql);

}

sub Match {
    my ( $class, $searchTerm ) = @_;

    return undef if ( $searchTerm eq '' );

    my $dbo = Common::RSApp::GetClientDB();
    my $where;

    $where = "WHERE song.title = " . $dbo->DBQuote($searchTerm);
    $where .= " OR song.iswc = " . $dbo->DBQuote($searchTerm);
    $where .= " OR composer.name = " . $dbo->DBQuote($searchTerm);

    my $sql = "SELECT song.* FROM song LEFT JOIN composer ON composer.composer_id=song.composer_id $where ORDER BY title";
    return $class->SUPER::GetAll($sql);
}

1;
