Created
August 11, 2012 10:56
-
-
Save Htbaa/3323805 to your computer and use it in GitHub Desktop.
Patch for maximus_mojo.pl to use DBI instead of DBIx::Class. Doesn't lower memory usage though!
This file contains hidden or 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
diff --git script/maximus_mojo.pl script/maximus_mojo.pl | |
index 161c349..f0c7373 100644 | |
--- script/maximus_mojo.pl | |
+++ script/maximus_mojo.pl | |
@@ -3,7 +3,7 @@ use lib "./lib"; | |
use local::lib; | |
use Mojolicious::Lite; | |
use Config::Any; | |
-use Maximus::Schema; | |
+use DBI; | |
use Mojo::Util 'md5_sum'; | |
# Set process name | |
@@ -23,53 +23,60 @@ helper 'cfg' => sub { | |
# Helper for setting up the database connection | |
helper 'db' => sub { | |
- return Maximus::Schema->connect( | |
- shift->cfg->{'Model::DB'}->{connect_info}); | |
+ my $cfg = shift->cfg->{'Model::DB'}->{connect_info}; | |
+ return DBI->connect($cfg->{dsn}, $cfg->{user}, $cfg->{password}) | |
+ or die "Couldn't connect to database: " . DBI->errstr; | |
}; | |
# Serve module file from database or remote_location | |
# Route matches that of the Catalyst application | |
-# TODO: Optimize by using DBI directly instead of DBIx::Class? | |
get '/module/download/:scope/:module/#version' => sub { | |
my $self = shift; | |
my $modscope = $self->stash('scope'); | |
my $module = $self->stash('module'); | |
my $version = $self->stash('version'); | |
- my @search = ( | |
- { 'modscope.name' => $modscope, | |
- 'me.name' => $module, | |
- 'module_versions.version' => $version, | |
- }, | |
- {join => [qw /modscope module_versions/],} | |
- ); | |
- my $rs = $self->db->resultset('Module')->search(@search); | |
- my $row = $rs->first; | |
+ my $dbh = $self->db; | |
+ my $sth = $dbh->prepare(' | |
+ SELECT module.name module | |
+ , modscope.name modscope | |
+ , module_version.version | |
+ , module_version.remote_location | |
+ , module_version.id version_id | |
+ FROM module | |
+ JOIN modscope ON modscope.id = module.modscope_id | |
+ JOIN module_version ON module_version.module_id = module.id | |
+ WHERE | |
+ modscope.name = ? | |
+ AND module.name = ? | |
+ AND module_version.version = ?') | |
+ or die "Couldn't prepare statement: " . $dbh->errstr; | |
+ | |
+ $sth->execute($modscope, $module, $version) or die "Couldn't execute statement: " . $sth->errstr; | |
+ my $row = $sth->fetchrow_hashref(); | |
+ $sth->finish; | |
# Not found | |
return $self->render_not_found unless $row; | |
- my $version_rs = $row->search_related( | |
- 'module_versions', | |
- {version => $version}, | |
- {columns => [qw/id remote_location/]} | |
- ); | |
- my $version_row = $version_rs->first(); | |
- return $self->render_not_found unless $version_row; | |
- | |
# Redirect to remote location if available | |
- return $self->redirect_to($version_row->remote_location) | |
- if $version_row->remote_location; | |
+ return $self->redirect_to($row->{remote_location}) | |
+ if $row->{remote_location}; | |
# Fetch archive data | |
- $version_row = $version_row->get_from_storage({columns => [qw/archive/]}); | |
+ $sth = $dbh->prepare('SELECT archive FROM module_version WHERE id = ?') | |
+ or die "Couldn't prepare statement: " . $dbh->errstr; | |
+ $sth->execute($row->{version_id}) or die "Couldn't execute statement: " . $sth->errstr; | |
+ my $version_row = $sth->fetchrow_hashref(); | |
+ $sth->finish; | |
+ $dbh->disconnect; | |
my $filename = sprintf('%s-%s-%s.zip', $modscope, $module, $version); | |
my $headers = $self->res->headers; | |
$headers->add('Content-Disposition', | |
qq[attachment; filename="$filename"]); | |
- $headers->add('ETag', md5_sum($version_row->archive)); | |
- $self->render_data($version_row->archive, format => 'zip'); | |
+ $headers->add('ETag', md5_sum($version_row->{archive})); | |
+ $self->render_data($version_row->{archive}, format => 'zip'); | |
}; | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment