The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::Class::RandomColumns - Implicit Random Columns

SYNOPSIS

  package My::Schema::Result::Utz;
  use parent 'DBIx::Class::Core';

  __PACKAGE__->load_components(qw/RandomColumns/);
  __PACKAGE__->table('utz');
  __PACKAGE__->add_columns(qw(id foo bar baz));
  __PACKAGE__->random_columns('id', bar => {size => 10});

  package My::Schema::Result::Gnarf;
  use parent 'DBIx::Class::Core';

  __PACKAGE__->load_components(qw/RandomColumns/);
  __PACKAGE__->table('gnarf');
  __PACKAGE__->add_columns(
    id => {
      datatype => 'integer',
      extra => {unsigned => 1},
      is_random => {max => 2**32-1, min => 0}
    },
    foo => {
      datatype => 'int',
      size => 10,
    },
    bar => {
      datatype => 'varchar',
      is_random => {size => 10},
      size => 32,
    },
    baz => {
      datatype => 'varchar',
      size => 255,
    },
  );

VERSION

This is version 0.004000

DESCRIPTION

This DBIx::Class component makes columns implicitly create random values.

The main reason why this module exists is to generate unpredictable primary keys to add some additional security to web applications. Most forms of char and integer field types are supported.

METHODS

remove_column

Hooks into "remove_column" in DBIx::Class::ResultSource to remove the random column configuration for the given column.

remove_columns

Hooks into "remove_columns" in DBIx::Class::ResultSource to remove the random column configuration for the given columns.

random_columns

  __PACKAGE__->random_columns(@column_names);
  __PACKAGE__->random_columns(name1 => \%options1, name2 => \%options2);
  __PACKAGE__->random_columns(name1, name2 => \%options2);
  $random_columns = __PACKAGE__->random_columns;

Define or query fields that get random strings at creation. Each column name may be followed by a hash reference containing options. In case no explicit options are given, the method tries to find reasonable values.

Valid options are:

max:

Maximum number for integer fields. Defaults to 2**31-1. Must be an integer number, that is greater than min and should be positive.

min:

Minimum number for integer fields. Defaults to 0. Must be an integer number, that is lower than max and can be negative unless the corresponding field is not an unsigned integer.

set:

A string or an array reference that contains the set of characters to use for building a random content for string fields. The default set is ['0'..'9', 'a'..'z'].

size:

Length of the random string to create. Defaults to the size of the column or - if this cannot be determined for whatever reason - to 32.

check:

Search table before insert until generated column value is not found. Defaults to false and must be set to a true value to activate. Provided Perl's rand() function has sufficient entropy this lookup is only usefull for short fields, because with the default set there are 36^field-size possible combinations.

Returns a hash reference, with column names of the random columns as keys and hash references as values, that contain the random column settings.

insert

Hooks into "insert" in DBIx::Class::Row to create a random value for each random column that is not defined.

get_random_value

  $value = $instance->get_random_value($column_name);

Compute a random value for the given $column_name.

Throws an exception if the concerning column has not been declared as a random column.

OPTIONS

is_random

  is_random => 1

  is_random => {size => 16, set => ['0'..'9','A'..'F']}

Instead of calling "random_columns" it is also possible to specify option is_random in add_columns. The value is either a true scalar value, indicating that this in fact is a random column, or a hash reference, that has the same meaning as described under "random_columns".

SEE ALSO

DBIx::Class

AUTHOR

Bernhard Graf <graf(a)cpan.org>

BUGS

Please report any bugs or feature requests to bug-dbix-class-randomclumns at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-RandomColumns. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

COPYRIGHT & LICENSE

Copyright 2008 - 2011 Bernhard Graf.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.