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

filter: Statementstmt 100% 62 / 62 Branchbran 100% 28 / 28 Conditioncond 97.0% 33 / 34 MC/DCmcdc 94.1% 16 / 17 Subroutinesub 100% 15 / 15 Podpod 100% 10 / 10 totaltotal 98.8% 164 / 166 CC 28 SCAR 15.8
CC
28
Cov
99%
matches
9.0
deflate
6.0
db_type_for
5.0
CRAP 4.9
« blib/lib/DBIx/QuickORM/Txn.pm ↑ Summary blib/lib/DBIx/QuickORM/JoinRow.pm »
1 package DBIx::QuickORM::Type::UUID;
2 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 71 our $VERSION = '0.000033';
6  
7 71 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 71 ▶use UUID();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 71 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 71 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 71 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 431 ▶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 $db_type = $type->db_type_for($dialect)
78  
79 C<uuid> where the database has the type (PostgreSQL, SQLite as a declared
80 name, DuckDB, MariaDB 10.7 and later), else C<binary(16)>.
81  
82 =back
83  
84 =cut
85  
86 13 ▶sub db_type_for {
Subroutine db_type_for: called CC=5 SCAR=16.1
Pod documented
87 13 my $self = shift;
88 13 my ($dialect) = @_;
89 13 my $name = $dialect->name;
90 13 ▶ return 'binary(16)' if $name eq 'MySQL';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "MySQL" 3 10 13
91 10 ▶ return 'binary(16)' if $name eq 'MariaDB' && ($dialect->server_version // 0) < 10_007_000;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "MariaDB" and ($dialect->server_version // 0) < 10007000 3 7 10
Condition80%4/5
$name eq "MariaDB" and ($dialect->server_version // 0) < 10007000
!ll&&!rl&&r
433
$dialect->server_version // 0
l!l
60
MC/DC50%1/2
$name eq "MariaDB" and ($dialect->server_version // 0) < 10007000
$name eq "MariaDB" $dialect->server_version
Truth table66%2/3
A and B
A= $name eq "MariaDB"B= $dialect->server_version
ABresult
0X0
100
111
92 7 return 'uuid';
93 }
94  
95 =pod
96  
97 =over 4
98  
99 =item $bool = $type->is_inflated($value)
100  
101 True for a 36-character UUID string.
102  
103 =back
104  
105 =cut
106  
107 80 ▶sub is_inflated {
Subroutine is_inflated: called CC=2 SCAR=6.9
Pod documented
108 80 my $class = shift;
109 80 my ($value) = @_;
110 80 ▶ return $value =~ $UUID ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /$UUID/i ? : 71 9 80
111 }
112  
113 =pod
114  
115 =over 4
116  
117 =item $bool = $type->matches($field_name, \%field, $dialect)
118  
119 True for a field named C<uuid>, C<..._uuid>, C<uuid_...>, or C<..._uuid_...>
120 that can hold one: C<binary> affinity of size 16 or no size, or C<string>
121 affinity of size 36 or no size.
122  
123 C<uuid_count int> is not a UUID.
124  
125 =back
126  
127 =cut
128  
129 2770 ▶sub matches {
Subroutine matches: called CC=9 SCAR=22.0
Pod documented
130 2770 my $class = shift;
131 2770 my ($name, $field) = @_;
132  
133 2770 ▶ return 0 unless $name =~ m/(?:^|_)uuid(?:_|$)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /(?:^|_)uuid(?:_|$)/i 2691 79 2770
134 79 my $size = $field->{size};
135 79 ▶ my $affinity = $field->{affinity} // '';
Condition100%2/2
$field->{"affinity"} // ""
l!l
781
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
Aresult
11
00
136 79 ▶ return 1 if $affinity eq 'binary' && (!$size || $size == 16);
Branch: 1 branch
BranchTrueFalseTotal
if $affinity eq "binary" and !$size || $size == 16 18 61 79
Condition100%6/6
$affinity eq "binary" and !$size || $size == 16
!ll&&!rl&&r
60118
not $size or $size == 16
l!l&&r!l&&!r
6121
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
137 61 ▶ return 1 if $affinity eq 'string' && (!$size || $size == 36);
Branch: 1 branch
BranchTrueFalseTotal
if $affinity eq "string" and !$size || $size == 36 43 18 61
Condition100%6/6
$affinity eq "string" and !$size || $size == 36
!ll&&!rl&&r
17143
not $size or $size == 36
l!l&&r!l&&!r
5381
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
138 18 return 0;
139 }
140  
141 =pod
142  
143 =over 4
144  
145 =item $uuid = $type->inflate($raw, \%field, $conn)
146  
147 The lowercase string for the 16 bytes of a binary field, or text as stored.
148 The example under C<deflate> shows both.
149  
150 A binary value that is not 16 bytes croaks, naming the field when its
151 record has its C<table> and C<name>.
152  
153 =back
154  
155 =cut
156  
157 68 ▶sub inflate {
Subroutine inflate: called CC=4 SCAR=13.9
Pod documented
158 68 my $class = shift;
159 68 my ($raw, $field) = @_;
160 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
161  
162 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
163  
164 37 my $str;
165 37 UUID::unparse_lower($raw, $str);
166 37 return $str;
167 }
168  
169 =pod
170  
171 =over 4
172  
173 =item $bool = $type->inflate_is_identity(\%field)
174  
175 True for a text field, which C<inflate> returns as stored.
176  
177 =back
178  
179 =cut
180  
181 131 ▶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' ? : 100 31 131
Condition100%2/2
$_[1]{"affinity"} // ""
l!l
1292
MC/DC100%1/1
$_[1]{"affinity"} // ""
$_[1]{"affinity"}
Truth table100%2/2
A
A= $_[1]{"affinity"}
Aresult
11
00
182  
183 =pod
184  
185 =over 4
186  
187 =item $raw = $type->deflate($uuid, \%field, $conn)
188  
189 The 16 bytes for a binary field, or the value as given for any other field.
190  
191 my $uuid = '0190a6e8-1d2c-7c3b-9f00-123456789abc';
192 my $type = 'DBIx::QuickORM::Type::UUID';
193  
194 my $bytes = $type->deflate($uuid, {affinity => 'binary'}); # 16 bytes
195 my $back = $type->inflate($bytes, {affinity => 'binary'}); # $uuid again
196  
197 my $text = $type->deflate($uuid, {affinity => 'string'}); # $uuid as given
198 my $same = $type->inflate($text, {affinity => 'string'}); # $uuid as stored
199  
200 A reference croaks. For a binary field, so does anything but a UUID string.
201 The croak names the field when its record has its C<table> and C<name>.
202  
203 =back
204  
205 =cut
206  
207 87 ▶sub deflate {
Subroutine deflate: called CC=6 SCAR=17.9
Pod documented
208 87 my $class = shift;
209 87 my ($value, $field) = @_;
210  
211 87 ▶ my $binary = ($field->{affinity} // '') eq 'binary';
Condition100%2/2
$field->{"affinity"} // ""
l!l
861
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
Aresult
11
00
212 87 ▶ 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 84 87
Condition100%6/6
ref $value or $binary and not $value =~ /$UUID/
l!l&&r!l&&!r
1284
$binary and not $value =~ /$UUID/
!ll&&!rl&&r
55292
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
213 84 ▶ return $value unless $binary;
Branch: 1 branch
BranchTrueFalseTotal
unless $binary 55 29 84
214  
215 29 my $bin;
216 29 UUID::parse($value, $bin);
217 29 return $bin;
218 }
219  
220 =pod
221  
222 =over 4
223  
224 =item $uuid = $type->generate_key_value(\%field, $conn)
225  
226 A new version 7 UUID string, from L<UUID/uuid7>.
227  
228 =back
229  
230 =cut
231  
232 13 ▶sub generate_key_value { UUID::uuid7() }
Subroutine generate_key_value: called CC=1 SCAR=0.0
Pod documented
233  
234 =pod
235  
236 =over 4
237  
238 =item $key = $type->canonical_key_value($value, \%field, $conn)
239  
240 The lowercase hyphenated string for a key value.
241  
242 my $type = 'DBIx::QuickORM::Type::UUID';
243 my $uuid = '0190a6e8-1d2c-7c3b-9f00-123456789abc';
244  
245 # each gives $uuid
246 my $from_text = $type->canonical_key_value($uuid);
247 my $from_upper = $type->canonical_key_value(uc $uuid);
248 my $from_bytes = $type->canonical_key_value($type->deflate($uuid, {affinity => 'binary'}));
249  
250 The value may be that string, in upper case, or 16 bytes, told apart by
251 length alone (the digits are not checked). Any other length is returned as
252 given.
253  
254 =back
255  
256 =cut
257  
258 101 ▶sub canonical_key_value {
Subroutine canonical_key_value: called CC=5 SCAR=16.1
Pod documented
259 101 my $class = shift;
260 101 my ($value) = @_;
261 101 my $len = length($value);
262 101 ▶ return $value if $value !~ m/[A-F]/ && $len == 36;
Branch: 1 branch
BranchTrueFalseTotal
if not $value =~ /[A-F]/ and $len == 36 45 56 101
Condition100%3/3
not $value =~ /[A-F]/ and $len == 36
!ll&&!rl&&r
233345
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
263 56 ▶ return lc($value) if $len == 36;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 36 14 42 56
264 42 ▶ return $class->inflate($value, {affinity => 'binary'}) if $len == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 16 35 7 42
265 7 return $value;
266 }
267  
268 71 1;
269  
270 __END__
« blib/lib/DBIx/QuickORM/Txn.pm ↑ Summary blib/lib/DBIx/QuickORM/JoinRow.pm »