Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map returning ResultSeq #25

Open
FCO opened this issue Aug 26, 2018 · 8 comments
Open

map returning ResultSeq #25

FCO opened this issue Aug 26, 2018 · 8 comments
Labels
hacktoberfest help wanted Extra attention is needed question Further information is requested

Comments

@FCO
Copy link
Owner

FCO commented Aug 26, 2018

Should Person.all.map: { .posts } return a ResultSeq of ResultSeqs?

@FCO FCO added help wanted Extra attention is needed question Further information is requested labels Aug 26, 2018
@FCO
Copy link
Owner Author

FCO commented Aug 26, 2018

What SQL should it generate?

@FCO
Copy link
Owner Author

FCO commented Aug 26, 2018

SELECT
   person.*,
   post.*
FROM
   person JOIN post ON(person.id = post.author_id)

@FCO
Copy link
Owner Author

FCO commented Aug 26, 2018

SELECT
   person.*,
   array_agg(post.*)
FROM
   person JOIN post ON(person.id = post.author_id)
GROUP BY
   person.id, person.name, ...

@FCO
Copy link
Owner Author

FCO commented Aug 27, 2018

SELECT
   array_agg(post.*)
FROM
   post
GROUP BY
   post.author_id

@FCO FCO changed the title map? map returning ResultSeq Dec 19, 2018
@jonathanstowe
Copy link
Contributor

For some reason I was thinking about this last night and came to the conclusion that actually it might be of least surprise to the user if it actually did something like :

SELECT
   post.*
FROM
   person JOIN post ON(person.id = post.author_id)

And returns a ResultSeq of Post.

Which of itself is fairly useless, until you have a grep before the map such as:

Person.^all.grep( ?*.active).map({ .posts})

And get:

SELECT
   post.*
FROM
   person JOIN post ON(person.id = post.author_id)
WHERE
   person.active

But just my ¢2

@FCO
Copy link
Owner Author

FCO commented Feb 12, 2019

O think that would be perfect for a .flatmap but I think it should return a Seq of Seqs...

Sent with GitHawk

@FCO
Copy link
Owner Author

FCO commented Feb 24, 2019

I had this problem today:

use Red;
my $*RED-DB = database "SQLite";
#my $*RED-DB = database "SQLite", :database</Users/fernando/test.db>;

model MongerPM { ... }

model Monger {
    has Int $!id             is serial;
    has Str $.name           is column;

    has MongerPM @.monger-pm is relationship{ .monger-id }

    method pms { @!monger-pm.map: *.pm }
}

model PMGroup is table<pm_group> {
    has Str $.city           is id;

    has MongerPM @!monger-pm is relationship{ .pm-id }

    method mongers { @!monger-pm.map: *.monger }
    method gist { "{ $!city }.PM" }
}

model MongerPM is table<monger_pm> {
    has Int $.monger-id  is referencing{ Monger.id };
    has Str $.pm-id      is referencing{ PMGroup.city };

    has Monger  $.monger is relationship{ .monger-id };
    has PMGroup $.pm     is relationship{ .pm-id     };
}

#my $*RED-DEBUG = True;
Monger.^create-table;
PMGroup.^create-table;
MongerPM.^create-table;

my $*RED-DEBUG = True;

my $pm        = PMGroup.^create: :city<Rio>;
my $monger    = Monger.^create: :name<Fernando>;
MongerPM.^create: :$monger, :$pm;

#.monger-pm>>.pm.say for Monger.^all
#for Monger.^all -> $monger {
#    for $monger.monger-pm.map: *.pm {
#        .say
#    }
#}

.pms.say for Monger.^all

i think relationships of model type objects should continue returning the model, but it could mixin a role that would store what relationship it was, so, on translate it could use that...

multi method translate(Red::AST::Value $_ where .type ~~ Red::Model, $context?) {
    die "NYI: map returning a relationship";
}

@FCO
Copy link
Owner Author

FCO commented Jan 10, 2023

Maybe we could use the same approach we did in #417 here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants