- Timestamp:
- 02/09/13 21:02:43 (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/BookmarkApp.pm
r13 r15 6 6 use CGI::Application::Plugin::TT; 7 7 8 use DBI;9 8 use Encode; 10 9 use JSON; … … 18 17 $base_uri->path($ENV{SCRIPT_NAME} . '/'); 19 18 20 my $dbname = 'new.db'; 21 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1 }); 19 my $dbname = 'fk.db'; 22 20 my $bookmarks = Bookmarks->new({ 23 db h => $dbh,21 dbname => $dbname, 24 22 base_uri => $base_uri->canonical, 25 23 }); -
trunk/Bookmarks.pm
r13 r15 1 1 package Bookmarks; 2 2 3 use Class::Accessor 'antlers';3 use Moose; 4 4 use SQL::Interp qw{:all}; 5 5 use Bookmark; 6 6 7 has dbh => ( is => 'r o' );7 has dbh => ( is => 'rw' ); 8 8 has base_uri => ( is => 'ro' ); 9 10 sub 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 } 9 26 10 27 sub get_bookmark { … … 91 108 my $title = $bookmark->{title}; 92 109 #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}; 94 113 95 114 # create an entry for the resource … … 110 129 111 130 # 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); 113 135 eval { 114 $sth_bookmark->execute( $uri, $ctime, $mtime);136 $sth_bookmark->execute(@bind_bookmark); 115 137 }; 116 138 if ($@) { -
trunk/bkmk
r14 r15 2 2 use strict; 3 3 4 use DBI;5 4 use YAML; 6 5 use Bookmarks; 6 use Getopt::Long; 7 7 8 my $dbname = 'new.db'; 8 GetOptions( 9 'file|f=s' => \my $DBNAME, 10 ); 9 11 10 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1, PrintError => 0 });12 die "Usage: $0 --file <dbname> <command>\n" unless $DBNAME; 11 13 12 14 my $bookmarks = Bookmarks->new({ 13 db h => $dbh,15 dbname => $DBNAME, 14 16 }); 15 17 … … 34 36 # TODO: coordinate this commandline script with the CGI app 35 37 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 }, 37 46 ); 38 47 -
trunk/bookmarks.sql
r2 r15 8 8 create table bookmarks ( 9 9 id integer primary key, 10 uri varchar ,10 uri varchar references resources(uri) on update cascade, 11 11 ctime integer, -- creation time of the bookmark 12 12 mtime integer, -- modification time of the bookmark … … 34 34 -- TODO: machine-tag style tags? e.g. format:video or creator:NASA; implicit tag prefix is "subject:" 35 35 create table tags ( 36 uri varchar ,36 uri varchar references resources(uri) on update cascade, 37 37 tag varchar, 38 38 constraint unique_tag primary key (uri, tag)
Note: See TracChangeset
for help on using the changeset viewer.