Subversion Repositories DevTools

Rev

Rev 7066 | Details | Compare with Previous | 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'];
6922 dpurdie 37
my $testRMCred  = ['jdbc:oracle:thin:@relmanu1.coidtfba5ouc.ap-southeast-2.rds.amazonaws.com:1521:RELMANU1', 'RM_READONLY', 'RM_READONLY'];
6910 dpurdie 38
my $rmCred;
39
 
40
my $VERSION = 1.0;
41
my $opt_help=0;
42
my $opt_verbose=0;
43
my $opt_debug=0;
44
my $opt_pulse;
45
my $opt_user;
46
my $opt_passwd;
47
my $opt_server;
6922 dpurdie 48
my $opt_test;
6910 dpurdie 49
 
50
 
51
#-------------------------------------------------------------------------------
52
# Function        : Mainline Entry Point
53
#
54
# Description     :
55
#
56
# Inputs          :
57
#
58
my $result = GetOptions (
59
                "help:+"        => \$opt_help,
60
                "manual:3"      => \$opt_help,
61
                "verbose:+"     => \$opt_verbose,
62
                "debug:+"       => \$opt_debug,
63
                "pulse!"        => \$opt_pulse,
6922 dpurdie 64
                "test!"         => \$opt_test,
6910 dpurdie 65
                "user:s"        => \$opt_user,
66
                "password:s"    => \$opt_passwd,
67
                "server:s"      => \$opt_server,
68
                );
69
 
70
                #
71
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
72
                #
73
 
74
#
75
#   Process help and manual options
76
#
77
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
78
pod2usage(-verbose => 1) if ( $opt_help == 2 );
79
pod2usage(-verbose => 2) if ( $opt_help > 2 );
80
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV < 0 );
81
 
82
#
83
#   Configure the error reporting rmMerge_process now that we have the user options
84
#
85
ErrorConfig( 'name'    =>'RMQRY',
86
             'verbose' => $opt_verbose,
87
             'debug' => $opt_debug,
88
            );
89
Error('No Query') unless (length($ARGV[0]) > 0);
90
 
91
# Configue the Database to use
92
$rmCred = $opt_pulse ? $newRMCred : $oldRMCred; 
6922 dpurdie 93
$rmCred = $opt_test ? $testRMCred : $rmCred;
6910 dpurdie 94
$rmCred->[0] = $opt_server if defined $opt_server; 
95
$rmCred->[1] = $opt_user if defined $opt_user; 
96
$rmCred->[2] = $opt_passwd if defined $opt_passwd; 
97
 
6922 dpurdie 98
my $colData;
99
my $newData = getDataFromRm('Data', $ARGV[0], $rmCred, {dump => $opt_verbose, oneRow => 0, 'error' => 'No data found', colData => \$colData});
100
 
