File Coverage

blib/lib/Teng/Plugin/TmpSuppressRowObjects.pm
Criterion Covered Total %
statement 18 18 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 26 27 96.3


line stmt bran cond sub pod time code
1             package Teng::Plugin::TmpSuppressRowObjects;
2 8     8   535036 use 5.008001;
  8         60  
3 8     8   41 use strict;
  8         15  
  8         165  
4 8     8   37 use warnings;
  8         15  
  8         724  
5              
6             our $VERSION = "0.03";
7              
8             our @EXPORT;
9              
10             {
11             my @origs = qw(
12             insert
13             single
14             single_by_sql
15             single_named
16             search
17             search_named
18             );
19             my $suffix = '_hashref';
20              
21 8     8   58 no strict 'refs';
  8         22  
  8         1819  
22             for my $orig (@origs) {
23             my $method = $orig . $suffix;
24             push @EXPORT, $method;
25             *{__PACKAGE__ . '::' . $method} = sub {
26 8     8   141620 my $self = shift;
27 8         30 local $self->{suppress_row_objects} = 1;
28 8         60 $self->$orig(@_);
29             };
30             }
31             push @EXPORT, qw(
32             search_by_sql_hashref
33             );
34             }
35              
36             sub search_by_sql_hashref {
37 2     2 0 27011 my ($self, $sql, $bind, $table_name) = @_;
38              
39 2 100       11 wantarray and return $self->dbh->selectall_array($sql, +{ Slice => +{} }, @$bind);
40 1         5 local $self->{suppress_row_objects} = 1;
41 1         11 $self->search_by_sql($sql, $bind, $table_name);
42             }
43              
44              
45             1;
46             __END__
47              
48             =encoding utf-8
49              
50             =head1 NAME
51              
52             Teng::Plugin::TmpSuppressRowObjects - add methods with temporaly use of suppress_row_objects
53              
54             =head1 SYNOPSIS
55              
56             # In your Model ...
57             package Your::Model;
58             use parent qw(Teng);
59              
60             __PACKAGE__->load_plugin('TmpSuppressRowObjects');
61              
62              
63             # In case suppress_row_objects = 0 ...
64             my $teng = Your::Model->new(dbh => $dbh, suppress_row_objects => 0);
65             my @rows;
66              
67             # same usage with original 'search'
68             @rows = $teng->search_hashref(test_table => +{ id => 100 }); # elements in @rows are hashref
69              
70             # does not affect original 'search'
71             @rows = $teng->search(test_table => +{ id => 100 }); # elements in @rows are row object
72              
73              
74             =head1 DESCRIPTION
75              
76             This plugin adds some methods, which return hashref as a result, rather than row objects, even when C<suppress_row_objects> is 0.
77             It is useful when we want row objects as default, and lightweight hashref in some cases to improve performance.
78              
79              
80             =head1 METHODS
81              
82             insert_hashref
83             search_hashref
84             single_hashref
85             search_by_sql_hashref
86             single_by_sql_hashref
87             search_named_hashref
88             single_named_hashref
89              
90             Usage of those methods are the same to original methods (without C<_hashref>).
91              
92              
93             =head1 LICENSE
94              
95             Copyright (C) egawata.
96              
97             This library is free software; you can redistribute it and/or modify
98             it under the same terms as Perl itself.
99              
100             =head1 AUTHOR
101              
102             egawata E<lt>egawa.takashi@gmail.comE<gt>
103              
104             =cut
105