| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 10 | ▶ | return 'binary(16)' if $name eq 'MariaDB' && ($dialect->server_version // 0) < 10_007_000; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$name eq "MariaDB" and ($dialect->server_version // 0) < 10007000
$dialect->server_version // 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 79 | my $size = $field->{size}; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 79 | ▶ | my $affinity = $field->{affinity} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$field->{"affinity"} // ""
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 79 | ▶ | return 1 if $affinity eq 'binary' && (!$size || $size == 16); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$affinity eq "binary" and !$size || $size == 16
not $size or $size == 16
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
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 61 | ▶ | return 1 if $affinity eq 'string' && (!$size || $size == 36); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$affinity eq "string" and !$size || $size == 36
not $size or $size == 36
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$field->{"affinity"} // ""
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$_[1]{"affinity"} // ""
MC/DC100%1/1
$_[1]{"affinity"} // ""
$_[1]{"affinity"}
Truth table100%2/2
A
A= $_[1]{"affinity"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC100%1/1
$field->{"affinity"} // ""
$field->{"affinity"}
Truth table100%2/2
A
A= $field->{"affinity"}
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 87 | ▶ | croak "'$value' is not a UUID" . $class->_for_field($field) if ref($value) || ($binary && $value !~ $UUID); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
ref $value or $binary and not $value =~ /$UUID/
$binary and not $value =~ /$UUID/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 84 | ▶ | return $value unless $binary; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $value =~ /[A-F]/ and $len == 36
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
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 56 | ▶ | return lc($value) if $len == 36; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 42 | ▶ | return $class->inflate($value, {affinity => 'binary'}) if $len == 16; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 7 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 71 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | __END__ | ||||||||||||||||||||||||||||||||||||||||||