File Coverage

blib/lib/DBIx/PgLink.pm
Criterion Covered Total %
statement 15 36 41.6
branch 0 10 0.0
condition n/a
subroutine 5 8 62.5
pod 3 3 100.0
total 23 57 40.3


line stmt bran cond sub pod time code
1             package DBIx::PgLink;
2              
3 1     1   24561 use 5.008006;
  1         4  
  1         41  
4 1     1   6 use strict;
  1         2  
  1         36  
5 1     1   5 use warnings;
  1         7  
  1         31  
6 1     1   5 use Exporter;
  1         1  
  1         42  
7 1     1   554 use DBIx::PgLink::Logger;
  1         2  
  1         453  
8              
9             our @ISA = qw/Exporter/;
10              
11             our @EXPORT = qw/named_params/;
12              
13             our $VERSION = '0.01';
14              
15             our %connection_by_name;
16              
17             sub connect {
18 0     0 1   my $self = shift;
19 0           my $conn_name = shift;
20              
21 0 0         trace_msg('INFO', "Using connection $conn_name")
22             if trace_level>=2;
23              
24 0 0         if (exists $connection_by_name{$conn_name}) {
25 0           return $connection_by_name{$conn_name};
26             } else {
27 0           require DBIx::PgLink::Connector;
28 0           my $conn = DBIx::PgLink::Connector->new(
29             conn_name => $conn_name,
30             @_
31             );
32 0           $connection_by_name{$conn_name} = $conn;
33 0           return $conn;
34             }
35             }
36              
37             sub disconnect {
38 0     0 1   my ($self, $conn_name) = @_;
39              
40 0           my $data = $main::_SHARED{'dbix_pglink'};
41 0 0         my $conn = $connection_by_name{$conn_name} or return;
42 0 0         trace_msg('INFO', "Disconnect from $conn_name")
43             if trace_level>=1;
44 0           $conn->adapter->disconnect;
45 0           delete $connection_by_name{$conn_name};
46             }
47              
48              
49             sub named_params {
50 0     0 1   my $params = shift;
51 0           my $i = 0;
52 0           my %p = map {
53 0           my $v = $params->[$i++];
54 0 0         defined $v ? ($_ => $v) : () # skip NULLs
55             } @_;
56 0           return \%p;
57             }
58              
59             1;
60              
61              
62             __END__
63              
64              
65             =head1 NAME
66              
67             DBIx::PgLink - external database access from PostgreSQL backend using Perl DBI
68              
69             =head1 SYNOPSIS
70              
71             For SQL script examples see L<DBIx::PgLink::Manual::Usage>.
72              
73             I<In PL/PerlU function>
74              
75             use DBIx::PgLink;
76              
77             $conn = DBIx::PgLink->connect('NORTHWIND');
78              
79             $db = $conn->adapter;
80             $db->begin_work;
81             $st = $db->prepare('SELECT * FROM Orders WHERE OrderID=?');
82             $st->execute(42);
83             while (@row = $st->fetchrow_array) {
84             ...
85             }
86             $db->commit;
87              
88             $conn->builder->build_accessors(
89             local_schema => 'northwind',
90             remote_schema => 'dbo',
91             remote_object => 'Order%',
92             );
93              
94             DBIx::PgLink->disconnect('NORTHWIND');
95              
96             =head1 DESCRIPTION
97              
98             I<PgLink> is based on I<DBI-Link> project for accessing
99             external data sources from PostgreSQL backend.
100              
101             This module can be used only in untrusted PL/Perl function.
102              
103             =head2 Differences from I<DBI-Link>
104              
105             =over
106              
107             =item *
108              
109             I<PgLink> is standard Perl module
110              
111             While I<DBI-Link> store all Perl code in PL/Perl functions,
112             DBIx-PgLink use Perl infrastructure for installation and testing.
113              
114             =item *
115              
116             Extensibility
117              
118             The main goal is to compose functionality without writing a line of Perl code.
119              
120             =item *
121              
122             Flexible data type mapping
123              
124             =item *
125              
126             Customizable SQL queries.
127              
128             =item *
129              
130             Parametrized queries
131              
132             Prevent SQL-injection attack.
133              
134             =item *
135              
136             Mapping between database accounts
137              
138             Can connect with different credentials for each PostgreSQL user.
139              
140             =item *
141              
142             Additional functionality for DBI
143              
144             Such as automatic reconnection after network outage,
145             nested transactions, charset conversion, prepared statement cache management.
146              
147              
148             =back
149              
150              
151             =head1 SUBROUTINES
152              
153             =over
154              
155             =item connect
156              
157             $adapter = connect($conn_name);
158              
159             Load connection metadata from PostgreSQL and connect to remote datasource.
160              
161             Returns instance of L<DBIx::PgLink::Connector> object.
162              
163             Subsequent calls return the same cached object.
164             Single connection persists while PostgreSQL session live
165             or until explicit C<disconnect>.
166              
167             =item disconnect
168              
169             disconnect($conn_name);
170              
171             Close connection to remote database and delete entry from cache.
172              
173             =item named_params
174              
175             my $hashref = named_params(\@_, qw/foo bar/); # { foo=>$_[0], bar=>$_[1] }
176              
177             Utility subroutine. Converts positional arguments of PL/Perl function (passed in @_) to named parameters.
178             NULL arguments are ignored.
179              
180             Exported by default.
181              
182             =back
183              
184              
185             =head1 SEE ALSO
186              
187             L<DBIx::PgLink::Manual::Install>,
188             L<DBIx::PgLink::Manual::Usage>,
189             L<DBI>,
190             L<DBIx::PgLink::Connector>,
191             L<DBIx::PgLink::Adapter>,
192             L<DBIx::PgLink::Accessor::Table>,
193             L<DBIx::PgLink::Local>,
194             L<http://pgfoundry.org/projects/dbi-link/>
195              
196             =head1 AUTHOR
197              
198             Alexey Sharafutdinov E<lt>alexey.s.v.br@gmail.comE<gt>
199              
200             =head1 COPYRIGHT AND LICENSE
201              
202             Copyright (C) 2007 by Alexey Sharafutdinov
203              
204             This library is free software; you can redistribute it and/or modify
205             it under the same terms as Perl itself, either Perl version 5.8.8 or,
206             at your option, any later version of Perl 5 you may have available.
207              
208             DBI-Link project by David Fetter under BSD License.
209              
210             =cut