Changeset 28 in bookmarks


Ignore:
Timestamp:
05/23/13 01:08:27 (12 years ago)
Author:
peter
Message:

only update the mtime of an existing bookmark if its tags change

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Bookmarks.pm

    r26 r28  
    154154    my $uri = $bookmark->{uri}; 
    155155    my $title = $bookmark->{title}; 
    156     #TODO: accept a ctime or mtime 
    157156    my $ctime = $bookmark->{ctime} || time; 
    158157    my $mtime = $bookmark->{mtime} || $ctime; 
     
    176175 
    177176    # create the bookmark 
     177    my $bookmark_exists = 0; 
    178178    my ($sql_bookmark, @bind_bookmark) = sql_interp( 
    179179        'insert into bookmarks', { ($id ? (id => $id) : ()), uri => $uri, ctime => $ctime, mtime => $mtime } 
     
    186186        if ($@ =~ /column uri is not unique/) { 
    187187            # this is not truly an error condition; the bookmark was already there 
    188             # update the mtime instead 
    189             # TODO: only update mtime if the tag list is changed? 
    190             my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?'); 
    191             $sth_update->execute($mtime, $uri); 
     188            # set this flag so that later we can update the mtime if tags change 
     189            $bookmark_exists = 1; 
    192190        } else { 
    193191            die $@; 
     
    195193    } 
    196194 
     195    my $changed_tags = 0; 
    197196    my %new_tags = map { $_ => 1 } @{ $bookmark->{tags} }; 
    198197    my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ? and tag = ?'); 
     
    204203            # if a current tag is not in the new tags, remove it from the database 
    205204            $sth_delete_tag->execute($uri, $tag); 
     205            $changed_tags++; 
    206206        } else { 
    207207            # if a new tag is already in the database, remove it from the list of tags to add 
     
    211211    for my $tag (keys %new_tags) { 
    212212        $sth_insert_tag->execute($uri, $tag); 
    213     } 
    214  
    215 =begin 
    216  
    217     # clear all tags 
    218     my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ?'); 
    219     $sth_delete_tag->execute($uri); 
    220     my $sth_tag = $self->dbh->prepare('insert into tags (uri, tag) values (?, ?)'); 
    221     for my $tag (@{ $bookmark->{tags} }) { 
    222         #print $tag, "\n"; 
    223         # prevent duplicate (uri,tag) pairs in the database 
    224         # TODO: should POST with a set of tags ever remove tags? 
    225         eval { 
    226             $sth_tag->execute($uri, $tag); 
    227         }; 
    228         if ($@) { 
    229             if ($@ =~ /columns uri, tag are not unique/) { 
    230                 # this is not truly an error condition; the tag was already there 
    231             } else { 
    232                 die $@; 
    233             } 
    234         } 
    235     } 
    236  
    237 =cut 
     213        $changed_tags++; 
     214    } 
     215 
     216    if ($bookmark_exists && $changed_tags) { 
     217        # update the mtime if the bookmark already existed but the tags were changed 
     218        my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?'); 
     219        $sth_update->execute($mtime, $uri); 
     220    } 
    238221 
    239222    # return the newly created or updated bookmark 
Note: See TracChangeset for help on using the changeset viewer.