diff options
author | dakkar <dakkar@thenautilus.net> | 2016-10-16 14:26:21 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2016-10-16 14:26:21 +0100 |
commit | e5d50fa5293ca55a5fea23be344c524c76e007ae (patch) | |
tree | 62cce48e457fbc33cfa5a9fb71d3a18fbdbdde91 /lib/AniDB/Datastore.pm | |
parent | rough skeleton of the script (diff) | |
download | anidb-renamer-e5d50fa5293ca55a5fea23be344c524c76e007ae.tar.gz anidb-renamer-e5d50fa5293ca55a5fea23be344c524c76e007ae.tar.bz2 anidb-renamer-e5d50fa5293ca55a5fea23be344c524c76e007ae.zip |
more rough pieces
Diffstat (limited to 'lib/AniDB/Datastore.pm')
-rw-r--r-- | lib/AniDB/Datastore.pm | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/lib/AniDB/Datastore.pm b/lib/AniDB/Datastore.pm new file mode 100644 index 0000000..8db4e16 --- /dev/null +++ b/lib/AniDB/Datastore.pm @@ -0,0 +1,97 @@ +package AniDB::Datastore; +use 5.024; +use experimental 'signatures'; +use Moo; +use Path::Tiny; +use AniDB::Hashing; +use namespace::clean; + +has database => ( is => 'ro', required => 1 ); +has dbh => ( is => 'ro', lazy => 1 ); + +sub _build_dbh($self) { + require DBI; + return DBI->connect( + 'dbi:SQLite:dbname='.$self->database, + '', '', + { + AutoCommit => 1, + RaiseError => 1, + PrintError => 0, + }, + ); +} + +sub update($self,$path) { + my $stat = $path->stat; + + return 0 unless $self->_has_changed($path,$stat); + my $hash = AniDB::Hashing::hash_fd($path->openr_raw); + $self->_update_path_info( + $path => { + size => $stat->size, + mtime => $stat->mtime, + hash => $hash, + }, + ); + return 1; +} + +sub _has_changed($self,$path,$stat) { + my $path_info = $self->path_info_for($path); + return ( + $path_info->{size} == $stat->size + && $path_info->{mtime} == $stat->mtime + ); +} + +sub rename($self,$path,$new_path) { + $self->dbh->do( + q{UPDATE path_info SET name=? WHERE name=?}, + $path->stringify,$new_path->stringify, + ); +} + +sub full_info_for($self,$path) { + my $path_info = $self->path_info_for($path); + my $episode_info = $self->episode_info_for( + $path_info, + ); + my $anime_info = $self->anime_info_for( + $episode_info, + ); + return { + path => $path_info, + episode => $episode_info, + anime => $anime_info, + }; +} + +sub path_info_for($self,$path) { + my $ret = $self->dbh->selectall_arrayref( + qr{SELECT * FROM path_info WHERE name=?}, + { Slice => {} }, + $path->stringify, + ); + return $ret->[0]; +} + +sub episode_info_for($self,$args) { + my $ret = $self->dbh->selectall_arrayref( + qr{SELECT * FROM episode_info WHERE hash=? AND size=?}, + { Slice => {} }, + @{$args}{qw(hash size)}, + ); + return $ret->[0]; +} + +sub anime_info_for($self,$args) { + my $ret = $self->dbh->selectall_arrayref( + qr{SELECT * FROM anime_info WHERE aid=?}, + { Slice => {} }, + @{$args}{qw(aid)}, + ); + return $ret->[0]; +} + +1; |