-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Iterator.pm6
61 lines (57 loc) · 1.92 KB
/
Iterator.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use Red::DB;
use Red::AST;
use Red::Driver;
use Red::Utils;
unit class Red::ResultSeq::Iterator does Iterator;
has Mu:U $.of is required;
has Red::AST $.ast is required;
has &.post;
has $!st-handler;
has Red::Driver $!driver = get-RED-DB;
submethod TWEAK(|) {
my $ast = $!driver.optimize: $!ast;
my @st-handler = $!driver.prepare: $ast;
@st-handler>>.execute unless $*RED-DRY-RUN;
$!st-handler = @st-handler.tail;
$!of.^emit: $ast;
CATCH {
default {
$!of.^emit: $ast, :error($_);
proceed
}
}
}
#method is-lazy { True }
method pull-one {
if $*RED-DRY-RUN { return $!of.bless }
my $data := $!st-handler.row;
return IterationEnd if $data =:= IterationEnd or not $data;
my %cols = $!of.^columns.map: { .column.attr-name => .column }
my $obj = $!of.new: |(%($data).kv
.map(-> $k, $v {
do with $v {
my $c = $k.split(".").tail;
CATCH {
dd $data;
dd %cols;
dd $c;
dd %cols{$c};
dd $!driver.^lookup("inflate").candidates>>.signature;
.rethrow
}
die "Column '$k' not found" without %cols{$c};
die "Inflator not defined for column '$k'" without %cols{$c}.inflate;
my $inflated = %cols{$c}.inflate.($v);
$inflated = $!driver.inflate(
%cols{$c}.inflate.($v),
:to($!of.^attributes.first(*.name.substr(2) eq $c).type)
) if \($!driver, $inflated, :to($!of.^attributes.first(*.name.substr(2) eq $c).type)) ~~ $!driver.^lookup("inflate").candidates.any.signature;
$c => $inflated
} else { Empty }
}).Hash)
;
$obj.^clean-up;
$obj.^saved-on-db;
return .($obj) with &!post;
$obj
}