blib/lib/DB/Object/Postgres/Lo.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 49 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 255 CC 27 SCAR 24.5
CC
27
Cov
0%
read
20.0
truncate
20.0
write
20.0
CRAP 11.6
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/Postgres/Lo.pm
4 ## Version v0.301.0
5 ## Copyright(c) 2024 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/03/22
9 ## All rights reserved
10 ##
11 ##
12 ## This program is free software; you can redistribute it and/or modify it
13 ## under the same terms as Perl itself.
14 ##----------------------------------------------------------------------------
15 ## Package for PostgreSQL large objects
16 package DB::Object::Postgres::Lo;
17 BEGIN
18 {
19 use strict;
20 use warnings;
21 use parent qw( Module::Generic );
22 use vars qw( $VERSION $EXCEPTION_CLASS );
23 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
24 our $VERSION = 'v0.301.0';
25 };
26  
27 use strict;
28 use warnings;
29  
30 sub new
31 {
32 my $this = shift( @_ );
33 my $dbh = shift( @_ ) || return( $this->error( "No database handler was provided." ) );
34 return( $this->error( "Value provided is not a database handler object." ) ) if( !ref( $dbh ) || ( ref( $dbh ) && !$dbh->isa( 'DB::Object' ) ) );
35 my $class = ref( $this ) ? ref( $this ) : $this;
36 my $self = {};
37 $self->{dbh} = $dbh;
38 bless( $self, $class );
39 $self->{_exception_class} = $EXCEPTION_CLASS;
40 return( $self );
41 }
42  
43 sub close
44 {
45 my $self = shift( @_ );
46 return( $self->error( "Deactivate AutoCommit before using this function." ) ) if( $self->{dbh}->{AutoCommit} );
47 my $fh = $self->{fh} || return( $self->error( "No file handle currently set. You must open the large object before using this method." ) );
48 my $rv = $self->{dbh}->pg_lo_close( $fh );
49 return( $self->error( "Unable to close the large object file descriptor." ) ) if( !defined( $rv ) );
50 return( $self );
51 }
52  
53 sub create
54 {
55 my $self = shift( @_ );
56 my $mode = shift( @_ ) || 077;
57 my $id = $self->{dbh}->pg_lo_creat( $mode );
58 $self->{id} = $id;
59 return( $id );
60 }
61  
62 sub export_file
63 {
64 my $self = shift( @_ );
65 my $file = shift( @_ ) || return( $self->error( "No file provided to export the large object to." ) );
66 my $rv = $self->{dbh}->pg_lo_export( $file );
67 return( $self->error( "Unable to export the large object to file $file." ) ) if( !defined( $rv ) );
68 return( $self );
69 }
70  
71 sub id
72 {
73 my $self = shift( @_ );
74 $self->{id} = shift( @_ ) if( @_ );
75 return( $self->{id} );
76 }
77  
78 sub import_file
79 {
80 my $self = shift( @_ );
81 my $file = shift( @_ ) || return( $self->error( "No file provided to import the large object from it." ) );
82 my $fh = $self->{fh} || return( $self->error( "No file handle currently set. You must open the large object before using this method." ) );
83 my $id = $self->{dbh}->pg_lo_import( $file );
84 return( $self->error( "Unable to import the large object." ) ) if( !defined( $id ) );
85 my $new = $self->new( $self->{dbh} );
86 $new->id( $id );
87 return( $new );
88 }
89  
90 sub import_with_oid
91 {
92 my $self = shift( @_ );
93 my $file = shift( @_ ) || return( $self->error( "No file provided to import the large object from it." ) );
94 my $oid = shift( @_ );
95 return( $self->error( "No oid was provided to import the large object." ) ) if( !defined( $oid ) || !length( $oid ) );
96 my $fh = $self->{fh} || return( $self->error( "No file handle currently set. You must open the large object before using this method." ) );
97 my $id = $self->{dbh}->pg_lo_import_with_oid( $file, $oid );
98 return( $self->error( "Unable to import the large object with the oid $oid." ) ) if( !defined( $id ) );
99 my $new = $self->new( $self->{dbh} );
100 $new->id( $id );
101 return( $new );
102 }
103  
104 sub open
105 {
106 my $self = shift( @_ );
107 my $id = shift( @_ );
108 return( $self->error( "No id was provided." ) ) if( !defined( $id ) || !length( $id ) );
109 ## $dbh->{pg_INV_READ}, $dbh->{pg_INV_WRITE}, $dbh->{pg_INV_READ} | $dbh->{pg_INV_WRITE}
110 my $mode = shift( @_ );
111 my $fh = $self->{dbh}->pg_lo_open( $id, $mode );
112 return( $self->error( "Cannot open large object. An error has occured." ) ) if( !defined( $fh ) );
113 $self->{fh} = $fh;
114 return( $self );
115 }
116  
117 sub read
118 {
119 my $self = shift( @_ );
120 my( $buffer, $len ) = @_;
121 return( $self->error( "No length was provided." ) ) if( !defined( $len ) || !length( $len ) );
122 return( $self->error( "Deactivate AutoCommit before using this function." ) ) if( $self->{dbh}->{AutoCommit} );
123 my $fh = $self->{fh} || return( $self->error( "No file handle currently set. You must open the large object before using this method." ) );
124 my $bytes = $self->{dbh}->pg_lo_read( $fh, $buffer, $len );
125 return( $self->error( "Unable to read from the large object file descriptor." ) ) if( !defined( $bytes ) );
126 return( $bytes );
127 }
128  
129 sub seek
130 {
131 my $self = shift( @_ );
132 my( $offset, $whence ) = @_;
133 return( $self->error( "Deactivate AutoCommit before using this function." ) ) if( $self->{dbh}->{AutoCommit} );
134 my $fh = $self->{fh} || return( $self->error( "No file handle currently set. You must open the large object before using this method." ) );
135 my $loc = $self->{dbh}->pg_lo_lseek( $fh, $offset, $whence );
136 return( $self->error( "Unable to seek a position from the large object file descriptor." ) ) if( !defined( $loc ) );
137 return( $loc );
138 }
139  
140 sub tell
141 {
142 my $self = shift( @_ );
143 return( $self->error( "Deactivate AutoCommit before using this function." ) ) if( $self->{dbh}->{AutoCommit} );
144 my $fh = $self->{fh} || return( $self->error( "No file handle currently set. You must open the large object before using this method." ) );
145 my $loc = $self->{dbh}->pg_lo_tell( $fh );
146 return( $self->error( "Unable to get the current position from the large object file descriptor." ) ) if( !defined( $loc ) );
147 return( $loc );
148 }
149  
150 sub truncate
151 {
152 my $self = shift( @_ );
153 my $len = shift( @_ );
154 return( $self->error( "No length was provided." ) ) if( !defined( $len ) || !length( $len ) );
155 return( $self->error( "Deactivate AutoCommit before using this function." ) ) if( $self->{dbh}->{AutoCommit} );
156 my $fh = $self->{fh} || return( $self->error( "No file handle currently set. You must open the large object before using this method." ) );
157 my $rv = $self->{dbh}->pg_lo_truncate( $fh );
158 return( $self->error( "Unable to truncate the large object file descriptor." ) ) if( !defined( $rv ) );
159 return( $self );
160 }
161  
162 sub unlink
163 {
164 my $self = shift( @_ );
165 return( $self->error( "Deactivate AutoCommit before using this function." ) ) if( $self->{dbh}->{AutoCommit} );
166 my $fh = $self->{fh} || return( $self->error( "No file handle currently set. You must open the large object before using this method." ) );
167 my $rv = $self->{dbh}->pg_lo_unlink( $fh );
168 return( $self->error( "Unable to delete the large object file descriptor." ) ) if( !defined( $rv ) );
169 return( $self );
170 }
171  
172 sub write
173 {
174 my $self = shift( @_ );
175 my( $buffer, $len ) = @_;
176 return( $self->error( "No length was provided." ) ) if( !defined( $len ) || !length( $len ) );
177 return( $self->error( "Deactivate AutoCommit before using this function." ) ) if( $self->{dbh}->{AutoCommit} );
178 my $fh = $self->{fh} || return( $self->error( "No file handle currently set. You must open the large object before using this method." ) );
179 my $bytes = $self->{dbh}->pg_lo_write( $fh, $buffer, $len );
180 return( $self->error( "Unable to write to the large object file descriptor." ) ) if( !defined( $bytes ) );
181 return( $bytes );
182 }
183  
184 1;
185  
186 # NOTE: POD
187 __END__
188  
189 =encoding utf-8
190  
191 =head1 NAME
192  
193 DB::Object::Postgres::Lo - Large Object
194  
195 =head1 SYNOPSIS
196  
197 use DB::Object::Postgres::Lo( $dbh );
198 my $lo = DB::Object::Postgres::Lo->new || die( DB::Object::Postgres::Lo->error, "\n" );
199  
200 =head1 DESCRIPTION
201  
202 This is the PostgreSQL large object class
203  
204 =head1 METHODS
205  
206 =head2 new
207  
208 Instantiate a new PostgreSQL large object.
209  
210 This takes only parameter: a database handler (L<DB::Object> or one of its inheriting package).
211  
212 =head2 close
213  
214 Close the large object by calling L<DBD::Pg/pg_lo_close>
215  
216 =head2 create
217  
218 Provided with an octal mode such as 077 and this creates a new large object by calling L<DBD::Pg/pg_lo_creat> passing it the octal mode.
219  
220 Save the id returned in the object property C<id>
221  
222 It returns the id.
223  
224 =head2 export_file
225  
226 Provided with a file and this will export the large object to file by calling L<DBD::Pg/pg_lo_export> and passing it the file.
227  
228 Upon error, this returns an error, otherwise this returns the current object.
229  
230 =head2 id
231  
232 Sets or gets the id.
233  
234 =head2 import_file
235  
236 Provided with a file and this will import it as a large object by calling L<DBD::Pg/pg_lo_import> and passing it the file.
237  
238 This creates a new L<DB::Object::Postgres::Lo> object, sets the id retrieved from PostgreSQL.
239  
240 Upon error, this returns an error, otherwise this returns the newly created object.
241  
242 =head2 import_with_oid
243  
244 Provided with a file and an oid and this will import the file by calling L<DBD::Pg/pg_lo_import_with_oid> passing it the file and the oid arguments.
245  
246 Upon error, this returns an error, otherwise this returns a newly created L<DB::Object::Postgres::Lo> object with the id set to the value returned by L<DBD::Pg/pg_lo_import_with_oid>.
247  
248 =head2 open
249  
250 Provided with large object id and an octal mode, and this will open it by calling L<DBD::Pg/pg_lo_open> passing it the id and the octal mode.
251  
252 It will save the file handle returned in the object property C<fh>
253  
254 Upon error, this returns an error, otherwise this returns the current object.
255  
256 =head2 read
257  
258 This method requires the large object to have been opened before so that a file handle is readily available.
259  
260 Provided with a buffer and a length and this will attempt to read the length provided into the buffer by calling L<DBD::Pg/pg_lo_read> passing it the file handle, buffer and length.
261  
262 Upon error, this returns an error, otherwise this returns the number of bytes actually read.
263  
264 =head2 seek
265  
266 This method requires the large object to have been opened before so that a file handle is readily available.
267  
268 Provided with an offset and a whence and this will attempt to do a seek by calling L<DBD::Pg/pg_lo_lseek> passing it the file handle, the offset and the whence.
269  
270 Upon error, this returns an error, otherwise this returns the new offset position received from L<DBD::Pg/pg_lo_lseek>
271  
272 =head2 tell
273  
274 This method requires the large object to have been opened before so that a file handle is readily available.
275  
276 This will attempt to do a tell by calling L<DBD::Pg/pg_lo_tell> passing it only the file handle.
277  
278 Upon error, this returns an error, otherwise this returns the current offset position received from L<DBD::Pg/pg_lo_tell>
279  
280 =head2 truncate
281  
282 This method requires the large object to have been opened before so that a file handle is readily available.
283  
284 This will attempt to do a tell by calling L<DBD::Pg/pg_lo_truncate> passing it only the file handle.
285  
286 Upon error, this returns an error, otherwise this returns the returned value received from L<DBD::Pg/pg_lo_truncate>
287  
288 =head2 unlink
289  
290 This method requires the large object to have been opened before so that a file handle is readily available.
291  
292 This will attempt to remove the file by calling L<DBD::Pg/pg_lo_unlink> passing it only the file handle.
293  
294 Upon error, this returns an error, otherwise this returns the returned value received from L<DBD::Pg/pg_lo_unlink>
295  
296 =head2 write
297  
298 This method requires the large object to have been opened before so that a file handle is readily available.
299  
300 Provided with a buffer and a length and this will attempt to write to the large object by calling L<DBD::Pg/pg_lo_write> passing it the file handle, the buffer and the length.
301  
302 Upon error, this returns an error, otherwise this returns the actual number writen from L<DBD::Pg/pg_lo_write>
303  
304 =head1 SEE ALSO
305  
306 L<DBD::Pg>, L<DBI>, L<Apache::DBI>
307  
308 =head1 AUTHOR
309  
310 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
311  
312 =head1 COPYRIGHT & LICENSE
313  
314 Copyright (c) 2019-2021 DEGUEST Pte. Ltd.
315  
316 You can use, copy, modify and redistribute this package and associated
317 files under the same terms as Perl itself.
318  
319 =cut