6910 dpurdie 101
#
6922 dpurdie 102
#   Determine the max width of every dislpay column
6912 dpurdie 103
#       Add Column Names as the first row of Data
6910 dpurdie 104
#       Process column names and raw data
105
#
6912 dpurdie 106
my @colNames;
107
foreach my $entry ( @{$colData}) {
108
    $entry =~ s~^(.*?):~~;
109
    push @colNames, $1;
110
}
6910 dpurdie 111
my @colWidth;
6922 dpurdie 112
my $colNum = ($#$colData);
6912 dpurdie 113
unshift @{$newData}, \@colNames;
6910 dpurdie 114
 
115
foreach my $entry ( @{$newData}) {
116
    for my $i (0 .. $colNum) {
117
        if (!defined $entry->[$i] ) {
6922 dpurdie 118
            $entry->[$i] = '(null)'
6910 dpurdie 119
        }
120
        my $len = length($entry->[$i]);
121
        if ( !defined($colWidth[$i]) || $len > $colWidth[$i]) {
122
            $colWidth[$i] = $len
123
        }
124
    }
125
}
126
#
127
#   Generate a sprintf formatted line
6922 dpurdie 128
#   With no width arg on the last element to prevent space filling
6910 dpurdie 129
#
130
my $fmtline ='';
131
my $fmtJoiner = '';
6922 dpurdie 132
pop @colWidth;
6910 dpurdie 133
foreach (@colWidth) {
134
    $fmtline .= $fmtJoiner . '%-' . $_ . 's';
135
    $fmtJoiner = ', ';
136
}
137
$fmtline .= $fmtJoiner . "%-s\n";
138
 
139
#
140
#   Display the data
141
#
142
foreach my $entry ( @{$newData}) {
143
    printf($fmtline, @{$entry});
144
}
145
 
146
#-------------------------------------------------------------------------------
147
# Function        : getDataFromRm 
148
#
149
# Description     : Get an array of data from RM 
150
#
151
# Inputs          : $name           - Query Name
152
#                   $m_sqlstr       - Query
153
#                   $rmRef          - Ref to RM
154
#                   $options        - Ref to a hash of options
155
#                                       sql     - show sql
156
#                                       data    - show data
157
#                                       dump    - show results
158
#                                       oneRow  - Only feth one row
159
#                                       error   - Must find data
6922 dpurdie 160
#                                       colData - Ref to place to store colum data
6910 dpurdie 161
#                                       
162
# Returns         : ref to array of data
163
#
164
sub getDataFromRm
165
{
166
    my ($name,$m_sqlstr, $rmRef, $options ) = @_;
167
    my @row;
168
    my $data;
169
 
170
    if (ref $options ne 'HASH') {
171
        $options = {}; 
172
    }
173
 
174
    $ENV{GBE_RM_LOCATION} = $rmRef->[0];
6922 dpurdie 175
    $ENV{GBE_RM_USERNAME} = $rmRef->[1];
176
    $ENV{GBE_RM_PASSWORD} = $rmRef->[2];
6910 dpurdie 177
    connectRM(\$RM_DB, $opt_verbose);
178
 
179
    if ($options->{sql}) {
180
        Message("$name: $m_sqlstr")
181
    }
182
    my $sth = $RM_DB->prepare($m_sqlstr);
183
    if ( defined($sth) )
184
    {
185
        if ( $sth->execute( ) ) {
6922 dpurdie 186
            if ($options->{colData}) {
187
                ${$options->{colData}} = $sth->fetch_columndata();
188
            }
6910 dpurdie 189
            if ( $sth->rows ) {
190
                while ( @row = $sth->fetchrow_array ) {
191
                    if ($options->{data}) {
192
                        Message ("$name: @row");
193
                    }
194
                    #Debug0("$name: @row");
195
                    push @{$data}, [@row];
196
 
197
                    last if $options->{oneRow};
198
                }
199
            }
200
            $sth->finish();
201
        } else {
202
            Error("Execute failure:$name: $m_sqlstr", $sth->errstr() );
203
        }
204
    } else {
205
        Error("Prepare failure:$name" );
206
    }
207
    disconnectRM(\$RM_DB);
208
 
209
    if (!$data && $options->{error}) {
210
        Error( $options->{error} );
211
    }
212
 
213
    if ($data && $options->{oneRow}) {
214
        $data = $data->[0];
215
    }
216
 
217
    if ($options->{dump}) {
218
        DebugDumpData("$name", $data);
219
    }
6922 dpurdie 220
    return $data;
6910 dpurdie 221
}
222
 
223
#-------------------------------------------------------------------------------
224
#   Documentation
225
#
226
 
227
=pod
228
 
229
=head1 NAME
230
 
7066 dpurdie 231
rm_query - Perform ad-hoc queries against the Release Manager database
6910 dpurdie 232
 
233
=head1 SYNOPSIS
234
 
7066 dpurdie 235
jats rm_query [options] 'sql command'
6910 dpurdie 236
 
237
 Options:
238
    -help              - brief help message
239
    -help -help        - Detailed help message
240
    -man               - Full documentation
241
    -verbose[=n]       - Enable additional output
242
    -pulse             - Use Pulse Database
6922 dpurdie 243
    -test              - Use Test Database
6910 dpurdie 244
    -user=nnn          - Specify alternate user name
6915 dpurdie 245
    -password=nnn      - Specify alternate password
6910 dpurdie 246
    -server=nnn        - Specify alternate server string
247
 
248
=head1 OPTIONS
249
 
250
=over 8
251
 
252
=item B<-help>
253
 
254
Print a brief help message and exits.
255
 
256
=item B<-help -help>
257
 
258
Print a detailed help message with an explanation for each option.
259
 
260
=item B<-versbose[=n]>
261
 
262
Enable additional output
263
 
264
=item B<-pulse>
265
 
266
This option will case the command to access the Pulse database. The default is to use the standard
267
Release Manager database and readonly credentails.
268
 
6922 dpurdie 269
=item B<-test>
270
 
271
This option will case the command to access the Release Manager Test database.
272
 
6910 dpurdie 273
=item B<-user=nnn>
274
 
275
Specify an alternate user name for the database connection.
276
 
277
=item B<-password=nnn>
278
 
279
Specify an alternate password for the database connection.
280
 
281
=item B<-server=nnn>
282
 
283
Specify an alternate server connection string for the database connection.
284
 
285
=back
286
 
287
=head1 EXAMPLE
288
 
289
jats rm_query  "select PV_ID,PKG_ID,PKG_VERSION,PKG_LABEL,SRC_PATH from release_manager.package_versions where pv_id=1223354"
290
 
7066 dpurdie 291
If no data is found the utility will return a non-zero exit code.
292
 
6910 dpurdie 293
=cut
294
 
295
 
296
 
297
 
298
 
299
 
300