FSpot-DbTool
view release on metacpan or search on metacpan
lib/FSpot/DbTool.pm view on Meta::CPAN
if( ! copy( $self->db_file(), $target ) ){
die( "Could not backup db_file: $!\n" );
}
}
=head2 search( %params )
Returns (an array of) rows (all columns) of matching entries
Usage:
$fs->search( table => $table,
search => [ [ 'filename', 'LIKE', '%123%' ], [ .... ] ] );
=cut
sub search{
my ( $self, %params ) = validated_hash(
\@_,
table => { isa => 'Str' },
search => { isa => 'ArrayRef[SearchEntry]', optional => 1 }
);
my( @where, @vals, %ids );
foreach my $entry( @{ $params{search} } ){
$self->column_must_exist( table => $params{table},
column => $entry->[0] );
push( @where, "$entry->[0] $entry->[1] ?" );
push( @vals, $entry->[2] );
}
# Get the entries from the photos table
my $sql = "SELECT * FROM $params{table}";
if( $#where >= 0 ){
$sql .= " WHERE " . join( ' AND ', @where );
}
my $sth = $self->dbh->prepare( $sql );
$sth->execute( @vals );
my( $row, @results );
while( $row = $sth->fetchrow_hashref ){
push( @results, $row );
}
$sth->finish();
return @results;
}
=head2 update_photo( %params )
Update a photo in the database
Usage:
$details = { 'filename' => $newname,
'base_uri' => $new_base_uri };
$fs->update_photo_version( photo_id => $id,
details => $details );
=cut
sub update_photo{
my ( $self, %params ) = validated_hash(
\@_,
photo_id => { isa => 'Int' },
details => { isa => 'NonEmptyHashRef' },
);
my( @cols, @vals );
foreach my $column( keys( %{ $params{details} } ) ){
$self->column_must_exist( table => 'photos',
column => $column );
push( @cols, "$column=?" );
push( @vals, $params{details}->{$column} );
}
my $sql = 'UPDATE photos SET ' . join( ', ', @cols ) . " WHERE id=?;";
push( @vals, $params{photo_id} );
$self->logger->debug( "Updating photo $params{photo_id} with details:\n " . Dump( $params{details} ) );
my $sth = $self->dbh->prepare( $sql );
$sth->execute( @vals );
$sth->finish();
}
=head2 update_photo_version( %params )
Update a version of a photo in the database
Usage:
$details = { 'filename' => $newname,
'base_uri' => $new_base_uri };
$fs->update_photo_version( photo_id => $id,
version_id => $version_id,
details => $details );
=cut
sub update_photo_version{
my ( $self, %params ) = validated_hash(
\@_,
photo_id => { isa => 'Int' },
version_id => { isa => 'Int' },
details => { isa => 'NonEmptyHashRef' },
);
my( @cols, @vals );
foreach my $column( keys( %{ $params{details} } ) ){
$self->column_must_exist( table => 'photo_versions',
column => $column );
push( @cols, "$column=?" );
push( @vals, $params{details}->{$column} );
}
my $sql = 'UPDATE photo_versions SET ' . join( ', ', @cols ) .
" WHERE photo_id=? AND version_id=?;";
push( @vals, $params{photo_id}, $params{version_id} );
my $sth = $self->dbh->prepare( $sql );
$self->logger->debug( "Updating photo_version $params{photo_id} with details:\n " . Dump( $params{details} ) );
$sth->execute( @vals );
$sth->finish();
}
=head2 add_tag( %params )
Add a tag.
Parent name is optional. If not defined, the tag will be attached to the root.
Usage:
$fs->add_tag( name => $name,
parent_name => $parent_name );
=cut
sub add_tag{
my ( $self, %params ) = validated_hash(
\@_,
name => { isa => 'Str' },
parent_name => { isa => 'Str', optional => 1 },
);
# If the parent was defined, try and find it
my $parent;
if( $params{parent_name} ){
my @result_parent = $self->search( table => 'tags',
search => [ [ 'name', '=', $params{parent_name} ] ] );
if( scalar( @result_parent ) == 0 ){
die( "Parent tag ($params{parent_name}) does not exist\n" );
}
$parent = $result_parent[0];
}
# If we found a parent, find the ID, otherwise just create it as a "root" tag
my( $sql, @vals );
if( $parent ){
if( $self->search( table => 'tags',
search => [ [ 'name', '=', $params{name} ], [ 'category_id', '=', $parent->{id} ] ] ) ){
( run in 1.543 second using v1.01-cache-2.11-cpan-39bf76dae61 )