Skip to content

Commit

Permalink
makes bookmarks datebase file if it does not exist
Browse files Browse the repository at this point in the history
uses sqlite3 executable instead of the DBI api should be changed at
some point.
  • Loading branch information
hidroto committed Aug 4, 2015
1 parent 6c187d6 commit 6d08238
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions bookmarkorgan.pl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Readonly;
Readonly my $DEFAULT_BOOKMARK_DATEBASE => 'bookmarks.db';
Readonly my $EMPTY => q[];
Readonly my $INPUT_PIPE_MODE => q[|-];
Readonly my $DESCRIPTION => <<"END"
organizes bookmarks.
END
Expand Down Expand Up @@ -77,7 +78,7 @@ END

sub main {
load_plugins();
my $bookmark_db = load_database();
my $bookmark_db = load_database($bookmark_database_file);

my $QUIT = qr{\A q(?:uit)? \z}i;
my %command_hash = (
Expand Down Expand Up @@ -145,10 +146,16 @@ sub load_plugins {
}

sub load_database {
my $db = DBI->connect(
"dbi:SQLite:$bookmark_database_file",
{ RaiseError => 1, AutoCommit => 0 }
);
my $db_filename = shift;
if ( not -e $db_filename ) {
open my $fh, $INPUT_PIPE_MODE, qq{sqlite3 $db_filename}
or croak "could not create $db_filename";
print {$fh} $BOOKMARKS_SQL_TEMPLATE
or croak "could not create $db_filename from SQL_TEMPLATE";
close $fh or croak 'could not close sqlite filehandle';
}
my $db = DBI->connect( "dbi:SQLite:$db_filename",
{ RaiseError => 1, AutoCommit => 0 } );
$db->do('PRAGMA foreign_keys = ON');
return $db;
}
Expand Down

0 comments on commit 6d08238

Please sign in to comment.