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

NAME

Mock::Data::Plugin::SQLTypes - Collection of generators that produce data matching a SQL column type

SYNOPSIS

  my $mock= Mock::Data->new(['SQL']);
  $mock->integer(11);
  $mock->sequence($seq_name);
  $mock->numeric([9,2]);
  $mock->float({ bits => 32 });
  $mock->bit;
  $mock->boolean;
  $mock->varchar(16);
  $mock->char(16);
  $mock->text(256);
  $mock->blob(1000);
  $mock->varbinary(32);
  $mock->datetime({ after => '1900-01-01', before => '1990-01-01' });
  $mock->date;
  $mock->uuid;
  $mock->json({ data => $data || {} });
  $mock->inet;
  $mock->cidr;
  $mock->macaddr;

This module defines generators that match the data type names used by various relational databases.

The output patterns are likely to change in future versions, but will always be valid for inserting into a column of that type.

EXPORTABLE FUNCTIONS

generator_for_type

  my $generatpr= generator_for_type($sqltype);

Return a generator which can generate valid strings for a given SQL type.

GENERATORS

(all generators are also exportable)

Numeric Generators

integer

See "integer" in Mock::Data::Plugin::Number

tinyint

Alias for integer({ bits => 8 }).

smallint

Alias for integer({ bits => 16 }).

bigint

Alias for integer({ bits => 63 }).

sequence

See "sequence" in Mock::Data::Plugin::Number

serial

Alias for sequence

smallserial

Alias for sequence

bigserial

Alias for sequence

decimal

See "decimal" in Mock::Data::Plugin::Numeric

numeric

Alias for decimal.

float

See "float" in Mock::Data::Plugin::Numeric

real, float4

Aliases for float({ size => 7 })

float8, double, double_precision

Aliases for float({ size => 15 })

bit

Return a 0 or a 1

bool, boolean

Alias for bit. While postgres prefers 'true' and 'false', it allows 0/1 and they are more convenient to use in Perl.

Text Generators

varchar

  $str= $mock->varchar($size);
  $str= $mock->varchar(\%options, $size);
  # %options:
  {
    size        => $max_chars,
        size_weight => sub($size) { ... }
    source      => $generator_or_name,
  }

Generate a string of random length, from 1 to $size characters. If $size is not given, it defaults to 16. size_weight is a function used to control the distribution of random lengths. The default applies a reduced chance of generating long strings when $size is greater than 32.

source is the name of a generator (or a generator reference) to use for generating words that get concatenated up to the random length. The default is the generator named 'word' in the current Mock::Data, and if that doesn't exist it uses "word" in Mock::Data::Plugin::Text.

nvarchar

Alias for varchar

text

Same as varchar, but the default size is 256.

tinytext, mediumtext, longtext, ntext

Aliases for text, and don't generate larger data because that would just slow things down.

char

  $str= $mock->char($size);
  $str= $mock->char(\%options, $size);

Same as varchar, but the default size is 1, and the string will be padded with whitespace up to $size.

Date Generators

datetime

  $datestr= $mock->datetime();
  $datestr= $mock->datetime({ before => $date, after => $date });

Returns a random date from a date range, defaulting to the past 10 years. The input and output date strings must all be in ISO-8601 format, or an object that stringifies to that format. The output does not have the 'T' in the middle or 'Z' at the end, for widest compatibility with being able to insert into databases.

date

Like datetime, but only the 'YYYY-MM-DD' portion.

timestamp

datetime2

datetime_without_time_zone

datetimeoffset

datetime_with_time_zone

Alias for datetime.

Binary Data Generators

blob

tinyblob, mediumblob, longblob, bytea, binary, varbinary

Aliases for blob. None of these change the default string length, because longer strings of data would just slow things down.

Structured Data Generators

uuid

See "uuid" in Mock::Data::Plugin::Numeric

json, jsonb

Return '{}'. This is just the minimal valid value that makes it most likely that you can perform operations on the column without type errors.

inet

See "ipv4" in Mock::Data::Plugin::Net

cidr

See "cidr" in Mock::Data::Plugin::Net

macaddr

See "macaddr" in Mock::Data::Plugin::Net

AUTHOR

Michael Conrad <mike@nrdvana.net>

VERSION

version 0.03

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Michael Conrad.

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