blib/lib/Pg/Blobs.pm

filter: Statementstmt 13.0% 6 / 46 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 16.6% 1 / 6 Podpod 100% 5 / 5 totaltotal 16.9% 12 / 71 CC 6 SCAR 19.2
CC
6
Cov
11%
pgblobs_create_blob
12.0
pgblobs_stream_in_blob
6.0
pgblobs_stream_out_blob
6.0
CRAP 6.8
↑ Summary
1 package Pg::Blobs;
2 2 $Pg::Blobs::VERSION = '0.003';
3 2 ▶use Moose::Role 2.0401;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 requires 'pgblobs_dbh';
6  
7 =head1 NAME
8  
9 Pg::Blobs - Blobs management methods for Postgresql related DB modules.
10  
11 =head2 SYNOPSIS
12  
13 This is a Moose::Role. Consume it in your DB management class and implement the method
14 pgblobs_dbh.
15  
16 Note that blob management in postgresql do not work outside a transaction.
17  
18 Blobs are just numeric OIDs in postgresql. You will have to store them in a classic OID table column
19 for later retrieval.
20  
21 Example:
22  
23 package My::DB;
24 use Moose;
25 with qw/Pg::Blobs/;
26  
27 sub pgblobs_dbh{ .. return the dbh connection of your choice ..}
28 ...
29  
30 package main;
31 my $db = .. an instance of My::DB ..;
32  
33 #### IMPORTANT: IN A TRANSACTION
34 my $blob = $db->pgblobs_store_blob('binary content');
35 my $content = $db->pgblobs_fetch_blob($blob);
36 etc..
37  
38 =cut
39  
40 =head2 pgblobs_create_blob
41  
42 Creates a Postgresql empty blob (oid) and returns it.
43  
44 Note that it is not very useful. Use pgblobs_stream_in_blob or
45 pgblobs_store_blob instead.
46  
47 Usage:
48  
49 my $blob = $this->pgblobs_create_blob()
50  
51 =cut
52  
53 0 ▶sub pgblobs_create_blob{
Subroutine pgblobs_create_blob: not called CC=3 SCAR=24.8
Pod documented
54 0 my ($self) = @_ ;
55 0 my $dbh = $self->pgblobs_dbh();
56  
57 0 ▶ my $oid = $dbh->func($dbh->{pg_INV_WRITE} || $dbh->{pg_INV_READ},'lo_creat')
Condition0%0/5
$dbh->func($dbh->{'pg_INV_WRITE'} || $dbh->{'pg_INV_READ'}, 'lo_creat') || confess("CANT CREATE BLOB\n")
l!l
00
$dbh->{'pg_INV_WRITE'} || $dbh->{'pg_INV_READ'}
l!l&&r!l&&!r
000
MC/DC0%0/1
$dbh->func($dbh->{'pg_INV_WRITE'} || $dbh->{'pg_INV_READ'}, 'lo_creat') || confess("CANT CREATE BLOB\n")
$dbh->func($dbh->{'pg_INV_WRITE'} || $dbh->{'pg_INV_READ'}, 'lo_creat')
MC/DC0%0/2
$dbh->{'pg_INV_WRITE'} || $dbh->{'pg_INV_READ'}
$dbh->{'pg_INV_WRITE'} $dbh->{'pg_INV_READ'}
Truth table0%0/2
A
A= $dbh->func($dbh->{'pg_INV_WRITE'} || $dbh->{'pg_INV_READ'}, 'lo_creat')
Aresult
11
00
Truth table0%0/3
A || B
A= $dbh->{'pg_INV_WRITE'}B= $dbh->{'pg_INV_READ'}
ABresult
1X1
011
000
58 || confess "CANT CREATE BLOB\n" ;
59 0 return $oid ;
60 }
61  
62  
63 =head2 pgblobs_store_blob ($buf)
64  
65 Stores the given binary content in the postgresql db and return the blob id.
66  
67 Usage:
68  
69 my $blob = $this->pgblobs_store_blob('Full short binary content');
70  
71  
72 =cut
73  
74 0 ▶sub pgblobs_store_blob{
Subroutine pgblobs_store_blob: not called CC=1 SCAR=6.9
Pod documented
75 0 my ($self , $buf ) = @_ ;
76 0 my $oid = $self->pgblobs_create_blob ;
77 0 my $dbh = $self->pgblobs_dbh();
78 my $fh = $dbh->func($oid,
79 $dbh->{pg_INV_WRITE},
80 0 'lo_open');
81 0 my $blength = length($buf) ;
82 0 my $nbytes = $dbh->func($fh, $buf, $blength , 'lo_write');
83 0 $dbh->func($fh, 'lo_close');
84 0 return $oid ;
85 }
86  
87 =head2 pgblobs_stream_in_blob ($sub)
88  
89 Pulls data using the given read code, storing it into a new blob.
90  
91 Returns the new blob id.
92  
93 Usage:
94  
95 my $blob = $this->pgblobs_stream_in_blob(sub{ return 'Next slice of bytes or undef' ;});
96  
97 =cut
98  
99 0 ▶sub pgblobs_stream_in_blob{
Subroutine pgblobs_stream_in_blob: not called CC=2 SCAR=17.9
Pod documented
100 0 my ($self,$read) = @_;
101 0 my $oid = $self->pgblobs_create_blob();
102 0 my $dbh = $self->pgblobs_dbh();
103 my $fh = $dbh->func($oid,$dbh->{pg_INV_WRITE},
104 0 'lo_open');
105 0 ▶ while( defined( my $buf = &{$read}() ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $buf = &{$read;}())) 0 0 0
106 0 my $blength = length($buf) ;
107 0 my $nbytes = $dbh->func($fh, $buf, $blength , 'lo_write');
108 }
109 0 $dbh->func($fh, 'lo_close');
110 0 return $oid;
111 }
112  
113 =head2 pgblobs_stream_out_blob
114  
115 Streams out the given blob ID in the given write sub and return the number of bytes
116 retrieved.
117  
118 Example:
119  
120 $s->stream_out_blob(sub{ my $fresh_bytes = shift ; ... ; } , $oid );
121  
122 =cut
123  
124 0 ▶sub pgblobs_stream_out_blob{
Subroutine pgblobs_stream_out_blob: not called CC=2 SCAR=17.9
Pod documented
125 0 my ($self,$write,$oid) = @_;
126 0 my $dbh = $self->pgblobs_dbh();
127  
128 0 my $fh = $dbh->func($oid , $dbh->{pg_INV_READ}, 'lo_open');
129 0 my $buf = '' ;
130 0 my $total_bytes = 0;
131 # Read by chunks of 1024
132 0 ▶ while( my $nbytes = $dbh->func($fh , $buf, 1024 , 'lo_read') ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $nbytes = $dbh->func($fh, $buf, 1024, "lo_read")) 0 0 0
133 0 $total_bytes += $nbytes;
134 ## Call write with this chunk
135 0 &{$write}(substr($buf, 0 , $nbytes ));
136 }
137 0 return $total_bytes;
138 }
139  
140 =head2 pgblobs_fetch_blob ($oid)
141  
142 Fectches the blob binary content in one go
143  
144 Usage:
145  
146 my $small_content = $this->pgblobs_fetch_blob($blob);
147  
148 =cut
149  
150 0 ▶sub pgblobs_fetch_blob{
Subroutine pgblobs_fetch_blob: not called CC=2 SCAR=17.9
Pod documented
151 0 my ($self , $oid ) = @_ ;
152 0 my $dbh = $self->pgblobs_dbh();
153  
154 0 my $fh = $dbh->func($oid , $dbh->{pg_INV_READ}, 'lo_open');
155 0 my $content ;
156 0 my $buf = '' ;
157 # Read by chunks of 1024
158 0 ▶ while( my $nbytes = $dbh->func($fh , $buf, 1024 , 'lo_read') ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $nbytes = $dbh->func($fh, $buf, 1024, "lo_read")) 0 0 0
159 0 $content .= substr($buf, 0 , $nbytes );
160 }
161 0 return $content ;
162 }
163  
164 2 1;
↑ Summary