lib/CrashTest/Plugin/Storage/Sql.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Sat, 12 Dec 2015 15:56:51 +0100
changeset 80 d33b5f6e0862
parent 78 0ebef32c34af
child 122 8692800ec9ba
permissions -rw-r--r--
Display exceptions when loading Mojo DB drivers

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package CrashTest::Plugin::Storage::Sql;
use Mojo::Base 'Mojolicious::Plugin';

sub register {
    my ($self, $app, $conf) = @_;

    push @{$app->commands->namespaces}, 'CrashTest::Plugin::Storage::Sql::Command';
    $app->storage->register(CrashTest::Plugin::Storage::Sql::Instance->new(app => $app, config => $conf));
}

1;

package CrashTest::Plugin::Storage::Sql::Instance;
use Mojo::Base "CrashTest::Plugin::Storage::Base";
use Mojo::Loader qw/load_class/;

has dbtype => sub {
    my $self = shift;
    my @dbtypes = keys %{$self->config->{db}};
    state $dbtype = $dbtypes[0];
};

has dbh => sub {
    my $self = shift;

    my $type = $self->dbtype;
    # Handle exceptions
    if(my $e = load_class "Mojo::$type") {
        die ref $e ? "Exception: $e" : "Mojo::$type is not installed";
    }
    state $dbh = "Mojo::$type"->new($self->config->{db}->{$type});
};

has models => sub {
    my $self = shift;

    state $models = $self->_load_models("CrashTest::Plugin::Storage::Sql::Model");
};

1;