Skip to content

Instantly share code, notes, and snippets.

@shoorick
Created December 23, 2010 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoorick/752990 to your computer and use it in GitHub Desktop.
Save shoorick/752990 to your computer and use it in GitHub Desktop.
DBI model for Catalyst with UTF-8 support
package MyApp::Model::DBI;
use strict;
use warnings;
use base 'Catalyst::Model::DBI';
# skipped some configuration statements
__PACKAGE__->config(
'dsn' => 'dbi:mysql:' . $config->{'sql'}->{'database'} . ':' . $config->{'sql'}->{'host'},
'user' => $config->{'sql'}->{'user'},
'password' => $config->{'sql'}->{'password'},
'options' => {
'AutoCommit' => 1,
'mysql_enable_utf8' => 1,
},
'on_connect_do' => [
'SET NAMES utf8',
],
# 'flags' => { 'some_flag' => 1 }, # used after connection
);
=head2 connect
Connect to database and perform some queries
=cut
sub connect {
my $self = shift;
my $dbh = $self->SUPER::connect;
return undef
unless defined $dbh;
# After connection
# set flags
while ( my ( $key, $value ) = each %{ $self->{ 'flags' } } ) {
$self->dbh->{ $key } = $value;
};
# do queries
map { $dbh->do( $_ ) } @{ $self->{ 'on_connect_do' } };
return $dbh;
} # sub connect
=head1 NAME
MyApp::Model::DBI - DBI Model Class
=head1 SYNOPSIS
See L<MyApp> and L<Catalyst::Model::DBI>
=head1 DESCRIPTION
DBI Model Class.
Extended from Catalyst::Model::DBI: added support for UTF-8 with MySQL
=head1 AUTHOR
Alex Pavlovic, Alexander Sapozhnikov
=head1 LICENSE
This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment