#!/usr/bin/perl use strict; use warnings; use DBI; use DBD::mysql; package AndySQL; sub new { my $self = {}; bless $self, "AndySQL"; my ($ignore,$user,$pass,$host,$port,$database,$debug) = @_; $self->{connect} = DBI->connect("dbi:mysql:$database:localhost:3306", $user, $pass) || die $DBI::errstr; $self->{debug} = $debug; return $self; } sub disconnect { } sub table { my ($self,$table) = @_; $self->{table} = $table; return $self; } sub select_options { my ($self,$opts) = @_; $self->{select_options} = $opts; return $self; } sub count { my ($self,$args) = @_; $self->{table} || die "No table defined!"; my $select_options = $self->{select_options} || ''; my @cond_vals; my @cond_fields; if (ref $args eq "HASH") { map { push @cond_fields, "$_ = ?"; push @cond_vals, $args->{$_}; } keys %$args; } my ($cond_string,$query); if ($cond_fields[0]) { $cond_string = "WHERE " . join " AND ", @cond_fields; $query = qq|SELECT COUNT(*) FROM $self->{table} $cond_string $select_options|; } else { $query = qq|SELECT COUNT(*) FROM $self->{table} $select_options|; } print "Query: $query \n" if $self->{debug}; my $sth = $self->{connect}->prepare($query); $sth->execute(@cond_vals); my $total = $sth->fetchrow() || 0; $sth->finish(); return $total; } sub select { } sub delete { } sub do_query { } 1;