Skip to content

Commit

Permalink
expand a little faster
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 25, 2016
1 parent 0edbce9 commit f854bdd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

2.26 2016-04-15
2.26 2016-04-25
- Improved expand performance slightly.

2.25 2016-03-26
- Added support for encoding and decoding of JSON notifications.
Expand Down
18 changes: 9 additions & 9 deletions lib/Mojo/Pg/Results.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ sub text { tablify shift->arrays }
sub _collect { Mojo::Collection->new(@_) }

sub _expand {
my ($self, @data) = @_;
my ($self, @rows) = @_;

return @data unless $self->{expand} && $data[0];
return @rows unless $self->{expand} && $rows[0];
my ($idx, $name) = @$self{qw(idx name)};
unless ($idx) {
my $types = $self->sth->{pg_type};
my @idx = grep { $types->[$_] eq 'json' || $types->[$_] eq 'jsonb' }
0 .. $#$types;
($idx, $name) = @$self{qw(idx name)} = (\@idx, [@{$self->columns}[@idx]]);
}
return @data unless @$idx;

for my $data (@data) {
if (ref $data eq 'HASH') { $data->{$_} and _json($data->{$_}) for @$name }
else { $data->[$_] and _json($data->[$_]) for @$idx }
return @rows unless @$idx;
if (ref $rows[0] eq 'HASH') {
for my $r (@rows) { $r->{$_} && ($r->{$_} = from_json $r->{$_}) for @$name }
}
else {
for my $r (@rows) { $r->[$_] && ($r->[$_] = from_json $r->[$_]) for @$idx }
}

return @data;
return @rows;
}

sub _json { $_[0] = from_json $_[0] }

1;

=encoding utf8
Expand Down

0 comments on commit f854bdd

Please sign in to comment.