Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6910 dpurdie 1
########################################################################
2
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
3
#
4
# Module name   : jats_rm_query.pl
5
# Module type   : JATS Utility
6
# Compiler(s)   : Perl
7
# Environment(s): jats
8
#
9
# Description   : Execute a simple RM SQL query
10
#
11
# Usage         : See POD at the end of this file
12
#
13
#......................................................................#
14
 
15
require 5.008_002;
16
use strict;
17
use warnings;
18
 
19
use Pod::Usage;
20
use Getopt::Long;
21
 
22
use Pod::Usage;
23
use Getopt::Long;
24
use JatsError;
25
use JatsRmApi;
26
use JatsSystem;
27
use FileUtils;
28
use ConfigurationFile;
29
use JatsProperties;
30
use File::Copy;
31
use DBI;
32
my $RM_DB;
33
 
34
# ReadOnly credentails to known databases
35
my $newRMCred  = ['jdbc:oracle:thin:@auawsards002:1521:RELEASEM', 'RM_READONLY', 'Tp8WmmDKMq2Z'];
36
my $oldRMCred  = ['jdbc:oracle:thin:@auawsards001:1521:RELEASEM', 'RM_READONLY', 'RM_READONLY'];
37
my $rmCred;
38
 
39
my $VERSION = 1.0;
40
my $opt_help=0;
41
my $opt_verbose=0;
42
my $opt_debug=0;
43
my $opt_pulse;
44
my $opt_user;
45
my $opt_passwd;
46
my $opt_server;
47
 
48
 
49
#-------------------------------------------------------------------------------
50
# Function        : Mainline Entry Point
51
#
52
# Description     :
53
#
54
# Inputs          :
55
#
56
my $result = GetOptions (
57
                "help:+"        => \$opt_help,
58
                "manual:3"      => \$opt_help,
59
                "verbose:+"     => \$opt_verbose,
60
                "debug:+"       => \$opt_debug,
61
                "pulse!"        => \$opt_pulse,
62
                "user:s"        => \$opt_user,
63
                "password:s"    => \$opt_passwd,
64
                "server:s"      => \$opt_server,
65
                );
66
 
67
                #
68
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
69
                #
70
 
71
#
72
#   Process help and manual options
73
#
74
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
75
pod2usage(-verbose => 1) if ( $opt_help == 2 );
76
pod2usage(-verbose => 2) if ( $opt_help > 2 );
77
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV < 0 );
78
 
79
#
80
#   Configure the error reporting rmMerge_process now that we have the user options
81
#
82
ErrorConfig( 'name'    =>'RMQRY',
83
             'verbose' => $opt_verbose,
84
             'debug' => $opt_debug,
85
            );
86
Error('No Query') unless (length($ARGV[0]) > 0);
87
 
88
# Configue the Database to use
89
$rmCred = $opt_pulse ? $newRMCred : $oldRMCred; 
90
$rmCred->[0] = $opt_server if defined $opt_server; 
91
$rmCred->[1] = $opt_user if defined $opt_user; 
92
$rmCred->[2] = $opt_passwd if defined $opt_passwd; 
93
 
94
my ($newData, $colNames) = getDataFromRm('Data', $ARGV[0], $rmCred, {dump => $opt_verbose, oneRow => 0, 'error' => 'No data found'});
95
 
