| 1 | package Class::DBI::UUID; | ||||||||||
| 2 | # $Id: UUID.pm,v 1.1 2005/01/31 18:51:22 cwest Exp $ | ||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Class::DBI::UUID - Provide Globally Unique Column Values | ||||||||||
| 8 | |||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||
| 10 | |||||||||||
| 11 | package MyApp::User; | ||||||||||
| 12 | use base qw[Class::DBI]; | ||||||||||
| 13 | |||||||||||
| 14 | __PACKAGE__->connection('dbi:SQLite:dbfile', '', ''); | ||||||||||
| 15 | __PACKAGE__->table(q[users]); | ||||||||||
| 16 | __PACKAGE__->columns(Primary => 'id'); | ||||||||||
| 17 | __PACKAGE__->columns(Essential => qw[username password]); | ||||||||||
| 18 | |||||||||||
| 19 | use Class::DBI::UUID; | ||||||||||
| 20 | __PACKAGE__->uuid_columns('id'); | ||||||||||
| 21 | |||||||||||
| 22 | # Elsewhere.. | ||||||||||
| 23 | my $user = MyApp::User->create({ | ||||||||||
| 24 | username => 'user', | ||||||||||
| 25 | password => 'pass', | ||||||||||
| 26 | }); | ||||||||||
| 27 | |||||||||||
| 28 | print $user->id; # A UUID string. | ||||||||||
| 29 | |||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||
| 31 | |||||||||||
| 32 | This module implements globally unique columns values. When an object | ||||||||||
| 33 | is created, the columns specified are given unique IDs. This is particularly | ||||||||||
| 34 | helpful when running in an environment where auto incremented primary | ||||||||||
| 35 | keys won't work, such as multi-master replication. | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | use base qw[Exporter Class::Data::Inheritable]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 1 | ▶ | use vars qw[@EXPORT $VERSION]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 1 | $VERSION = sprintf "%d.%02d", split m/\./, (qw$Revision: 1.1 $)[1]; | |||||||||
| 42 | 1 | @EXPORT = qw[uuid_columns uuid_columns_type]; | |||||||||
| 43 | |||||||||||
| 44 | 1 | ▶ | use Data::UUID; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | =head2 uuid_columns | ||||||||||
| 47 | |||||||||||
| 48 | MyApp::User->uuid_columns(MyApp::User->columns('Primary')); | ||||||||||
| 49 | |||||||||||
| 50 | A C<before_create> trigger will be set up to set the values of each column | ||||||||||
| 51 | listed as input to a C<Data::UUID> string. Change the type of string output | ||||||||||
| 52 | using the C<uuid_columns_type> class method. | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 2 | ▶ | sub uuid_columns { | ||||||||
|
Subroutine
uuid_columns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 57 | 2 | my ($class, @cols) = @_; | |||||||||
| 58 | 2 | my $type = 'create_' . __PACKAGE__->_uuid_type; | |||||||||
| 59 | 3 | ▶ | $class->add_trigger(before_create => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 60 | 3 | my ($self) = @_; | |||||||||
| 61 | 3 | foreach ( @cols ) { | |||||||||
| 62 | 6 | $self->{$_} = Data::UUID->new->$type; | |||||||||
| 63 | } | ||||||||||
| 64 | 2 | }); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | =head2 uuid_columns_type | ||||||||||
| 68 | |||||||||||
| 69 | MyApp::User->uuid_columns_type('bin'); # keep it small | ||||||||||
| 70 | |||||||||||
| 71 | By default the type will be C<str>. It's the largest, but its also the | ||||||||||
| 72 | safest for general use. Possible values are C<bin>, C<str>, C<hex>, and | ||||||||||
| 73 | C<b64>. Basically, anything that you can append to C<create_> and still | ||||||||||
| 74 | get a valid method name from C<Data::UUID>. Also returns the type to be | ||||||||||
| 75 | used. | ||||||||||
| 76 | |||||||||||
| 77 | Do not change this value on a whim. If you do change it, change it before | ||||||||||
| 78 | your call to C<uuid_columns>, or, call C<uuid_columns> again after it is | ||||||||||
| 79 | changed (therefore calling it before C<uuid_columns>, but also adding extra | ||||||||||
| 80 | triggers without need). | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 1 | __PACKAGE__->mk_classdata('_uuid_type'); | |||||||||
| 85 | 1 | __PACKAGE__->_uuid_type('str'); | |||||||||
| 86 | 2 | ▶ | sub uuid_columns_type { | ||||||||
|
Subroutine
uuid_columns_type: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 87 | 2 | my $class = shift; | |||||||||
| 88 | 2 | ▶ | return __PACKAGE__->_uuid_type(shift) if @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 1 | return __PACKAGE__->_uuid_type; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 1 | 1; | |||||||||
| 93 | |||||||||||
| 94 | __END__ | ||||||||||