Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created December 28, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/3c19fad3e02b771b84ddda7fc7a7b540 to your computer and use it in GitHub Desktop.
Save Xliff/3c19fad3e02b771b84ddda7fc7a7b540 to your computer and use it in GitHub Desktop.

Given a specific problem in Red (see https://www.irccloud.com/pastebin/WeSpYBEL/...

This script:

#!/usr/bin/env perl6

use v6;

use lib <lib ../../lib>;
use Red;

model Person {...}

model Post is rw {
    has Int         $.id        is serial;
    has Int         $.author-id is referencing{ Person.id };
    has Str         $.title     is column{ :unique };
    has Str         $.body      is column;
    has Person      $.author    is relationship{ .author-id };
    has Bool        $.deleted   is column = False;
    has DateTime    $.created   is column .= now;
    has Set         $.tags      is column{
        :type<string>,
        :deflate{ .keys.join: "," },
        :inflate{ set(.split: ",") }
    } = set();
    method delete { $!deleted = True; self.^save }
}

model Person is rw {
    has Int  $.id            is serial;
    has Str  $.name          is column;
    has Post @.posts         is relationship{ .author-id };
    method active-posts { @!posts.grep: not *.deleted }
}

my $*RED-DEBUG = True;
my $*RED-DB = database("SQLite");

say "✓ Creating tables for Person and Post";
Person.^create-table;
Post.^create-table;

say "✓ Creating a Person";
my $p  = Person.^create: :name<Fernando>;

say "✓ Creating a blog Post";
my $post = $p.posts.create: :title("Red's commit"), :body("Merge branch 'master' of https://github.com/FCO/Red");

$p.posts.create: :title("Another commit"), :body("Blablabla"), :tags(set <bla ble>);

say "✓ Available post title(s) → ", $p.posts.map: *.title;

say "✘ Deleting Post";
$post.delete;

say "✓ Available post id(s) → ", $p.active-posts.map: *.id;

say "✓ Date Inflated → ", $p.posts.head.created.^name;

say "✓ Data with custom inflator → ", $p.posts.map: *.tags;

Gives this output.

✓ Creating tables for Person and Post
SQL : CREATE TABLE person(
   id integer NOT NULL primary key AUTOINCREMENT,
   name varchar(255) NOT NULL
)
SQL : CREATE TABLE post(
   id integer NOT NULL primary key AUTOINCREMENT,
   created varchar(32) NOT NULL,
   body varchar(255) NOT NULL,
   deleted integer NOT NULL,
   tags varchar(255) NOT NULL,
   title varchar(255) NOT NULL,
   author_id integer NULL references person(id),
   UNIQUE (title)
)
✓ Creating a Person
SQL : INSERT INTO person(
   name
)
VALUES(
   'Fernando'
)
SQL : SELECT
   person.id , person.name 
FROM
   person
WHERE
   _rowid_ = last_insert_rowid()
LIMIT 1
✓ Creating a blog Post
SQL : INSERT INTO post(
   tags,
   created,
   deleted,
   author_id,
   title,
   body
)
VALUES(
   '',
   '2018-12-28T14:40:34.790420-05:00',
   0,
   1,
   'Red''s commit',
   'Merge branch ''master'' of https://github.com/FCO/Red'
)
SQL : SELECT
   post.id , post.created , post.body , post.deleted , post.tags , post.title , post.author_id as "author-id"
FROM
   post
WHERE
   _rowid_ = last_insert_rowid()
LIMIT 1
SQL : INSERT INTO post(
   author_id,
   title,
   body,
   tags,
   created,
   deleted
)
VALUES(
   1,
   'Another commit',
   'Blablabla',
   'bla,ble',
   '2018-12-28T14:40:34.815376-05:00',
   0
)
SQL : SELECT
   post.id , post.created , post.body , post.deleted , post.tags , post.title , post.author_id as "author-id"
FROM
   post
WHERE
   _rowid_ = last_insert_rowid()
LIMIT 1
SQL : SELECT
   post.title as "data"
FROM
   post
WHERE
   post.author_id = 1
✓ Available post title(s) → (Red's commit Another commit)
✘ Deleting Post
SQL : UPDATE post SET
   deleted = 1
WHERE id = 1

SQL : SELECT
   post.id as "data"
FROM
   post
WHERE
   post.author_id = 1 AND (post.deleted == 0 OR post.deleted IS NULL)
✓ Available post id(s) → (2)
SQL : SELECT
   post.id , post.created , post.body , post.deleted , post.tags , post.title , post.author_id as "author-id"
FROM
   post
WHERE
   post.author_id = 1
✓ Date Inflated → DateTime
SQL : SELECT
   post.tags as "data"
FROM
   post
WHERE
   post.author_id = 1
✓ Data with custom inflator → (set() set(bla ble))

Problem solved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment