]> xenbits.xensource.com Git - people/aperard/emesinae.git/commitdiff
Switch to Perl DBI selectrow_foo interface
authorIan Campbell <ian.campbell@citrix.com>
Tue, 2 Jul 2013 15:07:47 +0000 (16:07 +0100)
committerIan Campbell <ian.campbell@citrix.com>
Tue, 2 Jul 2013 15:21:37 +0000 (16:21 +0100)
Rather than open coding it.

Also remove all statement->finish calls, they are almost never required.

Emesinae/Bug.pm
Emesinae/Common.pm
Emesinae/Message.pm
TODO

index af618138b88bc1473d6d875183e5df10475e08de..cfac17e995f4cf235ebf19029e29fa18df0cf5fc 100644 (file)
@@ -21,13 +21,10 @@ sub new {
         $row = $args{Fields};
     }
     elsif ( $args{ID} ) {
-        my $sth = $dbh->prepare( "
+        $row = $dbh->selectrow_arrayref( "
             SELECT $SELECT_FIELDS FROM bugs WHERE bug_id = ?
-        " );
-        $sth->execute( $args{ID} ) or die "Failed to lookup bug";
-        $row = $sth->fetchrow_arrayref;
+        ", {}, $args{ID} );
         return undef unless $row;
-        $sth->finish;
     }
     else { die "Emesinae::Bug->new needs ID or Fields"; }
 
@@ -97,7 +94,6 @@ sub _set {
          WHERE bug_id = ?
     " );
     $sth->execute( $value, $self->{id} ) or die "Setting $field";
-    $sth->finish;
 
     $self->{$field} = $value;
 }
@@ -128,16 +124,12 @@ sub set_severity {
 sub _lookup_tag($$$) {
     my ( $self, $ns, $tag ) = @_;
 
-    my $sth = $self->{dbh}->prepare( "
+    my $row = $self->{dbh}->selectrow_arrayref( "
         SELECT tag_id
           FROM tags
          WHERE namespace = ?
            AND name = ?
-    " );
-    $sth->execute( $ns, $tag ) or die "Unable to find tag $ns:$tag";
-    my $row = $sth->fetchrow_arrayref;
-    $row or die "Unable to find tag $ns:$tag";
-    $sth->finish;
+    ", {}, $ns, $tag ) || die "Unable to find tag $ns:$tag";
 
     return $row->[0];
 }
@@ -154,8 +146,6 @@ sub set_tag($$$) {
     $sth->execute( $self->{id}, $tag_id )
       or die "Setting tag $ns:$tag on Bug #" . $self->{id};
     $sth->rows == 1 or die "Failed to set tag $ns:$tag on Bug #" . $self->{id};
-
-    $sth->finish;
 }
 
 sub clear_tag($$$) {
@@ -170,26 +160,19 @@ sub clear_tag($$$) {
     " );
     $sth->execute( $self->{id}, $tag_id )
       or die "Clearing tag $ns:$tag on Bug #" . $self->{id};
-
-    $sth->finish;
 }
 
 sub tags($$) {
     my ( $self, $ns ) = @_;
 
-    my $sth = $self->{dbh}->prepare( "
+    my $tref = $self->{dbh}->selectall_arrayref( "
         SELECT tags.name
           FROM tags,bug2tag
          WHERE tags.tag_id == bug2tag.tag_id
            AND tags.namespace == ?
            AND bug2tag.bug_id == ?
-    " );
-    $sth->execute( $ns, $self->{id} )
-      or die "Listing $ns tags on Bug #" . $self->{id};
+    ", {}, $ns, $self->{id} ) || die "Listing $ns tags on Bug #" . $self->{id};
 
-    # An array ref (rows) of array refs (columns)
-    my $tref = $sth->fetchall_arrayref();
-    $sth->finish;
     return map { $_->[0] } @{$tref};
 }
 
@@ -207,7 +190,6 @@ sub set_status {
 
     $sth->execute( $open ? "true" : "false", $self->{id} )
       or die "Failed to set status";
-    $sth->finish;
     $self->{open} = $open;
 }
 
@@ -236,7 +218,6 @@ sub addremove_subthread {
 
     $sth->execute( $add ? "true" : "false", $self->{id}, $m->{id} )
       or die "Failed to insert";
-    $sth->finish;
 }
 
 sub add_subthread {
index 6d5ef1cde1d9de5e5acec2867c91ca57ebf77f76..f5c58dd47b869a6d2d6709b137395163a36d2f97 100644 (file)
@@ -59,18 +59,14 @@ sub opendb () {
 sub list_tags($$) {
     my ( $dbh, $ns ) = @_;
 
-    my $sth = $dbh->prepare( "
+    my $tags = $dbh->selectall_arrayref( "
         SELECT name,active,advanced
           FROM tags
          WHERE namespace = ?
       ORDER BY sort_order DESC
-    " );
-    $sth->execute($ns) or die "Unable to obtain list of tags in namesapce $ns";
+    ", { Slice => {} }, $ns )
+      || die "Unable to obtain list of tags in namespace $ns";
 
-    # Gets an array ref of hash refs.
-    my $tags = $sth->fetchall_arrayref( {} );
-
-    $sth->finish;
     return @{$tags};
 }
 
index 659e2a627735b3b7cfb3a5d69c8cb13d1f476eaf..1a5cc512fe495cab0389f42f58567e397d0e6228 100644 (file)
@@ -14,25 +14,19 @@ sub lookup_id {
     my $dbh   = shift;
     my $id    = shift;
 
-    my $sth = $dbh->prepare(
+    my $row = $dbh->selectrow_arrayref(
         q{
         SELECT msgid_hdr_raw,present,inserttime,msgtype,msgdate_hdr_raw,msgfrom_hdr_raw,msgto_hdr_raw,msgcc_hdr_raw,subject_hdr_raw
           FROM messages
          WHERE message_id = ?
-    }
+    }, {}, $id
     );
 
-    $sth->execute($id);
-
-    my $row = $sth->fetch;
-
     return undef unless defined $row;
 
     my ( $msgid, $present, $inserted, $msgtype, $date, $from, $to, $cc, $subj )
       = @{$row};
 
-    $sth->finish;
-
     my $self = {
         dbh      => $dbh,
         present  => $present eq "true",
@@ -59,20 +53,14 @@ sub lookup_msgid {
 
     $args{Insert} = 0 unless exists $args{Insert};
 
-    my $sth = $dbh->prepare(
+    my $row = $dbh->selectrow_arrayref(
         q{
         SELECT message_id,present,inserttime,msgtype,msgdate_hdr_raw,msgfrom_hdr_raw,msgto_hdr_raw,msgcc_hdr_raw,subject_hdr_raw
           FROM messages
          WHERE msgid_hdr_raw = ?
-    }
+    }, {}, $msgid
     );
 
-    $sth->execute($msgid);
-
-    my $row = $sth->fetch;
-
-    $sth->finish;
-
     my ( $id, $present, $inserted, $msgtype, $date, $from, $to, $cc, $subj );
     if ( defined $row ) {
         ( $id, $present, $inserted, $msgtype, $date, $from, $to, $cc, $subj ) =
@@ -126,7 +114,6 @@ sub set_type {
          WHERE message_id = ?1}
     ) or die "prepare type update";
     $sth->execute( $self->{id}, $type ) or die "execute type update";
-    $sth->finish or die "finish type update";
 
     $self->{msgtype} = $type;
 }
@@ -164,7 +151,6 @@ sub update {
         $to   ? $to->stringify   : undef,
         $cc   ? $cc->stringify   : undef
     ) or die "execute update";
-    $sth->finish or die "finish update";
 
     my $irt = $header->get('in-reply-to');
     if ( defined $irt ) {
diff --git a/TODO b/TODO
index 8433c62196d8d679a3dd4c457d35ad15bc9ee01e..7242dfb612a2f3aba480eebf3e7ddcb33d365f8c 100644 (file)
--- a/TODO
+++ b/TODO
@@ -32,7 +32,6 @@ database:
    default.
  - Refactor email addresses into their own table and create references instead
    of inlining into the record. Should enable better searching.
- - Use Perl DBI select_* interface instead of prepare/executre/fetch/finish
 
 documentation:
  - have some