File Coverage

blib/lib/App/Dochazka/REST/Model/Tempintvl.pm
Criterion Covered Total %
statement 26 54 48.1
branch 0 8 0.0
condition 0 18 0.0
subroutine 9 12 75.0
pod 3 3 100.0
total 38 95 40.0


line stmt bran cond sub pod time code
1             # *************************************************************************
2             # Copyright (c) 2014-2017, SUSE LLC
3             #
4             # All rights reserved.
5             #
6             # Redistribution and use in source and binary forms, with or without
7             # modification, are permitted provided that the following conditions are met:
8             #
9             # 1. Redistributions of source code must retain the above copyright notice,
10             # this list of conditions and the following disclaimer.
11             #
12             # 2. Redistributions in binary form must reproduce the above copyright
13             # notice, this list of conditions and the following disclaimer in the
14             # documentation and/or other materials provided with the distribution.
15             #
16             # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17             # used to endorse or promote products derived from this software without
18             # specific prior written permission.
19             #
20             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30             # POSSIBILITY OF SUCH DAMAGE.
31             # *************************************************************************
32              
33              
34             use 5.012;
35 41     41   677 use strict;
  41         164  
36 41     41   201 use warnings;
  41         80  
  41         806  
37 41     41   179  
  41         84  
  41         1261  
38             use App::CELL qw( $CELL $log $site );
39 41     41   224 use App::Dochazka::REST::Model::Shared qw(
  41         82  
  41         3980  
40 41         2129 canonicalize_tsrange
41             cud
42             load_multiple
43             tsrange_intersection
44             );
45 41     41   275 use Data::Dumper;
  41         94  
46 41     41   254 use Params::Validate qw( :all );
  41         92  
  41         2764  
47 41     41   250  
  41         94  
  41         5918  
48             # we get 'spawn', 'reset', and accessors from parent
49             use parent 'App::Dochazka::Common::Model::Tempintvl';
50 41     41   329  
  41         75  
  41         235  
51              
52              
53              
54             =head1 NAME
55              
56             App::Dochazka::REST::Model::Tempintvl - tempintvl data model
57              
58              
59              
60              
61             =head1 SYNOPSIS
62              
63             use App::Dochazka::REST::Model::Tempintvl;
64              
65             ...
66              
67              
68             =head1 DESCRIPTION
69              
70             A description of the tempinvl data model follows.
71              
72              
73             =head2 Tempintvls in the database
74              
75             CREATE TABLE tempintvls (
76             int_id serial PRIMARY KEY,
77             tiid integer NOT NULL,
78             intvl tstzrange NOT NULL
79             )
80              
81              
82              
83             =head1 EXPORTS
84              
85             This module provides the following exports:
86              
87             =cut
88              
89             use Exporter qw( import );
90 41     41   40201 our @EXPORT_OK = qw(
  41         87  
  41         19758  
91             fetch_tempintvls_by_tiid_and_tsrange
92             );
93              
94              
95              
96             =head1 METHODS
97              
98              
99             =head2 delete
100              
101             Attempts to the delete the record (in the tempintvls table) corresponding
102             to the object. Returns a status object.
103              
104             =cut
105              
106             my $self = shift;
107             my ( $context ) = validate_pos( @_, { type => HASHREF } );
108 0     0 1    
109 0           my $status = cud(
110             conn => $context->{'dbix_conn'},
111             eid => $context->{'current'}->{'eid'},
112             object => $self,
113 0           sql => $site->SQL_TEMPINTVL_DELETE_SINGLE,
114             attrs => [ 'int_id' ],
115             );
116             $self->reset( int_id => $self->{int_id} ) if $status->ok;
117              
118 0 0         return $status;
119             }
120 0            
121              
122             =head2 insert
123              
124             Instance method. Attempts to INSERT a record. Field values are taken from the
125             object. Returns a status object.
126              
127             =cut
128              
129             my $self = shift;
130             my ( $context ) = validate_pos( @_, { type => HASHREF } );
131              
132 0     0 1   my $status = cud(
133 0           conn => $context->{'dbix_conn'},
134             eid => $context->{'current'}->{'eid'},
135             object => $self,
136             sql => $site->SQL_TEMPINTVL_INSERT,
137 0           attrs => [ 'tiid', 'intvl' ],
138             );
139              
140             return $status;
141             }
142              
143 0            
144              
145             =head1 FUNCTIONS
146              
147              
148             =head2 fetch_tempintvls_by_tiid_and_tsrange
149              
150             Given a L<DBIx::Connector> object, a tiid and a tsrange, return the set
151             (array) of C<tempintvl> objects that match the tiid and tsrange.
152              
153             =cut
154              
155             my ( $conn, $tiid, $tsrange ) = validate_pos( @_,
156             { isa => 'DBIx::Connector' },
157             { type => SCALAR },
158             { type => SCALAR },
159 0     0 1   );
160              
161             my $status = canonicalize_tsrange( $conn, $tsrange );
162             return $status unless $status->ok;
163             $tsrange = $status->payload;
164              
165 0           $status = load_multiple(
166 0 0         conn => $conn,
167 0           class => __PACKAGE__,
168             sql => $site->SQL_TEMPINTVLS_SELECT_BY_TIID_AND_TSRANGE,
169 0           keys => [ $tiid, $tsrange, $site->DOCHAZKA_INTERVAL_SELECT_LIMIT ],
170             );
171             return $status unless
172             ( $status->ok and $status->code eq 'DISPATCH_RECORDS_FOUND' ) or
173             ( $status->level eq 'NOTICE' and $status->code eq 'DISPATCH_NO_RECORDS_FOUND' );
174             my $whole_intervals = $status->payload;
175 0 0 0        
      0        
      0        
176             $status = load_multiple(
177             conn => $conn,
178 0           class => __PACKAGE__,
179             sql => $site->SQL_TEMPINTVLS_SELECT_BY_TIID_AND_TSRANGE_PARTIAL_INTERVALS,
180 0           keys => [ $tiid, $tsrange, $tiid, $tsrange ],
181             );
182             return $status unless
183             ( $status->ok and $status->code eq 'DISPATCH_RECORDS_FOUND' ) or
184             ( $status->level eq 'NOTICE' and $status->code eq 'DISPATCH_NO_RECORDS_FOUND' );
185             my $partial_intervals = $status->payload;
186 0 0 0        
      0        
      0        
187             map
188             {
189 0           $_->intvl(
190             tsrange_intersection( $conn, $tsrange, $_->intvl )
191             );
192             } ( @$partial_intervals );
193 0          
  0            
194             my @result_set = ();
195             push @result_set, @$whole_intervals, @$partial_intervals;
196              
197             # But now the intervals are out of order
198 0           my @sorted_tmpintvls = sort { $a->intvl cmp $b->intvl } @result_set;
199 0           return \@sorted_tmpintvls;
200              
201             return \sort { $a->intvl cmp $b->intvl } @result_set;
202 0            
  0            
203 0           }
204              
205 0            
  0            
206              
207             =head1 AUTHOR
208              
209             Nathan Cutler, C<< <presnypreklad@gmail.com> >>
210              
211             =cut
212              
213             1;
214              
215