From cdac5cd0d0eae09b9d2f36ab53907ccefed82fa8 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 23 Dec 2017 15:21:54 +0000 Subject: minimal db model --- lib/Ultramarine/Model/DB.pm6 | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/Ultramarine/Model/DB.pm6 (limited to 'lib/Ultramarine/Model/DB.pm6') diff --git a/lib/Ultramarine/Model/DB.pm6 b/lib/Ultramarine/Model/DB.pm6 new file mode 100644 index 0000000..cbc353d --- /dev/null +++ b/lib/Ultramarine/Model/DB.pm6 @@ -0,0 +1,42 @@ +use v6.d.PREVIEW; +use DBI::Async; +use Ultramarine::Model::DBMigration; + +class Ultramarine::Model::DB { + has $.db-driver is required; + has %.db-args is required; + + my @migrations = ( + -> $dbh { + $dbh.query(q:to/END/).finish; + CREATE TABLE songs ( + path TEXT PRIMARY KEY, + title TEXT, + artist TEXT + ); + END + }, + ); + + has $!dbh = do { + my DBI::Async $dbh .= new($!db-driver, |%!db-args); + my Ultramarine::Model::DBMigration $migration .= new(:$dbh,:@migrations); + $migration.ensure-schema; + $dbh; + }; + + method add-song(:$path!,:$title,:$artist) { + $!dbh.query(q:to/END/,$path,$title,$artist); + INSERT INTO songs(path,artist,title) + VALUES (?,?,?) + END + } + + method get-song($path) { + $!dbh.query(q:to/END/,$path).hash; + SELECT * + FROM songs + WHERE path=? + END + } +} -- cgit v1.2.3