]> git.ktnx.net Git - mpd-feeder.git/blobdiff - lib/App/MPD/Feeder/DB.pm
move 'connect' method next to the 'disconnect' method
[mpd-feeder.git] / lib / App / MPD / Feeder / DB.pm
index 62164364e167f6415d4c0538d0443cd2b8240687..ace454c5ef1f305a75bbc27b051268a86a592c18 100644 (file)
@@ -264,18 +264,6 @@ SQL
         );
     }
 
-    method connect {
-        return if $db;
-
-        $db = DBI->connect( "dbi:Pg:dbname=" . $opt->db_path,
-            $opt->db_user, $opt->db_password,
-            { RaiseError => 1, PrintError => 0, AutoCommit => 1 } );
-
-        $log->info( "Connected to database " . $opt->db_path );
-        $generation = $self->get_option('generation');
-        $log->debug("DB generation is $generation");
-    }
-
     method walk_unwanted_artists($callback) {
         $self->connect;
 
@@ -293,6 +281,71 @@ SQL
         return $count;
     }
 
+    method add_unwanted_album($album, $artist) {
+        $self->connect;
+
+        try {
+            $db->do(
+                <<'SQL',
+INSERT INTO unwanted_albums(album, artist, generation)
+VALUES($1, $2, $3)
+SQL
+                undef, $album, $artist, $generation
+            );
+            return 1;
+        }
+        catch {
+            my $err = $@;
+
+            $log->debug("PostgreSQL error: $err");
+            $log->debug( "SQLSTATE = " . $db->state );
+            return 0 if $db->state eq '23505';
+
+            die $err;
+        }
+    }
+
+    method del_unwanted_album($album, $artist) {
+        $self->connect;
+
+        return 1 == $db->do(
+            <<'SQL',
+DELETE FROM unwanted_albums
+WHERE album = $1 AND artist = $2
+SQL
+            undef, $album, $artist
+        );
+    }
+
+    method walk_unwanted_albums($callback) {
+        $self->connect;
+
+        my $count = 0;
+
+        my $sth = $db->prepare('SELECT album, artist FROM unwanted_albums ORDER BY 2, 1');
+        my ( $album, $artist );
+        $sth->execute;
+        $sth->bind_columns( \$album, \$artist );
+        while ( $sth->fetchrow_arrayref ) {
+            $count++;
+            $callback->($album, $artist);
+        }
+
+        return $count;
+    }
+
+    method connect {
+        return if $db;
+
+        $db = DBI->connect( "dbi:Pg:dbname=" . $opt->db_path,
+            $opt->db_user, $opt->db_password,
+            { RaiseError => 1, PrintError => 0, AutoCommit => 1 } );
+
+        $log->info( "Connected to database " . $opt->db_path );
+        $generation = $self->get_option('generation');
+        $log->debug("DB generation is $generation");
+    }
+
     method disconnect {
         return unless $db;