| 1 | package DBIx::ActiveRecord::Arel::Query; | ||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 7 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 6 | 7 | my ($self, $main) = @_; | |||||||||||
| 7 | 7 | bless { | |||||||||||
| 8 | main => $main, | ||||||||||||
| 9 | wheres => [], | ||||||||||||
| 10 | joins => [], | ||||||||||||
| 11 | binds => [], | ||||||||||||
| 12 | selects => [], | ||||||||||||
| 13 | group => [], | ||||||||||||
| 14 | order => [], | ||||||||||||
| 15 | limit => undef, | ||||||||||||
| 16 | offset => undef, | ||||||||||||
| 17 | lock => undef, | ||||||||||||
| 18 | as => {}, | ||||||||||||
| 19 | }, $self; | ||||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | 0 | ▶ | sub _to_sql { die 'implement!' } | ||||||||||
|
Subroutine
_to_sql: not called CC=1 SCAR=6.9
| |||||||||||||
| 23 | 85 | ▶ | sub main {shift->{main}} | ||||||||||
|
Subroutine
main: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 24 | 37 | ▶ | sub binds {@{shift->{binds}}} | ||||||||||
|
Subroutine
binds: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 25 | |||||||||||||
| 26 | 3 | ▶ | sub add_as { | ||||||||||
|
Subroutine
add_as: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 27 | 3 | my ($self, $table, $as) = @_; | |||||||||||
| 28 | 3 | $self->{as}->{$table} = $as; | |||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | 16 | ▶ | sub add_join { | ||||||||||
|
Subroutine
add_join: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 32 | 16 | my ($self, $join) = @_; | |||||||||||
| 33 | 16 | push @{$self->{joins}}, $join; | |||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | 40 | ▶ | sub add_where { | ||||||||||
|
Subroutine
add_where: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 37 | 40 | my ($self, $where) = @_; | |||||||||||
| 38 | 40 | push @{$self->{wheres}}, $where; | |||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | 11 | ▶ | sub add_select { | ||||||||||
|
Subroutine
add_select: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 42 | 11 | my ($self, $select) = @_; | |||||||||||
| 43 | 11 | push @{$self->{selects}}, $select; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 5 | ▶ | sub add_group { | ||||||||||
|
Subroutine
add_group: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 47 | 5 | my ($self, $group) = @_; | |||||||||||
| 48 | 5 | push @{$self->{group}}, $group; | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | 7 | ▶ | sub add_order { | ||||||||||
|
Subroutine
add_order: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 52 | 7 | my ($self, $order) = @_; | |||||||||||
| 53 | 7 | push @{$self->{order}}, $order; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | 1 | ▶ | sub set_limit { | ||||||||||
|
Subroutine
set_limit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 57 | 1 | my ($self, $limit) = @_; | |||||||||||
| 58 | 1 | $self->{limit} = $limit; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 1 | ▶ | sub set_offset { | ||||||||||
|
Subroutine
set_offset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 62 | 1 | my ($self, $offset) = @_; | |||||||||||
| 63 | 1 | $self->{offset} = $offset; | |||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | 1 | ▶ | sub set_lock { | ||||||||||
|
Subroutine
set_lock: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 67 | 1 | shift->{lock} = 1; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 1 | ▶ | sub reset_order { | ||||||||||
|
Subroutine
reset_order: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 71 | 1 | shift->{order} = []; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 0 | ▶ | sub reverse_order { | ||||||||||
|
Subroutine
reverse_order: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||
| 76 | 0 | $_->reverse for @{$self->{order}}; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | 12 | ▶ | sub merge { | ||||||||||
|
Subroutine
merge: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 80 | 12 | my ($self, $query) = @_; | |||||||||||
| 81 | 12 | push @{$self->{wheres}}, @{$query->{wheres}}; | |||||||||||
| 82 | 12 | push @{$self->{selects}}, @{$query->{selects}}; | |||||||||||
| 83 | 12 | push @{$self->{group}}, @{$query->{group}}; | |||||||||||
| 84 | 12 | push @{$self->{order}}, @{$query->{order}}; | |||||||||||
| 85 | 12 | push @{$self->{joins}}, @{$query->{joins}}; | |||||||||||
| 86 | 12 | $self->merge_as($query); | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 28 | ▶ | sub merge_as { | ||||||||||
|
Subroutine
merge_as: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 90 | 28 | my ($self, $query) = @_; | |||||||||||
| 91 | 28 | %{$self->{as}} = (%{$query->{as}}, %{$self->{as}}); | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | 38 | ▶ | sub build_where { | ||||||||||
|
Subroutine
build_where: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 95 | 38 | my ($self) = @_; | |||||||||||
| 96 | 38 | my @binds; | |||||||||||
| 97 | my @where; | ||||||||||||
| 98 | |||||||||||||
| 99 | 38 | foreach my $w (@{$self->{wheres}}) { | |||||||||||
| 100 | 40 | my ($where, $binds) = $w->build; | |||||||||||
| 101 | 40 | push @where, $where; | |||||||||||
| 102 | 40 | ▶ | push @binds, @$binds if $binds; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | } | ||||||||||||
| 104 | |||||||||||||
| 105 | 38 | $self->{binds} = \@binds; | |||||||||||
| 106 | 38 | join(' AND ', @where); | |||||||||||
| 107 | } | ||||||||||||
| 108 | |||||||||||||
| 109 | 35 | ▶ | sub build_options { | ||||||||||
|
Subroutine
build_options: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 110 | 35 | my $self = shift; | |||||||||||
| 111 | |||||||||||||
| 112 | 35 | my @sql; | |||||||||||
| 113 | |||||||||||||
| 114 | 35 | my $group = $self->build_group; | |||||||||||
| 115 | 35 | ▶ | push @sql, 'GROUP BY '.$group if $group; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 116 | |||||||||||||
| 117 | 35 | my $order = $self->build_order; | |||||||||||
| 118 | 35 | ▶ | push @sql, 'ORDER BY '.$order if $order; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | |||||||||||||
| 120 | 35 | ▶ | if ($self->{limit}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 1 | push @sql, 'LIMIT ?'; | |||||||||||
| 122 | 1 | push @{$self->{binds}}, $self->{limit}; | |||||||||||
| 123 | } | ||||||||||||
| 124 | 35 | ▶ | if ($self->{offset}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 1 | push @sql, 'OFFSET ?'; | |||||||||||
| 126 | 1 | push @{$self->{binds}}, $self->{offset}; | |||||||||||
| 127 | } | ||||||||||||
| 128 | 35 | ▶ | push @sql, 'FOR UPDATE' if $self->{lock}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | 35 | join (' ', @sql); | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | 35 | ▶ | sub build_group { | ||||||||||
|
Subroutine
build_group: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 133 | 35 | my $self = shift; | |||||||||||
| 134 | 35 | ▶ | my $g = $self->{group} || return; | ||||||||||
|
Condition50%1/2
$self->{'group'} || (return)
MC/DC0%0/1
$self->{'group'} || (return)
$self->{'group'}
Truth table50%1/2
A
A= $self->{'group'}
| |||||||||||||
| 135 | 35 | join(', ', map {$_->name} @$g); | |||||||||||
| 136 | } | ||||||||||||
| 137 | |||||||||||||
| 138 | 35 | ▶ | sub build_order { | ||||||||||
|
Subroutine
build_order: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 139 | 35 | my $self = shift; | |||||||||||
| 140 | 35 | ▶ | my $order = $self->{order} || return; | ||||||||||
|
Condition50%1/2
$self->{'order'} || (return)
MC/DC0%0/1
$self->{'order'} || (return)
$self->{'order'}
Truth table50%1/2
A
A= $self->{'order'}
| |||||||||||||
| 141 | 35 | join(', ', map {$_->build} @$order); | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | 35 | ▶ | sub build_select { | ||||||||||
|
Subroutine
build_select: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 145 | 35 | my ($self) = @_; | |||||||||||
| 146 | 35 | my @select = map {$_->name} @{$self->{selects}}; | |||||||||||
| 147 | 35 | ▶ | @select ? join(', ', @select) : $self->main->_col("*")->name; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | 36 | ▶ | sub build_join { | ||||||||||
|
Subroutine
build_join: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 151 | 36 | my ($self) = @_; | |||||||||||
| 152 | 36 | my @join = map {$_->build} @{$self->{joins}}; | |||||||||||
| 153 | 36 | join(" ", @join); | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | 75 | ▶ | sub has_join { | ||||||||||
|
Subroutine
has_join: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 157 | 75 | my $self = shift; | |||||||||||
| 158 | 75 | !!@{$self->{joins}}; | |||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | 39 | ▶ | sub to_sql { | ||||||||||
|
Subroutine
to_sql: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 162 | 39 | my $self = shift; | |||||||||||
| 163 | |||||||||||||
| 164 | 39 | my $org_use_full_name = $DBIx::ActiveRecord::Arel::Column::USE_FULL_NAME; | |||||||||||
| 165 | 39 | my $org_as = $DBIx::ActiveRecord::Arel::Column::AS; | |||||||||||
| 166 | |||||||||||||
| 167 | 39 | $DBIx::ActiveRecord::Arel::Column::USE_FULL_NAME = $self->has_join; | |||||||||||
| 168 | 39 | $DBIx::ActiveRecord::Arel::Column::AS = $self->{as}; | |||||||||||
| 169 | |||||||||||||
| 170 | 39 | my $sql = $self->_to_sql(@_); | |||||||||||
| 171 | |||||||||||||
| 172 | 39 | $DBIx::ActiveRecord::Arel::Column::USE_FULL_NAME = $org_use_full_name; | |||||||||||
| 173 | 39 | $DBIx::ActiveRecord::Arel::Column::AS = $self->{as}; | |||||||||||
| 174 | |||||||||||||
| 175 | 39 | $sql; | |||||||||||
| 176 | } | ||||||||||||
| 177 | |||||||||||||
| 178 | 16 | ▶ | sub main_table_with_alias { | ||||||||||
|
Subroutine
main_table_with_alias: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 179 | 16 | my $self = shift; | |||||||||||
| 180 | 16 | my $as = $DBIx::ActiveRecord::Arel::Column::AS; | |||||||||||
| 181 | 16 | ▶ | $as->{$self->main->table} ? $self->main->table." ".$as->{$self->main->table} : $self->main->table; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 182 | } | ||||||||||||
| 183 | |||||||||||||
| 184 | 2 | 1; | |||||||||||