1 package DBIx::Class::UUIDColumns;
6 use base qw/DBIx::Class/;
8 __PACKAGE__->mk_classdata( 'uuid_auto_columns' => [] );
9 __PACKAGE__->mk_classdata( 'uuid_maker' );
10 __PACKAGE__->uuid_class( __PACKAGE__->_find_uuid_module );
12 # be compatible with Class::DBI::UUID
16 $self->throw_exception("column $_ doesn't exist") unless $self->has_column($_);
18 $self->uuid_auto_columns(\@_);
22 my ($self, $class) = @_;
25 $class = "DBIx::Class::UUIDMaker$class" if $class =~ /^::/;
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");
32 $self->uuid_maker($class->new);
36 return ref $self->uuid_maker;
41 for my $column (@{$self->uuid_auto_columns}) {
42 $self->store_column( $column, $self->get_uuid )
43 unless defined $self->get_column( $column );
45 $self->next::method(@_);
49 return shift->uuid_maker->as_string;
52 sub _find_uuid_module {
53 if (eval{require Data::UUID}) {
54 return '::Data::UUID';
55 } elsif ($^O ne 'openbsd' && eval{require APR::UUID}) {
56 # APR::UUID on openbsd causes some as yet unfound nastiness for XS
58 } elsif (eval{require UUID}) {
61 # squelch the 'too late for INIT' warning in Win32::API::Type
63 require Win32::Guidgen;
65 return '::Win32::Guidgen';
66 } elsif (eval{require Win32API::GUID}) {
67 return '::Win32API::GUID';
69 shift->throw_exception('no suitable uuid module could be found')
78 DBIx::Class::UUIDColumns - Implicit uuid columns
83 __PACKAGE__->load_components(qw/UUIDColumns Core DB/);
84 __PACKAGE__->uuid_columns( 'artist_id' );
88 This L<DBIx::Class> component resembles the behaviour of
89 L<Class::DBI::UUID>, to make some columns implicitly created as uuid.
91 When loaded, C<UUIDColumns> will search for a suitable uuid generation module
92 from the following list of supported modules:
100 If no supporting module can be found, an exception will be thrown.
102 *APR::UUID will not be loaded under OpenBSD due to an as yet unidentified XS
105 If you would like to use a specific module, you can set C<uuid_class>:
107 __PACKAGE__->uuid_class('::Data::UUID');
108 __PACKAGE__->uuid_class('MyUUIDGenerator');
110 Note that the component needs to be loaded before Core.
114 =head2 uuid_columns(@columns)
116 Takes a list of columns to be filled with uuids during insert.
118 __PACKAGE__->uuid_columns('id');
120 =head2 uuid_class($classname)
122 Takes the name of a UUIDMaker subclass to be used for uuid value generation.
123 This can be a fully qualified class name, or a shortcut name starting with ::
124 that matches one of the available DBIx::Class::UUIDMaker subclasses:
126 __PACKAGE__->uuid_class('CustomUUIDGenerator');
127 # loads CustomeUUIDGenerator
129 __PACKAGE->uuid_class('::Data::UUID');
130 # loads DBIx::Class::UUIDMaker::Data::UUID;
132 Note that C<uuid_class> chacks to see that the specified class isa
133 DBIx::Class::UUIDMaker subbclass and throws and exception if it isn't.
137 Returns the current UUIDMaker instance for the given module.
139 my $uuid = __PACKAGE__->uuid_maker->as_string;
143 L<DBIx::Class::UUIDMaker>
147 Chia-liang Kao <clkao@clkao.org>
148 Chris Laco <claco@chrislaco.com>
152 You may distribute this code under the same terms as Perl itself.