Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 87 additions and 2 deletions.
  1. +18 −2 lib/Dancer/Plugin/DBIC.pm
  2. +69 −0 t/05-querylog.t
View
20 lib/Dancer/Plugin/DBIC.pm
@@ -5,11 +5,23 @@ package Dancer::Plugin::DBIC;
use strict;
use warnings;
use Dancer::Plugin;
+use Dancer qw(request);
use DBIx::Class;
use Module::Load;
my $schemas = {};
+sub _apply_debug {
+ my $schema = shift;
+ return $schema unless request && Plack::Middleware::DBIC::QueryLog->can("get_querylog_from_env");
+ if (my $debugobj = Plack::Middleware::DBIC::QueryLog->get_querylog_from_env(request->env)) {
+ $schema = $schema->clone;
+ $schema->storage->debugobj($debugobj);
+ $schema->storage->debug(1);
+ }
+ return $schema;
+}
+
register schema => sub {
my $name = shift;
my $cfg = plugin_setting;
@@ -24,7 +36,7 @@ register schema => sub {
}
}
- return $schemas->{$name} if $schemas->{$name};
+ return _apply_debug($schemas->{$name}) if $schemas->{$name};
my $options = $cfg->{$name} or die "The schema $name is not configured";
@@ -50,7 +62,7 @@ register schema => sub {
$schemas->{$name} = DBIx::Class::Schema::Loader->connect(@conn_info);
}
- return $schemas->{$name};
+ return _apply_debug($schemas->{$name});
};
register_plugin;
@@ -155,6 +167,10 @@ Otherwise, you B<must> provide C<schema()> with the name of the database:
my $user = schema('foo')->resultset('User')->find('bob');
+If L<Plack::Middleware::DBIC::QueryLog> is in use, the schema object
+will be cloned and have the query log object applied to it before it's
+returned.
+
=head1 SCHEMA GENERATION
There are two approaches for generating schema classes.
View
69 t/05-querylog.t
@@ -0,0 +1,69 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+BEGIN {
+ unless (eval {
+ require DBD::SQLite;
+ require Plack::Test;
+ require Plack::Middleware::DBIC::QueryLog;
+ 1;
+ }) {
+ plan skip_all => "DBD::SQLite, Plack::Test and Plack::Middleware::DBIC::QueryLog required to run these tests";
+ }
+}
+
+{
+ package QueryLogApp;
+ use Dancer;
+ use lib 't/lib';
+
+ use DBI;
+ use File::Temp qw(tempfile);
+
+ use Dancer::Plugin::DBIC;
+ use Plack::Middleware::DBIC::QueryLog;
+
+ my (undef, $dbfile) = tempfile(SUFFIX => '.db');
+
+ set plugins => {
+ DBIC => {
+ foo => {
+ schema_class => 'Foo',
+ dsn => "dbi:SQLite:dbname=$dbfile",
+ },
+ },
+ };
+
+ set apphandler => 'PSGI';
+
+ set plack_middlewares => [
+ ['DBIC::QueryLog'],
+ ];
+
+ my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile");
+ $dbh->do(q{
+ create table user (name varchar(100) primary key, age int)
+ });
+
+ get '/' => sub {
+ schema->resultset('User')->count;
+ my $ql = Plack::Middleware::DBIC::QueryLog->get_querylog_from_env(request->env);
+ $ql->count;
+ };
+}
+
+use HTTP::Request::Common;
+use Plack::Test;
+
+test_psgi
+ app => QueryLogApp->dance,
+ client => sub {
+ my $cb = shift;
+ my $res = $cb->(GET '/');
+ ok $res->is_success, "querylog request successful";
+ is $res->content, "1", "one query executed";
+ };
+
+done_testing;

No commit comments for this range

Something went wrong with that request. Please try again.