File Coverage

blib/lib/DBIx/QuickORM/Affinity.pm
Criterion Covered Total %
statement 27 32 84.3
branch 12 22 54.5
condition 2 10 20.0
subroutine 7 8 87.5
pod 0 4 0.0
total 48 76 63.1


line stmt bran cond sub pod time code
1             package DBIx::QuickORM::Affinity;
2 377     377   2938 use strict;
  377         1175  
  377         15817  
3 377     377   1975 use warnings;
  377         695  
  377         31280  
4              
5             our $VERSION = '0.000019';
6              
7 377     377   2400 use Carp qw/croak/;
  377         1309  
  377         29741  
8              
9 377     377   2625 use base 'Exporter';
  377         875  
  377         337708  
10             our @EXPORT = qw{
11             valid_affinities
12             validate_affinity
13             compare_affinity_values
14             affinity_from_type
15             };
16              
17             my %VALID_AFFINITY = (
18             string => 'string',
19             numeric => 'numeric',
20             binary => 'binary',
21             boolean => 'boolean',
22             );
23              
24 0     0 0 0 sub valid_affinities { my %seen; sort grep { !$seen{$_}++ } values %VALID_AFFINITY }
  0         0  
  0         0  
25              
26 40 50   40 0 124 sub validate_affinity { my $affinity = pop or return; return $VALID_AFFINITY{$affinity} }
  40         319  
27              
28             sub compare_affinity_values {
29 25     25 0 96 my ($valb, $vala, $affinity, $class_or_self) = reverse @_;
30              
31 25 50       76 croak "'affinity' is required" unless $affinity;
32 25 50       114 croak "'$affinity' is not a valid affinity" unless $VALID_AFFINITY{$affinity};
33              
34             # For boolean undef is false, so we do not do the undef check until after
35             # this
36 25 50 0     71 return ($vala xor $valb) if $affinity eq 'boolean';
37              
38             # For the rest of these it is false if only 1 of the 2 is defined
39 25 50 25     147 return 0 if (defined($vala) xor defined($valb));
40              
41 25 100       134 return ($vala eq $valb) if $affinity eq 'string';
42 11 50       92 return ($vala == $valb) if $affinity eq 'numeric';
43              
44             # I am sceptical about 'eq' here. Are there places where 2 strings may
45             # compare as equal because of encodings or other stuff, even when the
46             # binary data is different? According to #perl everyone says no, so I can
47             # just use 'eq'.
48 0 0       0 return ($vala eq $valb) if $affinity eq 'binary';
49              
50 0         0 croak "Comparison for affinity '$affinity' fell off the end. Please file a bug report.";
51             }
52              
53             my %AFFINITY_BY_TYPE = (
54             # Stringy
55             char => 'string',
56             json => 'string',
57             string => 'string',
58             text => 'string',
59             bpchar => 'string',
60              
61             # Special
62             enum => 'string',
63             jsonb => 'string',
64             money => 'string',
65             set => 'string',
66             uuid => 'string',
67              
68             # Binary
69             binary => 'binary',
70             blob => 'binary',
71             bytea => 'binary',
72              
73             # Numeric
74             'double precision' => 'numeric',
75              
76             bit => 'numeric',
77             dec => 'numeric',
78             decimal => 'numeric',
79             double => 'numeric',
80             float => 'numeric',
81             int => 'numeric',
82             integer => 'numeric',
83             number => 'numeric',
84             numeric => 'numeric',
85             real => 'numeric',
86             serial => 'numeric',
87              
88             # Date/Time
89             date => 'string',
90             day => 'string',
91             interval => 'string',
92             stamp => 'string',
93             time => 'string',
94             timestamp => 'string',
95             timestamptz => 'string',
96             year => 'string',
97              
98             # Boolean
99             bool => 'boolean',
100             boolean => 'boolean',
101             );
102              
103             sub affinity_from_type {
104 87 50   87 0 343 my $type = pop or return undef;
105              
106 87         208 $type = lc($type);
107 87         568 $type =~ s/\s*\(.*\)\s*$//;
108              
109 87 100       633 if ($type =~ m/^(?:tiny|medium|big|long|var)(.+)/i) {
110 36 50       758 return $AFFINITY_BY_TYPE{$1} if $AFFINITY_BY_TYPE{$1};
111             }
112              
113 51   50     651 return $AFFINITY_BY_TYPE{$type} // undef;
114             }
115              
116             1;