# 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;