-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson-decode.pl
More file actions
executable file
·208 lines (155 loc) · 4.62 KB
/
json-decode.pl
File metadata and controls
executable file
·208 lines (155 loc) · 4.62 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env perl
#
use strict;
use warnings;
# part of Perl core as of 5.14.0
# 2.273 required as 2.271 does not work here
# do not know about 2.272
use JSON::PP 2.273 ;
use Data::Dumper;
use DBI;
use 5.14.0;
# simulate setup in sqlrun.pl
my $driver='Oracle';
my $host='';
my $port='';
my $options='';
my $db='js01';
my $username='scott';
my $password='tiger';
my $oraSessionMode = '';
my $raiseError=1;
my $printError=0;
my $autoCommit=0;
# for postgres
$driver='Pg';
$host='ubuntu-20-pg02';
$port=5432;
$db='postgres';
$username='benchmark';
$password='grok';
# this should match exactly to all possible keys in driver-config.json
my %connectSetup = (
'connectParms' => {
'db' => $db,
'username' => $username,
'password' => $password,
'port' => $port,
'host' => $host,
'options' => $options
},
'dbhAttributes' => {
'RaiseError' => $raiseError,
'PrintError' => $printError,
'AutoCommit' => $autoCommit,
'ora_session_mode' => $oraSessionMode,
},
# these will be populated from the config file
'connectCode' => '',
);
our $unslurp=$/;
undef $/; # slurp mode for file read
my $jsonTxt=<>;
$/ = $unslurp;
my $jsonOut;
my $ppJSON = JSON::PP->new;
#$jsonOut = $ppJSON->pretty->encode($ppJSON->decode($jsonTxt));
#print Dumper($ppJSON->decode($jsonTxt));
# get just the parameters for this driver - nested hash
my %dbhInfo = %{%{$ppJSON->decode($jsonTxt)}{$driver}};;
print Dumper(\%dbhInfo);
my @connectParms = @{$dbhInfo{connectParms}};
my @dbhAttributes = @{$dbhInfo{dbhAttributes}};
my $connectCode = $dbhInfo{connectCode};
print 'Connection Parameters: ' . join(' , ',@connectParms) . "\n";
print 'dbh Attributes: ' . join(' , ',@dbhAttributes) . "\n";
print "Connect Code: $connectCode\n";
print "\n";
if ( !defined($dbhInfo{connectCode})) {
die "connectCode not found in JSON config file\n";
}
$connectSetup{connectCode} = $dbhInfo{connectCode};
#print Dumper(\%connectSetup) . "\n";
foreach my $key ( keys %dbhInfo ) {
#print "key: $key\n";
# determine datatype
my $ref = $dbhInfo{$key};
my $refType = ref( $dbhInfo{$key});
# if not a ref to hash or array, $refType will be an empty string
# this will pick up scalars
$refType = ref(\$ref) unless $refType;
#print "ref type: $refType \n\n";
# should be only ARRAR or SCALAR in the JSON config file
if ($refType eq 'ARRAY') {
# walk through the array
my @ary = @{$dbhInfo{$key}};
#print "working on key: $key\n";
foreach my $el ( @ary ) {
#print " $el\n";
#my $elName = $connectSetup{$dbhInfo}{$key}}; # array name
if ($key eq 'connectParms' ) {
# validate
if (! defined( $connectSetup{connectParms}->{$el} ) ) {
die "$el not defined in 'connectSetup{connectParms}' \n";
};
$connectSetup{connectCode} =~ s/<$el>/'$connectSetup{connectParms}->{$el}'/g;
} elsif ($key eq 'dbhAttributes' ) {
#
# validate
if (! defined( $connectSetup{dbhAttributes}->{$el} ) ) {
die "$el not defined in 'connectSetup{dbhAttributes}' \n";
};
#print " setting dbhAttribute: $key->$el\n";
#print " to: $connectSetup{dbhAttributes}->{$el}\n";
my $test = $connectSetup{dbhAttributes}->{$el};
eval {
use warnings 'FATAL' => 'numeric'; # fatalize the numeric warning
my $t = $test + 1 ;
$connectSetup{connectCode} =~ s/<$el>/$connectSetup{dbhAttributes}->{$el}/g;
} or do {
#print "\n\nsetting string\n\n";
my $attr = $connectSetup{dbhAttributes}->{$el};
$attr = "''" unless $attr;
$connectSetup{connectCode} =~ s/<$el>/$attr}/g;
};
#print "\n";
} else {
die "invalid key name in JSON\n";
};
}
#print "\n\n";
# database handle connect parameter
#my $tmp = $connectSetup{connectCode};
#$tmp =~ s/<$key>/$connectSetup{$key}
# database handle attributes
} elsif ($refType eq 'SCALAR') {
# the scalars of connectCode is set before this loop
# nothing to do here at this time
#print "working on $key\n";
#print "$dbhInfo{$key}\n";
#print "\n";
#$connectSetup{$key} = $dbhInfo{$key};
} else {
# crash
die "unsupported type found in JSON config file - '$refType' \n";
}
}
#print Dumper(\%connectSetup);
my $connectString = $connectSetup{connectCode};
print "connectString: $connectString\n";
my $dbh;
# this eval works
#$dbh=DBI->connect(eval "$connectString");
# this also works - no quotes
$dbh=DBI->connect(eval $connectString);
die "could not connect - $!\n" unless $dbh;
# oracle
#my $sql = "select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual";
#postgresql
my $sql = "select current_date";
my $sth = $dbh->prepare($sql);
$sth->execute;
my @ary=$sth->fetchrow_array;
print "currdate: $ary[0]\n";
$sth->finish;
$dbh->disconnect;