-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brian Duggan
committed
Apr 26, 2013
1 parent
1ecd890
commit 770af39
Showing
6 changed files
with
163 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
=head1 NAME | ||
Tuba::DB -- Rose::DB derived class for Tuba objects. | ||
=cut | ||
|
||
package Tuba::DB; | ||
use Tuba::Plugin::Db; | ||
use base 'Rose::DB'; | ||
use strict; | ||
use warnings; | ||
|
||
Tuba::DB->register_db( | ||
domain => "default", | ||
type => "default", | ||
driver => 'Pg', | ||
); | ||
|
||
sub dbi_connect { | ||
Tuba::Plugin::Db->connection->dbh; | ||
} | ||
|
||
1; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
=head1 NAME | ||
Tuba::Image : Controller class for images. | ||
=cut | ||
|
||
package Tuba::Image; | ||
|
||
use Mojo::Base qw/Mojolicious::Controller/; | ||
|
||
sub metadata { | ||
my $c = shift; | ||
$c->render_text("testing"); | ||
} | ||
|
||
1; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package Tuba::Plugin::Db; | ||
use Mojo::Base qw/Mojolicious::Plugin/; | ||
use DBIx::Connector; | ||
use DBIx::Simple; | ||
use SQL::Abstract; | ||
use SQL::Interp; | ||
use Rose::DB::Object::Loader; | ||
use Tuba::DB; | ||
|
||
{ | ||
my $dbix; | ||
|
||
sub register { | ||
my ($self, $app, $conf) = @_; | ||
my $dbname = $conf->{dbname} or die "no dbname in config file"; | ||
|
||
$app->log->info("Registering database $dbname"); | ||
|
||
my $dsn = "dbi:Pg:dbname=$dbname"; | ||
$dsn .= ":host=$conf->{host}" if $conf->{host}; | ||
$dsn .= ":port=$conf->{port}" if $conf->{port}; | ||
|
||
$dbix = DBIx::Connector->new( $dsn, ($conf->{user} || ''), | ||
( $conf->{password} || '' ), | ||
{ RaiseError => 1, AutoCommit => 1 } ); | ||
|
||
$app->helper( db => sub { $dbix } ); | ||
$app->helper( dbs => sub { DBIx::Simple->new( shift->db->dbh ) } ); | ||
|
||
my $rose_db = Tuba::DB->new(); | ||
my $loader = Rose::DB::Object::Loader->new( class_prefix => 'Tuba::Obj', db_schema => $conf->{schema} ); | ||
my @made = $loader->make_classes(db_class => 'Tuba::DB' ); | ||
$app->log->info("Loaded ".@made." classes"); | ||
|
||
my %orm; # Map table names to class names. | ||
for (@made) { | ||
if ($_->isa("Rose::DB::Object::Manager")) { | ||
$orm{$_->object_class->meta->table}{mng} = $_; | ||
} else { | ||
$orm{$_->meta->table}{obj} = $_; | ||
} | ||
} | ||
$app->helper(orm => sub { \%orm }); | ||
|
||
1; | ||
} | ||
|
||
sub connection { | ||
$dbix; | ||
} | ||
} | ||
|
||
|
||
1; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
=head1 NAME | ||
Tuba::Plugin::Debug - helpful diagnostics | ||
=head1 DESCRIPTION | ||
This plugin is if the server is in debug mode; it provides | ||
extra routes to aid in debugging. | ||
=cut | ||
|
||
package Tuba::Plugin::Debug; | ||
use Mojo::Base qw/Mojolicious::Plugin/; | ||
use Data::Dumper; | ||
|
||
sub _render_debug { | ||
my $c = shift; | ||
|
||
die "no database handle\n\n$DBI::errstr\n" unless $c->db->dbh; | ||
my @output; | ||
|
||
my $got = $c->db->dbh->selectall_arrayref('select 42') or die $DBI::errstr; | ||
push @output, "db said @{$got->[0]}"; | ||
|
||
my ($flat) = $c->dbs->query('select 42')->flat; | ||
push @output, "db said $flat"; | ||
|
||
my $orm = $c->orm; | ||
for my $table (sort keys %$orm) { | ||
push @output, "$table : ".join ' ', $orm->{$table}{obj}->meta->columns; | ||
} | ||
|
||
$c->res->headers->content_type('text/plain'); | ||
$c->render_text(join "\n", @output); | ||
} | ||
|
||
sub register { | ||
my ($self, $app, $conf) = @_; | ||
return if $app->mode eq 'production'; | ||
return unless $ENV{TUBA_DEBUG}; | ||
|
||
$app->routes->get('/debug' => \&_render_debug); | ||
|
||
1; | ||
} | ||
|
||
1; | ||
|