Changeset 15 in bookmarks


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)
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/BookmarkApp.pm

    r13 r15  
    66use CGI::Application::Plugin::TT; 
    77 
    8 use DBI; 
    98use Encode; 
    109use JSON; 
     
    1817$base_uri->path($ENV{SCRIPT_NAME} . '/'); 
    1918 
    20 my $dbname = 'new.db'; 
    21 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1 }); 
     19my $dbname = 'fk.db'; 
    2220my $bookmarks = Bookmarks->new({ 
    23     dbh => $dbh, 
     21    dbname   => $dbname, 
    2422    base_uri => $base_uri->canonical, 
    2523}); 
  • 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 ($@) { 
  • trunk/bkmk

    r14 r15  
    22use strict; 
    33 
    4 use DBI; 
    54use YAML; 
    65use Bookmarks; 
     6use Getopt::Long; 
    77 
    8 my $dbname = 'new.db'; 
     8GetOptions( 
     9    'file|f=s' => \my $DBNAME, 
     10); 
    911 
    10 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1, PrintError => 0 }); 
     12die "Usage: $0 --file <dbname> <command>\n" unless $DBNAME; 
    1113 
    1214my $bookmarks = Bookmarks->new({ 
    13     dbh => $dbh, 
     15    dbname => $DBNAME, 
    1416}); 
    1517 
     
    3436        # TODO: coordinate this commandline script with the CGI app 
    3537        print Dump(\@resources); 
    36     } 
     38    }, 
     39    load => sub { 
     40        my ($src_file) = @_; 
     41        my $src_bookmarks = YAML::LoadFile($src_file); 
     42        for my $bookmark (@{ $src_bookmarks }) { 
     43            $bookmarks->add($bookmark); 
     44        } 
     45    }, 
    3746); 
    3847 
  • trunk/bookmarks.sql

    r2 r15  
    88create table bookmarks ( 
    99    id integer primary key, 
    10     uri varchar, 
     10    uri varchar references resources(uri) on update cascade, 
    1111    ctime integer, -- creation time of the bookmark 
    1212    mtime integer,  -- modification time of the bookmark 
     
    3434-- TODO: machine-tag style tags? e.g. format:video or creator:NASA; implicit tag prefix is "subject:" 
    3535create table tags ( 
    36     uri varchar, 
     36    uri varchar references resources(uri) on update cascade, 
    3737    tag varchar, 
    3838    constraint unique_tag primary key (uri, tag) 
Note: See TracChangeset for help on using the changeset viewer.