Pedro Melo > DBICx-DataDictionary-0.002 > DBICx::DataDictionary

Download:
DBICx-DataDictionary-0.002.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.002   Source  

NAME ^

DBICx::DataDictionary - Define a data dictionary to use with your DBIx::Class Schema

VERSION ^

version 0.002

SYNOPSIS ^

    ## declare your data dictionary class
    package My::DataDictionary;
    
    use strict;
    use warnings;
    use DBICx::DataDictionary;
    
    add_type PK => {
      data_type         => 'integer',
      is_nullable       => 0,
      is_auto_increment => 1,
    };
    
    add_type NAME => {
      data_type   => 'varchar',
      is_nullable => 0,
      size        => 100,
    };
    
    # SHORT_NAME is based on NAME
    add_type SHORT_NAME => NAME(size => 40);
    
    1;
    
    
    ## Use it on your own Sources
    package My::Schema::Result::Table;
    
    use strict;
    use warnings;
    use base 'DBIx::Class';
    
    use My::Schema::DataDictionary qw( PK NAME );
    
    __PACKAGE__->load_components(qw(Core));
    __PACKAGE__->table('table');
    
    __PACKAGE__->add_columns(
      table_id => PK,
      name     => NAME(is_nullable => 1),
    );
    
    __PACKAGE__->set_primary_key('table_id');
    
    1;

DESCRIPTION ^

As your DBIx::Class-based application starts to grown, you start to use the same definitions for some columns.

All your primary keys are probably alike, and some fields, like names, addresses and other elements are also similar.

The DBICx::DataDictionary module allows you to create your own libraries of column types, and reuse them in your sources.

First you create a class for you class library and use the DBICx::DataDictionary module. This will update your class @ISA to subclass the Exporter:

    package My::DataDictionary;
    
    use DBICx::DataDictionary;

Then you declare your types using the add_type() function (imported by default) like this:

    add_type PK => {
      data_type         => 'integer',
      is_nullable       => 0,
      is_auto_increment => 1,
    };

Each type declared is available as an optional exported symbol from your class library.

You can even create another type extending a previous one like this:

    add_type SHORT_NAME => NAME(size => 40);

This creates the SHORT_NAME type, using NAME as a base and changing the size to 40.

To use these types in your sources, do:

    use My::DataDictionary qw( PK );

Alternatively you can import all your types with:

    use My::DataDictionary qw( :all );

To use your types in a column definition:

    __PACKAGE__->add_columns(
      id => PK,
    );

In this case the id column will use the PK type definition.

You can override the type definition passing as arguments the override values:

    __PACKAGE__->add_columns(
      id => PK(data_type => 'bigint'),
    );

FUNCTIONS ^

add_type()

    add_type($type_name, \%column_definition);
    add_type NAME => { data_type => 'varchar', size => 150 };

Defines a new type named $type_name. The default column specification is \%column_definition.

Each type is defined as a exportable function inside you data dictionary class. This function accepts a %hash with column definition options that will override the \%column_definition.

Returns nothing.

SEE ALSO ^

Inspired by MooseX::Types

AUTHOR ^

Pedro Melo, <melo@simplicidade.org>

API design by Matt S Trout

COPYRIGHT & LICENSE ^

Copyright 2010 Pedro Melo

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

syntax highlighting: