use strict;

# for now, just mapping from domains to groups

use DBI;

my $DBH;

sub get_dbh {
    return ($DBH ||= DBI->connect("DBI:CSV:f_dir=$::DBFILES", undef, undef, {RaiseError => 1}));
}

# return a single row as an array (or a single value in a scalar context)
sub db_query_value {
    my ($sql) = @_;
    return get_dbh()->selectrow_array($sql);
}

sub db_get_hashes {
    my ($sql, $params) = @_;
    $params ||= [];
    $params = [$params] if $params && !ref($params);
    my $sth = get_dbh()->prepare($sql);
    $sth->execute(@$params);
    my $rows = [];
    while(my $row = $sth->fetchrow_hashref()) {push(@$rows, $row);}
    # $sth->finish();
    return $rows;
}

sub db_get_hash {
    my ($sql, $params) = @_;
    $params ||= [];
    $params = [$params] if $params && !ref($params);
    return get_dbh()->selectrow_hashref($sql, undef, @$params);
}



1;
