1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information.
4 # Copyright 2017 by Don Armstrong <don@donarmstrong.com>.
6 package Debbugs::DB::Util;
10 Debbugs::DB::Util -- Utility routines for the database
26 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
27 use base qw(Exporter);
30 ($VERSION) = q$Revision$ =~ /^Revision:\s+([^\s+])/;
31 $DEBUG = 0 unless defined $DEBUG;
34 %EXPORT_TAGS = (select => [qw(select_one)],
35 execute => [qw(prepare_execute)]
38 Exporter::export_ok_tags(keys %EXPORT_TAGS);
39 $EXPORT_TAGS{all} = [@EXPORT_OK];
44 Routines for select requests
50 select_one($dbh,$sql,@bind_vals)
52 Returns the first column from the first row returned from a select statement
57 my ($dbh,$sql,@bind_vals) = @_;
60 {dbi_dummy => __FILE__.__LINE__ })
61 or die "Unable to prepare statement: $sql";
62 $sth->execute(@bind_vals) or
63 die "Unable to select one: ".$dbh->errstr();
64 my $results = $sth->fetchall_arrayref([0]);
66 return (ref($results) and ref($results->[0]))?$results->[0][0]:undef;
71 prepare_execute($dbh,$sql,@bind_vals)
73 Prepares and executes a statement
78 my ($dbh,$sql,@bind_vals) = @_;
81 {dbi_dummy => __FILE__.__LINE__ })
82 or die "Unable to prepare statement: $sql";
83 $sth->execute(@bind_vals) or
84 die "Unable to execute statement: ".$dbh->errstr();