|
NAMEData::Model::Schema - Schema DSL for Data::ModelSYNOPSISpackage 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 DSLinstall_model, schemamodel name and it schema is set up. install_model model_name schema { }; base_driverset driver ( Data::Model::Driver::* ) for current package's default.column_sugarcolumn_sugar promotes reuse of a schema definition.see head1 COLUMN SUGAR SCHEMA DSLdriverdriver used only in install_model of current.install_model local_driver => schema { my $driver = Data::Mode::Driver::DBI->new( dsn => 'DBI:SQLite:' ); driver($driver); } columnIt 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 ... ) columnssome columns are set up. However, options cannot be set.utf8_columncolumn with utf8 inflated.utf8_columnscolumns with utf8 inflated.alias_columnalias 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; keyset 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 indexindex 'name'; # index name(name); index name => [qw/ name name2 /]; # index name(name, name2) uniqueunique 'name'; # unique name(name); unique name => [qw/ name name2 /]; # unique name(name, name2) add_methodA 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_optionssome option to schema is added.It is used when using InnoDB in MySQL. schema_options create_sql_attributes => { mysql => 'ENGINE=InnoDB', }; COLUMN OPTIONSThe option which can be used in a column definition.Pasted the definition of ParamsValidate. It writes later. sizesize => { type => SCALAR, regex => qr/\A[0-9]+\z/, optional => 1, }, requiredrequired => { type => BOOLEAN, optional => 1, }, nullnull => { type => BOOLEAN, optional => 1, }, signedsigned => { type => BOOLEAN, optional => 1, }, unsignedunsigned => { type => BOOLEAN, optional => 1, }, decimalsdecimals => { type => BOOLEAN, optional => 1, }, zerofillzerofill => { type => BOOLEAN, optional => 1, }, binarybinary => { type => BOOLEAN, optional => 1, }, asciiascii => { type => BOOLEAN, optional => 1, }, unicodeunicode => { type => BOOLEAN, optional => 1, }, defaultdefault => { type => SCALAR | CODEREF, optional => 1, }, auto_incrementauto_increment => { type => BOOLEAN, optional => 1, }, inflateinflate => { type => SCALAR | CODEREF, optional => 1, }, deflatedeflate => { type => SCALAR | CODEREF, optional => 1, }, COLUMN SUGARUNDOCUMENTEDpackage 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 }; AUTHORKazuhiro Osawa <yappo <at> shibuya <doet> pl>LICENSEThis library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.POD ERRORSHey! The above document had some coding errors, which are explained below:
Visit the GSP FreeBSD Man Page Interface. |