From f4bb559d81a635a33fce8fa20830df0f67f6314c Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Thu, 31 Mar 2011 18:01:40 +0200 Subject: [PATCH] lookup into different dataset 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 | 46 ++++++++++++++++++++++++++++++++++++++ public/code/alpha.on_id.pl | 6 +++++ 2 files changed, 52 insertions(+) create mode 100644 public/code/alpha.on_id.pl diff --git a/lib/MojoFacets/Data.pm b/lib/MojoFacets/Data.pm index 63c98e2..4ad90c5 100644 --- a/lib/MojoFacets/Data.pm +++ b/lib/MojoFacets/Data.pm @@ -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 index 0000000..32aecf5 --- /dev/null +++ b/public/code/alpha.on_id.pl @@ -0,0 +1,6 @@ +lookup($row->{alpha}, 'test2.js', => 'alpha2', sub { + map { + push @{$update->{on_id}} => $_; + } @{$on->{id2}} +}); + -- 2.20.1