diff --git a/cpanfile b/cpanfile index 5205f50..2200a00 100644 --- a/cpanfile +++ b/cpanfile @@ -20,5 +20,6 @@ requires 'File::Spec'; requires 'Mojolicious'; requires 'Pg::Explain', '>= 2.1'; requires 'Number::Bytes::Human'; +requires 'Pg::SQL::PrettyPrinter'; # vim: set ft=perl: diff --git a/explain.json b/explain.json index 1e48e7f..b79c5f9 100755 --- a/explain.json +++ b/explain.json @@ -1,6 +1,8 @@ { "title" : "explain.depesz.com", + "parser-url" : "http://127.0.0.1:15283/", + "secret" : "|Erp--Wjgb)+eiB/|H=|V7!#+M|L{a8=J2|pd+N1=M|&pJWq|M&,f3q^XS", "database" : { diff --git a/lib/Explain/Controller.pm b/lib/Explain/Controller.pm index 73ce853..ab23be8 100755 --- a/lib/Explain/Controller.pm +++ b/lib/Explain/Controller.pm @@ -4,9 +4,11 @@ use Mojo::Base 'Mojolicious::Controller'; use English -no_match_vars; +use Data::Dumper; use Pg::Explain; use Pg::Explain::Hinter; use pgFormatter::Beautify; +use Pg::SQL::PrettyPrinter; use Encode; use Email::Valid; use Config; @@ -344,11 +346,34 @@ sub show { $self->stash->{ title } = $data->{ 'title' }; my $query = $data->{ 'query' } // $explain->query; if ( $query ) { - my $beautifier = pgFormatter::Beautify->new(); - $beautifier->query( $query ); - $beautifier->beautify(); + my $pretty; + eval { + my $pspp = Pg::SQL::PrettyPrinter->new( + 'service' => $self->app->config->{ 'parser-url' }, + 'sql' => $query, + ); + $pspp->parse(); + $pretty = join( ";\n\n", map { $_->pretty_print } @{ $pspp->{ 'statements' } } ) . ";\n"; + $pretty .= "-- Formatted by Pg::SQL::PrettyPrinter\n"; + }; + if ( $EVAL_ERROR ) { + $self->app->log->error( + "beautifying query failed: " . Dumper( + { + 'id' => $id, + 'query' => $query, + 'error' => $EVAL_ERROR->message + } + ) + ); + my $beautifier = pgFormatter::Beautify->new(); + $beautifier->query( $query ); + $beautifier->beautify(); + $pretty = $beautifier->content(); + $pretty .= "-- Formatted by pgFormatter::Beautify\n"; + } - $self->stash->{ bquery } = $beautifier->content(); + $self->stash->{ bquery } = $pretty; $self->stash->{ query } = $query; } $self->stash->{ stats } = $stats;