1 package DBIx::Class::Storage::DBI::UniqueIdentifier;
5 use base 'DBIx::Class::Storage::DBI';
8 __PACKAGE__->mk_group_accessors(inherited => 'new_guid');
12 DBIx::Class::Storage::DBI::UniqueIdentifier - Storage component for RDBMSes
17 This is a storage component for databases that support GUID types such as
18 C<uniqueidentifier>, C<uniqueidentifierstr> or C<guid>.
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.
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:
36 In which case it is used as the name of database function to create a new GUID,
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.
48 $schema->storage->new_guid(sub { Data::GUID->new->as_string });
52 my $GUID_TYPE = qr/^(?:uniqueidentifier(?:str)?|guid)\z/i;
55 my ($self, $data_type) = @_;
57 return $data_type =~ $GUID_TYPE;
60 sub _prefetch_autovalues {
62 my ($source, $col_info, $to_insert) = @_;
65 my @pk_cols = $source->primary_columns;
67 @pk_col_idx{@pk_cols} = ();
70 $col_info->{$_}{data_type}
72 $col_info->{$_}{data_type} =~ $GUID_TYPE
75 my @auto_guids = grep {
76 $col_info->{$_}{data_type}
78 $col_info->{$_}{data_type} =~ $GUID_TYPE
80 $col_info->{$_}{auto_nextval}
81 } grep { not exists $pk_col_idx{$_} } $source->columns;
84 grep { not exists $to_insert->{$_} } (@pk_guids, @auto_guids);
86 for my $guid_col (@get_guids_for) {
89 my $guid_method = $self->new_guid;
91 if (not defined $guid_method) {
92 $self->throw_exception(
93 'You must set new_guid() on your storage. See perldoc '
94 .'DBIx::Class::Storage::DBI::UniqueIdentifier'
98 if (ref $guid_method eq 'CODE') {
99 $to_insert->{$guid_col} = $guid_method->($self);
102 ($to_insert->{$guid_col}) = $self->_get_dbh->selectrow_array("SELECT $guid_method");
106 return $self->next::method(@_);
109 =head1 FURTHER QUESTIONS?
111 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
113 =head1 COPYRIGHT AND LICENSE
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>.