1 package DBIx::Class::UUIDColumns;
2 use base qw/DBIx::Class/;
4 __PACKAGE__->mk_classdata( 'uuid_auto_columns' => [] );
5 __PACKAGE__->mk_classdata( 'uuid_maker' );
6 __PACKAGE__->uuid_class( __PACKAGE__->_find_uuid_module );
8 # be compatible with Class::DBI::UUID
12 $self->throw_exception("column $_ doesn't exist") unless $self->has_column($_);
14 $self->uuid_auto_columns(\@_);
18 my ($self, $class) = @_;
21 $class = "DBIx::Class::UUIDMaker$class" if $class =~ /^::/;
23 if (!eval "require $class") {
24 $self->throw_exception("$class could not be loaded: $@");
25 } elsif (!$class->isa('DBIx::Class::UUIDMaker')) {
26 $self->throw_exception("$class is not a UUIDMaker subclass");
28 $self->uuid_maker($class->new);
32 return ref $self->uuid_maker;
37 for my $column (@{$self->uuid_auto_columns}) {
38 $self->store_column( $column, $self->get_uuid )
39 unless defined $self->get_column( $column );
41 $self->next::method(@_);
45 return shift->uuid_maker->as_string;
48 sub _find_uuid_module {
49 if (eval{require Data::UUID}) {
50 return '::Data::UUID';
51 } elsif ($^O ne 'openbsd' && eval{require APR::UUID}) {
52 # APR::UUID on openbsd causes some as yet unfound nastyness for XS
54 } elsif (eval{require UUID}) {
57 # squelch the 'too late for INIT' warning in Win32::API::Type
59 require Win32::Guidgen;
61 return '::Win32::Guidgen';
62 } elsif (eval{require Win32API::GUID}) {
63 return '::Win32API::GUID';
65 shift->throw_exception('no suitable uuid module could be found')
74 DBIx::Class::UUIDColumns - Implicit uuid columns
79 __PACKAGE__->load_components(qw/UUIDColumns Core DB/);
80 __PACKAGE__->uuid_columns( 'artist_id' );
84 This L<DBIx::Class> component resembles the behaviour of
85 L<Class::DBI::UUID>, to make some columns implicitly created as uuid.
87 When loaded, C<UUIDColumns> will search for a suitable uuid generation module
88 from the following list of supported modules:
96 If no supporting module can be found, an exception will be thrown.
98 *APR::UUID will not be loaded under OpenBSD due to an as yet unidentified XS
101 If you would like to use a specific module, you can set C<uuid_class>:
103 __PACKAGE__->uuid_class('::Data::UUID');
104 __PACKAGE__->uuid_class('MyUUIDGenerator');
106 Note that the component needs to be loaded before Core.
110 =head2 uuid_columns(@columns)
112 Takes a list of columns to be filled with uuids during insert.
114 __PACKAGE__->uuid_columns('id');
117 my ($self, $class) = @_;
120 $class = "DBIx::Class::UUIDMaker$class" if $class =~ /^::/;
122 if (!eval "require $class") {
123 $self->throw_exception("$class could not be loaded: $@");
124 } elsif (!$class->isa('DBIx::Class::UUIDMaker')) {
125 $self->throw_exception("$class is not a UUIDMaker subclass");
127 $self->uuid_maker($class->new);
131 return ref $self->uuid_maker;
136 for my $column (@{$self->uuid_auto_columns}) {
137 $self->store_column( $column, $self->get_uuid )
138 unless defined $self->get_column( $column );
140 $self->next::method(@_);
144 return shift->uuid_maker->as_string;
147 sub _find_uuid_module {
148 if (eval{require Data::UUID}) {
149 return '::Data::UUID';
150 } elsif ($^O ne 'openbsd' && eval{require APR::UUID}) {
151 # APR::UUID on openbsd causes some as yet unfound nastyness for XS
152 return '::APR::UUID';
153 } elsif (eval{require UUID}) {
156 # squelch the 'too late for INIT' warning in Win32::API::Type
158 require Win32::Guidgen;
160 return '::Win32::Guidgen';
161 } elsif (eval{require Win32API::GUID}) {
162 return '::Win32API::GUID';
164 shift->throw_exception('no suitable uuid module could be found')
173 DBIx::Class::UUIDColumns - Implicit uuid columns
178 __PACKAGE__->load_components(qw/UUIDColumns Core DB/);
179 __PACKAGE__->uuid_columns( 'artist_id' );
183 This L<DBIx::Class> component resembles the behaviour of
184 L<Class::DBI::UUID>, to make some columns implicitly created as uuid.
186 When loaded, C<UUIDColumns> will search for a suitable uuid generation module
187 from the following list of supported modules:
195 If no supporting module can be found, an exception will be thrown.
197 *APR::UUID will not be loaded under OpenBSD due to an as yet unidentified XS
200 If you would like to use a specific module, you can set C<uuid_class>:
202 __PACKAGE__->uuid_class('::Data::UUID');
203 __PACKAGE__->uuid_class('MyUUIDGenerator');
205 Note that the component needs to be loaded before Core.
209 =head2 uuid_columns(@columns)
211 Takes a list of columns to be filled with uuids during insert.
213 __PACKAGE__->uuid_columns('id');
215 =head2 uuid_class($classname)
217 Takes the name of a UUIDMaker subclass to be used for uuid value generation.
218 This can be a fully qualified class name, or a shortcut name starting with ::
219 that matches one of the available DBIx::Class::UUIDMaker subclasses:
221 __PACKAGE__->uuid_class('CustomUUIDGenerator');
222 # loads CustomeUUIDGenerator
224 __PACKAGE->uuid_class('::Data::UUID');
225 # loads DBIx::Class::UUIDMaker::Data::UUID;
227 Note that C<uuid_class> chacks to see that the specified class isa
228 DBIx::Class::UUIDMaker subbclass and throws and exception if it isn't.
232 Returns the current UUIDMaker instance for the given module.
234 my $uuid = __PACKAGE__->uuid_maker->as_string;
238 L<DBIx::Class::UUIDMaker>
242 Chia-liang Kao <clkao@clkao.org>
243 Chris Laco <claco@chrislaco.com>
247 You may distribute this code under the same terms as Perl itself.