-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_lob_loader.sql
483 lines (407 loc) · 12.3 KB
/
pg_lob_loader.sql
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
*/
CREATE OR REPLACE FUNCTION pg2l_get_filename(VARCHAR default '') RETURNS SETOF VARCHAR AS $$
my $filepath;
my $oraStatement = $_[0];
my $line;
my $retOID;
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,
};
#get configuration
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" => ""
);
foreach my $key (keys %pg2l_cfg)
{
my $get_query = sprintf("SHOW %s;",$key);
#elog (INFO,$get_query);
#get configuration
$sth = spi_query($get_query);
while (defined (my $row = spi_fetchrow($sth))) {
my $col = lc($key);
$pg2l_cfg{$key} = $row->{$col};
#elog (INFO,$pg2l_cfg{$key});
}
# checking configuration
}
my $system_cmd = sprintf ("%s %s %s %s %s %s %s %s %s '%s'", $pg2l_cfg{"PG2L.LOCATION_PERL"},
$pg2l_cfg{"PG2L.LOCATION_PRG"},
PG2L_CMD_EXPORT_FILE,
$pg2l_cfg{"PG2L.ORA_HOST"},
$pg2l_cfg{"PG2L.ORA_PORT"},
$pg2l_cfg{"PG2L.ORA_SID"},
$pg2l_cfg{"PG2L.ORA_ID"},
$pg2l_cfg{"PG2L.ORA_PWD"},
$pg2l_cfg{"PG2L.TARGET_PATH"},
$oraStatement
);
elog (INFO,"OID : $system_cmd ");
open CMD,'-|',$system_cmd or die $@;
while (defined($line=<CMD>)) {
$line =~ s/^\s+|\s+$//g;
my @ret = split('\^', $line);
if ($ret[0] == PG2L_RET_SUCCESS)
{
return_next ($ret[1]);
} elsif ($ret[0] == PG2L_RET_FAIL) {
elog (ERROR,$ret[1]);
}
}
close CMD;
return undef;
$$ LANGUAGE plperlu;
/*
*/
CREATE OR REPLACE FUNCTION pg2l_get_oid(VARCHAR default '',INTEGER default 1) RETURNS oid AS $$
use warnings ;
use strict ;
my $filepath;
my $oraStatement = $_[0];
my $delFileFlag = $_[1];
my $line;
my $retOID;
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,
};
#get configuration
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" => ""
);
foreach my $key (keys %pg2l_cfg)
{
my $get_query = sprintf("SHOW %s;",$key);
#elog (INFO,$get_query);
#get configuration
my $sth = spi_query($get_query);
while (defined (my $row = spi_fetchrow($sth))) {
my $col = lc($key);
$pg2l_cfg{$key} = $row->{$col};
#elog (INFO,$pg2l_cfg{$key});
}
# checking configuration
}
my $system_cmd = sprintf ("%s %s %s %s %s %s %s %s %s '%s'", $pg2l_cfg{"PG2L.LOCATION_PERL"},
$pg2l_cfg{"PG2L.LOCATION_PRG"},
PG2L_CMD_EXPORT_FILE,
$pg2l_cfg{"PG2L.ORA_HOST"},
$pg2l_cfg{"PG2L.ORA_PORT"},
$pg2l_cfg{"PG2L.ORA_SID"},
$pg2l_cfg{"PG2L.ORA_ID"},
$pg2l_cfg{"PG2L.ORA_PWD"},
$pg2l_cfg{"PG2L.TARGET_PATH"},
$oraStatement
);
elog (INFO,"OID : $system_cmd ");
open CMD,'-|',$system_cmd ;
while (defined($line=<CMD>)) {
$line =~ s/^\s+|\s+$//g;
# checking return format;
my @ret = split('\^', $line);
if ($ret[0] == PG2L_RET_SUCCESS)
{
my $insert_lo = sprintf ("select (lo_import('%s')) as tmp_oid;", $ret[1]);
elog (INFO,"OID : $insert_lo ");
my $sth = spi_query($insert_lo);
while (defined (my $row = spi_fetchrow($sth))) {
# get oid
$retOID = $row->{tmp_oid};
}
# check return
# remove files
if ($delFileFlag == 1)
{
my $delete_cmd = sprintf("rm -rf %s", $ret[1]);
my $rm_ret = system($delete_cmd);
elog (INFO,"--------------------------------");
elog (INFO,"line : $line");
elog (INFO,"insert_lo : $insert_lo ");
elog (INFO,"insert_lo : $retOID ");
if ($rm_ret == 0)
{
elog (INFO,"temporary files $ret[1] removed");
}
elog (INFO,"--------------------------------");
};
close CMD;
return $retOID;
} elsif ($ret[0] == PG2L_RET_FAIL) {
elog (ERROR,$ret[1]);
}
}
$$ LANGUAGE plperlu;
CREATE OR REPLACE FUNCTION pg2l_get_lob_length(VARCHAR default '',INTEGER default 1) RETURNS LONG AS $$
use warnings ;
use strict ;
return 0;
$$ LANGUAGE plperlu;
/*
*/
CREATE OR REPLACE FUNCTION pg2l_get_oid_from_script(VARCHAR default '') RETURNS SETOF INTEGER AS $$
use warnings ;
use strict ;
foreach (0..100) {
return_next($_);
}
return undef;
$$ LANGUAGE plperlu;
/*
input :
*/
CREATE OR REPLACE FUNCTION pg2l_get_filename_from_script(VARCHAR default '') RETURNS SETOF INTEGER AS $$
use warnings ;
use strict ;
foreach (0..100) {
return_next($_);
}
return undef;
$$ LANGUAGE plperlu;
/*
*/
CREATE OR REPLACE FUNCTION pg2l_get_b1gb(VARCHAR default '') RETURNS SETOF INTEGER AS $$
use warnings ;
use strict ;
foreach (0..100) {
return_next($_);
}
return undef;
$$ LANGUAGE plperlu;
/*
*/
CREATE OR REPLACE FUNCTION pg2l_get_status(out no integer, out pg2lcfg text , out value text)
RETURNS SETOF record
LANGUAGE SQL AS
$func$
SELECT * from (
select 1 as no,'PG2L.LOCATION_PERL' as pg2lcfg, pg2l_get_conf('PG2L.LOCATION_PERL') as value
UNION
select 2 as no ,'PG2L.LOCATION_PRG' as pg2lcfg, pg2l_get_conf('PG2L.LOCATION_PRG') as value
UNION
select 3 as no ,'PG2L.ORA_HOST' as pg2lcfg, pg2l_get_conf('PG2L.ORA_HOST') as value
UNION
select 4 as no ,'PG2L.ORA_PORT' as pg2lcfg, pg2l_get_conf('PG2L.ORA_PORT') as value
UNION
select 5 as no ,'PG2L.ORA_SID' as pg2lcfg , pg2l_get_conf('PG2L.ORA_SID') as value
UNION
select 6 as no ,'PG2L.ORA_ID' as pg2lcfg, pg2l_get_conf('PG2L.ORA_ID') as value
UNION
select 7 as no ,'PG2L.ORA_PWD' as pg2lcfg, pg2l_get_conf('PG2L.ORA_PWD') as value
UNION
select 8 as no ,'PG2L.TARGET_PATH' as pg2lcfg, pg2l_get_conf('PG2L.TARGET_PATH') as value
UNION
select 9,'Status Of Oracle' as pg2lcfg, pg2l_ora_status() as values
) order by no;
$func$;
CREATE OR REPLACE FUNCTION pg2l_ora_status() RETURNS SETOF VARCHAR AS $$
use warnings ;
use strict ;
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,
};
#get configuration
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" => ""
);
foreach my $key (keys %pg2l_cfg)
{
my $get_query = sprintf("SHOW %s;",$key);
#elog (INFO,$get_query);
#get configuration
my $sth = spi_query($get_query);
while (defined (my $row = spi_fetchrow($sth))) {
my $col = lc($key);
$pg2l_cfg{$key} = $row->{$col};
#elog (INFO,$pg2l_cfg{$key});
}
# checking configuration
}
my $system_cmd = sprintf ("%s %s %s %s %s %s %s %s", $pg2l_cfg{"PG2L.LOCATION_PERL"},
$pg2l_cfg{"PG2L.LOCATION_PRG"},
PG2L_CMD_ORA_STATUS,
$pg2l_cfg{"PG2L.ORA_HOST"},
$pg2l_cfg{"PG2L.ORA_PORT"},
$pg2l_cfg{"PG2L.ORA_SID"},
$pg2l_cfg{"PG2L.ORA_ID"},
$pg2l_cfg{"PG2L.ORA_PWD"}
);
elog (INFO,"OID : $system_cmd ");
open CMD,'-|',$system_cmd or die $@;
while (defined(my $line=<CMD>)) {
$line =~ s/^\s+|\s+$//g;
my @ret = split('\^', $line);
if ($ret[0] == PG2L_RET_SUCCESS)
{
return_next ("connection ok");
} elsif ($ret[0] == PG2L_RET_FAIL)
{
return_next ("connection fail\n".$ret[1]);
}
}
close CMD;
return undef;
$$ LANGUAGE plperlu;
/*
*/
CREATE OR REPLACE FUNCTION pg2l_set_conf(VARCHAR default '',VARCHAR default '') RETURNS SETOF VARCHAR AS $$
use warnings ;
use strict ;
#get configuration
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 $sth;
my $key = $_[0];
my $val = $_[1];
$key =~ s/^\s+|\s+$//g;
$val =~ s/^\s+|\s+$//g;
if ($key eq '' or $val eq '')
{
elog (ERROR,"pg2l_set_conf functions should be set 2 parameter which key and value");
return undef;
}
# check validation of key
my $check_exist_key = 0;
foreach my $tmpkey (keys %pg2l_cfg)
{
if ($tmpkey eq $key)
{
$check_exist_key = 1;
last;
}
}
if ($check_exist_key == 0)
{
elog(ERROR,"[pg2l - error] $key does not support. Please check list of configuration parameter ");
exit;
}
# get current database name
my $dbnm;
$sth = spi_query("SELECT current_database();");
while (defined (my $row = spi_fetchrow($sth))) {
# get dbname
$dbnm = $row->{current_database};
}
# set configuration
my $set_query = sprintf("ALTER DATABASE %s SET %s = '%s'; ",$dbnm,$key,$val);
my $get_query = sprintf("SHOW %s;",$key);
my $retSet;
my $retGet;
my $retLog = "";
$retSet = spi_exec_query($set_query);
if ($retSet->{status} eq 'SPI_OK_UTILITY')
{
$retLog = sprintf("configuration of %s has been set %s",$key,$val);
elog(INFO,$retLog);
return_next($val);
}else
{
$retLog = sprintf("while try to exec statment it encournted errors [%s]",$set_query);
elog(ERROR,$retLog);
return undef;
};
return undef;
$$ LANGUAGE plperlu;
CREATE OR REPLACE FUNCTION pg2l_get_conf(VARCHAR default '') RETURNS SETOF VARCHAR AS $$
use warnings ;
use strict ;
#get configuration
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 $sth;
my $key = $_[0];
$key =~ s/^\s+|\s+$//g;
if ($key eq '')
{
elog (ERROR,"pg2l_get_conf functions should be set 1 parameter which key ");
return undef;
}
# check validation of key
my $check_exist_key = 0;
foreach my $tmpkey (keys %pg2l_cfg)
{
if ($tmpkey eq $key)
{
$check_exist_key = 1;
last;
}
}
if ($check_exist_key == 0)
{
elog(ERROR,"$key does not support. Please check list of configuration parameter ");
exit;
}
# get current database name
my $dbnm;
$sth = spi_query("SELECT current_database();");
while (defined (my $row = spi_fetchrow($sth))) {
# get dbname
$dbnm = $row->{current_database};
}
my $get_query = sprintf("SHOW %s;",$key);
$sth = spi_query($get_query);
while (defined (my $row = spi_fetchrow($sth))) {
my $col = lc($key);
return_next( $row->{$col} );
}
return undef;
$$ LANGUAGE plperlu;