#!/usr/bin/perl

use warnings;
use strict;

# ---
package Addresses::DBI;
use base 'Class::DBI';

Addresses::DBI->connection(
    'dbi:DB2:MCO',       # DBName
    '',                  # User
    '',                  # Password
    { AutoCommit => 1 }  # Options
);

# ---
package Addresses::Names;
use base 'Addresses::DBI';
use Class::DBI::AbstractSearch;

Addresses::Names->table( 'ADDRESS' );

Addresses::Names->columns(
	All => qw(ID FIRST LAST EMAIL DEPARTMENT TASK)
);

# ---
sub PrintRow {
    my( $Row ) = @_;

    printf( "% 5d %-15s %-10s".
		" %-30s % 5d %s\n",
        $Row->ID,
        $Row->FIRST,
        $Row->LAST,
        $Row->EMAIL,
        $Row->DEPARTMENT,
        $Row->TASK
    );
}

use Switch;
use Data::Dumper;

sub SearchWhere {
	my ( $demo ) = @_;
	my ( $where, $attrs );

	my $adr = new Addresses::Names;

	switch ( $demo ) {
		case 'or' {
			$where = {
				department => [ 140, 150 ]
			}
		}

		case 'and' {
			$where = {
				department => [ 140, 150 ],
				task => 'design'
			}
		}

		case 'range' {
			$where = {
				department => { '>' => 140, '<' => 170 }
			}
		}	

		case 'like' {
			$where = {
				last => { -like => 'Dow%' }
			}
		}

		case 'like-or' {
			$where = {
				last => { -like => [ 'Dow%', '%e' ] }
			}
		}

		case 'between' {
			$where = {
				id => { -between => [ 6, 10 ] }
			}
		}

		case 'not-between' {
			$where = {
				id => { -not_between => [ 12, 15 ] }
			}
		}

		case 'order' {
			$where = {
				id => { '>' => 0 }
			};
			$attrs = {
				order_by => [ 'department ASC', 'last ASC' ]
			};
		}

	}

	print "Query was: ", Dumper( $where ),
	"\nAttributes for this Query: ", Dumper( $attrs ), "\n";

	# ---

	my $Iterator = $adr->search_where(
		$where, $attrs
	);
	
	while ( my $Row = $Iterator->next ) {
		PrintRow( $Row );
	}
}

SearchWhere( $ARGV[0] ) if ( @ARGV > 0 );
