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 65 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 65 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 65 our $VERSION = '0.000032';
6  
7 65 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 65 ▶use UUID();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 65 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 65 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 65 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 314 ▶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 75 ▶sub is_inflated {
Subroutine is_inflated: called CC=2 SCAR=6.9
Pod documented
86 75 my $class = shift;
87 75 my ($value) = @_;
88 75 ▶ return $value =~ $UUID ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /$UUID/i ? : 66 9 75
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 2219 ▶sub matches {
Subroutine matches: called CC=9 SCAR=22.0
Pod documented
108 2219 my $class = shift;
109 2219 my ($name, $field) = @_;
110  
111 2219 ▶ return 0 unless $name =~ m/(?:^|_)uuid(?:_|$)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /(?:^|_)uuid(?:_|$)/i 2147 72 2219
112 72 my $size = $field->{size};
113 72 ▶ my $affinity = $field->{affinity} // '';
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
114 72 ▶ return 1 if $affinity eq 'binary' && (!$size || $size == 16);
Branch: 1 branch
BranchTrueFalseTotal
if $affinity eq "binary" and !$size || $size == 16 16 56 72
Condition100%6/6
$affinity eq "binary" and !$size || $size == 16
!ll&&!rl&&r
55116
not $size or $size == 16
l!l&&r!l&&!r
5111
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 56 ▶ return 1 if $affinity eq 'string' && (!$size || $size == 36);
Branch: 1 branch
BranchTrueFalseTotal
if $affinity eq "string" and !$size || $size == 36 39 17 56
Condition100%6/6
$affinity eq "string" and !$size || $size == 36
!ll&&!rl&&r
16139
not $size or $size == 36
l!l&&r!l&&!r
5341
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 17 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 68 ▶sub inflate {
Subroutine inflate: called CC=4 SCAR=13.9
Pod documented
135 68 my $class = shift;
136 68 my ($raw, $field) = @_;
137 68 ▶ return $raw unless ($field->{affinity} // '') eq 'binary';
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->{"affinity"} // "") eq "binary" 29 39 68
Condition100%2/2
$field->{"affinity"} // ""
l!l
671
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
Aresult
11
00
138  
139 39 ▶ 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 37 39
140  
141 37 my $str;
142 37 UUID::unparse_lower($raw, $str);
143 37 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 120 ▶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' ? : 91 29 120
Condition100%2/2
$_[1]{"affinity"} // ""
l!l
1182
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 81 ▶sub deflate {
Subroutine deflate: called CC=6 SCAR=17.9
Pod documented
176 81 my $class = shift;
177 81 my ($value, $field) = @_;
178  
179 81 ▶ my $binary = ($field->{affinity} // '') eq 'binary';
Condition100%2/2
$field->{"affinity"} // ""
l!l
801
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
Aresult
11
00
180 81 ▶ 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 78 81
Condition100%6/6
ref $value or $binary and not $value =~ /$UUID/
l!l&&r!l&&!r
1278
$binary and not $value =~ /$UUID/
!ll&&!rl&&r
49292
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 78 ▶ return $value unless $binary;
Branch: 1 branch
BranchTrueFalseTotal
unless $binary 49 29 78
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 13 ▶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 83 ▶sub canonical_key_value {
Subroutine canonical_key_value: called CC=5 SCAR=16.1
Pod documented
217 83 my $class = shift;
218 83 my ($value) = @_;
219 83 my $len = length($value);
220 83 ▶ return $value if $value !~ m/[A-F]/ && $len == 36;
Branch: 1 branch
BranchTrueFalseTotal
if not $value =~ /[A-F]/ and $len == 36 30 53 83
Condition100%3/3
not $value =~ /[A-F]/ and $len == 36
!ll&&!rl&&r
193430
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 53 ▶ return lc($value) if $len == 36;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 36 11 42 53
222 42 ▶ return $class->inflate($value, {affinity => 'binary'}) if $len == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 16 35 7 42
223 7 return $value;
224 }
225  
226 65 1;
227  
228 __END__
« blib/lib/DBIx/QuickORM/Source/Table.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect.pm »