Skip to content

Instantly share code, notes, and snippets.

@tommybutler
Last active August 29, 2015 13:58
Show Gist options
  • Save tommybutler/10320832 to your computer and use it in GitHub Desktop.
Save tommybutler/10320832 to your computer and use it in GitHub Desktop.
DBIx::Class Example Script
#!/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