-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecuteSQL.pl
180 lines (149 loc) · 4.5 KB
/
executeSQL.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/perl
#
################################################################################
#
# executeSQL.pl
# - executes a given SQL statement specified by option s
# - statement can be in a file or entered on command line
# - any parsing or execution errors are trapped and reported via standard
# ErrorExit routine
#
################################################################################
# Enhancements
#
# 20080213
# - added code to strip trailing semicolon, "quit" and "exit" statements from
# SQL
#
# 20080930
# - changed options o,p,u to uppercase O,P,U to match t2t
################################################################################
### Make perl complain about sloppy code
use strict;
### Load the DBI, IO and other modules
use DBI;
use Getopt::Std;
use IO::Handle;
### Load CSICommon package
# need following if CSICommon package is not installed in Perl include path
use lib $ENV{UTIL};
use CSICommon;
### Define common script processing variables
my $delim = "\t";
my $err = false;
my ( $i, $j, $k );
my (%OPT);
# fix problem with log outputting only on exit
autoflush STDOUT 1;
################################################################################
# Option validation
# - See ShowUsage subroutine for explanation of options
################################################################################
if ( getopts( "i:s:DO:P:U:?", \%OPT ) ) { ; }
else {
$err = true;
ShowUsage();
}
# option ? reserved for ShowUsage call
# option D reserved for DEBUG mode flag
#
if ( $OPT{"?"} ) {
ShowUsage();
}
# check option D
if ( $OPT{D} ) { $debug = true; }
else { $debug = false; }
# chec for additional identifier option
if ( $OPT{i} ) {
# reset ME adding identifier if specified
$ME = "$ME-$OPT{i}";
}
if ( $OPT{s} ) { ; }
else {
print "ERROR: arg s is required\n";
$err = true;
}
# check for overriding database, userID, and password
$dbname = $OPT{O} if ( $OPT{O} );
$dbuser = $OPT{U} if ( $OPT{U} );
$dbpass = $OPT{P} if ( $OPT{P} );
# execute ShowUsage subroutine if there are any errors in option validation
ShowUsage() if ( $err == true );
################################################################################
# Mainline
################################################################################
EchoStep("Start of $ME");
# define script specific variables (group as appropriate)
my $subj = "";
# Define Oracle connection variables
my ( %db, $dbh, $sql, $sth );
### Connect to the database, assuming: $dbname, $dbuser, $dbpass
$dbh = DBI->connect(
"dbi:Oracle:$dbname",
$dbuser, $dbpass,
{
PrintError => 1, ### Do report errors via warn( )
RaiseError => 0 ### Don't report errors via die( )
}
);
# The following line is required for strings to work as sql argument with placeholders
$dbh->{ora_ph_type} = 96;
if ( -f $OPT{s} ) {
if ( open( IN, $OPT{s} ) ) {
# undefine the end of record character to read in the whole file
undef $/;
$sql = <IN>;
close(IN);
# restore the end of record character
$/ = "\n";
}
}
else {
$sql = $OPT{s};
}
print "before sub sql: $sql\n" if ($debug);
# remove any terminating semicolon, "quit" or "exit" statement from $sql
$sql =~ s/\nexit[\n]*$//gi;
$sql =~ s/\nquit[\n]*$//gi;
$sql =~ s/;$//g;
print "after sub sql: $sql\n" if ($debug);
if ( $sth = $dbh->prepare($sql) ) {
if ( $sth->execute() ) { ; }
else {
$err = 2;
$subj = "$ME - error during execute of SQL";
}
}
else {
$err = 1;
$subj = "$ME - error during prepare of SQL";
}
# if $err (non-zero value) execute ErrorExit otherwise exit with zero
if ($err) {
print "error = $DBI::errstr\n" if ($debug);
# set $MESSAGE used in ErrorExit routine to add to email body
$MESSAGE = $DBI::errstr;
ErrorExit($subj);
}
else {
EchoStep("End of $ME");
exit(0);
}
################################################################################
# Subfunctions
################################################################################
sub ShowUsage() {
print qq{
Usage:
$ME -s _ [-i _] [-O _] [-P _] [-U _] [-D]
Option Description
------------------- ---------------------------------------------------------
-i identifier string to identify this call for error reporting
-s sql_or_file sql query or file containing sql query
-O override Oracle database name [$dbname]
-P override password [$dbpass]
-U override user name [$dbuser]
-D turn debug mode on
\n};
exit($err);
}