Subject: | DBIx-Class-RandomStringColumns broken with DBIC 0.05007 |
This module doesn't work - at least not with recent DBIC 0.05007
(make test also fails).
http://lists.rawmode.org/pipermail/dbix-class/2006-March/000827.html
Attached is a fix that also enhances (and documents) the interface a
bit.
Subject: | DBIx-Class-RandomStringColumns-0.02.patch |
--- lib/DBIx/Class/RandomStringColumns.pm.orig 2006-03-01 15:25:03.666337886 +0100
+++ lib/DBIx/Class/RandomStringColumns.pm 2006-03-01 16:26:26.819885868 +0100
@@ -10,8 +10,6 @@
use String::Random;
__PACKAGE__->mk_classdata( 'rs_auto_columns' => [] );
-__PACKAGE__->mk_classdata( 'rs_length' => {} );
-__PACKAGE__->mk_classdata( 'rs_salt' => {} );
=head1 NAME
@@ -19,7 +17,7 @@
=head1 SYNOPSIS
- pacakge Proj::Data;
+ package Proj::Data;
use base qw(DBIx::Class);
__PACKAGE__->load_components(qw/RandomStringColumns Core DB/);
@@ -38,36 +36,57 @@
=head2 random_string_columns
-=cut
+ __PACKAGE__->random_string_columns(@column_names);
+ __PACKAGE__->random_string_columns(name1 => \%options1, name2 => \%options2);
-sub random_string_columns {
- my $self = shift;
+Define fields that get random strings at creation. Each column name can
+be followed by an options hash reference.
- my $length = 32;
- my $salt = '[A-Za-z0-9]';
+Valid options are:
- my $opt = pop @_;
- if (ref $opt ne 'HASH') {
- push @_, $opt;
- } else {
- $length = $opt->{length} || 32;
- $salt = $opt->{salt } || '[A-Za-z0-9]';
- }
+=over 4
+
+=item salt
+
+A pseudo regex as described in L<String::Random|String::Random>. Defaults to
+C<[A-Za-z0-9]>.
+
+=item length
+
+Length of the string. Defaults to 32.
+
+=item check
- for (@_) {
- die "column $_ doesn't exist" unless $self->has_column($_);
- $self->rs_length->{$_} = $length;
- $self->rs_salt->{$_} = $salt;
+Search table before insert until generated column value is not found.
+Defaults to false and must be set to a true value to activate.
+This lookup is not really usefull in combination with the default
+salt and length, which provides 62^32 possible combinations.
+
+=back
+
+=cut
+
+sub random_string_columns {
+ my ($self, @cols) = @_;
+
+ my ($col, $opt);
+ while ($col = shift @cols) {
+ $self->throw_exception("column $col doesn't exist") unless $self->has_column($col);
+ $opt = ref $cols[0] eq 'HASH' ? shift(@cols) : {};
+ push @{$self->rs_auto_columns}, [
+ $col,
+ $opt->{salt} || '[A-Za-z0-9]',
+ $opt->{length} || 32,
+ $opt->{check}
+ ];
}
- push @{$self->rs_auto_columns}, @_;
}
sub insert {
my ($self) = @_;
for my $column (@{$self->rs_auto_columns}) {
-
- $self->store_column( $column, $self->get_random_string($column) )
- unless defined $self->get_column( $column );
+ $self->store_column($column->[0], $self->get_random_string($column))
+ unless defined $self->get_column($column->[0]);
}
$self->next::method;
}
@@ -77,9 +96,9 @@
my $column = shift;
my $val;
- do { # must be unique
- $val = String::Random->new->randregex(sprintf('%s{%d}', $self->rs_salt->{$column} , $self->rs_length->{$column}));
- } while ($self->search({$column => $val}));
+ do { # check uniqueness if check => 1 for this column
+ $val = String::Random->new->randregex(sprintf('%s{%d}', $column->[1], $column->[2]));
+ } while ($column->[3] and $self->result_source->resultset->search({$column->[0] => $val})->next);
return $val;
}