96
#
97
#   Determine the max width of every dislpay column (except the last)
98
#       Add Colum Names as the first row of Data
99
#       Process column names and raw data
100
#
101
my @colWidth;
102
my $colNum = ($#$colNames) - 1;
103
unshift @{$newData}, $colNames;
104
 
105
foreach my $entry ( @{$newData}) {
106
    for my $i (0 .. $colNum) {
107
        if (!defined $entry->[$i] ) {
108
            $entry->[$i] = ''
109
        }
110
        my $len = length($entry->[$i]);
111
        if ( !defined($colWidth[$i]) || $len > $colWidth[$i]) {
112
            $colWidth[$i] = $len
113
        }
114
    }
115
}
116
#
117
#   Generate a sprintf formatted line
118
#   With no width arg on the last element
119
#
120
my $fmtline ='';
121
my $fmtJoiner = '';
122
foreach (@colWidth) {
123
    $fmtline .= $fmtJoiner . '%-' . $_ . 's';
124
    $fmtJoiner = ', ';
125
}
126
$fmtline .= $fmtJoiner . "%-s\n";
127
 
128
#
129
#   Display the data
130
#
131
foreach my $entry ( @{$newData}) {
132
    printf($fmtline, @{$entry});
133
}
134
 
135
 
136
#-------------------------------------------------------------------------------
137
# Function        : getDataFromRm 
138
#
139
# Description     : Get an array of data from RM 
140
#
141
# Inputs          : $name           - Query Name
142
#                   $m_sqlstr       - Query
143
#                   $rmRef          - Ref to RM
144
#                   $options        - Ref to a hash of options
145
#                                       sql     - show sql
146
#                                       data    - show data
147
#                                       dump    - show results
148
#                                       oneRow  - Only feth one row
149
#                                       error   - Must find data
150
#                                       
151
# Returns         : ref to array of data
152
#                   ref to array of Column Names
153
#
154
sub getDataFromRm
155
{
156
    my ($name,$m_sqlstr, $rmRef, $options ) = @_;
157
    my @row;
158
    my $data;
159
    my $colNames;
160
 
161
    if (ref $options ne 'HASH') {
162
        $options = {}; 
163
    }
164
 
165
    $ENV{GBE_RM_LOCATION} = $rmRef->[0];
166
    $ENV{GBE_RM_USERNAME} = $rmRef->[2];
167
    $ENV{GBE_RM_PASSWORD} = $rmRef->[1];
168
    connectRM(\$RM_DB, $opt_verbose);
169
 
170
    if ($options->{sql}) {
171
        Message("$name: $m_sqlstr")
172
    }
173
    my $sth = $RM_DB->prepare($m_sqlstr);
174
    if ( defined($sth) )
175
    {
176
        if ( $sth->execute( ) ) {
177
            $colNames = $sth->fetch_colNames();
178
            if ( $sth->rows ) {
179
                while ( @row = $sth->fetchrow_array ) {
180
                    if ($options->{data}) {
181
                        Message ("$name: @row");
182
                    }
183
                    #Debug0("$name: @row");
184
                    push @{$data}, [@row];
185
 
186
                    last if $options->{oneRow};
187
                }
188
            }
189
            $sth->finish();
190
        } else {
191
            Error("Execute failure:$name: $m_sqlstr", $sth->errstr() );
192
        }
193
    } else {
194
        Error("Prepare failure:$name" );
195
    }
196
    disconnectRM(\$RM_DB);
197
 
198
    if (!$data && $options->{error}) {
199
        Error( $options->{error} );
200
    }
201
 
202
    if ($data && $options->{oneRow}) {
203
        $data = $data->[0];
204
    }
205
 
206
    if ($options->{dump}) {
207
        DebugDumpData("$name", $data);
208
    }
209
    return $data, $colNames;
210
}
211
 
212
#-------------------------------------------------------------------------------
213
#   Documentation
214
#
215
 
216
=pod
217
 
218
=for htmltoc    GENERAL::ClearCase::
219
 
220
=head1 NAME
221
 
222
rmMerge_listVersions - list versions of a package
223
 
224
=head1 SYNOPSIS
225
 
226
jats rmMerge_listVersions [options] PackageName PackageVersion -- commands
227
 
228
 Options:
229
    -help              - brief help message
230
    -help -help        - Detailed help message
231
    -man               - Full documentation
232
    -verbose[=n]       - Enable additional output
233
    -pulse             - Use Pulse Database
234
    -user=nnn          - Specify alternate user name
235
    -password=nnn       - Specify alternate password
236
    -server=nnn        - Specify alternate server string
237
 
238
=head1 OPTIONS
239
 
240
=over 8
241
 
242
=item B<-help>
243
 
244
Print a brief help message and exits.
245
 
246
=item B<-help -help>
247
 
248
Print a detailed help message with an explanation for each option.
249
 
250
=item B<-versbose[=n]>
251
 
252
Enable additional output
253
 
254
=item B<-pulse>
255
 
256
This option will case the command to access the Pulse database. The default is to use the standard
257
Release Manager database and readonly credentails.
258
 
259
=item B<-user=nnn>
260
 
261
Specify an alternate user name for the database connection.
262
 
263
=item B<-password=nnn>
264
 
265
Specify an alternate password for the database connection.
266
 
267
=item B<-server=nnn>
268
 
269
Specify an alternate server connection string for the database connection.
270
 
271
=back
272
 
273
=head1 EXAMPLE
274
 
275
jats rm_query  "select PV_ID,PKG_ID,PKG_VERSION,PKG_LABEL,SRC_PATH from release_manager.package_versions where pv_id=1223354"
276
 
277
=cut
278
 
279
 
280
 
281
 
282
 
283
 
284