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
19 # be compatible with Class::DBI::UUID
23 $self->throw_exception("column $_ doesn't exist") unless $self->has_column($_);
25 $self->uuid_auto_columns(\@_);
29 my ($self, $class) = @_;
32 $class = "DBIx::Class::UUIDColumns::UUIDMaker$class" if $class =~ /^::/;
34 if (!eval "require $class") {
35 $self->throw_exception("$class could not be loaded: $@");
36 } elsif (!$class->isa('DBIx::Class::UUIDColumns::UUIDMaker')) {
37 $self->throw_exception("$class is not a UUIDMaker subclass");
39 $self->uuid_maker($class->new);
43 return ref $self->uuid_maker;
48 for my $column (@{$self->uuid_auto_columns}) {
49 $self->store_column( $column, $self->get_uuid )
50 unless defined $self->get_column( $column );
52 $self->next::method(@_);
56 return shift->uuid_maker->as_string;
59 sub _find_uuid_module {
60 if (eval{require Data::UUID}) {
61 return '::Data::UUID';
62 } elsif ($^O ne 'openbsd' && eval{require APR::UUID}) {
63 # APR::UUID on openbsd causes some as yet unfound nastiness for XS
65 } elsif (eval{require UUID}) {
68 # squelch the 'too late for INIT' warning in Win32::API::Type
70 require Win32::Guidgen;
72 return '::Win32::Guidgen';
73 } elsif (eval{require Win32API::GUID}) {
74 return '::Win32API::GUID';
76 shift->throw_exception('no suitable uuid module could be found')
85 DBIx::Class::UUIDColumns - Implicit uuid columns
90 __PACKAGE__->load_components(qw/UUIDColumns Core DB/);
91 __PACKAGE__->uuid_columns( 'artist_id' );
95 This L<DBIx::Class> component resembles the behaviour of
96 L<Class::DBI::UUID>, to make some columns implicitly created as uuid.
98 When loaded, C<UUIDColumns> will search for a suitable uuid generation module
99 from the following list of supported modules:
107 If no supporting module can be found, an exception will be thrown.
109 *APR::UUID will not be loaded under OpenBSD due to an as yet unidentified XS
112 If you would like to use a specific module, you can set C<uuid_class>:
114 __PACKAGE__->uuid_class('::Data::UUID');
115 __PACKAGE__->uuid_class('MyUUIDGenerator');
117 Note that the component needs to be loaded before Core.
121 =head2 uuid_columns(@columns)
123 Takes a list of columns to be filled with uuids during insert.
125 __PACKAGE__->uuid_columns('id');
127 =head2 uuid_class($classname)
129 Takes the name of a UUIDMaker subclass to be used for uuid value generation.
130 This can be a fully qualified class name, or a shortcut name starting with ::
131 that matches one of the available DBIx::Class::UUIDColumns::UUIDMaker subclasses:
133 __PACKAGE__->uuid_class('CustomUUIDGenerator');
134 # loads CustomeUUIDGenerator
136 __PACKAGE->uuid_class('::Data::UUID');
137 # loads DBIx::Class::UUIDMaker::Data::UUID;
139 Note that C<uuid_class> chacks to see that the specified class isa
140 DBIx::Class::UUIDColumns::UUIDMaker subbclass and throws and exception if it isn't.
144 Returns the current UUIDMaker instance for the given module.
146 my $uuid = __PACKAGE__->uuid_maker->as_string;
150 L<DBIx::Class::UUIDColumns::UUIDMaker>
154 Chia-liang Kao <clkao@clkao.org>
155 Chris Laco <claco@chrislaco.com>
159 You may distribute this code under the same terms as Perl itself.