| 1 | package DBIx::ActiveRecord::Scope; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | use base qw/Exporter/; | ||
| 7 | |||
| 8 | our @ISA = qw/Exporter/; | ||
| 9 | my @delegates = qw/eq ne in not_in null not_null gt lt ge le like contains starts_with ends_with between where select limit offset lock group asc desc reorder reverse/; | ||
| 10 | our @EXPORT = (@delegates, qw/scoped_instance _scope _execute merge joins includes _includes _loads_includes update_all delete_all _is_relation/); | ||
| 11 | |||
| 12 | { | ||
| 13 | no strict 'refs'; | ||
| 14 | foreach my $m (@delegates) { | ||
| 15 | *{__PACKAGE__."::$m"} = sub { | ||
| 16 | my $self = shift; | ||
| 17 | my @args = @_; | ||
| 18 | @args = map { $self->_is_relation($_) ? $_->{arel} : $_ } @args; | ||
| 19 | $self->_scope($m, @args); | ||
| 20 | }; | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _is_relation { | ||
| 25 | my ($self, $obj) = @_; | ||
| 26 | return 0 if !ref $obj; | ||
| 27 | return 0 if ref $obj eq 'HASH'; | ||
| 28 | return 0 if ref $obj eq 'ARRAY'; | ||
| 29 | return $obj->isa('DBIx::ActiveRecord::Relation'); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _scope { | ||
| 33 | my $self = shift; | ||
| 34 | my $method = shift; | ||
| 35 | my $s = $self->scoped; | ||
| 36 | $s->{arel} = $s->{arel}->$method(@_); | ||
| 37 | $s; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub scoped_instance { | ||
| 41 | my $self = shift; | ||
| 42 | ref $self ? $self : $self->scoped; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub update_all { | ||
| 46 | my ($self, $sets) = @_; | ||
| 47 | $self = $self->scoped; | ||
| 48 | my $s = $self->{arel}->update($sets); | ||
| 49 | my $sth = $self->{model}->dbh->prepare($s->to_sql); | ||
| 50 | $sth->execute($s->binds) || croak $sth->errstr; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub delete_all { | ||
| 54 | my ($self) = @_; | ||
| 55 | $self = $self->scoped; | ||
| 56 | my $s = $self->{arel}->delete; | ||
| 57 | my $sth = $self->{model}->dbh->prepare($s->to_sql); | ||
| 58 | $sth->execute($s->binds) || croak $sth->errstr; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub all { | ||
| 62 | shift->_execute; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub first { | ||
| 66 | my $self = shift; | ||
| 67 | my $s = $self->scoped_instance; | ||
| 68 | my $org = $s->{arel}->clone; | ||
| 69 | $s->{arel} = $s->{arel}->limit(1); | ||
| 70 | my $r = $s->_execute; | ||
| 71 | $s->{arel} = $org; | ||
| 72 | @$r ? $r->[0] : undef; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub last { | ||
| 76 | my $self = shift; | ||
| 77 | my $s = $self->scoped_instance; | ||
| 78 | my $org = $s->{arel}->clone; | ||
| 79 | $s->{arel} = $s->{arel}->limit(1)->reverse; | ||
| 80 | my $r = $s->_execute; | ||
| 81 | $s->{arel} = $org; | ||
| 82 | @$r ? $r->[0] : undef; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _execute { | ||
| 86 | my $self = shift; | ||
| 87 | my $s = $self->scoped_instance; | ||
| 88 | my $rs = $s->{model}->instantiates_by_relation($s); | ||
| 89 | $s->_loads_includes($s->{_includes}, $rs); | ||
| 90 | $rs; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _loads_includes { | ||
| 94 | my ($self, $inc, $rs) = @_; | ||
| 95 | foreach my $k (keys %$inc) { | ||
| 96 | my $opt = $inc->{$k}; | ||
| 97 | my $model = $opt->{model}; | ||
| 98 | my $primary_key = $opt->{primary_key}; | ||
| 99 | my $foreign_key = $opt->{foreign_key}; | ||
| 100 | my %pkeys; | ||
| 101 | my $search_key = $opt->{belongs_to} ? $primary_key : $foreign_key; | ||
| 102 | my $my_key = $opt->{belongs_to} ? $foreign_key : $primary_key; | ||
| 103 | my $s = $opt->{belongs_to} ? $model->unscoped : $model->scoped; | ||
| 104 | my @keys = map {$_->$my_key} grep {$pkeys{$_->$my_key} = 1} @$rs; | ||
| 105 | next if !keys %pkeys; | ||
| 106 | my $ir = $s->in($search_key => \@keys)->all; | ||
| 107 | $self->_loads_includes($opt->{_includes}, $ir) if $opt->{_includes}; | ||
| 108 | foreach my $r (@$rs) { | ||
| 109 | my @r = grep {$r->$my_key eq $_->$search_key} @$ir; | ||
| 110 | if ($opt->{one}) { | ||
| 111 | $r->{associates_cache}->{$opt->{name}} = $r[0]; | ||
| 112 | } else { | ||
| 113 | my $s = $model->eq($foreign_key => $r->$primary_key); | ||
| 114 | $s->{cache}->{all} = \@r; | ||
| 115 | $r->{associates_cache}->{$opt->{name}} = $s; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | sub merge { | ||
| 122 | my ($self, $relation) = @_; | ||
| 123 | my $s = $self->scoped; | ||
| 124 | $s->{arel} = $s->{arel}->merge($relation->{arel}); | ||
| 125 | $s; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub joins { | ||
| 129 | my $self = shift; | ||
| 130 | my $s = $self->scoped; | ||
| 131 | my $model = $s->{model}; | ||
| 132 | my $arel = $s->{arel}; | ||
| 133 | foreach my $name (@_) { | ||
| 134 | my $opt = $model->_global->{joins}->{$name} || croak "no relation!"; | ||
| 135 | my $m = $opt->{model}; | ||
| 136 | if ($opt->{belongs_to}) { | ||
| 137 | $arel = $arel->merge($model->arel->inner_join($m->arel->as($name), $opt)); | ||
| 138 | } else { | ||
| 139 | $arel = $arel->merge($model->arel->left_join($m->arel->as($name), $opt)); | ||
| 140 | } | ||
| 141 | $model = $m; | ||
| 142 | } | ||
| 143 | $s->{arel} = $arel; | ||
| 144 | $s; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub includes { | ||
| 148 | my $self = shift; | ||
| 149 | my $s = $self->scoped; | ||
| 150 | $s->{_includes} ||= {}; | ||
| 151 | my $inc = $s->{_includes}; | ||
| 152 | my $parent = $s->{model}; | ||
| 153 | my $h; | ||
| 154 | foreach my $name (@_) { | ||
| 155 | $inc->{$name} ||= {}; | ||
| 156 | $h = $inc->{$name}; | ||
| 157 | my $opt = $parent->_global->{joins}->{$name} || croak "no relation!"; | ||
| 158 | my $i = {%$opt, name => $name}; | ||
| 159 | map {$h->{$_} = $i->{$_}} keys %$i; | ||
| 160 | $parent = $opt->{model}; | ||
| 161 | $h->{_includes} ||= {}; | ||
| 162 | $inc = $h->{_includes}; | ||
| 163 | } | ||
| 164 | $s; | ||
| 165 | } | ||
| 166 | |||
| 167 | 1; |