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             package App::Dochazka::REST::Model::Tempintvl;
34              
35 41     41   651 use 5.012;
  41         162  
36 41     41   189 use strict;
  41         77  
  41         697  
37 41     41   181 use warnings;
  41         83  
  41         1120  
38              
39 41     41   206 use App::CELL qw( $CELL $log $site );
  41         80  
  41         3873  
40 41         1912 use App::Dochazka::REST::Model::Shared qw(
41             canonicalize_tsrange
42             cud
43             load_multiple
44             tsrange_intersection
45 41     41   247 );
  41         115  
46 41     41   225 use Data::Dumper;
  41         85  
  41         2566  
47 41     41   229 use Params::Validate qw( :all );
  41         76  
  41         5346  
48              
49             # we get 'spawn', 'reset', and accessors from parent
50 41     41   246 use parent 'App::Dochazka::Common::Model::Tempintvl';
  41         83  
  41         215  
51              
52              
53              
54              
55             =head1 NAME
56              
57             App::Dochazka::REST::Model::Tempintvl - tempintvl data model
58              
59              
60              
61              
62             =head1 SYNOPSIS
63              
64             use App::Dochazka::REST::Model::Tempintvl;
65              
66             ...
67              
68              
69             =head1 DESCRIPTION
70              
71             A description of the tempinvl data model follows.
72              
73              
74             =head2 Tempintvls in the database
75              
76             CREATE TABLE tempintvls (
77             int_id serial PRIMARY KEY,
78             tiid integer NOT NULL,
79             intvl tstzrange NOT NULL
80             )
81              
82              
83              
84             =head1 EXPORTS
85              
86             This module provides the following exports:
87              
88             =cut
89              
90 41     41   32106 use Exporter qw( import );
  41         85  
  41         17409  
91             our @EXPORT_OK = qw(
92             fetch_tempintvls_by_tiid_and_tsrange
93             );
94              
95              
96              
97             =head1 METHODS
98              
99              
100             =head2 delete
101              
102             Attempts to the delete the record (in the tempintvls table) corresponding
103             to the object. Returns a status object.
104              
105             =cut
106              
107             sub delete {
108 0     0 1   my $self = shift;
109 0           my ( $context ) = validate_pos( @_, { type => HASHREF } );
110              
111             my $status = cud(
112             conn => $context->{'dbix_conn'},
113 0           eid => $context->{'current'}->{'eid'},
114             object => $self,
115             sql => $site->SQL_TEMPINTVL_DELETE_SINGLE,
116             attrs => [ 'int_id' ],
117             );
118 0 0         $self->reset( int_id => $self->{int_id} ) if $status->ok;
119              
120 0           return $status;
121             }
122              
123              
124             =head2 insert
125              
126             Instance method. Attempts to INSERT a record. Field values are taken from the
127             object. Returns a status object.
128              
129             =cut
130              
131             sub insert {
132 0     0 1   my $self = shift;
133 0           my ( $context ) = validate_pos( @_, { type => HASHREF } );
134              
135             my $status = cud(
136             conn => $context->{'dbix_conn'},
137 0           eid => $context->{'current'}->{'eid'},
138             object => $self,
139             sql => $site->SQL_TEMPINTVL_INSERT,
140             attrs => [ 'tiid', 'intvl' ],
141             );
142              
143 0           return $status;
144             }
145              
146              
147              
148             =head1 FUNCTIONS
149              
150              
151             =head2 fetch_tempintvls_by_tiid_and_tsrange
152              
153             Given a L<DBIx::Connector> object, a tiid and a tsrange, return the set
154             (array) of C<tempintvl> objects that match the tiid and tsrange.
155              
156             =cut
157              
158             sub fetch_tempintvls_by_tiid_and_tsrange {
159 0     0 1   my ( $conn, $tiid, $tsrange ) = validate_pos( @_,
160             { isa => 'DBIx::Connector' },
161             { type => SCALAR },
162             { type => SCALAR },
163             );
164              
165 0           my $status = canonicalize_tsrange( $conn, $tsrange );
166 0 0         return $status unless $status->ok;
167 0           $tsrange = $status->payload;
168              
169 0           $status = load_multiple(
170             conn => $conn,
171             class => __PACKAGE__,
172             sql => $site->SQL_TEMPINTVLS_SELECT_BY_TIID_AND_TSRANGE,
173             keys => [ $tiid, $tsrange, $site->DOCHAZKA_INTERVAL_SELECT_LIMIT ],
174             );
175 0 0 0       return $status unless
      0        
      0        
176             ( $status->ok and $status->code eq 'DISPATCH_RECORDS_FOUND' ) or
177             ( $status->level eq 'NOTICE' and $status->code eq 'DISPATCH_NO_RECORDS_FOUND' );
178 0           my $whole_intervals = $status->payload;
179              
180 0           $status = load_multiple(
181             conn => $conn,
182             class => __PACKAGE__,
183             sql => $site->SQL_TEMPINTVLS_SELECT_BY_TIID_AND_TSRANGE_PARTIAL_INTERVALS,
184             keys => [ $tiid, $tsrange, $tiid, $tsrange ],
185             );
186 0 0 0       return $status unless
      0        
      0        
187             ( $status->ok and $status->code eq 'DISPATCH_RECORDS_FOUND' ) or
188             ( $status->level eq 'NOTICE' and $status->code eq 'DISPATCH_NO_RECORDS_FOUND' );
189 0           my $partial_intervals = $status->payload;
190              
191             map
192             {
193 0           $_->intvl(
  0            
194             tsrange_intersection( $conn, $tsrange, $_->intvl )
195             );
196             } ( @$partial_intervals );
197            
198 0           my @result_set = ();
199 0           push @result_set, @$whole_intervals, @$partial_intervals;
200              
201             # But now the intervals are out of order
202 0           my @sorted_tmpintvls = sort { $a->intvl cmp $b->intvl } @result_set;
  0            
203 0           return \@sorted_tmpintvls;
204              
205 0           return \sort { $a->intvl cmp $b->intvl } @result_set;
  0            
206              
207             }
208              
209              
210              
211             =head1 AUTHOR
212              
213             Nathan Cutler, C<< <presnypreklad@gmail.com> >>
214              
215             =cut
216              
217             1;
218              
219