#!/usr/bin/perl -w package MyTest; use strict; use warnings; use DBI; use Devel::Peek; use Data::Dumper qw/Dumper/; use Perl6::Say; use FindBin qw/$Bin/; use lib $Bin; use Test::Schema; =head2 new( $cfg_file ) =cut sub new { my $class = shift; my $self = bless({}, $class); $self->{'__CONFIG'} = { db => { dsn => 'DBI:mysql:test:localhost;mysql_enable_utf8=1', username => 'test', password => 'test', attributes => { autocommit => 1, }, }, # /db }; return $self; } # /mew =head2 cfg( $key? ) Reads the value for $key from the config file. If $key is omitted, the config hash is returned at once. =cut sub cfg { my $self = shift; my $key = shift; # may be undef if( $key ) { return $self->{'__CONFIG'}->{$key}; }else{ return $self->{'__CONFIG'}; } } # /cfg =head2 schema() Connect to a schema. Anything else than the authentication credentials is hardcoded :-) =cut sub schema { my $self = shift; my $schema1 = $self->{'__SCHEMA'}; unless( $schema1 ) { my $dbc = $self->cfg('db') or die("Missing configuration values: db connection attributes"); $schema1 = Test::Schema->connect( $dbc->{dsn}, $dbc->{username}, $dbc->{password}, $dbc->{attributes}, ); $schema1->storage->sql_maker->quote_char('`'); $schema1->storage->sql_maker->name_sep('.'); $self->{'__SCHEMA'} = $schema1; } return $schema1; } # /schema =head2 with_dbic() Get a word with special chars from test database using DBIx::Class. =cut sub with_dbic { my $self = shift; my $schema = $self->schema(); my $word = $schema->resultset('Words')->first()->word(); say STDERR $word; Devel::Peek::Dump($word); } # /run =head2 with_dbi() Get a word with special chars from test database using DBI. =cut sub with_dbi { my $self = shift; my $dbc = $self->cfg('db') or die("Missing configuration values: db connection attributes"); my $dbh = DBI->connect( $dbc->{dsn}, $dbc->{username}, $dbc->{password}, $dbc->{attributes}, ) or die("Could not connect to DB " . DBI->errstr()); my $sql = qq~ SELECT word FROM words LIMIT 1 ~; my $sth = $dbh->prepare($sql) or die("prep: " . $dbh->errstr()); my $rv = $sth->execute() or die("exec: " . $dbh->errstr()); while( my $href = $sth->fetchrow_hashref() ) { my $word = $href->{word}; say STDERR $word; Devel::Peek::Dump( $word ); } } # /with_dbi 1; use strict; use warnings; use FindBin qw/$Bin/; use lib $Bin; my $app = MyTest->new(); $app->with_dbic(); $app->with_dbi();