blib/lib/DBIx/QuickORM/Type/UUID.pm

filter: Statementstmt 100% 56 / 56 Branchbran 100% 24 / 24 Conditioncond 100% 29 / 29 MC/DCmcdc 100% 15 / 15 Subroutinesub 100% 14 / 14 Podpod 100% 9 / 9 totaltotal 100% 147 / 147 CC 24 SCAR 15.8
CC
24
Cov
100%
matches
9.0
deflate
6.0
canonical_key_value
5.0
CRAP 4.8
« blib/lib/DBIx/QuickORM/Source/Table.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect.pm »
1 package DBIx::QuickORM::Type::UUID;
2 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 50 our $VERSION = '0.000031';
6  
7 50 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 50 ▶use UUID();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 50 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 50 with 'DBIx::QuickORM::Role::Type';
12  
13 =pod
14  
15 =encoding UTF-8
16  
17 =head1 NAME
18  
19 DBIx::QuickORM::Type::UUID - UUIDs stored as text or as 16 bytes.
20  
21 =head1 DESCRIPTION
22  
23 The inflated form is the 36-character string. A field of C<binary> affinity
24 (C<binary(16)>, C<bytea>, C<BLOB>) stores the 16 bytes. Any other field,
25 including a native C<uuid> column, stores the string as given.
26  
27 A primary key field of this type that an insert leaves out gets a new
28 version 7 UUID, unless the database fills the column and can return it
29 with C<RETURNING> (so on MySQL it is generated). The field's
30 C<generate> hint changes that (see
31 L<DBIx::QuickORM::Role::Type/Field hints>).
32  
33 =head1 SYNOPSIS
34  
35 # CREATE TABLE sessions (id UUID PRIMARY KEY, owner_uuid BYTEA)
36  
37 my $uuid = '0190a6e8-1d2c-7c3b-9f00-123456789abc';
38 my $session = $orm->insert(
39 sessions => {
40 id => $uuid,
41 owner_uuid => $uuid,
42 },
43 );
44  
45 my $sid = $session->field('id'); # the string, from a native uuid column
46 my $owner = $session->field('owner_uuid'); # the string, stored as 16 bytes
47  
48 my $fresh = $orm->insert(sessions => {owner_uuid => $uuid}); # id gets a new version 7 UUID
49  
50 =cut
51  
52 50 my $UUID = qr/^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}\z/i;
53  
54 =head1 PUBLIC METHODS
55  
56 =over 4
57  
58 =item @types = $type->base_types
59  
60 C<uuid>.
61  
62 =item $bool = $type->inflated_is_immutable_for_cache
63  
64 True: a UUID inflates to a plain string.
65  
66 =back
67  
68 =cut
69  
70 268 ▶sub base_types { 'uuid' }
Subroutine base_types: called CC=1 SCAR=0.0
Pod documented
71 1 ▶sub inflated_is_immutable_for_cache { 1 }
Subroutine inflated_is_immutable_for_cache: called CC=1 SCAR=0.0
Pod documented
72  
73 =pod
74  
75 =over 4
76  
77 =item $bool = $type->is_inflated($value)
78  
79 True for a 36-character UUID string.
80  
81 =back
82  
83 =cut
84  
85 61 ▶sub is_inflated {
Subroutine is_inflated: called CC=2 SCAR=6.9
Pod documented
86 61 my $class = shift;
87 61 my ($value) = @_;
88 61 ▶ return $value =~ $UUID ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /$UUID/i ? : 52 9 61
89 }
90  
91 =pod
92  
93 =over 4
94  
95 =item $bool = $type->matches($field_name, \%field, $dialect)
96  
97 True for a field named C<uuid>, C<..._uuid>, C<uuid_...>, or C<..._uuid_...>
98 that can hold one: C<binary> affinity of size 16 or no size, or C<string>
99 affinity of size 36 or no size.
100  
101 C<uuid_count int> is not a UUID.
102  
103 =back
104  
105 =cut
106  
107 1759 ▶sub matches {
Subroutine matches: called CC=9 SCAR=22.0
Pod documented
108 1759 my $class = shift;
109 1759 my ($name, $field) = @_;
110  
111 1759 ▶ return 0 unless $name =~ m/(?:^|_)uuid(?:_|$)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /(?:^|_)uuid(?:_|$)/i 1697 62 1759
112 62 my $size = $field->{size};
113 62 ▶ my $affinity = $field->{affinity} // '';
Condition100%2/2
$field->{"affinity"} // ""
l!l
611
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
Aresult
11
00
114 62 ▶ return 1 if $affinity eq 'binary' && (!$size || $size == 16);
Branch: 1 branch
BranchTrueFalseTotal
if $affinity eq "binary" and !$size || $size == 16 15 47 62
Condition100%6/6
$affinity eq "binary" and !$size || $size == 16
!ll&&!rl&&r
46115
not $size or $size == 16
l!l&&r!l&&!r
5101
MC/DC100%3/3
$affinity eq "binary" and !$size || $size == 16
$affinity eq "binary" not $size $size == 16
Truth table100%4/4
A and not(B) or C
A= $affinity eq "binary"B= not $sizeC= $size == 16
ABCresult
0XX0
1000
11X1
1011
115 47 ▶ return 1 if $affinity eq 'string' && (!$size || $size == 36);
Branch: 1 branch
BranchTrueFalseTotal
if $affinity eq "string" and !$size || $size == 36 31 16 47
Condition100%6/6
$affinity eq "string" and !$size || $size == 36
!ll&&!rl&&r
15131
not $size or $size == 36
l!l&&r!l&&!r
5261
MC/DC100%3/3
$affinity eq "string" and !$size || $size == 36
$affinity eq "string" not $size $size == 36
Truth table100%4/4
A and not(B) or C
A= $affinity eq "string"B= not $sizeC= $size == 36
ABCresult
0XX0
1000
11X1
1011
116 16 return 0;
117 }
118  
119 =pod
120  
121 =over 4
122  
123 =item $uuid = $type->inflate($raw, \%field, $conn)
124  
125 The lowercase string for the 16 bytes of a binary field, or text as stored.
126  
127 A binary value that is not 16 bytes croaks, naming the field when its
128 record has its C<table> and C<name>.
129  
130 =back
131  
132 =cut
133  
134 72 ▶sub inflate {
Subroutine inflate: called CC=4 SCAR=13.9
Pod documented
135 72 my $class = shift;
136 72 my ($raw, $field) = @_;
137 72 ▶ return $raw unless ($field->{affinity} // '') eq 'binary';
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->{"affinity"} // "") eq "binary" 29 43 72
Condition100%2/2
$field->{"affinity"} // ""
l!l
711
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
Aresult
11
00
138  
139 43 ▶ croak "A binary UUID must be 16 bytes, got " . length($raw) . $class->_for_field($field) unless length($raw) == 16;
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw == 16 2 41 43
140  
141 41 my $str;
142 41 UUID::unparse_lower($raw, $str);
143 41 return $str;
144 }
145  
146 =pod
147  
148 =over 4
149  
150 =item $bool = $type->inflate_is_identity(\%field)
151  
152 True for a text field, which C<inflate> returns as stored.
153  
154 =back
155  
156 =cut
157  
158 105 ▶sub inflate_is_identity { ($_[1]->{affinity} // '') ne 'binary' ? 1 : 0 }
Subroutine inflate_is_identity: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
($_[1]{'affinity'} // '') ne 'binary' ? : 77 28 105
Condition100%2/2
$_[1]{"affinity"} // ""
l!l
1032
MC/DC100%1/1
$_[1]{"affinity"} // ""
$_[1]{"affinity"}
Truth table100%2/2
A
A= $_[1]{"affinity"}
Aresult
11
00
159  
160 =pod
161  
162 =over 4
163  
164 =item $raw = $type->deflate($uuid, \%field, $conn)
165  
166 The 16 bytes for a binary field, or the value as given for any other field.
167  
168 A reference croaks. For a binary field, so does anything but a UUID string.
169 The croak names the field when its record has its C<table> and C<name>.
170  
171 =back
172  
173 =cut
174  
175 67 ▶sub deflate {
Subroutine deflate: called CC=6 SCAR=17.9
Pod documented
176 67 my $class = shift;
177 67 my ($value, $field) = @_;
178  
179 67 ▶ my $binary = ($field->{affinity} // '') eq 'binary';
Condition100%2/2
$field->{"affinity"} // ""
l!l
661
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
Aresult
11
00
180 67 ▶ croak "'$value' is not a UUID" . $class->_for_field($field) if ref($value) || ($binary && $value !~ $UUID);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value or $binary and not $value =~ /$UUID/ 3 64 67
Condition100%6/6
ref $value or $binary and not $value =~ /$UUID/
l!l&&r!l&&!r
1264
$binary and not $value =~ /$UUID/
!ll&&!rl&&r
35292
MC/DC100%3/3
ref $value or $binary and not $value =~ /$UUID/
ref $value $binary not $value =~ /$UUID/
Truth table100%4/4
A or B and not(C)
A= ref $valueB= $binaryC= not $value =~ /$UUID/
ABCresult
1XX1
0111
00X0
0100
181 64 ▶ return $value unless $binary;
Branch: 1 branch
BranchTrueFalseTotal
unless $binary 35 29 64
182  
183 29 my $bin;
184 29 UUID::parse($value, $bin);
185 29 return $bin;
186 }
187  
188 =pod
189  
190 =over 4
191  
192 =item $uuid = $type->generate_key_value(\%field, $conn)
193  
194 A new version 7 UUID string, from L<UUID/uuid7>.
195  
196 =back
197  
198 =cut
199  
200 11 ▶sub generate_key_value { UUID::uuid7() }
Subroutine generate_key_value: called CC=1 SCAR=0.0
Pod documented
201  
202 =pod
203  
204 =over 4
205  
206 =item $key = $type->canonical_key_value($value, \%field, $conn)
207  
208 The lowercase hyphenated string for a key value given as that string, in
209 upper case, or as 16 bytes, told apart by length alone (the digits are not
210 checked). Any other length is returned as given.
211  
212 =back
213  
214 =cut
215  
216 90 ▶sub canonical_key_value {
Subroutine canonical_key_value: called CC=5 SCAR=16.1
Pod documented
217 90 my $class = shift;
218 90 my ($value) = @_;
219 90 my $len = length($value);
220 90 ▶ return $value if $value !~ m/[A-F]/ && $len == 36;
Branch: 1 branch
BranchTrueFalseTotal
if not $value =~ /[A-F]/ and $len == 36 30 60 90
Condition100%3/3
not $value =~ /[A-F]/ and $len == 36
!ll&&!rl&&r
213930
MC/DC100%2/2
not $value =~ /[A-F]/ and $len == 36
not $value =~ /[A-F]/ $len == 36
Truth table100%3/3
not(A) and B
A= not $value =~ /[A-F]/B= $len == 36
ABresult
0X0
100
111
221 60 ▶ return lc($value) if $len == 36;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 36 12 48 60
222 48 ▶ return $class->inflate($value, {affinity => 'binary'}) if $len == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 16 39 9 48
223 9 return $value;
224 }
225  
226 50 1;
227  
228 __END__
« blib/lib/DBIx/QuickORM/Source/Table.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect.pm »