· 8 years ago · Mar 27, 2018, 09:06 PM
1 #!/usr/bin/env perl6
2 2
3 3 use v6;
4 4 use DBIish;
5 5
6 6 constant DB = 'budgetpro.sqlite3';
7 7 my $dbh = DBIish.connect('SQLite', database => DB);
8 8
9 9 #$dbh.do('drop table if exists Essential, Savings, Personal');
10 10
11 11 sub create-schema {
12 12 $dbh.do(qq:to/SCHEMA/);
13 13 create table if not exists Essential(
14 14 id integer primary key not null,
15 15 name varchar not null,
16 16 quantity integer not null,
17 17 price numeric(5,2) not null,
18 18 description varchar not null,
19 19 date timestamp not null default (datetime('now'))
20 20 );
21 21 create table is not exists Savings(
22 22 id integer primary key not null,
23 23 name varchar not null,
24 24 quantity integer not null,
25 25 price numeric(5,2) not null,
26 26 description varchar not null,
27 27 date timestamp not null default (datetime('now'))
28 28 );
29 29 create table if not exists Personal(
30 30 id integer primary key not null,
31 31 name varchar not null,
32 32 quantity integer not null,
33 33 price numeric(5,2) not null,
34 34 description varchar not null,
35 35 date timestamp not null default (datetime('now'))
36 36 );
37 37 SCHEMA
38 38 }
39 39
40 40 create-schema;
41 41
42 42 class Item {
43 43 has $!table = 'Essential';
44 44 has $.name;
45 45 has $.quantity;
46 46 has $.price;
47 47 has $.description is rw;
48 48 has $.timestamp;
49 49 has Str $.notes is rw;
50 50 method notes() { "$!notes\n" };
51 51
52 52 method insert($name, $quantity?, $price?, $description?, $timestamp?) {
53 53 my $sth = $dbh.prepare("insert into Essential(name,quantity,price,description,date) values (?,?,?,?,?)");
54 54 $sth.execute($name, $quantity, $price, $description, $timestamp);
55 55
56 56 say "Inserted $name, $quantity, $price, $description, $timestamp";
57 57 }
58 58 }
59 59
60 60 class Essential is Item {
61 61 method greet($me: $person) {
62 62 say "Hi, I am $me.^name(), nice to meet you, $person";
63 63 }
64 64 }
65 65
66 66 class Savings is Item {
67 67 }
68 68
69 69 class Personal is Item {
70 70 }
71 71
72 72 my $food = Essential.new(
73 73 name => 'Apple',
74 74 price => .99,
75 75 quantity => 2,
76 76 notes => 'An apple a day keeps the doctor away'
77 77 );
78 78
79 79 say $food.name;
80 80 say $food.notes;
81 81 Essential.new.greet('Eggman');
82 82 say '';
83 83
84 84 my $test = Item.new();
85 85
86 86 $test.insert("Cheese", 2, 1.99, 'Block of cheddar', Date.new(now));
87 87
88 88 say $test.name;