Subversion Repositories DevTools

Rev

Rev 5919 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1038 dpurdie 1
########################################################################
5919 dpurdie 2
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
1038 dpurdie 3
#
5919 dpurdie 4
# Module name   : JatsRmApi
1038 dpurdie 5
# Module type   : Makefile system
6
# Compiler(s)   : n/a
7
# Environment(s): jats
8
#
9
# Description   : Provides common access function to the Release Manager
10
#                 and Deployment Manager Databases
11
#
12
#                 Note: The interface provided is constrained
13
#                       by the existing usage.
14
#
15
#                       This is an attempt to provide common accessor
16
#                       functions to open and close a connection
17
#
18
#                Note: This is not a class
19
#                      The connection WILL NOT be released if the
20
#                      containing variables go out of scope, BUT the
21
#                      underlying connection is a class and it WILL release
22
#                      the connection.
23
#
24
#......................................................................#
25
 
26
require 5.006_001;
27
 
28
use strict;
29
use warnings;
30
 
31
package JatsRmApi;
32
use DBI;
33
 
34
our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION);
35
use Exporter;
36
 
37
$VERSION = 1.00;
38
@ISA = qw(Exporter);
39
 
40
# Symbols to autoexport (:DEFAULT tag)
41
@EXPORT = qw(
42
            connectRM
7539 dpurdie 43
            connectDM
1038 dpurdie 44
            disconnectRM
7539 dpurdie 45
            disconnectDM
1038 dpurdie 46
            );
47
 
48
#
49
#   Globals to contain connection information
50
#
51
our $GBE_RM_LOCATION;
52
our $GBE_RM_USERNAME;
53
our $GBE_RM_PASSWORD;
54
 
55
#-------------------------------------------------------------------------------
56
# Function        : ValidateRef
57
#
58
# Description     : Ensure that the user has passed a valid reference
59
#
60
# Inputs          : $fname      - Function name for error reporting
61
#                   $ref        - Data to test
62
#
63
# Returns         : Will error on error
64
#
65
 
66
sub ValidateRef
67
{
68
    my ($fname, $ref) = @_;
69
 
70
    Error("$fname: No parameter passed") unless ( $ref );
71
 
72
    my $rtype = ref($ref) || 'Simple Type';
73
    Error("$fname: Must pass a scalar REF, not a $rtype") unless ( $rtype =~ m/SCALAR|REF/ );
74
}
75
 
76
#-------------------------------------------------------------------------------
77
# Function        : GetConnData
78
#
79
# Description     : Determine the connection data for either RM or DM
80
#
81
# Inputs          : $fname  - Caller function
82
#                   @rest   - 3 EnvVars to process
83
#
84
# Returns         : An array of 3 variables
5919 dpurdie 85
#                   location, username, password
1038 dpurdie 86
#
87
sub GetConnData
88
{
89
    my ($fname, @rest) = @_;
90
    my @result;
91
 
92
    foreach my $var ( @rest )
93
    {
94
        my $val = $ENV{$var};
95
        Error( "Environment Variable '$var' not defined." ) unless ( $val );
96
        push @result, $val;
97
    }
98
 
99
    ::Verbose ("$fname:",
100
             "Database: $result[0]",
101
             "Username: $result[1]",
102
             "Password: $result[2]");
103
 
104
    return @result;
105
}
106
 
107
#-------------------------------------------------------------------------------
108
# Function        : connectRM
109
#
110
# Description     : Connect to the RM/DM database
111
#
112
# Inputs          : gref        - Reference to a scalar that will hold
113
#                                 the database connection class data
114
#
115
#                                 If 'undef' this function will create
116
#                                 the connection. Otherwise the function
117
#                                 will simply return.
118
#
119
#                   opt_verbose - Verbose connection
120
#
121
# Returns         : 
122
#
123
sub connectRM
124
{
125
    my ($gref, $opt_verbose) = @_;
126
    connectDB('connectRM', $gref, $opt_verbose, 'GBE_RM_LOCATION', 'GBE_RM_USERNAME', 'GBE_RM_PASSWORD');
127
}
128
 
7539 dpurdie 129
sub connectDM
130
{
131
    my ($gref, $opt_verbose) = @_;
132
    connectDB('connectDM', $gref, $opt_verbose, 'GBE_DM_LOCATION', 'GBE_DM_USERNAME', 'GBE_DM_PASSWORD');
133
}
134
 
1038 dpurdie 135
sub connectDB
136
{
137
    my ($fname, $gref, $opt_verbose, @parms) = @_;
138
 
139
    #
140
    #   Sanity Check user parameters
141
    #
142
    ValidateRef( $fname, $gref);
143
    my @condata = GetConnData( $fname , @parms);
144
    my $location = $condata[0];
145
 
146
    unless ( $$gref )
147
    {
148
        $$gref = DBI->connect(@condata, {verbose => $opt_verbose});
149
 
150
        unless ( defined $$gref )
151
        {
152
            ::Error("$fname: Database [$location]",
153
                  "Reported Error[$DBI::errstr]");
154
        }
155
        ::Verbose("$fname: Connected to database [$location]");
156
    }
157
    else
158
    {
159
        ::Warning ("$fname: Connection Already open");
160
    }
161
}
162
 
163
#-------------------------------------------------------------------------------
164
# Function        : disconnectRM
165
#
166
# Description     : Disconnect from the DM/RM database
167
#
168
# Inputs          : gref        - Reference to a scalar that will hold
169
#                                 the database connection class data
170
#
171
#                                 If not 'undef' this function will disconnect
172
#                                 the connection. Otherwise the function
173
#                                 will simply return.
174
#
175
 
176
sub disconnectRM
177
{
178
    disconnectDB( 'disconnectRM', @_);
179
}
180
 
7539 dpurdie 181
sub disconnectDM
182
{
183
    disconnectDB( 'disconnectDM', @_);
184
}
185
 
1038 dpurdie 186
sub disconnectDB
187
{
188
    my ($fname, $gref) = @_;
189
 
190
    #
191
    #   Sanity Check user parameters
192
    #
193
    ValidateRef( $fname, $gref);
194
 
195
    ::Verbose("$fname: Disconnect from database");
196
    if ( $$gref )
197
    {
198
        $$gref->disconnect() || ::Warning ("$fname:Disconnect failed");
199
        $$gref = undef;
7539 dpurdie 200
        ::Verbose("$fname: Disconnect from database complete");
1038 dpurdie 201
    }
202
    else
203
    {
204
        ::Warning("$fname: Connection not open");
205
    }
206
}
207
 
208
1;
209