Spleling fixes all over.
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / UUIDColumns.pm
CommitLineData
7da9cd47 1package DBIx::Class::UUIDColumns;
bf5ecff9 2
3use strict;
4use warnings;
5
1edd1722 6use base qw/DBIx::Class/;
7da9cd47 7
7da9cd47 8__PACKAGE__->mk_classdata( 'uuid_auto_columns' => [] );
947acfd9 9__PACKAGE__->mk_classdata( 'uuid_maker' );
10__PACKAGE__->uuid_class( __PACKAGE__->_find_uuid_module );
7da9cd47 11
7da9cd47 12# be compatible with Class::DBI::UUID
13sub uuid_columns {
14 my $self = shift;
15 for (@_) {
701da8c4 16 $self->throw_exception("column $_ doesn't exist") unless $self->has_column($_);
7da9cd47 17 }
18 $self->uuid_auto_columns(\@_);
19}
20
947acfd9 21sub uuid_class {
22 my ($self, $class) = @_;
23
24 if ($class) {
25 $class = "DBIx::Class::UUIDMaker$class" if $class =~ /^::/;
26
27 if (!eval "require $class") {
28 $self->throw_exception("$class could not be loaded: $@");
29 } elsif (!$class->isa('DBIx::Class::UUIDMaker')) {
30 $self->throw_exception("$class is not a UUIDMaker subclass");
31 } else {
32 $self->uuid_maker($class->new);
33 };
34 };
35
36 return ref $self->uuid_maker;
37};
38
7da9cd47 39sub insert {
3b7992e1 40 my $self = shift;
7da9cd47 41 for my $column (@{$self->uuid_auto_columns}) {
103647d5 42 $self->store_column( $column, $self->get_uuid )
43 unless defined $self->get_column( $column );
7da9cd47 44 }
3b7992e1 45 $self->next::method(@_);
7da9cd47 46}
47
48sub get_uuid {
947acfd9 49 return shift->uuid_maker->as_string;
7da9cd47 50}
51
947acfd9 52sub _find_uuid_module {
e78f023a 53 if (eval{require Data::UUID}) {
54 return '::Data::UUID';
55 } elsif ($^O ne 'openbsd' && eval{require APR::UUID}) {
880a1a0c 56 # APR::UUID on openbsd causes some as yet unfound nastiness for XS
947acfd9 57 return '::APR::UUID';
58 } elsif (eval{require UUID}) {
59 return '::UUID';
947acfd9 60 } elsif (eval{
61 # squelch the 'too late for INIT' warning in Win32::API::Type
62 local $^W = 0;
63 require Win32::Guidgen;
64 }) {
65 return '::Win32::Guidgen';
66 } elsif (eval{require Win32API::GUID}) {
67 return '::Win32API::GUID';
68 } else {
69 shift->throw_exception('no suitable uuid module could be found')
70 };
71};
72
e78f023a 731;
74__END__
75
76=head1 NAME
77
78DBIx::Class::UUIDColumns - Implicit uuid columns
79
80=head1 SYNOPSIS
81
82 package Artist;
83 __PACKAGE__->load_components(qw/UUIDColumns Core DB/);
84 __PACKAGE__->uuid_columns( 'artist_id' );
85
86=head1 DESCRIPTION
87
88This L<DBIx::Class> component resembles the behaviour of
89L<Class::DBI::UUID>, to make some columns implicitly created as uuid.
90
91When loaded, C<UUIDColumns> will search for a suitable uuid generation module
92from the following list of supported modules:
93
94 Data::UUID
95 APR::UUID*
96 UUID
97 Win32::Guidgen
98 Win32API::GUID
99
100If no supporting module can be found, an exception will be thrown.
101
102*APR::UUID will not be loaded under OpenBSD due to an as yet unidentified XS
103issue.
104
105If you would like to use a specific module, you can set C<uuid_class>:
106
107 __PACKAGE__->uuid_class('::Data::UUID');
108 __PACKAGE__->uuid_class('MyUUIDGenerator');
109
110Note that the component needs to be loaded before Core.
111
112=head1 METHODS
113
114=head2 uuid_columns(@columns)
115
116Takes a list of columns to be filled with uuids during insert.
117
118 __PACKAGE__->uuid_columns('id');
119
120=head2 uuid_class($classname)
121
122Takes the name of a UUIDMaker subclass to be used for uuid value generation.
123This can be a fully qualified class name, or a shortcut name starting with ::
124that matches one of the available DBIx::Class::UUIDMaker subclasses:
125
126 __PACKAGE__->uuid_class('CustomUUIDGenerator');
127 # loads CustomeUUIDGenerator
128
129 __PACKAGE->uuid_class('::Data::UUID');
130 # loads DBIx::Class::UUIDMaker::Data::UUID;
131
132Note that C<uuid_class> chacks to see that the specified class isa
133DBIx::Class::UUIDMaker subbclass and throws and exception if it isn't.
134
135=head2 uuid_maker
136
137Returns the current UUIDMaker instance for the given module.
138
139 my $uuid = __PACKAGE__->uuid_maker->as_string;
140
141=head1 SEE ALSO
142
143L<DBIx::Class::UUIDMaker>
144
7da9cd47 145=head1 AUTHORS
146
147Chia-liang Kao <clkao@clkao.org>
e78f023a 148Chris Laco <claco@chrislaco.com>
7da9cd47 149
150=head1 LICENSE
151
152You may distribute this code under the same terms as Perl itself.