-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp_lob_from_ora.pl
288 lines (240 loc) · 6.9 KB
/
exp_lob_from_ora.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/perl
use warnings ;
use strict ;
use Time::HiRes qw(time);
use DBI;
use Env;
use Data::Dumper;
$\="\n";
use constant {
PG2L_CMD_EXPORT_FILE => 0,
PG2L_CMD_ORA_STATUS => 1,
PG2L_CMD_RUN_SCRIPT => 2,
PG2L_CMD_RUN_SQL => 3,
# --------------------------------------
# PG2L RETURN MEG
# --------------------------------------
PG2L_RET_SUCCESS => 100,
PG2L_RET_FAIL => 901,
# --------------------------------------
# PG2L RETURN MEG
# --------------------------------------
PG2L_MSG_DELIMITER => '^'
};
my %pg2l_cfg = (
"PG2L.LOCATION_PERL"=> "",
"PG2L.LOCATION_PRG" => "",
"PG2L.ORA_SID" => "",
"PG2L.ORA_ID" => "",
"PG2L.ORA_HOST" => "",
"PG2L.ORA_PWD" => "",
"PG2L.ORA_PORT" => "",
"PG2L.TARGET_PATH" => ""
);
my $cmd_type;
my $dbh;
my $ORA_STATEMENT;
my @sql_element;
my $idx_column = 1;
my $idx_where_cond = 5;
my $where_statment = "";
my $sql_element_length = 0;
my $TARGET_FILENAME = "";
my $TBL_NAME = "";
my $file_extension = "";
sub main()
{
getEnvVariable();
if ($cmd_type eq PG2L_CMD_EXPORT_FILE) {
fnc_preprocessing_exportFile();
} elsif ($cmd_type eq PG2L_CMD_ORA_STATUS )
{
fnc_check_ora_connection();
} elsif ($cmd_type eq PG2L_CMD_RUN_SCRIPT )
{
} elsif ($cmd_type eq PG2L_CMD_RUN_SQL ){
}
}
sub fnc_preprocessing_exportFile()
{
@sql_element = split(' ', $ORA_STATEMENT);
if ($sql_element[1] =~ /,/ or uc($sql_element[2]) ne 'FROM' )
{
print " This function support only one column for lob\n Please check sql statements";
exit -1;
}
# if (uc($sql_element[4]) ne 'WHERE')
# {
# print " sql statement has wrong \n";
# exit -1;
# }
$sql_element_length = scalar @sql_element - 1;
if (defined $sql_element[4])
{
$where_statment = join ' ', @sql_element[$idx_where_cond .. $sql_element_length];
}else
{
$where_statment = "none_where";
print " sql statement has wrong or none where condition\n";
}
# Remove Special Character from Where Condition.
my $foo = $where_statment;
$foo =~ s/[^\p{PosixAlnum},]/ /g;
my @where_cond = split(' ', $foo);
foreach my $cond (@where_cond){
$file_extension = $file_extension.$cond."_";
}
$TBL_NAME = $sql_element[3];
my $dbHandle = new oraConnection();
$dbHandle->exportFile();
$dbh->disconnect();
undef ($dbHandle);
}
sub fnc_check_ora_connection()
{
my $dbHandle = new oraConnection();
}
sub getEnvVariable()
{
# validation check and copy env variable into the local variable
my $retVal = "";
$cmd_type = $ARGV[0];
if ($cmd_type eq PG2L_CMD_EXPORT_FILE) {
$pg2l_cfg{"PG2L.ORA_HOST"} = $ARGV[1];
$pg2l_cfg{"PG2L.ORA_PORT"} = $ARGV[2];
$pg2l_cfg{'PG2L.ORA_SID'} = $ARGV[3];
$pg2l_cfg{"PG2L.ORA_ID"} = $ARGV[4];
$pg2l_cfg{"PG2L.ORA_PWD"} = $ARGV[5];
$pg2l_cfg{"PG2L.TARGET_PATH"} = $ARGV[6];
$ORA_STATEMENT = $ARGV[7];
} elsif ($cmd_type eq PG2L_CMD_ORA_STATUS )
{
$pg2l_cfg{"PG2L.ORA_HOST"} = $ARGV[1];
$pg2l_cfg{"PG2L.ORA_PORT"} = $ARGV[2];
$pg2l_cfg{'PG2L.ORA_SID'} = $ARGV[3];
$pg2l_cfg{"PG2L.ORA_ID"} = $ARGV[4];
$pg2l_cfg{"PG2L.ORA_PWD"} = $ARGV[5];
$pg2l_cfg{"PG2L.TARGET_PATH"} = $ARGV[6];
} elsif ($cmd_type eq PG2L_CMD_RUN_SCRIPT )
{
} elsif ($cmd_type eq PG2L_CMD_RUN_SQL ){
}
}
sub trim {
my @result = @_;
foreach (@result) {
s/^\s+//;
s/\s+$//;
}
return wantarray ? @result : $result[0];
}
main();
package oraConnection;
use constant {
PG2L_CMD_EXPORT_FILE => 0,
PG2L_CMD_ORA_STATUS => 1,
PG2L_CMD_RUN_SCRIPT => 2,
PG2L_CMD_RUN_SQL => 3,
# --------------------------------------
# PG2L RETURN MEG
# --------------------------------------
PG2L_RET_SUCCESS => 100,
PG2L_RET_FAIL => 901,
# --------------------------------------
# PG2L RETURN MEG
# --------------------------------------
PG2L_MSG_DELIMITER => '^'
};
#sub handle_error {
# my $message = shift;
# my $code = shift;
# #write error message wherever you want
# print "[$code] the message is '$message'\n";
# exit; #stop the program
#}
sub new()
{
#my %attrs = (RaiseError => 1, PrintError => 1, AutoCommit => 0, HandleError => \&handle_error);
my $class = shift;
my %attrs = (RaiseError => 1, PrintError => 0);
my $ORACLE_URI = sprintf ("dbi:Oracle:host=%s;sid=%s;port=%s", $pg2l_cfg{'PG2L.ORA_HOST'},
$pg2l_cfg{'PG2L.ORA_SID'},
$pg2l_cfg{'PG2L.ORA_PORT'});
$dbh = eval {
DBI->connect( $ORACLE_URI,
$pg2l_cfg{'PG2L.ORA_ID'} ,
$pg2l_cfg{'PG2L.ORA_PWD'} ,
\%attrs
);
};
if (!$dbh) {
#say $dbh->get_info($GetInfoType{SQL_DBMS_NAME});
#say $dbh->get_info($GetInfoType{SQL_DBMS_VER});
my $retfmt = sprintf("%s%s[%s]%s", PG2L_RET_FAIL , PG2L_MSG_DELIMITER , $DBI::err, $DBI::errstr);
print $retfmt;
exit;
}else
{
my $self = {_dbh => $dbh};
bless $self, $class;
if ($cmd_type eq PG2L_CMD_ORA_STATUS)
{
my $retfmt = sprintf("%s%s%s", PG2L_RET_SUCCESS , PG2L_MSG_DELIMITER , "connection ok");
print $retfmt;
}
return $self;
}
}
sub getLengthOfLob()
{
# Get length of LOB
my $self = shift;
my $sql = sprintf("SELECT dbms_lob.getlength(%s) FROM %s WHERE %s",$sql_element[1],$TBL_NAME,$where_statment);
my $sth = $self->{_dbh}->prepare($ORA_STATEMENT , { ora_auto_lob => 0 } );
#print $sql;
}
sub exportFile()
{
my $self = shift;
# Query Rewirte
my $sqlrewrite;
if (defined $sql_element[4])
{
$sqlrewrite = sprintf("SELECT dbms_lob.getlength(%s),%s FROM %s WHERE %s ",$sql_element[1] , $sql_element[1],$TBL_NAME,$where_statment);
}else
{
$sqlrewrite = sprintf("SELECT dbms_lob.getlength(%s),%s FROM %s ",$sql_element[1] , $sql_element[1],$TBL_NAME);
}
my $sth = $self->{_dbh}->prepare($sqlrewrite , { ora_auto_lob => 0 } );
$sth->execute( );
my $seq = 0;
while ( my @row = $sth->fetchrow_array()) {
my ($lob_len, $lob) = @row;
my $chunk_size = 32767;
my $amount = 32767;
my $offset = 1; # Offsets start at 1, not 0
$seq++;
my $tmp_extension = $file_extension . time().'_'.$seq;
$TARGET_FILENAME = $sql_element[3].".".$tmp_extension;
# Create file :
my $filepath = sprintf("%s/%s",$pg2l_cfg{'PG2L.TARGET_PATH'} , $TARGET_FILENAME);
open (FILE, ">:raw", $filepath);
binmode FILE;
while ($offset <= $lob_len)
{
my $data = $dbh->ora_lob_read( $lob,$offset, $chunk_size) ;
syswrite FILE,$data,length($data);
$offset = $offset + $amount ;
$data = undef;
}
my $absPath = $pg2l_cfg{'PG2L.TARGET_PATH'}."/".$TARGET_FILENAME;
$absPath =~ s/\/\//\//g;
# setting return format
my $retfmt = sprintf("%s%s%s", PG2L_RET_SUCCESS , PG2L_MSG_DELIMITER , $pg2l_cfg{'PG2L.TARGET_PATH'}."/".$TARGET_FILENAME);
print $retfmt;
undef $lob;
close FILE;
}
$sth->finish();
# print ("done\n");
}