r76337 - in /branches/upstream/libdancer-plugin-database-perl/current: Changes META.yml README lib/Dancer/Plugin/Database.pm lib/Dancer/Plugin/Database/Handle.pm t/01-basic.t t/lib/TestApp.pm

mxey-guest at users.alioth.debian.org mxey-guest at users.alioth.debian.org
Thu Jun 23 12:18:49 UTC 2011


Author: mxey-guest
Date: Thu Jun 23 12:18:39 2011
New Revision: 76337

URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=76337
Log:
[svn-upgrade] new version libdancer-plugin-database-perl (1.40)

Modified:
    branches/upstream/libdancer-plugin-database-perl/current/Changes
    branches/upstream/libdancer-plugin-database-perl/current/META.yml
    branches/upstream/libdancer-plugin-database-perl/current/README
    branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database.pm
    branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database/Handle.pm
    branches/upstream/libdancer-plugin-database-perl/current/t/01-basic.t
    branches/upstream/libdancer-plugin-database-perl/current/t/lib/TestApp.pm

Modified: branches/upstream/libdancer-plugin-database-perl/current/Changes
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libdancer-plugin-database-perl/current/Changes?rev=76337&op=diff
==============================================================================
--- branches/upstream/libdancer-plugin-database-perl/current/Changes (original)
+++ branches/upstream/libdancer-plugin-database-perl/current/Changes Thu Jun 23 12:18:39 2011
@@ -1,4 +1,22 @@
 Revision history for Dancer-Plugin-Database
+
+1.40    2011-05-29
+        - Be fork/thread-safe - don't allow processes/threads to share handles.
+          Thanks to Matt S Trout for pointing this out on IRC - cheers mst.
+        - If we're given a pre-assembled DSN, extract the driver from it to
+          avoid a warning, and to allow auto UTF-8 to work.
+          Thanks to Matthew Vickers (mvickers) for bringing this up.
+
+1.30    2011-05-23
+        - Allow passing an empty hashref for where clause to signify that no
+          where clause is desired (i.e. return all rows).
+          Requested by Carlos Sosa (gnusosa) - thanks!
+
+1.24    2011-05-09
+        - Bugfixes in logging - avoid DBI swallowing up the param I'd wrongly
+          named, and avoid warnings if any params are undef.
+          Thanks to Martin J Evans (mje) for bringing this up on IRC - thanks
+          for your help Martin.
 
 1.23    2011-04-15
         - Only log queries generated by quick_*() helpers in D::P::D::Handle if

Modified: branches/upstream/libdancer-plugin-database-perl/current/META.yml
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libdancer-plugin-database-perl/current/META.yml?rev=76337&op=diff
==============================================================================
--- branches/upstream/libdancer-plugin-database-perl/current/META.yml (original)
+++ branches/upstream/libdancer-plugin-database-perl/current/META.yml Thu Jun 23 12:18:39 2011
@@ -1,6 +1,6 @@
 --- #YAML:1.0
 name:               Dancer-Plugin-Database
-version:            1.23
+version:            1.40
 abstract:           easy database connections for Dancer applications
 author:
     - David Precious <davidp at preshweb.co.uk>

Modified: branches/upstream/libdancer-plugin-database-perl/current/README
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libdancer-plugin-database-perl/current/README?rev=76337&op=diff
==============================================================================
--- branches/upstream/libdancer-plugin-database-perl/current/README (original)
+++ branches/upstream/libdancer-plugin-database-perl/current/README Thu Jun 23 12:18:39 2011
@@ -50,6 +50,11 @@
     database if not. If the connection has gone away, a new connection will
     be obtained and returned. This avoids any problems for a long-running
     script where the connection to the database might go away.
+
+    Care is taken that handles are not shared across processes/threads, so
+    this should be thread-safe with no issues with transactions etc. (Thanks
+    to Matt S Trout for pointing out the previous lack of thread safety.
+    Inspiration was drawn from DBIx::Connector.)
 
 CONFIGURATION
     Connection details will be taken from your Dancer application config
@@ -177,6 +182,9 @@
       # Delete the row with id 42:
       database->quick_delete($table_name, { id => 42 });
 
