File Coverage

blib/lib/RPC/Oracle.pm
Criterion Covered Total %
statement 0 74 0.0
branch 0 32 0.0
condition 0 3 0.0
subroutine 0 7 0.0
pod 5 5 100.0
total 5 121 4.1


line stmt bran cond sub pod time code
1             package RPC::Oracle;
2              
3             our $VERSION = '1.3';
4              
5             sub new {
6 0     0 1   my ($self, $class, $dbh, $schema) = ({}, @_);
7 0           $self = bless $self, $class;
8              
9 0           $self->schema($schema);
10              
11 0           $self->dbh($dbh);
12 0           return $self;
13             }
14              
15             sub dbh {
16 0     0 1   my ($self, $dbh) = @_;
17 0 0         if(@_ == 2) {
18 0           $self->{dbh} = $dbh;
19 0           return $self;
20             }
21 0           return $self->{dbh};
22             }
23              
24             sub schema {
25 0     0 1   my ($self, $schema) = @_;
26 0 0         if(@_ == 2) {
27 0           $self->{schema} = $schema;
28 0           return $self;
29             }
30 0           return $self->{schema};
31             }
32              
33             sub call {
34 0     0 1   my ($self, $method, @args) = @_;
35              
36 0 0         die "Invalid identifier: $method"
37             unless $self->_check_identifier($method);
38              
39 0 0         if(! $self->dbh) {
40 0           die "No database handle";
41             }
42              
43 0           my $sql = "BEGIN ";
44 0           my @bind = ();
45 0           my $return;
46              
47             # if not called in void context (list or scalar), call as function
48 0 0         if(defined wantarray) {
49 0           $sql .= " ? := ";
50 0           push @bind, \$return;
51             }
52              
53             # prefix the schema name if set
54 0 0         if($self->schema) {
55 0           $method = $self->schema . ".$method";
56             }
57              
58 0           $sql .= "$method";
59 0 0         if(@args > 0) {
60 0           $sql .= "(";
61              
62             # bind as name-based parameters
63 0 0 0       if(@args == 1 && ref $args[0] eq 'HASH') {
64 0           my $first = 1;
65 0           while(my($var_name, $var_value) = each(%{ $args[0] })) {
  0            
66 0 0         $sql .= $first ? "" : ", ";
67 0           $sql .= "$var_name => ?";
68 0           push @bind, $var_value;
69            
70 0           $first = 0;
71             }
72             }
73             else {
74 0           $sql .= join(', ', ('?') x scalar(@args));
75 0           push @bind, @args;
76             }
77 0           $sql .= ")";
78             }
79 0           $sql .= "; END;";
80              
81 0           my $sth = $self->dbh->prepare($sql);
82 0           my $i = 1;
83 0           for my $bindvar (@bind) {
84 0 0         if(ref $bindvar) {
85 0           $sth->bind_param_inout($i, $bindvar, $self->dbh->{LongReadLen});
86             }
87             else {
88 0           $sth->bind_param($i, $bindvar);
89             }
90 0           $i++;
91             }
92              
93 0           $sth->execute;
94              
95 0           return $return;
96             }
97              
98             sub constant {
99 0     0 1   my ($self, $constant_name) = @_;
100              
101 0 0         die "Usage: constant('constant_name')" unless $constant_name;
102              
103 0 0         die "Invalid identifier: $constant_name"
104             unless $self->_check_identifier($constant_name);
105              
106             # return from cache if available
107 0 0         if($self->{uc $schema}->{uc $constant_name}) {
108 0           return $self->{uc $schema}->{uc $constant_name};
109             }
110              
111 0 0         die "No database handle"
112             unless $self->dbh;
113              
114 0           my $sql = "BEGIN ? := ";
115 0           my $schema = $self->schema;
116 0 0         $sql .= $schema ? "$schema.$constant_name" : $constant_name;
117 0           $sql .= "; END;";
118              
119 0           my $sth = $self->dbh->prepare($sql);
120 0           my $value;
121 0           $sth->bind_param_inout(1, \$value, $self->dbh->{LongReadLen});
122              
123 0           $sth->execute;
124              
125             # cache this for later
126 0           $self->{uc $schema}->{uc $constant_name} = $value;
127              
128 0           return $value;
129             }
130              
131             sub _check_identifier {
132 0     0     my ($self, $ident) = @_;
133 0           return $ident =~ /^[a-z][a-z0-9\$\#\_\.]*$/i;
134             }
135              
136             sub AUTOLOAD {
137 0     0     my ($self, @args) = @_;
138 0           my $sub = $AUTOLOAD;
139 0           $sub =~ s/.*:://;
140 0 0         return if $sub eq 'DESTROY';
141              
142 0           return $self->call($sub, @args);
143             }
144              
145             1;
146              
147             __END__