· 8 years ago · Mar 29, 2018, 05:20 PM
1 3 use v6;
2 4 use DBIish;
3 5
4 6 constant DB = 'budgetpro.sqlite3';
5 7 my $dbh = DBIish.connect('SQLite', database => DB);
6 8
7 9 $dbh.do('drop table if exists Essential');
8 10
9 11 class Item {
10 12 has $!db;
11 13 has $!table = 'Essential';
12 14 has $.name;
13 15 has $.quantity;
14 16 has $.price;
15 17 has Str $.description is rw;
16 18 has $.timestamp;
17 19
18 20 method !db() {
19 21 return $!db if $!db;
20 22 $!db = DBIish.connect('SQLite', :database('budgetpro.sqlite3'));
21 23 self!create-schema();
22 24 return $!db;
23 25 }
24 26
25 27 method !create-schema {
26 28 $!db.do(qq:to/SCHEMA/);
27 29 create table if not exists Essential(
28 30 id integer primary key not null,
29 31 name varchar not null,
30 32 quantity integer not null,
31 33 price numeric(5,2) not null,
32 34 description varchar not null,
33 35 date timestamp default (datetime('now'))
34 36 );
35 37 SCHEMA
36 38 }
37 39
38 40 method insert() {
39 41 self!db.do(qq:to/INSERT/, $.name, $.quantity, $.price, $.description);
40 42 insert into $!table (name, quantity, price, description) values (?,?,?,?)
41 43 INSERT
42 44 say "Inserted $.name, $.quantity, $.price, $.description";
43 45 }
44 46
45 47 method select() {
46 48 self!db.do(qq:to/SELECT/, $.name, $.quantity, $.price, $.description);
47 49 select * from $!table
48 50 SELECT
49 51 }
50 52 }
51 53
52 54 class Essential is Item {
53 55 has $.x;
54 56 has $.y;
55 57
56 58 submethod BUILD(:$!x, :$!y) {
57 59 say "Instantiating...";
58 60 }
59 61
60 62 method invert {
61 63 self.new(x => - $.x, y => - $.y);
62 64 }
63 65 }
64 66
65 67 class Savings is Item {
66 68 }
67 69
68 70 class Personal is Item {
69 71 }
70 72
71 73 say "\t--";
72 74
73 75 my $item = Item.new(
74 76 name => 'Eggman',
75 77 quantity => 5,
76 78 price => 10.99,
77 79 description => "It's me!"
78 80 ).insert;
79 81
80 82 say $item.name;