]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/DB/Util.pm
556524415a177236f6c818db3c4eb9ecd20bd034
[debbugs.git] / Debbugs / DB / Util.pm
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>.
5
6 package Debbugs::DB::Util;
7
8 =head1 NAME
9
10 Debbugs::DB::Util -- Utility routines for the database
11
12 =head1 SYNOPSIS
13
14
15 =head1 DESCRIPTION
16
17
18 =head1 BUGS
19
20 None known.
21
22 =cut
23
24 use warnings;
25 use strict;
26 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
27 use base qw(Exporter);
28
29 BEGIN{
30      ($VERSION) = q$Revision$ =~ /^Revision:\s+([^\s+])/;
31      $DEBUG = 0 unless defined $DEBUG;
32
33      @EXPORT = ();
34      %EXPORT_TAGS = (select => [qw(select_one)],
35                     );
36      @EXPORT_OK = ();
37      Exporter::export_ok_tags(keys %EXPORT_TAGS);
38      $EXPORT_TAGS{all} = [@EXPORT_OK];
39 }
40
41 =head2 select
42
43 Routines for select requests
44
45 =over
46
47 =item select_one
48
49         select_one($dbh,$sql,@bind_vals)
50
51 Returns the first column from the first row returned from a select statement
52
53 =cut
54
55 sub select_one {
56     my ($dbh,$sql,@bind_vals) = @_;
57     my $sth = $dbh->
58         prepare_cached($sql,
59                       {dbi_dummy => __FILE__.__LINE__ })
60         or die "Unable to prepare statement: $sql";
61     $sth->execute(@bind_vals) or
62         die "Unable to select one: ".$dbh->errstr();
63     my $results = $sth->fetchall_arrayref([0]);
64     $sth->finish();
65     return (ref($results) and ref($results->[0]))?$results->[0][0]:undef;
66 }
67
68
69 =back
70
71 =cut
72
73 1;
74
75
76 __END__