Skip to content
Browse files

add some stub classes, and the untested beginnings of a micro ORM (un…

…tested)
  • Loading branch information...
1 parent 72caa8a commit 66846ff8c74862a4bc1499fd879e804191d2af2e @moritz committed Sep 28, 2012
Showing with 103 additions and 0 deletions.
  1. +10 −0 lib/Soonish/Album.pm
  2. +12 −0 lib/Soonish/Common.pm
  3. +8 −0 lib/Soonish/Concert.pm
  4. +5 −0 lib/Soonish/Link.pm
  5. +3 −0 lib/Soonish/Schema.pm
  6. +65 −0 lib/Soonish/Table.pm
View
10 lib/Soonish/Album.pm
@@ -0,0 +1,10 @@
+use v6;
+use Soonish::Common;
+
+class Soonish::Album does Soonish::Common {
+ has $.artist;
+ has $.name;
+ has $.release-date;
+}
+
+# vim: ft=perl6
View
12 lib/Soonish/Common.pm
@@ -0,0 +1,12 @@
+use v6;
+use Soonish::Table;
+
+# TODO: Really need a better name for this;
+role Soonish::Common does Soonish::Table {
+ has $.entered-by;
+ has $.creation-date = now;
+ # schould really be multiple links, but the "ORM" can't handle that yet
+ has $.link;
+}
+
+# vim: ft=perl6
View
8 lib/Soonish/Concert.pm
@@ -0,0 +1,8 @@
+use v6;
+use Soonish::Common;
+
+class Soonish::Concert does Soonish::Common {
+ has $.artist;
+ has $.date;
+ has $.location;
+}
View
5 lib/Soonish/Link.pm
@@ -0,0 +1,5 @@
+class Soonish::Link {
+ # TODO: also implement local links
+ has $.url;
+ has $.text;
+}
View
3 lib/Soonish/Schema.pm
@@ -0,0 +1,3 @@
+class Soonish::Schema {
+ has $.dbh;
+}
View
65 lib/Soonish/Table.pm
@@ -0,0 +1,65 @@
+use v6;
+
+role Soonish::Table {
+ has Int $.id;
+ has $._table;
+ has $._schema;
+
+ method attributes {
+ self.^attributes.grep({ .name.substr(0,1) ne '_' }).map({; $.name => self."$_.name()"()}).hash;
+ }
+
+ method insert-or-create(Bool :$recursive) {
+ self.id ?? self.update(:$recursive) !! self.insert(:$recursive)
+ }
+
+ method insert(Bool :$recursive) {
+ my %a := self.attributes;
+ my $id = %a.delete('id');
+ unless $id {
+ die "Cannot insert an object that already has an id!";
+ }
+ my $dbh = $._schema.dbh;
+ my $sql = join ' ',
+ "INSERT INTO $dbh.quote-identifier($.table) ( ",
+ %a.keys.map({$dbh.quote-identifier($_)}).join(', '),
+ ') VALUES (',
+ ('?' xx %a.elems).join(', '),
+ ') RETURNING id';
+ my $sth = $dbh.prepare($sql);
+ if $recursive {
+ $sth.execute(%a.values.map({ $_ ~~ Soonish::Table ?? .insert-or-update(:recursive).id !! $_ }));
+ }
+ else {
+ $sth.execute(%a.values.map({ $_ ~~ Soonish::Table ?? .id || .insert.id !! $_}));
+ }
+ ($id) = $sth.fetchrow;
+ $sth.finish;
+ $.id = $id;
+ self;
+ }
+
+ method update(Bool :$recursive) {
+ my %a := self.attributes;
+ my $id = %a.delete('id');
+ unless $id.defined && $id != 0 {
+ die "Can only update an object that already has an id!";
+ }
+ my $dbh = $._schema.dbh;
+ my $sql = join ' ',
+ "UPDATE $dbh.quote-identifier($.table) ( ",
+ %a.keys.map({$dbh.quote-identifier($_)}).join(', '),
+ ') VALUES (',
+ ('?' xx %a.elems).join(', '),
+ ')';
+ my $sth = $dbh.prepare($sql);
+ if $recursive {
+ $sth.execute(%a.values.map({ $_ ~~ Soonish::Table ?? (.insert-or-update.id) !! $_ } ));
+ }
+ else {
+ $sth.execute(%a.values.map({ $_ ~~ Soonish::Table ?? (.id || .insert.id) !! $_ } ));
+ }
+ $sth.finish;
+ self;
+ }
+}

0 comments on commit 66846ff

Please sign in to comment.
Something went wrong with that request. Please try again.