| 1 | package CatalystX::CRUD::Model::RDBO; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use base qw( CatalystX::CRUD::Model CatalystX::CRUD::Model::Utils ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use CatalystX::CRUD::Iterator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Data::Dump qw( dump ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | our $VERSION = '0.304'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | __PACKAGE__->mk_ro_accessors( | |||||||||||||||||||||||||||
| 14 | qw( name manager treat_like_int load_with related_load_with )); | ||||||||||||||||||||||||||||
| 15 | 4 | __PACKAGE__->config( object_class => 'CatalystX::CRUD::Object::RDBO' ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | CatalystX::CRUD::Model::RDBO - Rose::DB::Object CRUD | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | package MyApp::Model::Foo; | ||||||||||||||||||||||||||||
| 24 | use base qw( CatalystX::CRUD::Model::RDBO ); | ||||||||||||||||||||||||||||
| 25 | __PACKAGE__->config( | ||||||||||||||||||||||||||||
| 26 | name => 'My::RDBO::Foo', | ||||||||||||||||||||||||||||
| 27 | manager => 'My::RDBO::Foo::Manager', | ||||||||||||||||||||||||||||
| 28 | load_with => [qw( bar )], | ||||||||||||||||||||||||||||
| 29 | related_load_with => { | ||||||||||||||||||||||||||||
| 30 | bars => ['doof'] | ||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||
| 32 | page_size => 50, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | 1; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | CatalystX::CRUD::Model::RDBO is a CatalystX::CRUD implementation for Rose::DB::Object. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | The config options can be set as in the SYNOPSIS example. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 METHODS | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head2 name | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | The name of the Rose::DB::Object-based class that the model represents. | ||||||||||||||||||||||||||||
| 49 | Accessible via name() or config->{name}. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head2 manager | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | If C<manager> is not defined in config(), | ||||||||||||||||||||||||||||
| 54 | the Xsetup() method will attempt to load a class | ||||||||||||||||||||||||||||
| 55 | named with the C<name> value from config() | ||||||||||||||||||||||||||||
| 56 | with C<::Manager> appended. | ||||||||||||||||||||||||||||
| 57 | This assumes the namespace convention of Rose::DB::Object::Manager. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | If there is no such module in your @INC path, then | ||||||||||||||||||||||||||||
| 60 | the fall-back default is Rose::DB::Object::Manager. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 load_with | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | The value of C<load_with> should be an array ref of relationship | ||||||||||||||||||||||||||||
| 65 | names. The array ref is passed into all the Manager | ||||||||||||||||||||||||||||
| 66 | get_objects* methods as the C<with_objects> value. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 related_load_with | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Similar to C<load_with>, but the C<with_objects> argument is passed | ||||||||||||||||||||||||||||
| 71 | in all the *_related methods. The C<related_load_with> value should | ||||||||||||||||||||||||||||
| 72 | be a hash ref with keys using relationships names and the values | ||||||||||||||||||||||||||||
| 73 | being array refs of relationship names in the foreign (related) classes. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head2 Xsetup | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Implements the required Xsetup() method. Instatiates the model's | ||||||||||||||||||||||||||||
| 80 | name() and manager() values based on config(). | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | ▶ | sub Xsetup { | ||||||||||||||||||||||||||
|
Subroutine
Xsetup: called CC=10 SCAR=28.1
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | $self->next::method(@_); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 4 | $self->{name} = $self->config->{name}; | |||||||||||||||||||||||||||
| 90 | 4 | ▶ | if ( !$self->name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return if $self->throw_error("need to configure a Rose class name"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | ▶ | $self->{manager} = $self->config->{manager} || $self->name . '::Manager'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->config->{'manager'} || $self->name . '::Manager'
MC/DC0%0/1
$self->config->{'manager'} || $self->name . '::Manager'
$self->config->{'manager'}
Truth table50%1/2
A
A= $self->config->{'manager'}
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | my $name = $self->name; | |||||||||||||||||||||||||||
| 97 | 4 | my $mgr = $self->manager; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | eval "require $name"; | |||||||||||||||||||||||||||
| 100 | 4 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return if $self->throw_error($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # what kind of db driver are we using. makes a difference in make_query(). | ||||||||||||||||||||||||||||
| 105 | 4 | my $db = $name->new->db; | |||||||||||||||||||||||||||
| 106 | 4 | ▶ | $self->use_ilike(1) if $db->driver eq 'pg'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # rdbo sql uses 'ne' for not equal | ||||||||||||||||||||||||||||
| 109 | 4 | $self->ne_sign('ne'); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # cache the treat_like_int hash | ||||||||||||||||||||||||||||
| 112 | 4 | $self->_treat_like_int; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # load the Manager | ||||||||||||||||||||||||||||
| 115 | 4 | eval "require $mgr"; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # don't fret -- just use RDBO::Manager | ||||||||||||||||||||||||||||
| 118 | 4 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 4 | $self->{manager} = 'Rose::DB::Object::Manager'; | |||||||||||||||||||||||||||
| 120 | 4 | require Rose::DB::Object::Manager; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # turn on debugging help | ||||||||||||||||||||||||||||
| 124 | 4 | ▶ | if ( $ENV{CATALYST_DEBUG} && $ENV{CATALYST_DEBUG} > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"CATALYST_DEBUG"} and $ENV{"CATALYST_DEBUG"} > 1
MC/DC0%0/2
$ENV{"CATALYST_DEBUG"} and $ENV{"CATALYST_DEBUG"} > 1
$ENV{"CATALYST_DEBUG"}
$ENV{"CATALYST_DEBUG"} > 1
Truth table33%1/3
A and B
A= $ENV{"CATALYST_DEBUG"}B= $ENV{"CATALYST_DEBUG"} > 1
| |||||||||||||||||||||||||||||
| 125 | 0 | $Rose::DB::Object::QueryBuilder::Debug = 1; | |||||||||||||||||||||||||||
| 126 | 0 | $Rose::DB::Object::Debug = 1; | |||||||||||||||||||||||||||
| 127 | 0 | $Rose::DB::Object::Manager::Debug = 1; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 new_object( @param ) | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Returns a CatalystX::CRUD::Object::RDBO object. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 10 | ▶ | sub new_object { | ||||||||||||||||||||||||||
|
Subroutine
new_object: called CC=5 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | 10 | my $rdbo = $self->name; | |||||||||||||||||||||||||||
| 141 | 10 | my $obj; | |||||||||||||||||||||||||||
| 142 | 10 | eval { $obj = $rdbo->new(@_) }; | |||||||||||||||||||||||||||
| 143 | 10 | ▶ | if ( $@ or !$obj ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $obj
MC/DC0%0/2
$@ or not $obj
$@
not $obj
Truth table33%1/3
A or not(B)
A= $@B= not $obj
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my $err = defined($obj) ? $obj->error : $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | return if $self->throw_error("can't create new $rdbo object: $err"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 10 | return $self->next::method( delegate => $obj ); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 fetch( @params ) | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | If present, | ||||||||||||||||||||||||||||
| 153 | @I<params> is passed directly to name()'s new() method, | ||||||||||||||||||||||||||||
| 154 | and is expected to be an array of key/value pairs. | ||||||||||||||||||||||||||||
| 155 | Then the load() method is called on the resulting object. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | If @I<params> are not present, the new() object is simply returned, | ||||||||||||||||||||||||||||
| 158 | which is equivalent to calling new_object(). | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | All the methods called within fetch() are wrapped in an eval() | ||||||||||||||||||||||||||||
| 161 | and sanity checked afterwards. If there are any errors, | ||||||||||||||||||||||||||||
| 162 | throw_error() is called. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Example: | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | my $foo = $c->model('Foo')->fetch( id => 1234 ); | ||||||||||||||||||||||||||||
| 167 | if (@{ $c->error }) | ||||||||||||||||||||||||||||
| 168 | { | ||||||||||||||||||||||||||||
| 169 | # do something to deal with the error | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | B<NOTE:> If the object's presence in the database is questionable, | ||||||||||||||||||||||||||||
| 173 | your controller code may want to use new_object() and then call | ||||||||||||||||||||||||||||
| 174 | load_speculative() yourself. Example: | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | my $foo = $c->model('Foo')->new_object( id => 1234 ); | ||||||||||||||||||||||||||||
| 177 | $foo->load_speculative; | ||||||||||||||||||||||||||||
| 178 | if ($foo->not_found) | ||||||||||||||||||||||||||||
| 179 | { | ||||||||||||||||||||||||||||
| 180 | # do something | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 9 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=9 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 9 | ▶ | my $obj = $self->new_object(@_) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 9 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 9 | my %v = @_; | |||||||||||||||||||||||||||
| 191 | 9 | my $ret; | |||||||||||||||||||||||||||
| 192 | 9 | my $name = $self->name; | |||||||||||||||||||||||||||
| 193 | 9 | my @arg = (); | |||||||||||||||||||||||||||
| 194 | 9 | eval { $ret = $obj->read(@arg); }; | |||||||||||||||||||||||||||
| 195 | 9 | ▶ | if ( $@ or !$ret ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $ret
MC/DC0%0/2
$@ or not $ret
$@
not $ret
Truth table33%1/3
A or not(B)
A= $@B= not $ret
| |||||||||||||||||||||||||||||
| 196 | return | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | if $self->throw_error( join( " : ", $@, "no such $name" ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # special handling of fetching | ||||||||||||||||||||||||||||
| 201 | # e.g. Catalyst::Plugin::Session::Store::DBI records. | ||||||||||||||||||||||||||||
| 202 | 9 | ▶ | if ( $v{id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # stringify in case it's a char instead of int | ||||||||||||||||||||||||||||
| 205 | # as is the case with session ids | ||||||||||||||||||||||||||||
| 206 | 9 | my $pid = $obj->delegate->id; | |||||||||||||||||||||||||||
| 207 | 9 | $pid =~ s,\s+$,,; | |||||||||||||||||||||||||||
| 208 | 9 | ▶ | unless ( $pid eq $v{id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | return | ||||||||||||||||||||||||||||
| 211 | if $self->throw_error( | ||||||||||||||||||||||||||||
| 212 | "Error fetching correct id:\nfetched: $v{id} " | ||||||||||||||||||||||||||||
| 213 | . length( $v{id} ) | ||||||||||||||||||||||||||||
| 214 | 0 | ▶ | . "\nbut got: $pid" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | . length($pid) ); | ||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 9 | return $obj; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head2 search( @params ) | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | @I<params> is passed directly to the Manager get_objects() method. | ||||||||||||||||||||||||||||
| 226 | See the Rose::DB::Object::Manager documentation. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Returns an array or array ref (based on wantarray) of | ||||||||||||||||||||||||||||
| 229 | CatalystX::CRUD::Object::RDBO objects. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 8 | ▶ | sub search { | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 234 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 235 | 8 | my $objs = $self->_get_objects( 'get_objects', @_ ); | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # save ourselves lots of method-call overhead. | ||||||||||||||||||||||||||||
| 238 | 8 | my $class = $self->object_class; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 8 | my @wrapped = map { $class->new( delegate => $_ ) } @$objs; | |||||||||||||||||||||||||||
| 241 | 8 | ▶ | return wantarray ? @wrapped : \@wrapped; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head2 count( @params ) | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | @I<params> is passed directly to the Manager get_objects_count() method. | ||||||||||||||||||||||||||||
| 247 | See the Rose::DB::Object::Manager documentation. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | Returns an integer. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =cut | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 8 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 254 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 255 | 8 | return $self->_get_objects( 'get_objects_count', @_ ); | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =head2 iterator( @params ) | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | @I<params> is passed directly to the Manager get_objects_iterator() method. | ||||||||||||||||||||||||||||
| 261 | See the Rose::DB::Object::Manager documentation. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Returns a CatalystX::CRUD::Iterator object whose next() method | ||||||||||||||||||||||||||||
| 264 | will return a CatalystX::CRUD::Object::RDBO object. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | sub iterator { | ||||||||||||||||||||||||||
|
Subroutine
iterator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 269 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 270 | 0 | my $iter = $self->_get_objects( 'get_objects_iterator', @_ ); | |||||||||||||||||||||||||||
| 271 | 0 | return CatalystX::CRUD::Iterator->new( $iter, $self->object_class ); | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head2 search_related( I<obj>, I<relationship> ) | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | Implements required method. Returns array or array ref based on calling | ||||||||||||||||||||||||||||
| 277 | context, for objects related to I<obj> via I<relationship>. I<relationship> | ||||||||||||||||||||||||||||
| 278 | should be a method name callable on I<obj>. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =head2 iterator_related( I<obj>, I<relationship> ) | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | Like search_related() but returns an iterator. | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =head2 count_related( I<obj>, I<relationship> ) | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Like search_related() but returns an integer. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 0 | ▶ | sub _related_query { | ||||||||||||||||||||||||||
|
Subroutine
_related_query: not called CC=14 SCAR=53.5
| |||||||||||||||||||||||||||||
| 291 | 0 | my ( $self, $obj, $rel_name ) = @_; | |||||||||||||||||||||||||||
| 292 | 0 | my $c = $self->context; | |||||||||||||||||||||||||||
| 293 | 0 | ▶ | my $relationship = $self->has_relationship( $obj, $rel_name ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | or $self->throw_error("no relationship for $rel_name"); | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # set the param so sort is correctly mangled in make_query() | ||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if ($relationship->isa( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 'Rose::DB::Object::Metadata::Relationship::ManyToMany') | ||||||||||||||||||||||||||||
| 299 | ) | ||||||||||||||||||||||||||||
| 300 | { | ||||||||||||||||||||||||||||
| 301 | 0 | $c->req->params->{'cxc-m2m'} = 1; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | 0 | my $query = $self->make_query; | |||||||||||||||||||||||||||
| 304 | 0 | my @arg; | |||||||||||||||||||||||||||
| 305 | 0 | ▶ | if ( @{ $query->{query} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | @arg = ( query => $query->{query} ); | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | 0 | for (qw( limit offset sort_by )) { | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # only want to include the sort_by if it makes sense. | ||||||||||||||||||||||||||||
| 311 | 0 | ▶ | if ( $_ eq 'sort_by' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # can't reliably predict table prefixes in a m2m. | ||||||||||||||||||||||||||||
| 314 | # NOTE this effectively ignores whatever make_query did. | ||||||||||||||||||||||||||||
| 315 | 0 | ▶ | if ( $c->req->params->{'cxc-m2m'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 0 | next; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | # if sort_by was derived from PK, it may refer to a parent table, | ||||||||||||||||||||||||||||
| 320 | # not the related table. So skip it unless it was explicit. | ||||||||||||||||||||||||||||
| 321 | 0 | ▶ | if ( !$c->req->params->{'cxc-order'} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$c->req->params->{"cxc-order"} or $c->req->params->{"cxc-sort"}
MC/DC0%0/2
$c->req->params->{"cxc-order"} or $c->req->params->{"cxc-sort"}
$c->req->params->{"cxc-order"}
$c->req->params->{"cxc-sort"}
Truth table0%0/3
A or B
A= $c->req->params->{"cxc-order"}B= $c->req->params->{"cxc-sort"}
| |||||||||||||||||||||||||||||
| 322 | and !$c->req->params->{'cxc-sort'} ) | ||||||||||||||||||||||||||||
| 323 | { | ||||||||||||||||||||||||||||
| 324 | 0 | next; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 0 | ▶ | if ( exists $query->{$_} and length $query->{$_} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $query->{$_} and length $query->{$_}
MC/DC0%0/2
exists $query->{$_} and length $query->{$_}
exists $query->{$_}
length $query->{$_}
Truth table0%0/3
A and B
A= exists $query->{$_}B= length $query->{$_}
| |||||||||||||||||||||||||||||
| 329 | 0 | push( @arg, $_ => $query->{$_} ); | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | 0 | ▶ | if ( $self->related_load_with | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->related_load_with and exists $self->related_load_with->{$rel_name}
MC/DC0%0/2
$self->related_load_with and exists $self->related_load_with->{$rel_name}
$self->related_load_with
exists $self->related_load_with->{$rel_name}
Truth table0%0/3
A and B
A= $self->related_load_withB= exists $self->related_load_with->{$rel_name}
| |||||||||||||||||||||||||||||
| 333 | && exists $self->related_load_with->{$rel_name} ) | ||||||||||||||||||||||||||||
| 334 | { | ||||||||||||||||||||||||||||
| 335 | push( | ||||||||||||||||||||||||||||
| 336 | @arg, | ||||||||||||||||||||||||||||
| 337 | 0 | with_objects => $self->related_load_with->{$rel_name}, | |||||||||||||||||||||||||||
| 338 | multi_many_ok => 1 | ||||||||||||||||||||||||||||
| 339 | ); | ||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | ▶ | $c->log->debug( "related_query: " . dump \@arg ) if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 0 | return @arg; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 0 | ▶ | sub search_related { | ||||||||||||||||||||||||||
|
Subroutine
search_related: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 348 | 0 | my ( $self, $obj, $rel ) = @_; | |||||||||||||||||||||||||||
| 349 | 0 | return CatalystX::CRUD::Iterator->new( | |||||||||||||||||||||||||||
| 350 | $obj->$rel( $self->_related_query( $obj, $rel ) ), | ||||||||||||||||||||||||||||
| 351 | $self->object_class ); | ||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | ▶ | sub iterator_related { | ||||||||||||||||||||||||||
|
Subroutine
iterator_related: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 355 | 0 | my ( $self, $obj, $rel ) = @_; | |||||||||||||||||||||||||||
| 356 | 0 | my $method = $rel . '_iterator'; | |||||||||||||||||||||||||||
| 357 | 0 | return CatalystX::CRUD::Iterator->new( | |||||||||||||||||||||||||||
| 358 | $obj->$method( $self->_related_query( $obj, $rel ) ), | ||||||||||||||||||||||||||||
| 359 | $self->object_class ); | ||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 0 | ▶ | sub count_related { | ||||||||||||||||||||||||||
|
Subroutine
count_related: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 363 | 0 | my ( $self, $obj, $rel ) = @_; | |||||||||||||||||||||||||||
| 364 | 0 | my $method = $rel . '_count'; | |||||||||||||||||||||||||||
| 365 | 0 | return $obj->$method( $self->_related_query( $obj, $rel ) ); | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =head2 find_related( I<obj>, I<relationship>, I<foreign_value> ) | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Implements required method. Returns array or array ref based on calling | ||||||||||||||||||||||||||||
| 371 | context, for objects related to I<obj> via I<relationship> | ||||||||||||||||||||||||||||
| 372 | that match I<foreign_value>. I<relationship> | ||||||||||||||||||||||||||||
| 373 | should be a method name callable on I<obj>. | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | =cut | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 2 | ▶ | sub find_related { | ||||||||||||||||||||||||||
|
Subroutine
find_related: called CC=4 SCAR=15.8
Pod
documented
| |||||||||||||||||||||||||||||
| 378 | 2 | my ( $self, $obj, $rel, $foreign_pk_value ) = @_; | |||||||||||||||||||||||||||
| 379 | 2 | ▶ | my $relationship = $self->has_relationship( $obj, $rel ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | or $self->throw_error("no relationship for $rel"); | ||||||||||||||||||||||||||||
| 381 | 2 | my $method = 'find_' . $rel; | |||||||||||||||||||||||||||
| 382 | 2 | my $args; | |||||||||||||||||||||||||||
| 383 | 2 | ▶ | if ($relationship->isa( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 'Rose::DB::Object::Metadata::Relationship::ManyToMany') | ||||||||||||||||||||||||||||
| 385 | ) | ||||||||||||||||||||||||||||
| 386 | { | ||||||||||||||||||||||||||||
| 387 | 2 | my $meta = $self->_get_rel_meta( $obj, $rel ); | |||||||||||||||||||||||||||
| 388 | 2 | $args = [ $meta->{map_to}->[1] => $foreign_pk_value ]; | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | else { | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | # all the PKs and Unique cols for the foreign class, OR'd together. | ||||||||||||||||||||||||||||
| 393 | 0 | my $pk_cols = $relationship->class->meta->primary_key_column_names; | |||||||||||||||||||||||||||
| 394 | 0 | my $uniq_cols = [ map {@$_} | |||||||||||||||||||||||||||
| 395 | 0 | @{ $relationship->class->meta->unique_keys_column_names } ]; | |||||||||||||||||||||||||||
| 396 | $args = [ | ||||||||||||||||||||||||||||
| 397 | or => [ | ||||||||||||||||||||||||||||
| 398 | 0 | map { $_ => $foreign_pk_value } ( @$pk_cols, @$uniq_cols ) | |||||||||||||||||||||||||||
| 399 | ] | ||||||||||||||||||||||||||||
| 400 | ]; | ||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | #dump $args; | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 2 | my $r = $obj->$method( query => $args ); | |||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | # save ourselves lots of method-call overhead. | ||||||||||||||||||||||||||||
| 408 | 2 | my $class = $self->object_class; | |||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | # delegate | ||||||||||||||||||||||||||||
| 411 | 2 | my @wrapped = map { $class->new( delegate => $_ ) } @$r; | |||||||||||||||||||||||||||
| 412 | 2 | ▶ | return wantarray ? @wrapped : \@wrapped; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =head2 add_related( I<obj>, I<rel_name>, I<foreign_value> ) | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | Associate foreign object identified by I<foreign_value> with I<obj> | ||||||||||||||||||||||||||||
| 418 | via the relationship I<rel_name>. | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | B<CAUTION:> For many-to-many relationships only. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =head2 rm_related( I<obj>, I<rel_name>, I<foreign_value> ) | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | Dissociate foreign object identified by I<foreign_value> from I<obj> | ||||||||||||||||||||||||||||
| 425 | via the relationship I<rel_name>. | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | B<CAUTION:> For many-to-many relationships only. | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =cut | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 6 | ▶ | sub _get_rel_meta { | ||||||||||||||||||||||||||
|
Subroutine
_get_rel_meta: called CC=4 SCAR=16.5
| |||||||||||||||||||||||||||||
| 432 | 6 | my ( $self, $obj, $rel_name ) = @_; | |||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 6 | ▶ | my $rel = $self->has_relationship( $obj, $rel_name ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | or $self->throw_error("no such relationship $rel_name"); | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 6 | ▶ | if ( $rel->isa('Rose::DB::Object::Metadata::Relationship::ManyToMany') ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 6 | my $map_class = $rel->map_class; | |||||||||||||||||||||||||||
| 440 | 6 | my $mcm = $map_class->meta; | |||||||||||||||||||||||||||
| 441 | 6 | my @map_to = $mcm->relationship( $rel->map_to )->column_map; | |||||||||||||||||||||||||||
| 442 | 6 | my @map_from = $mcm->relationship( $rel->map_from )->column_map; | |||||||||||||||||||||||||||
| 443 | 6 | my %m = ( | |||||||||||||||||||||||||||
| 444 | map_to => \@map_to, | ||||||||||||||||||||||||||||
| 445 | map_from => \@map_from, | ||||||||||||||||||||||||||||
| 446 | map_class => $map_class, | ||||||||||||||||||||||||||||
| 447 | ); | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | #carp dump \%m; | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 6 | return \%m; | |||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | elsif ( $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany') ) | ||||||||||||||||||||||||||||
| 455 | { | ||||||||||||||||||||||||||||
| 456 | 0 | my $column_map = $rel->column_map; | |||||||||||||||||||||||||||
| 457 | 0 | my %m = ( | |||||||||||||||||||||||||||
| 458 | map_to => [ reverse %$column_map ], # yes, coerce into array | ||||||||||||||||||||||||||||
| 459 | ); | ||||||||||||||||||||||||||||
| 460 | 0 | return \%m; | |||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||
| 463 | else { | ||||||||||||||||||||||||||||
| 464 | 0 | $self->throw_error( "unsupport relationship type: " . ref($rel) ); | |||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | =head2 has_relationship( I<obj>, I<rel_name> ) | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | Returns the Rose::DB::Object::Metadata::Relationship instance | ||||||||||||||||||||||||||||
| 471 | for I<rel_name> if it exists, or undef if it does not. | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =cut | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 8 | ▶ | sub has_relationship { | ||||||||||||||||||||||||||
|
Subroutine
has_relationship: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 476 | 8 | my ( $self, $obj, $rel_name ) = @_; | |||||||||||||||||||||||||||
| 477 | 8 | ▶ | if ( !$obj ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 478 | 0 | $self->throw_error("obj not defined"); | |||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | 8 | return $obj->delegate->meta->relationship($rel_name); | |||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 2 | ▶ | sub add_related { | ||||||||||||||||||||||||||
|
Subroutine
add_related: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 484 | 2 | my ( $self, $obj, $rel_name, $fk_val ) = @_; | |||||||||||||||||||||||||||
| 485 | 2 | my $addmethod = 'add_' . $rel_name; | |||||||||||||||||||||||||||
| 486 | 2 | my $meta = $self->_get_rel_meta( $obj, $rel_name ); | |||||||||||||||||||||||||||
| 487 | 2 | my $fpk = $meta->{map_to}->[1]; | |||||||||||||||||||||||||||
| 488 | 2 | $obj->$addmethod( { $fpk => $fk_val } ); | |||||||||||||||||||||||||||
| 489 | 2 | my $rt = $obj->save; | |||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | # so next access reflects change. | ||||||||||||||||||||||||||||
| 492 | 2 | $obj->forget_related($rel_name); | |||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 2 | return $rt; | |||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 2 | ▶ | sub rm_related { | ||||||||||||||||||||||||||
|
Subroutine
rm_related: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 498 | 2 | my ( $self, $obj, $rel_name, $fk_val ) = @_; | |||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 2 | my $meta = $self->_get_rel_meta( $obj, $rel_name ); | |||||||||||||||||||||||||||
| 501 | my $obj_method = $obj->delegate->meta->column_accessor_method_name( | ||||||||||||||||||||||||||||
| 502 | 2 | $meta->{map_from}->[1] ); | |||||||||||||||||||||||||||
| 503 | my $query = [ | ||||||||||||||||||||||||||||
| 504 | $meta->{map_from}->[0] => $obj->$obj_method, | ||||||||||||||||||||||||||||
| 505 | 2 | $meta->{map_to}->[0] => $fk_val, | |||||||||||||||||||||||||||
| 506 | ]; | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | #carp dump $query; | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | $self->manager->delete_objects( | ||||||||||||||||||||||||||||
| 511 | object_class => $meta->{map_class}, | ||||||||||||||||||||||||||||
| 512 | 2 | where => $query, | |||||||||||||||||||||||||||
| 513 | ); | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | # so next access reflects change | ||||||||||||||||||||||||||||
| 516 | 2 | $obj->forget_related($rel_name); | |||||||||||||||||||||||||||
| 517 | 2 | return $obj; | |||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | =head2 make_query( I<field_names> ) | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | Implement a RDBO-specific query factory based on request parameters. | ||||||||||||||||||||||||||||
| 523 | Return value can be passed directly to search(), iterator() or count() as | ||||||||||||||||||||||||||||
| 524 | documented in the CatalystX::CRUD::Model API. | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | See CatalystX::CRUD::Model::Utils::make_sql_query() for API details. | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | =cut | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | 12 | ▶ | sub _get_field_names { | ||||||||||||||||||||||||||
|
Subroutine
_get_field_names: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 531 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 532 | 12 | ▶ | return $self->{_field_names} if $self->{_field_names}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 533 | 4 | my @cols = $self->name->meta->column_names; | |||||||||||||||||||||||||||
| 534 | 4 | $self->{_field_names} = \@cols; | |||||||||||||||||||||||||||
| 535 | 4 | return \@cols; | |||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | =head2 treat_like_int | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | Returns hash ref of all column names that return type =~ m/^date(time)$/. | ||||||||||||||||||||||||||||
| 541 | This is so that wildcard searches for date and datetime-based columns | ||||||||||||||||||||||||||||
| 542 | will get proper SQL rendering. | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | =cut | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | 4 | ▶ | sub _treat_like_int { | ||||||||||||||||||||||||||
|
Subroutine
_treat_like_int: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||
| 547 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 548 | 4 | ▶ | return $self->{treat_like_int} if $self->{treat_like_int}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 549 | 4 | $self->{treat_like_int} = {}; | |||||||||||||||||||||||||||
| 550 | 4 | my $col_names = $self->_get_field_names; | |||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | # treat wildcard timestamps like ints not text (>= instead of ILIKE) | ||||||||||||||||||||||||||||
| 553 | 4 | for my $name (@$col_names) { | |||||||||||||||||||||||||||
| 554 | 8 | my $col = $self->name->meta->column($name); | |||||||||||||||||||||||||||
| 555 | 8 | ▶ | if ( $col->type =~ m/date|time|boolean|int/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 556 | 0 | $self->{treat_like_int}->{$name} = 1; | |||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | 4 | return $self->{treat_like_int}; | |||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | 8 | ▶ | sub _join_with_table_prefix { | ||||||||||||||||||||||||||
|
Subroutine
_join_with_table_prefix: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 564 | 8 | my ( $self, $q, $prefix ) = @_; | |||||||||||||||||||||||||||
| 565 | return join( ', ', | ||||||||||||||||||||||||||||
| 566 | 10 | map { $prefix . '.' . $_->[0] . ' ' . $_->[1] } | |||||||||||||||||||||||||||
| 567 | 8 | map { [%$_] } @{ $q->{sort_order} } ); | |||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | 8 | ▶ | sub make_query { | ||||||||||||||||||||||||||
|
Subroutine
make_query: called CC=9 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||
| 571 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 572 | 8 | my $c = $self->context; | |||||||||||||||||||||||||||
| 573 | 8 | ▶ | my $field_names = shift || $self->_get_field_names; | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $self->_get_field_names
MC/DC0%0/2
shift() || $self->_get_field_names
shift()
$self->_get_field_names
Truth table33%1/3
A || B
A= shift()B= $self->_get_field_names
| |||||||||||||||||||||||||||||
| 574 | 8 | my $q = $self->make_sql_query($field_names); | |||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | # many2many relationships always have two tables, | ||||||||||||||||||||||||||||
| 577 | # and we are sorting by the 2nd one. The 1st one is the mapper. | ||||||||||||||||||||||||||||
| 578 | # however, we leave sort_by alone if it already has . in it, | ||||||||||||||||||||||||||||
| 579 | # since then we assume the request knew enough to ask. | ||||||||||||||||||||||||||||
| 580 | 8 | ▶ | if ( length( $q->{sort_by} ) && !( $q->{sort_by} =~ m/\./ ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $q->{"sort_by"} and not $q->{"sort_by"} =~ /\./
MC/DC0%0/2
length $q->{"sort_by"} and not $q->{"sort_by"} =~ /\./
length $q->{"sort_by"}
not $q->{"sort_by"} =~ /\./
Truth table33%1/3
A and not(B)
A= length $q->{"sort_by"}B= not $q->{"sort_by"} =~ /\./
| |||||||||||||||||||||||||||||
| 581 | 8 | ▶ | if ( $c->req->params->{'cxc-m2m'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 582 | 0 | ▶ | if ( !( $q->{sort_by} =~ m/t\d\./ ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 583 | 0 | $q->{sort_by} = $self->_join_with_table_prefix( $q, 't2' ); | |||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||
| 586 | else { | ||||||||||||||||||||||||||||
| 587 | 8 | ▶ | if ( !( $q->{sort_by} =~ m/t\d\./ ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 588 | 8 | $q->{sort_by} = $self->_join_with_table_prefix( $q, 't1' ); | |||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | 8 | ▶ | $c->log->debug("make_query: WHERE $q->{query_obj} ORDER BY $q->{sort_by}") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 594 | if $c->debug; | ||||||||||||||||||||||||||||
| 595 | 8 | ▶ | $c->log->debug( "query: " . dump $q ) if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | 8 | return $q; | |||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | 16 | ▶ | sub _get_objects { | ||||||||||||||||||||||||||
|
Subroutine
_get_objects: called CC=5 SCAR=18.2
| |||||||||||||||||||||||||||||
| 601 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 602 | 16 | ▶ | my $method = shift || 'get_objects'; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'get_objects'
MC/DC0%0/1
shift() || 'get_objects'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 603 | 16 | my @args = @_; | |||||||||||||||||||||||||||
| 604 | 16 | my $manager = $self->manager; | |||||||||||||||||||||||||||
| 605 | 16 | my $name = $self->name; | |||||||||||||||||||||||||||
| 606 | 16 | my @params = ( object_class => $name ); # not $self->object_class | |||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | #carp dump \@args; | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | 16 | ▶ | if ( ref $args[0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 611 | 16 | push( @params, %{ $args[0] } ); | |||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||
| 613 | elsif ( ref $args[0] eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 614 | 0 | push( @params, @{ $args[0] } ); | |||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||
| 616 | else { | ||||||||||||||||||||||||||||
| 617 | 0 | push( @params, @args ); | |||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | 16 | ▶ | push( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 621 | @params, | ||||||||||||||||||||||||||||
| 622 | with_objects => $self->load_with, | ||||||||||||||||||||||||||||
| 623 | multi_many_ok => 1 | ||||||||||||||||||||||||||||
| 624 | ) if $self->load_with; | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | 16 | return $manager->$method(@params); | |||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | 4 | 1; | |||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | __END__ | ||||||||||||||||||||||||||||