package DBIx::Library::SQL; use overload '""' => sub {return shift->{sql}}; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $sql = shift; my $dbh = shift || undef;; my $self = { dbh => $dbh, sql => $sql, }; bless $self, $class; return $self; } sub sql { my $self = shift; return $self->{sql}; } my @okay = qw/do selectall_arrayref selectall_hashref selectcol_arrayref selectcol_arrayref selectrow_array selectrow_arrayref selectrow_hashref prepare prepare_cached/; our $AUTOLOAD; sub AUTOLOAD { my $self = shift; my $type = ref($self) or die "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion unless (defined $self->{dbh}) { die "Please define the database handle before calling methods"; } unless (grep {$_ eq $name} @okay ) { die "Bad method"; } if (@_) { return $self->{dbh}->$name($self->{sql}, @_); } else { return $self->{dbh}->$name($self->{sql}); } } 1;