Subversion Repositories DevTools

Rev

Rev 1038 | Go to most recent revision | 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
43
            disconnectRM
44
            );
45
 
46
#
47
#   Globals to contain connection information
48
#
49
our $GBE_RM_LOCATION;
50
our $GBE_RM_USERNAME;
51
our $GBE_RM_PASSWORD;
52
 
53
#-------------------------------------------------------------------------------
54
# Function        : ValidateRef
55
#
56
# Description     : Ensure that the user has passed a valid reference
57
#
58
# Inputs          : $fname      - Function name for error reporting
59
#                   $ref        - Data to test
60
#
61
# Returns         : Will error on error
62
#
63
 
64
sub ValidateRef
65
{
66
    my ($fname, $ref) = @_;
67
 
68
    Error("$fname: No parameter passed") unless ( $ref );
69
 
70
    my $rtype = ref($ref) || 'Simple Type';
71
    Error("$fname: Must pass a scalar REF, not a $rtype") unless ( $rtype =~ m/SCALAR|REF/ );
72
}
73
 
74
#-------------------------------------------------------------------------------
75
# Function        : GetConnData
76
#
77
# Description     : Determine the connection data for either RM or DM
78
#
79
# Inputs          : $fname  - Caller function
80
#                   @rest   - 3 EnvVars to process
81
#
82
# Returns         : An array of 3 variables
5919 dpurdie 83
#                   location, username, password
1038 dpurdie 84
#
85
sub GetConnData
86
{
87
    my ($fname, @rest) = @_;
88
    my @result;
89
 
90
    foreach my $var ( @rest )
91
    {
92
        my $val = $ENV{$var};
93
        Error( "Environment Variable '$var' not defined." ) unless ( $val );
94
        push @result, $val;
95
    }
96
 
97
    ::Verbose ("$fname:",
98
             "Database: $result[0]",
99
             "Username: $result[1]",
100
             "Password: $result[2]");
101
 
102
    return @result;
103
}
104
 
105
#-------------------------------------------------------------------------------
106
# Function        : connectRM
107
#
108
# Description     : Connect to the RM/DM database
109
#
110
# Inputs          : gref        - Reference to a scalar that will hold
111
#                                 the database connection class data
112
#
113
#                                 If 'undef' this function will create
114
#                                 the connection. Otherwise the function
115
#                                 will simply return.
116
#
117
#                   opt_verbose - Verbose connection
118
#
119
# Returns         : 
120
#
121
sub connectRM
122
{
123
    my ($gref, $opt_verbose) = @_;
124
    connectDB('connectRM', $gref, $opt_verbose, 'GBE_RM_LOCATION', 'GBE_RM_USERNAME', 'GBE_RM_PASSWORD');
125
}
126
 
127
sub connectDB
128
{
129
    my ($fname, $gref, $opt_verbose, @parms) = @_;
130
 
131
    #
132
    #   Sanity Check user parameters
133
    #
134
    ValidateRef( $fname, $gref);
135
    my @condata = GetConnData( $fname , @parms);
136
    my $location = $condata[0];
137
 
138
    unless ( $$gref )
139
    {
140
        $$gref = DBI->connect(@condata, {verbose => $opt_verbose});
141
 
142
        unless ( defined $$gref )
143
        {
144
            ::Error("$fname: Database [$location]",
145
                  "Reported Error[$DBI::errstr]");
146
        }
147
        ::Verbose("$fname: Connected to database [$location]");
148
    }
149
    else
150
    {
151
        ::Warning ("$fname: Connection Already open");
152
    }
153
}
154
 
155
#-------------------------------------------------------------------------------
156
# Function        : disconnectRM
157
#
158
# Description     : Disconnect from the DM/RM database
159
#
160
# Inputs          : gref        - Reference to a scalar that will hold
161
#                                 the database connection class data
162
#
163
#                                 If not 'undef' this function will disconnect
164
#                                 the connection. Otherwise the function
165
#                                 will simply return.
166
#
167
 
168
sub disconnectRM
169
{
170
    disconnectDB( 'disconnectRM', @_);
171
}
172
 
173
sub disconnectDB
174
{
175
    my ($fname, $gref) = @_;
176
 
177
    #
178
    #   Sanity Check user parameters
179
    #
180
    ValidateRef( $fname, $gref);
181
 
182
    ::Verbose("$fname: Disconnect from database");
183
    if ( $$gref )
184
    {
185
        $$gref->disconnect() || ::Warning ("$fname:Disconnect failed");
186
        $$gref = undef;
187
    }
188
    else
189
    {
190
        ::Warning("$fname: Connection not open");
191
    }
192
}
193
 
194
1;
195