-
-
Notifications
You must be signed in to change notification settings - Fork 27
Time Tracker
Fernando Correa de Oliveira edited this page Mar 14, 2019
·
2 revisions
use Red;
model Person { ... }
model Track {
has UInt $!id is serial;
has UInt $!person-id is referencing{ Person.name };
has Person $.person is relationship{ .person-id };
has Instant $.time is column;
}
model Person {
has Str $.name is id;
has Track @.tracks is relationship{ .person-id }
method total {
@!tracks.sort(*.time).map(*.time).batch(2).map({ .[1] - .[0] }).sum
}
}
my $*RED-DB = database "SQLite";
Person.^create-table;
Track.^create-table;
my $*RED-DEBUG = True;
my \fernando = Person.^create: :name<Fernando>;
fernando.tracks.create: :time(now - 2000);
fernando.tracks.create: :time(now - 1000);
fernando.tracks.create: :time(now - 500);
fernando.tracks.create: :time(now);
say fernando.total
SQL : INSERT INTO person(
name
)
VALUES(
?
)
BIND: ["Fernando"]
SQL : SELECT
person.name
FROM
person
WHERE
_rowid_ = last_insert_rowid()
LIMIT 1
BIND: []
SQL : INSERT INTO track(
time,
person_id
)
VALUES(
?,
?
)
BIND: [Instant.from-posix(<1277758939041/823>, Bool::False), "Fernando"]
SQL : SELECT
track.person_id as "person-id", track.time , track.id
FROM
track
WHERE
_rowid_ = last_insert_rowid()
LIMIT 1
BIND: []
SQL : INSERT INTO track(
time,
person_id
)
VALUES(
?,
?
)
BIND: [Instant.from-posix(<1181500824934/761>, Bool::False), "Fernando"]
SQL : SELECT
track.person_id as "person-id", track.time , track.id
FROM
track
WHERE
_rowid_ = last_insert_rowid()
LIMIT 1
BIND: []
SQL : INSERT INTO track(
time,
person_id
)
VALUES(
?,
?
)
BIND: [Instant.from-posix(<1333652477626/859>, Bool::False), "Fernando"]
SQL : SELECT
track.person_id as "person-id", track.time , track.id
FROM
track
WHERE
_rowid_ = last_insert_rowid()
LIMIT 1
BIND: []
SQL : INSERT INTO track(
time,
person_id
)
VALUES(
?,
?
)
BIND: [Instant.from-posix(<6267702894198/4037>, Bool::False), "Fernando"]
SQL : SELECT
track.person_id as "person-id", track.time , track.id
FROM
track
WHERE
_rowid_ = last_insert_rowid()
LIMIT 1
BIND: []
SQL : SELECT
track.person_id as "person-id", track.time , track.id
FROM
track
WHERE
track.person_id = ?
BIND: ["Fernando"]
1500.02590750909277