lookup into different dataset
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 31 Mar 2011 16:01:40 +0000 (18:01 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 31 Mar 2011 16:01:40 +0000 (18:01 +0200)
using syntax like:

lookup($row->{from}, 'on_path' => 'on_col', sub {
# $on is record from on_path dataset
# where $on_col matches $row->{$from}

map {
push @{$update->{on_id}} => $_;
} @{$on->{id2}}
});

lib/MojoFacets/Data.pm
public/code/alpha.on_id.pl [new file with mode: 0644]

index 63c98e2..4ad90c5 100644 (file)
@@ -626,6 +626,49 @@ sub __all_filters {
 
 our ($out, $key,$value);
 
+our $lookup_path_col;
+our $on;
+
+sub __commit_begin {
+       warn "__commit_begin";
+       $lookup_path_col = undef;
+       $on = undef;
+}
+
+sub __commit_end {
+       warn "__commit_end";
+       $lookup_path_col = undef; # cleanup memory
+       $on = undef;
+}
+
+sub lookup {
+       warn "# lookup ",dump @_;
+       my ( $vals, $on_path, $on_col, $code ) = @_;
+       die "code is not sub{ ... } but ", dump $code unless ref $code eq 'CODE';
+       my $items = $loaded->{$on_path}->{data}->{items} || die "no items for $on_path";
+
+       if ( ! exists $lookup_path_col->{$on_path}->{$on_col} ) {
+               warn "create lookup_path_col $on_path $on_col";
+               foreach my $i ( 0 .. $#$items ) {
+                       my $item = $items->[$i];
+                       if ( exists $item->{$on_col} ) {
+                               foreach my $v ( @{ $item->{$on_col} } ) {
+                                       push @{ $lookup_path_col->{$on_path}->{$on_col}->{$v} }, $i;
+                               }
+                       }
+               }
+               warn dump $lookup_path_col->{$on_path}->{$on_col};
+       }
+
+       foreach my $v ( @$vals ) {
+               foreach my $i ( @{ $lookup_path_col->{$on_path}->{$on_col}->{$v} } ) {
+                       $on = $items->[$i];
+                       warn "# lookup code $v $i ",dump $on;
+                       $code->();
+               }
+       }
+}
+
 sub __commit_path_code {
        my ( $path, $i, $code, $commit_changed ) = @_;
 
@@ -747,6 +790,7 @@ sub items {
        my $test = $self->param('test');
 
        my $commit_changed;
+       __commit_begin;
 
        if ( $code && ( $test || $commit ) ) {
                # XXX find columns used in code snippet and show them to user
@@ -937,6 +981,8 @@ sub items {
 
        } # test?
 
+       __commit_end;
+
        $self->render(
                order => $order,
                offset => $offset,
diff --git a/public/code/alpha.on_id.pl b/public/code/alpha.on_id.pl
new file mode 100644 (file)
index 0000000..32aecf5
--- /dev/null
@@ -0,0 +1,6 @@
+lookup($row->{alpha}, 'test2.js', => 'alpha2', sub {
+ map {
+  push @{$update->{on_id}} => $_;
+ } @{$on->{id2}} 
+});
+