| 1 | package OpenERP::OOM::Link::DBIC; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | OpenERP::OOM::Link::DBIC | ||||||||||
| 6 | |||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||
| 8 | |||||||||||
| 9 | Class used to link OpenERP data with data in DBIC. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 PROPERTIES | ||||||||||
| 12 | |||||||||||
| 13 | =head2 dbic_schema | ||||||||||
| 14 | |||||||||||
| 15 | This is the DBIC Schema object. If you need a generic DBIC schema object | ||||||||||
| 16 | this is normally the simplest way to access it. | ||||||||||
| 17 | |||||||||||
| 18 | =head1 METHODS | ||||||||||
| 19 | |||||||||||
| 20 | These methods are not normally called directly. | ||||||||||
| 21 | |||||||||||
| 22 | =head2 create | ||||||||||
| 23 | |||||||||||
| 24 | Returns the new ID of a row it creates in a table using DBIC. | ||||||||||
| 25 | |||||||||||
| 26 | my $id = $link->create({ class => 'RSName' }, $object_data); | ||||||||||
| 27 | |||||||||||
| 28 | It also ensures there is no _source property on the object. | ||||||||||
| 29 | |||||||||||
| 30 | =head2 retrieve | ||||||||||
| 31 | |||||||||||
| 32 | This is equivalent to doing a find on a ResultSet. | ||||||||||
| 33 | |||||||||||
| 34 | my $object = $link->retrieve({ class => 'RSName' }, $id); | ||||||||||
| 35 | |||||||||||
| 36 | =head2 search | ||||||||||
| 37 | |||||||||||
| 38 | This is equivalent to doing a search on a ResultSet and then returning a list | ||||||||||
| 39 | of all the id fields. | ||||||||||
| 40 | |||||||||||
| 41 | my @ids = $link->search({ class => 'RSName' }, $search, $options); | ||||||||||
| 42 | |||||||||||
| 43 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 44 | |||||||||||
| 45 | Copyright (C) 2011 OpusVL | ||||||||||
| 46 | |||||||||||
| 47 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | 2 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 2 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 2 | extends 'OpenERP::OOM::Link'; | |||||||||
| 55 | 2 | with 'OpenERP::OOM::DynamicUtils'; | |||||||||
| 56 | |||||||||||
| 57 | 2 | has 'dbic_schema' => ( | |||||||||
| 58 | is => 'ro', | ||||||||||
| 59 | lazy => 1, | ||||||||||
| 60 | builder => '_build_dbic_schema', | ||||||||||
| 61 | ); | ||||||||||
| 62 | |||||||||||
| 63 | sub _build_dbic_schema { | ||||||||||
| 64 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_dbic_schema: not called CC=1 SCAR=6.9
| |||||||||||
| 65 | |||||||||||
| 66 | 0 | $self->ensure_class_loaded($self->config->{schema_class}); | |||||||||
| 67 | |||||||||||
| 68 | 0 | return $self->config->{schema_class}->connect(@{$self->config->{connect_info}}); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | #------------------------------------------------------------------------------- | ||||||||||
| 73 | |||||||||||
| 74 | sub create { | ||||||||||
| 75 | 0 | ▶ | my ($self, $args, $data) = @_; | ||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 76 | |||||||||||
| 77 | try { | ||||||||||
| 78 | 0 | ▶ | if ($self->dbic_schema->resultset($args->{class})->result_class->can('_source')) { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 79 | # Because the _source() method is added to the result class, we need | ||||||||||
| 80 | # to remove it in case it gets called during create(). If that happens, | ||||||||||
| 81 | # we get bleed-through from one object to another (not good!) | ||||||||||
| 82 | 0 | $self->dbic_schema->resultset($args->{class})->result_class->meta->make_mutable; | |||||||||
| 83 | 0 | $self->dbic_schema->resultset($args->{class})->result_class->meta->remove_method('_source'); | |||||||||
| 84 | } | ||||||||||
| 85 | 0 | my $object = $self->dbic_schema->resultset($args->{class})->create($data); | |||||||||
| 86 | ### Created linked object with ID $object->id | ||||||||||
| 87 | 0 | return $object->id; | |||||||||
| 88 | } catch { | ||||||||||
| 89 | 0 | ▶ | die "Could not create linked object: $_"; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 90 | 0 | }; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | #------------------------------------------------------------------------------- | ||||||||||
| 95 | |||||||||||
| 96 | sub retrieve { | ||||||||||
| 97 | 0 | ▶ | my ($self, $args, $id) = @_; | ||||||||
|
Subroutine
retrieve: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | if (my $object = $self->dbic_schema->resultset($args->{class})->find($id)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | return $object; | |||||||||
| 101 | } | ||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | #------------------------------------------------------------------------------- | ||||||||||
| 106 | |||||||||||
| 107 | sub search { | ||||||||||
| 108 | 0 | ▶ | my ($self, $args, $search, $options) = @_; | ||||||||
|
Subroutine
search: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 109 | |||||||||||
| 110 | # FIXME - Foreign primary key column is hard-coded to "id" | ||||||||||
| 111 | 0 | return map {$_->id} $self->dbic_schema->resultset($args->{class})->search($search, $options)->all; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | #------------------------------------------------------------------------------- | ||||||||||
| 116 | |||||||||||
| 117 | 2 | 1; | |||||||||