1 package DBIx::Class::UUIDColumns;
7 use base qw/DBIx::Class/;
9 __PACKAGE__->mk_classdata( 'uuid_auto_columns' => [] );
10 __PACKAGE__->mk_classdata( 'uuid_maker' );
11 __PACKAGE__->uuid_class( __PACKAGE__->_find_uuid_module );
13 # Always remember to do all digits for the version even if they're 0
14 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
15 # brain damage and presumably various other packaging systems too
22 $self->throw_exception("column $_ doesn't exist") unless $self->has_column($_);
24 $self->uuid_auto_columns(\@_);
28 my ($self, $class) = @_;
31 $class = "DBIx::Class::UUIDColumns::UUIDMaker$class" if $class =~ /^::/;
33 if (!eval "require $class") {
34 $self->throw_exception("$class could not be loaded: $@");
35 } elsif (!$class->isa('DBIx::Class::UUIDColumns::UUIDMaker')) {
36 $self->throw_exception("$class is not a UUIDMaker subclass");
38 $self->uuid_maker($class->new);
42 return ref $self->uuid_maker;
47 for my $column (@{$self->uuid_auto_columns}) {
48 $self->store_column( $column, $self->get_uuid )
49 unless defined $self->get_column( $column );
51 $self->next::method(@_);
55 return shift->uuid_maker->as_string;
58 sub _find_uuid_module {
59 if (eval{require Data::UUID}) {
60 return '::Data::UUID';
61 } elsif (eval{require Data::GUID}) {
62 return '::Data::GUID';
63 } elsif ($^O ne 'openbsd' && eval{require APR::UUID}) {
64 # APR::UUID on openbsd causes some as yet unfound nastiness for XS
66 } elsif (eval{require UUID}) {
69 # squelch the 'too late for INIT' warning in Win32::API::Type
71 require Win32::Guidgen;
73 return '::Win32::Guidgen';
74 } elsif (eval{require Win32API::GUID}) {
75 return '::Win32API::GUID';
77 die 'no suitable uuid module could be found for use with DBIx::Class::UUIDColumns';
86 DBIx::Class::UUIDColumns - Implicit uuid columns
90 In your L<DBIx::Class> table class:
92 __PACKAGE__->load_components(qw/UUIDColumns ... Core/);
93 __PACKAGE__->uuid_columns('artist_id');
95 B<Note:> The component needs to be loaded I<before> Core.
99 This L<DBIx::Class> component resembles the behaviour of L<Class::DBI::UUID>,
100 to make some columns implicitly created as uuid.
102 When loaded, C<UUIDColumns> will search for a suitable uuid generation module
103 from the following list of supported modules:
111 If no supporting module can be found, an exception will be thrown.
113 *APR::UUID will not be loaded under OpenBSD due to an as yet unidentified XS
116 If you would like to use a specific module, you can set L</uuid_class>:
118 __PACKAGE__->uuid_class('::Data::UUID');
119 __PACKAGE__->uuid_class('MyUUIDGenerator');
125 Returns a uuid string from the current uuid_maker.
129 Inserts a new uuid string into each column in L</uuid_columns>.
133 Takes a list of columns to be filled with uuids during insert.
135 __PACKAGE__->uuid_columns('artist_id');
139 Takes the name of a UUIDMaker subclass to be used for uuid value generation.
140 This can be a fully qualified class name, or a shortcut name starting with ::
141 that matches one of the available L<DBIx::Class::UUIDColumns::UUIDMaker> subclasses:
143 __PACKAGE__->uuid_class('CustomUUIDGenerator');
144 # loads CustomeUUIDGenerator
146 __PACKAGE__->uuid_class('::Data::UUID');
147 # loads DBIx::Class::UUIDMaker::Data::UUID;
149 Note that C<uuid_class> checks to see that the specified class isa
150 L<DBIx::Class::UUIDColumns::UUIDMaker> subclass and throws and exception if it isn't.
154 Returns the current UUIDMaker instance for the given module.
156 my $uuid = __PACKAGE__->uuid_maker->as_string;
160 L<DBIx::Class::UUIDColumns::UUIDMaker>
164 Chia-liang Kao <clkao@clkao.org>
168 Chris Laco <claco@chrislaco.com>
172 You may distribute this code under the same terms as Perl itself.