Changeset 15 in bookmarks for trunk/Bookmarks.pm


Ignore:
Timestamp:
02/09/13 21:02:43 (11 years ago)
Author:
peter
Message:
  • Bookmarks uses Moose instead of Class::Accessor
  • if not dbh is specified in the Bookmarks constructor, it can use a dbname parameter to create a SQLite DBH
  • the SQLite DBH created form dbname has the foreign_keys pragma set
  • added foreign key constraints to the bookmarks.sql table definitions for bookmarks and tags
  • added a required --file option to bkmk to specify the database file to use
  • added a load command to bkmk that loads bookmarks dumped as YAML using bkmk list
  • Bookmarks::add() can take mtime and id parameters (useful for reconstructing a database from the YAML dump of bkmk list)
  • BookmarkApp and bkmk no longer use DBI directly; just pass a dbname to the Bookmarks constructor
  • changed the default database for BookmarkApp to fk.db (schema from this revision's updated bookmarks.sql, with foreign keys)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Bookmarks.pm

    r13 r15  
    11package Bookmarks; 
    22 
    3 use Class::Accessor 'antlers'; 
     3use Moose; 
    44use SQL::Interp qw{:all}; 
    55use Bookmark; 
    66 
    7 has dbh      => ( is => 'ro' ); 
     7has dbh      => ( is => 'rw' ); 
    88has base_uri => ( is => 'ro' ); 
     9 
     10sub BUILD { 
     11    my $self = shift; 
     12    my $args = shift; 
     13 
     14    if (!$self->dbh) { 
     15        if ($args->{dbname}) { 
     16            require DBI; 
     17            $self->dbh(DBI->connect("dbi:SQLite:dbname=$$args{dbname}", "", "", { RaiseError => 1, PrintError => 0 })); 
     18            # enable foreign key support (requires DBD::SQLite 1.26_05 or above (sqlite 3.6.19 or above)) 
     19            $self->dbh->do('pragma foreign_keys = on;'); 
     20        } else { 
     21            #TODO: figure out how to make croak play nice with Moose to get the user-visible caller line 
     22            die "No dbh or dbname specified in the constructor"; 
     23        } 
     24    } 
     25} 
    926 
    1027sub get_bookmark { 
     
    91108    my $title = $bookmark->{title}; 
    92109    #TODO: accept a ctime or mtime 
    93     my $mtime = my $ctime = $bookmark->{ctime} || time; 
     110    my $ctime = $bookmark->{ctime} || time; 
     111    my $mtime = $bookmark->{mtime} || $ctime; 
     112    my $id = $bookmark->{id}; 
    94113 
    95114    # create an entry for the resource 
     
    110129 
    111130    # create the bookmark 
    112     my $sth_bookmark = $self->dbh->prepare('insert into bookmarks (uri, ctime, mtime) values (?, ?, ?)'); 
     131    my ($sql_bookmark, @bind_bookmark) = sql_interp( 
     132        'insert into bookmarks', { ($id ? (id => $id) : ()), uri => $uri, ctime => $ctime, mtime => $mtime } 
     133    ); 
     134    my $sth_bookmark = $self->dbh->prepare($sql_bookmark); 
    113135    eval { 
    114         $sth_bookmark->execute($uri, $ctime, $mtime); 
     136        $sth_bookmark->execute(@bind_bookmark); 
    115137    }; 
    116138    if ($@) { 
Note: See TracChangeset for help on using the changeset viewer.