Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Mojolicious::Plugin::Database;
- use Mojo::Base 'Mojolicious::Plugin';
- use DBIx::Simple;
- sub register {
- my ($self, $app) = @_;
- $app->attr(dbh => sub {
- my $self = shift;
- my $c = $self->config;
- my $dbh = DBIx::Simple->new(
- "dbi:mysql:database=$c->{db_name};host=$c->{db_host};port=$c->{db_port}",
- $c->{db_user},
- $c->{db_pass},
- {
- RaiseError => 1,
- mysql_enable_utf8 => 1,
- mysql_auto_reconnect => 1,
- Callbacks => {
- connected => sub {
- shift->do('SET NAMES UTF8');
- return;
- }
- }
- }
- ) or die DBIx::Simple->error;
- return $dbh;
- });
- }
- 1;
- __END__
- # And then in your Mojolicious app:
- $self->plugin('Database');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement