Last active
August 29, 2015 13:58
-
-
Save tommybutler/10320832 to your computer and use it in GitHub Desktop.
DBIx::Class Example Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env/ perl | |
use strict; | |
use warnings; | |
use 5.019; | |
use lib './lib'; | |
use My::App::Schema; | |
$ENV{DBIC_TRACE} = 1; | |
my $db_config = | |
{ | |
dsn => 'dbi:mysql:dfwpm', | |
username => 'dfwpm', | |
password => 'dfwpm', | |
params => { AutoCommit => 1 }, | |
}; | |
my $db = db_connect( $db_config ); | |
my $rs = $db->resultset('User'); | |
my $search = $rs->search | |
( | |
{ username => 'tommy' }, | |
{ | |
order_by => { -asc => [ 'username', 'first_name', 'last_name' ] }, | |
rows => 1, | |
} | |
); | |
$rs = $db->resultset('Phonebook'); | |
$search = $rs->search | |
( | |
{ | |
-or => | |
[ | |
{ fname => { -like => '%b%' } }, | |
{ lname => { -like => '%r' } }, | |
-and => | |
[ | |
{ lastmod => { '>=' => '2013-01-01 00:00:00' } }, | |
{ nick => { '!=' => 'large eggs' } } | |
], | |
], | |
} | |
); | |
for my $entry ( $search->all ) | |
{ | |
say $entry->user->first_name; | |
say $entry->user->last_name; | |
} | |
sub db_connect | |
{ | |
my $config = shift; | |
return My::App::Schema->connect | |
( | |
$config->{dsn}, | |
$config->{username}, | |
$config->{password}, | |
$config->{params}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment