File Coverage

blib/lib/Dblink.pm
Criterion Covered Total %
statement 24 162 14.8
branch 0 62 0.0
condition 0 24 0.0
subroutine 8 21 38.1
pod 0 4 0.0
total 32 273 11.7


line stmt bran cond sub pod time code
1             package Dblink;
2              
3 1     1   6448 use 5.006;
  1         4  
  1         45  
4 1     1   7 use strict;
  1         2  
  1         34  
5 1     1   6 use warnings;
  1         2  
  1         42  
6 1     1   7 use Carp;
  1         1  
  1         85  
7              
8             require Exporter;
9             require DynaLoader;
10 1     1   1182 use AutoLoader;
  1         1683  
  1         6  
11              
12             our @ISA = qw(Exporter DynaLoader);
13              
14             # Items to export into callers namespace by default. Note: do not export
15             # names by default without a very good reason. Use EXPORT_OK instead.
16             # Do not simply export all your public functions/methods/constants.
17              
18             # This allows declaration use Dblink ':all';
19             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
20             # will save memory.
21             our %EXPORT_TAGS = ( 'all' => [ qw(
22            
23             ) ] );
24              
25             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26              
27             our @EXPORT = qw(
28            
29             );
30             our $VERSION = '1.0';
31              
32             sub AUTOLOAD {
33             # This AUTOLOAD is used to 'autoload' constants from the constant()
34             # XS function. If a constant is not found then control is passed
35             # to the AUTOLOAD in AutoLoader.
36              
37 0     0     my $constname;
38 0           our $AUTOLOAD;
39 0           ($constname = $AUTOLOAD) =~ s/.*:://;
40 0 0         croak "& not defined" if $constname eq 'constant';
41 0 0         my $val = constant($constname, @_ ? $_[0] : 0);
42 0 0         if ($! != 0) {
43 1 0 0 1   1021 if ($! =~ /Invalid/ || $!{EINVAL}) {
  1         1300  
  1         64  
  0            
44 0           $AutoLoader::AUTOLOAD = $AUTOLOAD;
45 0           goto &AutoLoader::AUTOLOAD;
46             }
47             else {
48 0           croak "Your vendor has not defined Dblink macro $constname";
49             }
50             }
51             {
52 1     1   6 no strict 'refs';
  1         3  
  1         145  
  0            
53             # Fixed between 5.005_53 and 5.005_61
54 0 0         if ($] >= 5.00561) {
55 0     0     *$AUTOLOAD = sub () { $val };
  0            
56             }
57             else {
58 0     0     *$AUTOLOAD = sub { $val };
  0            
59             }
60             }
61 0           goto &$AUTOLOAD;
62             }
63              
64             bootstrap Dblink $VERSION;
65              
66             # Preloaded methods go here.
67              
68 1     1   6 use strict;
  1         1  
  1         1801  
69              
70             sub new
71             {
72 0     0 0   my ($Proto, $Obj_Handler) = @_;
73 0           my ($Class, $Self);
74            
75 0           $Self = {};
76 0   0       $Class = ref($Proto) || $Proto;
77 0           bless($Self, $Class);
78            
79 0           return $Self;
80             }
81              
82             sub Dprint
83             {
84 0 0   0 0   if ($main::DEBUG)
85             {
86 0           print @_;
87 0 0         if ($main::STDIN)
88             {
89 0           print "Press To Continue .. \n";
90 0           ;
91             };
92             }
93             }
94              
95             sub create_remote_view
96             {
97 0     0 0   my ($Pack, $SDbh, $DDbh, $Prepend, $Table) = @_;
98 0           my ($Ret, $Viewname, $Select_Query, $Records);
99 0           my ($Dbh);
100              
101 0 0 0       if (!$SDbh || !$DDbh || !$Table)
      0        
102             {
103 0           print "DBLINK-ERROR: create_remote_view() Invalid parameters\n";
104 0           return 'ERROR';
105             }
106              
107 0 0         $Prepend = "dbl_" if (!$Prepend);
108 0           $Viewname = $Prepend . $Table;
109              
110 0           ($Ret, @{$Records}) = $Pack->_get_fields_and_types($SDbh, $Table);
  0            
111 0 0         return $Ret if ($Ret eq 'ERROR');
112              
113 0           ($Ret, $Select_Query) = $Pack->_dblink_select_query($Table, $Records, '');
114 0 0         return $Ret if ($Ret eq 'ERROR');
115              
116 0           print "Creating remote view for $Table ... ";
117 0           $Ret = $Pack->_create_view($DDbh, $Viewname, $Select_Query);
118 0 0         if ($Ret eq 'ERROR')
119             {
120 0           print "failed.\n";
121 0           print "$DBI::errstr\n";
122 0           return $Ret;
123             }
124             else
125             {
126 0           print "success.\n";
127 0           return $Ret;
128             }
129             }
130              
131             # This function returns the fields, types of a particular table.
132             sub _get_fields_and_types
133             {
134 0     0     my ($Pack, $Dbh, $Table) = @_;
135 0           my ($Ret, $Sth, $Sql, $Records);
136              
137 0 0         if (!$Table)
138             {
139 0           print "DBLINK-ERROR: Get_Fields() Invalid parameters\n";
140 0           return 'ERROR';
141             }
142              
143 0           $Sql = "SELECT a.attname, t.typname from pg_attribute a, pg_class c, pg_type t where a.attrelid = c.oid and c.relkind = 'r' and a.attnum > 0 and c.relname = '$Table' and a.atttypid = t.oid and a.attisdropped is false ORDER BY attnum ASC";
144 0           Dprint "Table :$Table:\n";
145 0           $Sth = $Dbh->prepare($Sql);
146 0           $Sth->execute();
147 0           $Records = $Sth->fetchall_arrayref;
148 0           $Sth->finish();
149              
150 0 0         return wantarray() ? ('SUCCESS', @{$Records}) : 'SUCCESS';
  0            
151             }
152              
153             sub _dblink_select_query
154             {
155 0     0     my ($Pack, $Table, $Records, $Distinct) = @_;
156 0           my ($Ret, $Sql, $Pointer, $Select_Query, $Record);
157              
158 0 0 0       if (!$Table || !$Records)
159             {
160 0           print "DBLINK-ERROR: Dblink_Select_Query() Invalid parameters\n";
161 0           return 'ERROR';
162             }
163              
164 0           ($Ret, $Sql, $Pointer) = $Pack->_dblink_selects($Distinct, $Table, $Records);
165 0 0         return $Ret if ($Ret eq 'ERROR');
166              
167 0           $Select_Query = "SELECT ";
168 0           foreach $Record (@{$Records}) { $Select_Query .= $Record->[0]. ', '; }
  0            
  0            
169 0           $Select_Query =~ s/,\s*$//g;
170              
171 0           $Select_Query .= " FROM dblink('$Sql') as t($Pointer)";
172              
173 0 0         return wantarray() ? ('SUCCESS', $Select_Query) : 'SUCCESS';
174             }
175              
176             sub _dblink_selects
177             {
178 0     0     my ($Pack, $Distinct, $Table, $Records) = @_;
179 0           my ($Field, $Fieldnum, $Fieldname, $Datatype, $Sql, $Pointer);
180              
181 0 0         if (!$Records)
182             {
183 0           print "DBLINK-ERROR: Dblink_Selects() Invalid parameters\n";
184 0           return 'ERROR';
185             }
186            
187 0 0         if ($Distinct) { $Sql = "SELECT DISTINCT "; }
  0            
188 0           else { $Sql = "SELECT "; }
189              
190 0           foreach $Fieldnum ( 0 .. $#$Records)
191             {
192 0           $Fieldname = $$Records[$Fieldnum][0];
193 0           $Datatype = $$Records[$Fieldnum][1];
194              
195 0           $Sql .= "$Fieldname, ";
196 0           $Pointer .= "$Fieldname $Datatype, ";
197             }
198 0           $Sql =~ s/,\s*$//g;
199 0           $Pointer =~ s/,\s*$//g;
200              
201 0           $Sql .= " FROM $Table";
202 0 0         return wantarray() ? ('SUCCESS', $Sql, $Pointer) : 'SUCCESS';
203             }
204              
205             sub _get_field_number
206             {
207 0     0     my ($Pack, $Dbh, $Table, $Fieldname) = @_;
208 0           my ($Ret, $Sql, $Fields);
209              
210 0 0 0       if (!$Table || !$Fieldname)
211             {
212 0           print "DBLINK-ERROR: Get_Field_Number() Invalid parameters\n";
213 0           return 'ERROR';
214             }
215            
216 0           $Sql = "select a.attnum from pg_attribute a, pg_class b where a.attname = '$Fieldname' and a.attrelid = b.oid and b.relname = '$Table'";
217 0           $Fields = $Dbh->selectcol_arrayref($Sql);
218 0 0         return wantarray() ? ('SUCCESS', $$Fields[0]) : 'SUCCESS';
219             }
220              
221             sub _create_view
222             {
223 0     0     my ($Pack, $Dbh, $Viewname, $Select_Query) = @_;
224 0           my ($Ret, $Sth, $Viewsql);
225              
226 0 0 0       if (!$Viewname || !$Select_Query)
227             {
228 0           print "DBLINK-ERROR: Create_View() Invalid parameters\n";
229 0           return 'ERROR';
230             }
231              
232 0           $Viewsql = "CREATE VIEW $Viewname AS " . $Select_Query;
233 0           Dprint "Viewsql :$Viewsql:\n";
234              
235 0           $Sth = $Dbh->prepare($Viewsql);
236 0           $Ret = $Sth->execute();
237 0 0         if (!$Ret) { return 'ERROR'; }
  0            
238 0           else { return 'SUCCESS'; }
239             }
240              
241             sub _dblink_connect
242             {
243 0     0     my ($Pack, $id) = @_;
244 0           my ($Ret, $Sth, $Dbh, $Sql);
245 0           my ($Params);
246              
247 0           $Params = $Pack->get_db_params($id);
248 0           $Sql = "SELECT dblink_connect($Params)";
249 0           $Sth = $Dbh->prepare($Sql);
250 0           $Ret = $Sth->execute($Sql);
251 0           $Sth->finish();
252              
253 0 0 0       if ($Ret == -1 || !$Ret) { return 'ERROR'; }
  0            
254 0           print "dblink_connect() is successful\n";
255 0           return 'SUCCESS';
256             }
257              
258             sub get_db_params
259             {
260 0     0 0   my ($Pack, $index) = @_;
261 0           my ($host, $port, $db, $user, $pass, $db_params, @content);
262              
263 0 0         open (CFILE, $main::CFILE) || die "$main::CFILE: $!";
264 0           @content = ;
265 0           close(CFILE);
266              
267 0           $db_params = $content[$index];
268 0           chop($db_params);
269 0           ($host, $port, $db, $user, $pass) = split(/:/, $db_params);
270              
271 0 0         $host = 'localhost' if (!$host);
272 0 0         $port = 5432 if (!$port);
273 0 0         $db = $ENV{'LOGNAME'} if (!$db);
274 0 0         $user = $ENV{'LOGNAME'} if (!$user);
275              
276 0 0         return wantarray() ? ($host, $port, $db, $user, $pass) :
277             "hostaddr=$host port=$port dbname=$db user=$user password=$pass";
278             }
279              
280             1;
281             __END__