Subversion Repositories DevTools

Rev

Rev 227 | Rev 4466 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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