NAME

Data::Model::Schema - Schema DSL for Data::Model

SYNOPSIS

package Your::Model;
use base 'Data::Model';
use Data::Model::Schema;
use Data::Model::Driver::DBI;

my $dbfile = '/foo/bar.db';
my $driver = Data::Model::Driver::DBI->new(
    dsn => "dbi:SQLite:dbname=$dbfile",
);
base_driver( $driver ); # set the storage driver for Your::Model


install_model tweet => schema { # CREATE TABLE tweet (
  key 'id'; # primary key
  index index_name [qw/ user_id at /]; # index index_name(user_id, at);

  column id
      => int => {
          auto_increment => 1,
          required       => 1,
          unsigned       => 1,
      }; # id   UNSIGNED INT NOT NULL AUTO_INCREMENT,

  column user_id
      => int => {
          required       => 1,
          unsigned       => 1,
      }; # user_id   UNSIGNED INT NOT NULL,

  column at
      => int => {
          required       => 1,
          default        => sub { time() },
          unsigned       => 1,
      }; # at   UNSIGNED INT NOT NULL, # If it is empty at the time of insert   time() is used.

  utf8_column body # append to auto utf8 inflating
      => varchar => {
          required       => 1,
          size           => 140,
          default        => '-',
      }; # body   VARCHAR(140) NOT NULL DEFAULT'-',


  column field_name
      => char => {
          default    => 'aaa', # default value
          auto_increment => 1, # auto_increment
          inflate => sub { unpack("H*", $_[0]) }, # inflating by original function
          deflate => sub { pack("H*", $_[0]) },   # deflating by original function
      };

  column field_name_2
      => char => {
          inflate => 'URI', # use URI inflate see L<Data::Model::Schema::Inflate>
          deflate => 'URI', # use URI deflate see L<Data::Model::Schema::Inflate>
      };

  columns qw/ foo bar /; # create columns uses default config
};

GLOBAL DSL

install_model, schema

model name and it schema is set up.

install_model model_name schema {
};

base_driver

set driver ( Data::Model::Driver::* ) for current package's default.

column_sugar

column_sugar promotes reuse of a schema definition.

see head1 COLUMN SUGAR

SCHEMA DSL

driver

driver used only in install_model of current.

install_model local_driver => schema {
    my $driver = Data::Mode::Driver::DBI->new( dsn => 'DBI:SQLite:' );
    driver($driver);
 }

column

It is a column definition.

column column_name => column_type => \%options;

column_name puts in the column name of SQL schema.

column_type puts in the column type of SQL schema. ( INT CHAR BLOB ... )

columns

some columns are set up. However, options cannot be set.

utf8_column

column with utf8 inflated.

utf8_columns

columns with utf8 inflated.

alias_column

alias is attached to a specific column.

It is helpful. I can use, when leaving original data and inflateing.

{ package Name; use Moose; has 'name' => ( is => 'rw' ); }
# in schema 
columns qw( name nickname );
alias_column name     => 'name_name';
alias_column nickname => 'nickname_name'
    => {
        inflate => sub {
            my $value = shift;
            Name->new( name => $value );
        }

# in your script
is $row->nickname, $row->nickname_name->name;

key

set the primary key. Unless it specifies key, it does not move by lookup and lookup_multi.

key 'id';
key [qw/ id sub_id /]; # multiple key

index

index 'name'; # index name(name);
index name => [qw/ name name2 /]; # index name(name, name2)

unique

unique 'name'; # unique name(name);
unique name => [qw/ name name2 /]; # unique name(name, name2)

add_method

A method is added to Row class which install_model created.

add_method show_name => sub {
    my $row = shift;
    printf "Show %s\n", $row->name;
};

$row->name('yappo');
$row->show_name; # print "Show yappo\n"

schema_options

some option to schema is added.

It is used when using InnoDB in MySQL.

schema_options create_sql_attributes => {
    mysql => 'ENGINE=InnoDB',
};

COLUMN OPTIONS

The option which can be used in a column definition.

Pasted the definition of ParamsValidate. It writes later.

size

size   => {
    type     => SCALAR,
    regex    => qr/\A[0-9]+\z/,
    optional => 1,
},

required

required   => {
    type     => BOOLEAN,
    optional => 1,
},

null

null       => {
    type     => BOOLEAN,
    optional => 1,
},

signed

signed     => {
    type     => BOOLEAN,
    optional => 1,
},

unsigned

unsigned   => {
    type     => BOOLEAN,
    optional => 1,
},

decimals

decimals   => {
    type     => BOOLEAN,
    optional => 1,
},

zerofill

zerofill   => {
    type     => BOOLEAN,
    optional => 1,
},

binary

binary     => {
    type     => BOOLEAN,
    optional => 1,
},

ascii

ascii      => {
    type     => BOOLEAN,
    optional => 1,
},

unicode

unicode    => {
    type     => BOOLEAN,
    optional => 1,
},

default

default    => {
    type     => SCALAR | CODEREF,
    optional => 1,
},

auto_increment

auto_increment => {
    type     => BOOLEAN,
    optional => 1,
},

inflate

inflate => {
    type     => SCALAR | CODEREF,
    optional => 1,
},

deflate

deflate => {
    type     => SCALAR | CODEREF,
    optional => 1,
},

COLUMN SUGAR

UNDOCUMENTED

package Mock::ColumnSugar;
use strict;
use warnings;
use base 'Data::Model';
use Data::Model::Schema sugar => 'column_sugar';

column_sugar 'author.id'
    => 'int' => +{
        unsigned => 1,
        required => 1, # we can used to require or required
    };
column_sugar 'author.name'
    => 'varchar' => +{
        size    => 128,
        require => 1,
    };

column_sugar 'book.id'
    => 'int' => +{
        unsigned => 1,
        require  => 1,
    };
column_sugar 'book.title'
    => 'varchar' => +{
        size    => 255,
        require => 1,
    };
column_sugar 'book.description'
    => 'text' => +{
        require => 1,
        default => 'not yet writing'
    };
column_sugar 'book.recommend'
    => 'text';


install_model author => schema {
    driver $main::DRIVER;
    key 'id';

    column 'author.id' => { auto_increment => 1 }; # column name is id
    column 'author.name'; # column name is name
};

install_model book => schema {
    driver $main::DRIVER;
    key 'id';
    index 'author_id';

    column 'book.id'   => { auto_increment => 1 }; # column name is id
    column 'author.id'; # column name is author_id
    column 'author.id' => 'sub_author_id' => { required => 0 }; # column name is sub_author_id
    column 'book.title'; # column name is title
    column 'book.description'; # column name is description
    column 'book.recommend'; # column name is recommend
};

AUTHOR

Kazuhiro Osawa <yappo <at> shibuya <döt> pl>

LICENSE

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

1 POD Error

The following errors were encountered while parsing the POD:

Around line 641:

Non-ASCII character seen before =encoding in '<döt>'. Assuming UTF-8