Arthur Axel fREW Schmidt
YAPC::NA
Orlando, Florida
2014 June 24, 10:00
Questions?
use DBIx::Class::Schema::Loader 'make_schema_at'; make_schema_at( 'My::Schema', { debug => 1, dump_directory => './lib' }, [ 'dbi:Pg:dbname="foo"', 'myuser', 'mypassword' ], )
package My::Schema; use 5.20.0; use warnings; our $VERSION = 1; use base 'DBIx::Class::Schema'; __PACKAGE__->load_namespaces( default_resultset_class => 'ResultSet', ); 1;
Optional, but a good idea
package My::Schema::Result; use parent 'DBIx::Class::Core'; __PACKAGE__->load_components( 'Helper::Row::RelationshipDWIM', # shorter slides ); 1;
Super Optional, but good for slides
package My::Schema::Candy; use 5.20.0; use warnings; use parent 'DBIx::Class::Candy'; sub base { 'My::Schema::Result' } sub perl_version { 20 } sub autotable { 1 } 1;
package My::Schema::Result::Device; use My::Schema::Candy; primary_column id => { data_type => 'int', is_auto_increment => 1, }; column type_id => { data_type => 'int' }; column name => { data_type => 'varchar', size => 128, }; column last_checkin => { data_type => 'datetime', is_nullable => 1, }; column is_enabled => { data_type => 'bit' }; has_many computer_device_links => '::Computer_Device', 'device_id'; belongs_to type => '::DeviceType', 'type_id'; unique_constraint name_and_type => [qw(name type_id)]; 1;
album belongs to artist
non-undef accessor*
*caveat because users are liars and databases are theives
artist has many albums
RS accessor
Questions?
artist might have a previous name
Natalie Portman's Shaved Head β Brite Futures
undefable accessor
artist has one first album
non-undef accessor*
*caveat because ...
artist many_to_many labels
minimal sugar
package My::Schema::ResultSet; use 5.20.0; use warnings; use parent 'DBIx::Class::ResultSet'; __PACKAGE__->load_components( 'Helper::ResultSet::IgnoreWantarray', 'Helper::ResultSet::Me', # slides ); 1;
package My::Schema::ResultSet::Device; use 5.20.0; use warnings; use parent 'My::Schema::ResultSet'; sub search_by_name { $_[0]->search({ $_[0]->me . name => $_[1] }) } sub enabled { $_[0]->search({ $_[0]->me . is_enabled => 1 }) } sub find_by_name { $_[0]->search_by_name($_[1])->single } 1;
say $_->name for $schema->resultset('Device') ->search({ 'me.name' => 'phone' })->all
say $_->name for $schema->resultset('Device') ->search_by_name('phone')->all
say $_->name for $schema->resultset('Device') ->enabled ->search_by_name('phone') ->all
$schema->resultset('Computer')->search({ 'me.id' => { -in => $location_rs->get_column('id')->as_query }, })
select => ['foo'], as => 'bar'
columns => { bar => 'foo' }
package My::Schema::ResultClass::ComputerWithMacros; use Moo; use warnings NONFATAL => 'all'; use Lynx::Macro; use My::Schema::Result::Computer; has schema => ( is => 'ro', required => 1, ); has wmp => ( is => 'ro', lazy => 1, init_arg => undef, builder => '_build_wmp', ); sub _build_wmp { Lynx::Macro->new(schema => $_[0]->schema) } has _macros => ( is => 'ro', lazy => 1, init_arg => undef, builder => '_build_macros', ); sub _build_macros { return +{ map { lc $_->{from} => $_ } $_[0]->schema->resultset('Macro')->search({ 'me.from' => { -not_like => "%[%]%" }, }, { result_class => '::HRI', })->all } } sub inflate_result { my ($self, $src, $data, $prefetch) = @_; unless (defined $prefetch->{_macro}[0]{to}) { if (my $macro = $self->_macros->{lc $data->{name}}) { $prefetch->{_macro} = [$macro] } elsif (my $wc_macro = $self->wmp->choose_macro($data->{name})) { $prefetch->{_macro} = [$wc_macro] } } My::Schema::Result::Computer->inflate_result($src, $data, $prefetch) }; 1;
/
#