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