File Coverage

blib/lib/CA/WAAE.pm
Criterion Covered Total %
statement 9 65 13.8
branch 0 26 0.0
condition 0 8 0.0
subroutine 3 8 37.5
pod 4 4 100.0
total 16 111 14.4


line stmt bran cond sub pod time code
1             #
2             # CA::WAAE - Perl Interface to CA's AutoSys job control.
3             #
4             # Original CA::AutoSys code:
5             # Copyright (c) 2007 Sinisa Susnjar
6             # See LICENSE for terms of distribution.
7             #
8             # This library is free software; you can redistribute it and/or
9             # modify it under the terms of the GNU Lesser General Public
10             # License as published by the Free Software Foundation; either
11             # version 2.1 of the License, or (at your option) any later version.
12             #
13             # This library is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16             # Lesser General Public License for more details.
17             #
18             # You should have received a copy of the GNU Lesser General Public
19             # License along with this library; if not, write to the Free Software
20             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21             #
22              
23             package CA::WAAE;
24              
25             require CA::WAAE::Job;
26              
27 1     1   6933 use strict;
  1         2  
  1         35  
28 1     1   5 use warnings;
  1         1  
  1         27  
29 1     1   2195 use DBI;
  1         21106  
  1         6621  
30              
31             our $VERSION = '0.03';
32              
33             sub new {
34 0     0 1   my $self = {};
35 0           my $class = shift();
36              
37 0           my %args = @_;
38 0           for my $attr (qw(dsn user password dbh db_type table_prefix schema)) {
39 0           $self->{$attr} = $args{$attr};
40             }
41              
42 0 0         if ( !$self->{dbh} ) {
43 0 0         die "no dbh/dsn given in new()" if !$self->{dsn};
44 0           $self->{dbh}
45             = DBI->connect( $self->{dsn}, $self->{user}, $self->{password},
46             { PrintError => 0, RaiseError => 1 } );
47             }
48              
49 0           my $db_type = $self->{dbh}{Driver}{Name};
50 0 0         if ( $db_type eq 'Oracle' ) {
51              
52             # Default and untaint schema/table_prefix
53 0   0       $self->{schema} ||= 'aedbadmin';
54 0           $self->{schema} =~ /(\w+)/;
55 0           $self->{schema} = $1;
56              
57 0   0       $self->{table_prefix} ||= 'ujo_';
58 0           $self->{table_prefix} =~ /([\w.]+)/;
59 0           $self->{table_prefix} = $1;
60              
61 0           $self->{dbh}->do("alter session set current_schema=$self->{schema}");
62             }
63              
64 0           bless $self, $class;
65 0           return $self;
66             } # new()
67              
68             sub _query {
69 0     0     my $self = shift;
70 0   0       my $prefix = $self->{table_prefix} || '';
71 0           my $query = <
72             select j.*, s.*, j2.job_name as box_name, m.name job_type_name
73             from ${prefix}job j join ${prefix}job_status s
74             on j.joid = s.joid
75             left outer join ${prefix}job j2
76             on j.box_joid = j2.joid
77             left outer join ${prefix}meta_types m
78             on j.job_type = m.type_id and m.sub_type = 'STRINGS'
79             SQL
80 0           return $query;
81             } # _query()
82              
83             sub find_jobs {
84 0     0 1   my $self = shift();
85 0           my $job_name = shift();
86              
87 0           $job_name =~ s/\*/%/g;
88 0 0         my $op = ( $job_name =~ /[%?]/ ) ? 'like' : '=';
89              
90 0           my $query = $self->_query() . <
91             where j.job_name $op ?
92             order by j.joid
93             SQL
94 0           my $sth = $self->{dbh}->prepare($query);
95 0           $sth->execute($job_name);
96              
97 0           return CA::WAAE::JobList->new(
98             parent => $self->{parent},
99             database_handle => $self->{dbh},
100             statement_handle => $sth,
101             table_prefix => $self->{table_prefix},
102             );
103              
104             } # find_jobs()
105              
106             sub job_list {
107 0     0 1   my $self = shift;
108 0           my $h = $self->find_jobs(@_);
109 0           my @list;
110 0           while ( my $job = $h->next_job() ) {
111 0           push @list, $job;
112             }
113 0 0         return wantarray ? @list : \@list;
114             }
115              
116             sub send_event {
117 0     0 1   my $self = shift();
118 0           my ( $job_name, $event, $status, $event_time );
119 0 0         if (@_) {
120 0           my %args = @_;
121 0 0         $job_name = $args{job_name} ? $args{job_name} : '';
122 0 0         $event = $args{event} ? $args{event} : '';
123 0 0         $status = $args{status} ? $args{status} : '';
124 0 0         $event_time = $args{event_time} ? $args{event_time} : '';
125             }
126              
127 0           my $dbh = $self->{dbh};
128 0           my $db_type = $self->{db_type};
129              
130 0           $_ = $dbh->quote($_) for $job_name, $event, $status, $event_time;
131 0   0       my $prefix = $self->{table_prefix} || '';
132 0 0         my $sql
133             = ( $db_type eq 'Oracle' )
134             ? "BEGIN :rtn := ${prefix}sendevent( $event, $job_name, $status, '', $event_time ); END;"
135             : qq( exec sendevent $event, $job_name, $status, '', $event_time, '' );
136 0           my $sth = $dbh->prepare($sql);
137              
138 0           my $rc;
139 0 0         $sth->bind_param_inout( ':rtn', \$rc, 128 ) if $db_type eq 'Oracle';
140 0           $sth->execute();
141 0 0         ($rc) = $sth->fetchrow_array() if $db_type ne 'Oracle';
142              
143 0           return $rc;
144             } # send_event()
145              
146             1;
147             __END__