Dominic J. Thoreau

dom_db.pm

~\Desktop\prem2005\dom_db.pm.html

# to do :
#   rewrite connect code for old-skool dual value pair returns:
#      ie (Ok, handle), or (null, reason) sets
#   write error handling exit routines
#   genericise database connection function by adding schema/database parameter
use DBI;
use strict;
my $dbh;
$|=1;

sub db_connect {
        my $opt_database=shift;
        use vars qw($user_dbh $opt_help $opt_Information $opt_force $opt_debug
            $opt_verbose $opt_server $opt_root_user $opt_pwd $opt_user
            $opt_host $version $user $tables_cols $columns_cols);

$opt_help=$opt_Information=$opt_force=$opt_debug=$opt_verbose=0;
$opt_host="127.0.0.1",
$opt_server="mysql";
$opt_root_user="useracc";
$opt_pwd="password";
$opt_user="useracc";


$dbh = DBI->connect("DBI:mysql\:$opt_database\:".$opt_host, $opt_user, $opt_pwd);

return ($dbh);
}

sub prog_option {
        $ENV{'PATH_INFO'};
}

# simple, elegant, no wasted variables. Perfect
# ... at least until data grabbed could come from too different places....

# UNUSED in the football system. Just a library function I wrote once
sub grab {
        my $fn=shift;
        my $dbh=shift;
        if (defined $dbh) {
                my $sql = qq{SELECT count(*) from html where label= '$fn';};
                my $csr=$dbh->prepare($sql);
                $csr->execute;
                my ($retcode)=$csr->fetchrow;
                $csr->finish;
                if ($retcode == 0 ) {
                        open INN, ($fn);
                        my @blob=<INN>;
                        close INN;
                        return(join('',@blob));
                } else {
                        $sql = qq{SELECT code FROM html WHERE label = '$fn';};
                        $csr=$dbh->prepare($sql);
                        $csr->execute;
                        ($retcode)=$csr->fetchrow;
                        $csr->finish;
                        return ($retcode);
                }
        } else {
                open INN, ($fn);
                my @blob=<INN>;
                close INN;
                return(join('',@blob));
        }
}

# like all things there's a catch : mySQL doesn't use named parameters.
#  so, if you want to refer to a parameter twice it needs to be passed twice
sub select_sql {
        my ($sql, @args)=@_;
        my $csr=$dbh->prepare($sql);
        my $argc=$csr->{'NUM_OF_PARAMS'};
        unless($argc == @args) {
                $csr->finish;
                print "parameter counts don't match\n";
                die "parameter counts are important boyo";
        }
        $csr->execute(@args)|| die ;
        return $csr;
}

sub exec_sql {
        my ($sql,@args)=@_;
        my $csr=$dbh->prepare($sql);
        my $argc=$csr->{'NUM_OF_PARAMS'};
        unless($argc == @args) {
                $csr->finish;
                print "Parameter counts don't match\n";
                die "parameter count mismatch\n";
        }
        $csr->execute(@args)|| die ;
        $csr->finish;

}

© 2004 Dominic J. Thoreau - this is http://www.thoreau-online.net/dom_db.pm.html
Updated and uploaded Fri Dec 29 11:45:07 2006