+      # Fetch all rows from a table (since version 1.30):
+      database->quick_select($table_name, {});
+
 AUTHOR
     David Precious, `<davidp at preshweb.co.uk>'
 
@@ -201,6 +209,14 @@
     Christian Sánchez
 
     Michael Stiller
+
+    Martin J Evans
+
+    Carlos Sosa
+
+    Matt S Trout
+
+    Matthew Vickers
 
 BUGS
     Please report any bugs or feature requests to

Modified: branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database.pm?rev=76337&op=diff
==============================================================================
--- branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database.pm (original)
+++ branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database.pm Thu Jun 23 12:18:39 2011
@@ -12,7 +12,7 @@
 
 =cut
 
-our $VERSION = '1.23';
+our $VERSION = '1.40';
 
 my $settings = undef;
 
@@ -55,8 +55,14 @@
         }
     }
 
+    # To be fork safe and thread safe, use a combination of the PID and TID (if
+    # running with use threads) to make sure no two processes/threads share
+    # handles.  Implementation based on DBIx::Connector by David E. Wheeler.
+    my $pid_tid = $$;
+    $pid_tid .= '_' . threads->tid if $INC{'threads.pm'};
+
     # OK, see if we have a matching handle
-    $handle = $handles{$handle_key} || {};
+    $handle = $handles{$pid_tid}{$handle_key} || {};
     
     if ($handle->{dbh}) {
         if ($conn_details->{connection_check_threshold} &&
@@ -81,7 +87,7 @@
         # Get a new connection
         if ($handle->{dbh} = _get_connection($conn_details)) {
             $handle->{last_connection_check} = time;
-            $handles{$handle_key} = $handle;
+            $handles{$pid_tid}{$handle_key} = $handle;
             return $handle->{dbh};
         } else {
             return;
@@ -97,10 +103,13 @@
 
     # Assemble the DSN:
     my $dsn;
+    my $driver;
     if ($settings->{dsn}) {
         $dsn = $settings->{dsn};
+        ($driver) = $dsn =~ m{dbi:([^:]+)};
     } else {
         $dsn = "dbi:" . $settings->{driver};
+        $driver = $settings->{driver};
         my @extra_args;
 
         # DBD::SQLite wants 'dbname', not 'database', so special-case this
@@ -109,7 +118,7 @@
         # things easier for our users by handling this for them):
         # (DBD::SQLite will support 'database', too, as of 1.32 when it's
         # released)
-        if ($settings->{driver} eq 'SQLite' 
+        if ($driver eq 'SQLite' 
             && $settings->{database} && !$settings->{dbname}) {
             $settings->{dbname} = delete $settings->{database};
         }
@@ -135,7 +144,7 @@
             mysql  => 'mysql_enable_utf8',
             Pg     => 'pg_enable_utf8',
         );
-        if (my $param = $param_for_driver{ $settings->{driver} }) {
+        if (my $param = $param_for_driver{$driver}) {
             Dancer::Logger::debug(
                 "Adding $param to DBI connection params to enable UTF-8 support"
             );
@@ -165,7 +174,9 @@
     # Dancer::Plugin::Database::Handle should be logged or not; this seemed a
     # little dirty, but DBI's docs encourage it
     # ("You can stash private data into DBI handles via $h->{private_..._*}..")
-    $dbh->{_private_log_queries} = $settings->{log_queries};
+    $dbh->{private_dancer_plugin_database} = {
+        log_queries => $settings->{log_queries} || 0,
+    };
 
     # Re-bless it as a Dancer::Plugin::Database::Handle object, to provide nice
     # extra features:
@@ -287,6 +298,11 @@
 connection will be obtained and returned.  This avoids any problems for
 a long-running script where the connection to the database might go away.
 
+Care is taken that handles are not shared across processes/threads, so this
+should be thread-safe with no issues with transactions etc.  (Thanks to Matt S
+Trout for pointing out the previous lack of thread safety.  Inspiration was
+drawn from DBIx::Connector.)
+
 =head1 CONFIGURATION
 
 Connection details will be taken from your Dancer application config file, and
@@ -421,6 +437,9 @@
   # Delete the row with id 42:
   database->quick_delete($table_name, { id => 42 });
 
+  # Fetch all rows from a table (since version 1.30):
+  database->quick_select($table_name, {});
+
 
 =head1 AUTHOR
 
@@ -452,6 +471,15 @@
 
 Michael Stiller
 
+Martin J Evans
+
+Carlos Sosa
+
+Matt S Trout
+
+Matthew Vickers
+
+
 =head1 BUGS
 
 Please report any bugs or feature requests to C<bug-dancer-plugin-database at rt.cpan.org>, or through

Modified: branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database/Handle.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database/Handle.pm?rev=76337&op=diff
==============================================================================
--- branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database/Handle.pm (original)
+++ branches/upstream/libdancer-plugin-database-perl/current/lib/Dancer/Plugin/Database/Handle.pm Thu Jun 23 12:18:39 2011
@@ -5,7 +5,7 @@
 use DBI;
 use base qw(DBI::db);
 
-our $VERSION = '0.04';
+our $VERSION = '0.06';
 
 =head1 NAME
 
@@ -24,8 +24,12 @@
   # Updating a record where id = 42:
   database->quick_update($tablename, { id => 42 }, { foo => 'New value' });
 
-  # Fetching a row quickly
+  # Fetching a single row quickly in scalar context
   my $employee = database->quick_select('employees', { id => $emp_id });
+
+  # Fetching multiple rows in list context - passing an empty hashref to signify
+  # no where clause (i.e. return all rows -  so "select * from $table_name"):
+  my @all_employees = database->quick_select('employees', {});
 
 
 =head1 Added features
@@ -145,7 +149,9 @@
         push @bind_params, values %$data;
     }
     
-    if ($type eq 'UPDATE' || $type eq 'DELETE' || $type eq 'SELECT') {
+    if (($type eq 'UPDATE' || $type eq 'DELETE' || $type eq 'SELECT') 
+        && keys %$where)
+    {
         $sql .= " WHERE " . join " AND ",
             map { $self->quote_identifier($_) . '=?' } keys %$where;
         push @bind_params, values %$where;
@@ -159,13 +165,14 @@
 
     # Dancer::Plugin::Database will have looked at the log_queries setting and
     # stashed it away for us to see:
-    if ($self->{_private_log_queries}) {
+    if ($self->{private_dancer_plugin_database}{log_queries}) {
         Dancer::Logger::debug(
             "Executing $type query $sql with params " . join ',', 
-            map { 
+            map {
+                defined $_ ? 
                 $_ =~ /^[[:ascii:]]+$/ ? 
                     length $_ > 50 ? substr($_, 0, 47) . '...' : $_
-                : "[non-ASCII data not logged]" 
+                : "[non-ASCII data not logged]" : 'undef'
             } @bind_params
         );
     }
@@ -222,6 +229,13 @@
 injection attacks will not work, but it's easier to illustrate as though the
 values were interpolated directly.  Don't worry, they're not.))
 
+You can pass an empty hashref if you  want all rows, e.g.:
+
+  database->quick_select('mytable', {});
+
+... is the same as C<"SELECT * FROM 'mytable'">
+
+
 TODO: this isn't very flexible; it would be nice to easily use other logic
 combinations, and other comparisons other than a straightforward equality
 comparison.  However, supporting this abstraction without the syntax used

Modified: branches/upstream/libdancer-plugin-database-perl/current/t/01-basic.t
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libdancer-plugin-database-perl/current/t/01-basic.t?rev=76337&op=diff
==============================================================================
--- branches/upstream/libdancer-plugin-database-perl/current/t/01-basic.t (original)
+++ branches/upstream/libdancer-plugin-database-perl/current/t/01-basic.t Thu Jun 23 12:18:39 2011
@@ -12,7 +12,7 @@
     plan skip_all => 'DBD::SQLite required to run these tests';
 }
 
-plan tests => 19;
+plan tests => 20;
 
 my $dsn = "dbi:SQLite:dbname=:memory:";
 
@@ -66,3 +66,6 @@
 response_content_like [ GET => '/runtime_config' ], qr/ok/,
     "runtime_config got a usable database handle";
 
+# Test that we get the same handle each time we call the database() keyword
+# (i.e., that handles are cached appropriately)
+response_content_like [ GET => '/handles_cached' ], qr/Same handle returned/;

Modified: branches/upstream/libdancer-plugin-database-perl/current/t/lib/TestApp.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libdancer-plugin-database-perl/current/t/lib/TestApp.pm?rev=76337&op=diff
==============================================================================
--- branches/upstream/libdancer-plugin-database-perl/current/t/lib/TestApp.pm (original)
+++ branches/upstream/libdancer-plugin-database-perl/current/t/lib/TestApp.pm Thu Jun 23 12:18:39 2011
@@ -75,4 +75,9 @@
     $dbh ? 'ok' : '';
 };
 
+# Check we get the same handle each time we call database()
+get '/handles_cached' => sub {
+    database() eq database() and return "Same handle returned";
+};
+
 1;




More information about the Pkg-perl-cvs-commits mailing list