File Coverage

blib/lib/DBIx/Core/Handle/ReturnValue.pm
Criterion Covered Total %
statement 9 22 40.9
branch 0 14 0.0
condition 0 6 0.0
subroutine 3 4 75.0
pod 1 1 100.0
total 13 47 27.6


line stmt bran cond sub pod time code
1             package DBIx::Core::Handle::ReturnValue;
2              
3 1     1   52110 use strict;
  1         2  
  1         24  
4 1     1   4 use warnings;
  1         1  
  1         23  
5              
6 1     1   378 use parent qw(Dancer::Plugin::Database::Core::Handle);
  1         233  
  1         4  
7              
8             our $VERSION = '0.01';
9              
10             =head1 NAME
11            
12             DBIx::Core::Handle::ReturnValue - subclassed DBI connection handle
13            
14             =head1 DESCRIPTION
15            
16             Subclassed DBI connection handle with added convenience features. In addition
17             to the features added by Dancer::Plugin::Database::Core::Handle, this module
18             adds a return value to C if requested.
19            
20            
21             =head1 SYNOPSIS
22              
23             Set this Handle in you app config:
24              
25              
26             plugins:
27             Database:
28             driver: 'Pg'
29             database: 'postgres'
30             host: '172.17.0.3'
31             port: 5432
32             username: 'postgres'
33             password: 'pwd'
34             dbi_params:
35             RaiseError: 1
36             AutoCommit: 1
37             log_queries: 1
38             handle_class: 'DBIx::Core::Handle::ReturnValue'
39              
40              
41              
42             # in your Dancer app:
43             my $id = database->quick_insert($tablename, \%data, { last_insert_id => [ ... ] });
44            
45             my $uuid = database->quick_insert('mytable', { foo => 'Bar', baz => 5 }, { returning => 'entry_uuid' });
46              
47              
48             =head1 Added features
49            
50             A C object is a subclassed L L
51             database handle, with the following added convenience methods in addition to
52             those added by Dancer::Plugin::Database::Core::Handle:
53            
54             =over 4
55            
56             =item quick_insert
57            
58             database->quick_insert('mytable', { foo => 'Bar', baz => 5 }, { returning => 'id' });
59            
60             This is a PostgreSQL-specific functionality which is very flexible and easy. It is
61             especially useful when you use UUIDs as primary keys which are not returned by the
62             normal last_insert_id functionality.
63            
64             database->quick_insert('mytable', { foo => 'Bar', baz => 5 }, { last_insert_id => [ .. ] });
65              
66             The C variant calls the DBI method C with the parameters
67             given in the array ref above. It depends on your database driver what needs to be filled
68             in there.
69              
70             If no third parameter is given, the call acts like defined by
71             Dancer::Plugin::Database::Core::Handle
72              
73             =back
74              
75             =cut
76              
77             sub quick_insert {
78 0     0 1   my ($self, $table_name, $data, $options) = @_;
79 0           my $type = 'INSERT';
80 0 0 0       if (ref $options ne 'HASH' or (!exists $options->{returning} and !exists $options->{last_insert_id})) {
      0        
81 0           return $self->_quick_query($type, $table_name, $data);
82             }
83              
84 0           my ($sql, @bind_params) = $self->_generate_sql(
85             $type, $table_name, $data
86             );
87              
88 0 0         $sql .= ' RETURNING '.$options->{returning} if exists $options->{returning};
89              
90             # this is copied from Dancer::Plugin::Database::Core::Handle::_quick_query:
91 0 0         if ($self->{private_dancer_plugin_database}{log_queries}) {
92             $self->{private_dancer_plugin_database}{logger}->(debug =>
93             "Executing $type query $sql with params " . join ',',
94             map {
95 0 0         defined $_ ?
  0 0          
    0          
96             $_ =~ /^[[:ascii:]]+$/ ?
97             length $_ > 50 ? substr($_, 0, 47) . '...' : $_
98             : "[non-ASCII data not logged]" : 'undef'
99             } @bind_params
100             );
101             }
102              
103 0 0         return $self->selectrow_arrayref($sql, undef, @bind_params)->[0] if exists $options->{returning};
104              
105 0           $self->do($sql, undef, @bind_params);
106 0           return $self->last_insert_id(@{ $options->{last_insert_id} });
  0            
107             }
108              
109             1;
110             __END__