Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Universal #32

Merged
merged 10 commits into from

2 participants

@ambs
Collaborator

Dancer 2 ready, I would say.

lib/Dancer/Plugin/Database.pm
((7 lines not shown))
- database_connection_lost
- database_connection_failed
- database_error
- )
-);
+my @dpd_hooks = qw(
+ database_connected
+ database_connection_lost
+ database_connection_failed
+ database_error
+ );
+if ($dancer_version == 1) {
+ Dancer::Factory::Hook->instance->install_hooks(@dpd_hooks);
+} else {
+ register_hook(@dpd_hooks);
+}
@bigpresh Owner

It might make sense to have an $install_hooks coderef which knows how to register hooks depending on the Dancer version, in much the same way as the cunningly named $hooker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Dancer/Plugin/Database.pm
((7 lines not shown))
use DBI;
use Dancer::Plugin::Database::Handle;
+use Scalar::Util 'blessed';
+
+my $dancer_version = (exists &dancer_version) ? int(dancer_version()) : 1;
+my ($logger, $hooker);
+if ($dancer_version == 1) {
+ require Dancer::Config;
+ Dancer::Config->import();
+
+ $logger = sub { Dancer::Logger->can($_[0])->($_[1]) };
+ $hooker = sub { Dancer::Factory::Hook->instance->execute_hooks(@_) };
@bigpresh Owner

Whist the name amuses me, I think that should probably be $execute_hooks or similar, and a similar $register_hooks could also be provided (see the other note further down)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bigpresh bigpresh merged commit 9f0f8f1 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 21, 2012
  1. @ambs

    In the way for a Universal DPD

    ambs authored
  2. @ambs

    Nothing relevant

    ambs authored
Commits on Jul 22, 2012
  1. @ambs

    More passing tests

    ambs authored
  2. @ambs

    Nothing relevant

    ambs authored
Commits on Jul 24, 2012
  1. @ambs
  2. @ambs

    Clean up code

    ambs authored
  3. @ambs

    Clean up some more

    ambs authored
  4. @ambs

    Better var naming

    ambs authored
Commits on Jul 25, 2012
  1. @ambs

    New Dancer 2 way for hooks

    ambs authored
  2. @ambs

    Use last commits on dancer 1

    ambs authored
This page is out of date. Refresh to see the latest.
View
4 Changes
@@ -1,5 +1,9 @@
Revision history for Dancer-Plugin-Database
+2.00 ...
+ [ ENHANCEMENTS ]
+ - Support for Dancer 2
+
1.82 2012-06-28
[ BUGFIXES ]
- Fix for Debian RT #665221 :
View
4 Makefile.PL
@@ -13,7 +13,9 @@ WriteMakefile(
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
- 'Dancer' => 1.3050,
+ # We need support for "var("foo" => "bar")".
+ # Not sure if this is the correct version
+ 'Dancer' => 1.3059,
'DBI' => 0,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
View
91 lib/Dancer/Plugin/Database.pm
@@ -1,10 +1,23 @@
package Dancer::Plugin::Database;
use strict;
+use Dancer ':syntax';
use Dancer::Plugin;
-use Dancer::Config;
use DBI;
use Dancer::Plugin::Database::Handle;
+use Scalar::Util 'blessed';
+
+my $dancer_version = (exists &dancer_version) ? int(dancer_version()) : 1;
+my ($logger);
+if ($dancer_version == 1) {
+ require Dancer::Config;
+ Dancer::Config->import();
+
+ $logger = sub { Dancer::Logger->can($_[0])->($_[1]) };
+} else {
+ $logger = sub { log @_ };
+}
+
=encoding utf8
@@ -14,11 +27,13 @@ Dancer::Plugin::Database - easy database connections for Dancer applications
=cut
-our $VERSION = '1.82';
+our $VERSION = '2.00';
my $settings = undef;
-sub _load_db_settings { $settings = plugin_setting(); }
+sub _load_db_settings {
+ $settings = plugin_setting();
+}
my %handles;
# Hashref used as key for default handle, so we don't have a magic value that
@@ -27,7 +42,8 @@ my %handles;
my $def_handle = {};
register database => sub {
- my $arg = shift;
+ my ($self, $arg) = plugin_args(@_);
+ $arg = shift if blessed($arg) and $arg->isa('Dancer::Core::DSL');
_load_db_settings() if (!$settings);
@@ -50,9 +66,7 @@ register database => sub {
$handle_key = defined $arg ? $arg : $def_handle;
$conn_details = _get_settings($arg);
if (!$conn_details) {
- Dancer::Logger::error(
- "No DB settings for " . ($arg || "default connection")
- );
+ $logger->(error => "No DB settings for " . ($arg || "default connection"));
return;
}
}
@@ -65,7 +79,7 @@ register database => sub {
# OK, see if we have a matching handle
$handle = $handles{$pid_tid}{$handle_key} || {};
-
+
if ($handle->{dbh}) {
if ($handle->{dbh}{Active} && $conn_details->{connection_check_threshold} &&
time - $handle->{last_connection_check}
@@ -77,13 +91,10 @@ register database => sub {
$handle->{last_connection_check} = time;
return $handle->{dbh};
} else {
- Dancer::Logger::debug(
- "Database connection went away, reconnecting"
- );
- Dancer::Factory::Hook->instance->execute_hooks(
- 'database_connection_lost', $handle->{dbh}
- );
+ $logger->(debug => "Database connection went away, reconnecting");
+ execute_hook('database_connection_lost', $handle->{dbh});
+
if ($handle->{dbh}) { eval { $handle->{dbh}->disconnect } }
return $handle->{dbh}= _get_connection($conn_details);
@@ -113,16 +124,11 @@ register database => sub {
}
};
-Dancer::Factory::Hook->instance->install_hooks(
- qw(
- database_connected
- database_connection_lost
- database_connection_failed
- database_error
- )
-);
-
-register_plugin;
+register_hook(qw(database_connected
+ database_connection_lost
+ database_connection_failed
+ database_error));
+register_plugin(for_versions => ['1', '2']);
# Given the settings to use, try to get a database connection
sub _get_connection {
@@ -163,10 +169,10 @@ sub _get_connection {
# If the app is configured to use UTF-8, the user will want text from the
# database in UTF-8 to Just Work, so if we know how to make that happen, do
# so, unless they've set the auto_utf8 plugin setting to a false value.
- my $app_charset = Dancer::Config::setting('charset');
+ my $app_charset = setting('charset');
my $auto_utf8 = exists $settings->{auto_utf8} ? $settings->{auto_utf8} : 1;
if (lc $app_charset eq 'utf-8' && $auto_utf8) {
-
+
# The option to pass to the DBI->connect call depends on the driver:
my %param_for_driver = (
SQLite => 'sqlite_unicode',
@@ -177,9 +183,8 @@ sub _get_connection {
my $param = $param_for_driver{$driver};
if ($param && !$settings->{dbi_params}{$param}) {
- Dancer::Logger::debug(
- "Adding $param to DBI connection params to enable UTF-8 support"
- );
+ $logger->(debug,
+ "Adding $param to DBI connection params to enable UTF-8 support");
$settings->{dbi_params}{$param} = 1;
}
}
@@ -187,36 +192,28 @@ sub _get_connection {
# To support the database_error hook, use DBI's HandleError option
$settings->{dbi_params}{HandleError} = sub {
my ($error, $handle) = @_;
- Dancer::Factory::Hook->instance->execute_hooks(
- 'database_error', $error, $handle
- );
+ execute_hook('database_error', $error, $handle);
};
-
- my $dbh = DBI->connect($dsn,
+ my $dbh = DBI->connect($dsn,
$settings->{username}, $settings->{password}, $settings->{dbi_params}
);
if (!$dbh) {
- Dancer::Logger::error(
- "Database connection failed - " . $DBI::errstr
- );
- Dancer::Factory::Hook->instance->execute_hooks(
- 'database_connection_failed', $settings
- );
+ $logger->(error => "Database connection failed - " . $DBI::errstr);
+ execute_hook('database_connection_failed', $settings);
return;
} elsif (exists $settings->{on_connect_do}) {
my $to_do = ref $settings->{on_connect_do} eq 'ARRAY'
? $settings->{on_connect_do}
: [ $settings->{on_connect_do} ];
for (@$to_do) {
- $dbh->do($_) or Dancer::Logger::error(
- "Failed to perform on-connect command $_"
- );
+ $dbh->do($_) or
+ $logger->(error => "Failed to perform on-connect command $_");
}
}
- Dancer::Factory::Hook->instance->execute_hooks('database_connected', $dbh);
+ execute_hook('database_connected', $dbh);
# Indicate whether queries generated by quick_query() etc in
# Dancer::Plugin::Database::Handle should be logged or not; this seemed a
@@ -281,9 +278,9 @@ sub _get_settings {
$return_settings = { %$settings };
} else {
# OK, didn't match anything
- Dancer::Logger::error(
- "Asked for a database handle named '$name' but no matching "
- ."connection details found in config"
+ $logger->('error',
+ "Asked for a database handle named '$name' but no matching "
+ ."connection details found in config"
);
}
}
View
8 t/00-load.t
@@ -1,10 +1,12 @@
-#!perl -T
+#!perl
+# had -T
-use Test::More tests => 1;
+use Test::More import => ['!pass'], tests => 1;
+use Dancer;
BEGIN {
use_ok( 'Dancer::Plugin::Database' ) || print "Bail out!
";
}
-diag( "Testing Dancer::Plugin::Database $Dancer::Plugin::Database::VERSION, Perl $], $^X" );
+diag( "Testing Dancer::Plugin::Database $Dancer::Plugin::Database::VERSION, with Dancer $Dancer::VERSION in Perl $], $^X" );
View
49 t/01-basic.t
@@ -4,7 +4,17 @@ use warnings;
use Test::More import => ['!pass'];
use t::lib::TestApp;
use Dancer ':syntax';
-use Dancer::Test;
+
+my $dancer_version;
+BEGIN {
+ $dancer_version = (exists &dancer_version) ? int(dancer_version()) : 1;
+ require Dancer::Test;
+ if ($dancer_version == 1) {
+ Dancer::Test->import();
+ } else {
+ Dancer::Test->import('t::lib::TestApp');
+ }
+}
eval { require DBD::SQLite };
if ($@) {
@@ -15,20 +25,29 @@ plan tests => 41;
my $dsn = "dbi:SQLite:dbname=:memory:";
-set plugins => {
- Database => {
- dsn => $dsn,
- connection_check_threshold => 0.1,
- dbi_params => {
- RaiseError => 0,
- PrintError => 0,
- PrintWarn => 0,
- },
- handle_class => 'TestHandleClass',
- }
-};
-set logger => 'capture'; set log => 'debug';
-
+my $conf = {
+ Database => {
+ dsn => $dsn,
+ connection_check_threshold => 0.1,
+ dbi_params => {
+ RaiseError => 0,
+ PrintError => 0,
+ PrintWarn => 0,
+ },
+ handle_class => 'TestHandleClass',
+ }
+ };
+
+
+if ($dancer_version == 1) {
+ set plugins => $conf;
+ set logger => 'capture';
+ set log => 'debug';
+} else {
+ t::lib::TestApp->dancer_app->setting( plugins => $conf );
+ t::lib::TestApp->dancer_app->setting( logger => 'capture');
+ t::lib::TestApp->dancer_app->setting( log => 'debug');
+}
response_content_is [ GET => '/connecthookfired' ], 1,
'database_connected hook fires';
View
18 t/lib/TestApp.pm
@@ -4,18 +4,16 @@ use Dancer;
use Dancer::Plugin::Database;
no warnings 'uninitialized';
-
hook database_connected => sub {
my $dbh = shift;
- vars->{connecthookfired} = $dbh;
-
+ var(connecthookfired => $dbh);
};
-get '/connecthookfired' => sub {
+get '/connecthookfired' => sub {
my $database = database();
# If the hook fired, it'll have squirreled away a reference to the DB handle
# for us to look for.
- my $h = vars->{connecthookfired};
+ my $h = var('connecthookfired');
if (ref $h && $h->isa('DBI::db')) {
return 1;
} else {
@@ -189,22 +187,22 @@ get '/handles_cached' => sub {
# Check that the database_connection_lost hook fires when we force a db handle
# to go away:
-hook database_connection_lost => sub { vars->{lost_connection} = 1; };
+hook database_connection_lost => sub { var(lost_connection => 1); };
get '/database_connection_lost_fires' => sub {
- vars->{lost_connection} = 0;
+ var(lost_connection => 0);
database()->disconnect;
# We set connection_check_threshold to 0.1 at the start, so wait a second
# then check that the code detects the handle is no longer connected and
# triggers the hook
sleep 1;
my $handle = database();
- return vars->{lost_connection};
+ return var('lost_connection');
};
# Check that database_connection_failed hook fires if we can't connect - pass
# bogus connection details to make that happen
hook database_connection_failed => sub {
- vars->{connection_failed} = 1;
+ var connection_failed => 1;
};
get '/database_connection_failed_fires' => sub {
# Give a ridiculous database filename which should never exist in order to
@@ -217,7 +215,7 @@ get '/database_connection_failed_fires' => sub {
PrintError => 0,
},
});
- return vars->{connection_failed};
+ return var 'connection_failed';
};
# Check that the handle isa() subclass of the named class
Something went wrong with that request. Please try again.