| 1 | package DBIx::Class::Storage::DBI::UniqueIdentifier; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use base 'DBIx::Class::Storage::DBI'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 6 | __PACKAGE__->mk_group_accessors(inherited => 'new_guid'); | |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | DBIx::Class::Storage::DBI::UniqueIdentifier - Storage component for RDBMSes | ||||||||||||||||||||||||||||||||||
| 13 | supporting GUID types | ||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | This is a storage component for databases that support GUID types such as | ||||||||||||||||||||||||||||||||||
| 18 | C<uniqueidentifier>, C<uniqueidentifierstr> or C<guid>. | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | GUIDs are generated automatically for PK columns with a supported | ||||||||||||||||||||||||||||||||||
| 21 | L<data_type|DBIx::Class::ResultSource/data_type>, as well as non-PK with | ||||||||||||||||||||||||||||||||||
| 22 | L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> set. | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | =head2 new_guid | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | The composing class must set C<new_guid> to the method used to generate a new | ||||||||||||||||||||||||||||||||||
| 29 | GUID. It can also set it to C<undef>, in which case the user is required to set | ||||||||||||||||||||||||||||||||||
| 30 | it, or a runtime error will be thrown. It can be: | ||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | =over 4 | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | =item string | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | In which case it is used as the name of database function to create a new GUID, | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | =item coderef | ||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | In which case the coderef should return a string GUID, using L<Data::GUID>, or | ||||||||||||||||||||||||||||||||||
| 41 | whatever GUID generation method you prefer. It is passed the C<$self> | ||||||||||||||||||||||||||||||||||
| 42 | L<DBIx::Class::Storage> reference as a parameter. | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | =back | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | For example: | ||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | $schema->storage->new_guid(sub { Data::GUID->new->as_string }); | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | 6 | my $GUID_TYPE = qr/^(?:uniqueidentifier(?:str)?|guid)\z/i; | |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub _is_guid_type { | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_guid_type: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 55 | 0 | my ($self, $data_type) = @_; | |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | 0 | return $data_type =~ $GUID_TYPE; | |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub _prefetch_autovalues { | ||||||||||||||||||||||||||||||||
|
Subroutine
_prefetch_autovalues: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||
| 61 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 62 | 0 | my ($source, $col_info, $to_insert) = @_; | |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | 0 | my %guid_cols; | |||||||||||||||||||||||||||||||||
| 65 | 0 | my @pk_cols = $source->primary_columns; | |||||||||||||||||||||||||||||||||
| 66 | 0 | my %pk_col_idx; | |||||||||||||||||||||||||||||||||
| 67 | 0 | @pk_col_idx{@pk_cols} = (); | |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | my @pk_guids = grep { | ||||||||||||||||||||||||||||||||||
| 70 | 0 | $col_info->{$_}{data_type} | |||||||||||||||||||||||||||||||||
| 71 | && | ||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $col_info->{$_}{data_type} =~ $GUID_TYPE | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$col_info->{$_}{'data_type'} && $col_info->{$_}{'data_type'} =~ /$GUID_TYPE/
MC/DC0%0/2
$col_info->{$_}{'data_type'} && $col_info->{$_}{'data_type'} =~ /$GUID_TYPE/
$col_info->{$_}{'data_type'}
$col_info->{$_}{'data_type'} =~ /$GUID_TYPE/
Truth table0%0/3
A && B
A= $col_info->{$_}{'data_type'}B= $col_info->{$_}{'data_type'} =~ /$GUID_TYPE/
| |||||||||||||||||||||||||||||||||||
| 73 | } @pk_cols; | ||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | my @auto_guids = grep { | ||||||||||||||||||||||||||||||||||
| 76 | $col_info->{$_}{data_type} | ||||||||||||||||||||||||||||||||||
| 77 | && | ||||||||||||||||||||||||||||||||||
| 78 | $col_info->{$_}{data_type} =~ $GUID_TYPE | ||||||||||||||||||||||||||||||||||
| 79 | && | ||||||||||||||||||||||||||||||||||
| 80 | $col_info->{$_}{auto_nextval} | ||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | } grep { not exists $pk_col_idx{$_} } $source->columns; | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
$col_info->{$_}{'data_type'} && $col_info->{$_}{'data_type'} =~ /$GUID_TYPE/ && $col_info->{$_}{'auto_nextval'}
$col_info->{$_}{"data_type"} and $col_info->{$_}{"data_type"} =~ /$GUID_TYPE/
MC/DC0%0/3
$col_info->{$_}{'data_type'} && $col_info->{$_}{'data_type'} =~ /$GUID_TYPE/ && $col_info->{$_}{'auto_nextval'}
$col_info->{$_}{"data_type"}
$col_info->{$_}{"data_type"} =~ /$GUID_TYPE/
$col_info->{$_}{'auto_nextval'}
Truth table0%0/4
A and B && C
A= $col_info->{$_}{"data_type"}B= $col_info->{$_}{"data_type"} =~ /$GUID_TYPE/C= $col_info->{$_}{'auto_nextval'}
| |||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | my @get_guids_for = | ||||||||||||||||||||||||||||||||||
| 84 | 0 | grep { not exists $to_insert->{$_} } (@pk_guids, @auto_guids); | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 0 | for my $guid_col (@get_guids_for) { | |||||||||||||||||||||||||||||||||
| 87 | 0 | my $new_guid; | |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | 0 | my $guid_method = $self->new_guid; | |||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (not defined $guid_method) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | 0 | $self->throw_exception( | |||||||||||||||||||||||||||||||||
| 93 | 'You must set new_guid() on your storage. See perldoc ' | ||||||||||||||||||||||||||||||||||
| 94 | .'DBIx::Class::Storage::DBI::UniqueIdentifier' | ||||||||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (ref $guid_method eq 'CODE') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 99 | 0 | $to_insert->{$guid_col} = $guid_method->($self); | |||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||||||||
| 102 | 0 | ($to_insert->{$guid_col}) = $self->_get_dbh->selectrow_array("SELECT $guid_method"); | |||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | 0 | return $self->next::method(@_); | |||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | =head1 FURTHER QUESTIONS? | ||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. | ||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> | ||||||||||||||||||||||||||||||||||
| 116 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can | ||||||||||||||||||||||||||||||||||
| 117 | redistribute it and/or modify it under the same terms as the | ||||||||||||||||||||||||||||||||||
| 118 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | 6 | 1; | |||||||||||||||||||||||||||||||||