]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/DB/Util.pm
switch to compatibility level 12
[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                      execute => [qw(prepare_execute)]
36                     );
37      @EXPORT_OK = ();
38      Exporter::export_ok_tags(keys %EXPORT_TAGS);
39      $EXPORT_TAGS{all} = [@EXPORT_OK];
40 }
41
42 =head2 select
43
44 Routines for select requests
45
46 =over
47
48 =item select_one
49
50         select_one($dbh,$sql,@bind_vals)
51
52 Returns the first column from the first row returned from a select statement
53
54 =cut
55
56 sub select_one {
57     my ($dbh,$sql,@bind_vals) = @_;
58     my $sth = $dbh->
59         prepare_cached($sql,
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]);
65     $sth->finish();
66     return (ref($results) and ref($results->[0]))?$results->[0][0]:undef;
67 }
68
69 =item prepare_execute
70
71         prepare_execute($dbh,$sql,@bind_vals)
72
73 Prepares and executes a statement
74
75 =cut
76
77 sub prepare_execute {
78     my ($dbh,$sql,@bind_vals) = @_;
79     my $sth = $dbh->
80         prepare_cached($sql,
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();
85     $sth->finish();
86 }
87
88
89 =back
90
91 =cut
92
93 1;
94
95
96 __END__