Skip to content
Snippets Groups Projects
Commit 45b756a9 authored by Andreas Romeyke's avatar Andreas Romeyke
Browse files

- refactoring, moved SQL statement preparation in prepare_addsql() to avoid multiple preparation

parent ac71aee2
No related branches found
No related tags found
No related merge requests found
......@@ -150,36 +150,7 @@ sub write_prepare_insert ($dbh) {
return 1;
}
###############################################################################
# write add SQL entry, expects a hashref which contains ff. params
# (foreach file location/copy):
# INSERT INTO aip (ie_id) VALUES ($ieid);
# INSERT INTO iefile (aip_id, location, sourcetype) VALUES (
# (SELECT id FROM aip where aip.ieid = $ieid), $location, $sourcetype);
# INSERT INTO file (aip_id, name) VALUES (
# (SELECT id FROM aip where aip.ieid = $ieid), $name);
# INSERT INTO locat (file_id, location, sourcetype) VALUES (
# (SELECT file.aip_id FROM file where file.aip_id = aip.id
# AND aip.ie_id=$ieid), $location, $sourcetype)
# INSERT INTO dc (aip_id, element, value) VALUES (
# (SELECT id FROM aip where aip.ieid = $ieid), $element, $value);
# TODO: needs additional work
# expects a reference of an hash:
# $ret{"filename" } = $filename;
# $ret{"title"} = $title;
# $ret{"repid"} = $repid;
# $ret{"files"} = \@files;
# $ret{"dcrecords"} = \@dcrecords;
###############################################################################
sub write_addsql ($dbh, $refhash) {
my $iefile = path($refhash->{"filename"})->basename();
my ($ieid,$ieversion);
if ($iefile =~ m/^V(\d+)-(IE\d*)\.xml$/) {
$ieversion=$1; $ieid=$2;
} else {
die "Could not detect PID and Version from IEFile '$iefile'\n";
}
sub prepare_addsql( $dbh) {
my $sql_aip_plan=<<"SQL_AIP_PLAN";
INSERT INTO aip (ie_id, version) VALUES (\$1, \$2);
SQL_AIP_PLAN
......@@ -205,27 +176,65 @@ SQL_LOCAT_PLAN
(SELECT id FROM aip WHERE aip.ie_id=\$1 AND aip.version=\$2), \$3, \$4
);
SQL_DC_PLAN
$dbh->begin_work;
my $sth_aip_plan = $dbh->prepare($sql_aip_plan);
my $sth_ie_plan = $dbh->prepare($sql_ie_plan);
my $sth_file_plan = $dbh->prepare($sql_file_plan);
my $sth_locat_plan = $dbh->prepare($sql_locat_plan);
my $sth_dc_plan = $dbh->prepare($sql_dc_pan);
my $plans;
$plans->{aip} = $sth_aip_plan;
$plans->{ie} = $sth_ie_plan;
$plans->{file} = $sth_file_plan;
$plans->{locat} = $sth_locat_plan;
$plans->{dc} = $sth_dc_plan;
return $plans;
}
###############################################################################
# write add SQL entry, expects a hashref which contains ff. params
# (foreach file location/copy):
# INSERT INTO aip (ie_id) VALUES ($ieid);
# INSERT INTO iefile (aip_id, location, sourcetype) VALUES (
# (SELECT id FROM aip where aip.ieid = $ieid), $location, $sourcetype);
# INSERT INTO file (aip_id, name) VALUES (
# (SELECT id FROM aip where aip.ieid = $ieid), $name);
# INSERT INTO locat (file_id, location, sourcetype) VALUES (
# (SELECT file.aip_id FROM file where file.aip_id = aip.id
# AND aip.ie_id=$ieid), $location, $sourcetype)
# INSERT INTO dc (aip_id, element, value) VALUES (
# (SELECT id FROM aip where aip.ieid = $ieid), $element, $value);
# TODO: needs additional work
# expects a reference of an hash:
# $ret{"filename" } = $filename;
# $ret{"title"} = $title;
# $ret{"repid"} = $repid;
# $ret{"files"} = \@files;
# $ret{"dcrecords"} = \@dcrecords;
###############################################################################
sub write_addsql ($dbh, $plans, $refhash) {
my $iefile = path($refhash->{"filename"})->basename();
my ($ieid,$ieversion);
if ($iefile =~ m/^V(\d+)-(IE\d*)\.xml$/) {
$ieversion=$1; $ieid=$2;
} else {
die "Could not detect PID and Version from IEFile '$iefile'\n";
}
$dbh->begin_work;
# start SQL insert
$sth_aip_plan->execute($ieid, $ieversion) or die "sql problem detected", $dbh->errstr;
$plans->{aip}->execute($ieid, $ieversion) or die "sql problem detected", $dbh->errstr;
# FIXME if multiple locations exists
$sth_ie_plan->execute( $ieid, $ieversion, $iefile, $sourcetype) or die "sql problem detected", $dbh->errstr;
$plans->{ie}->execute( $ieid, $ieversion, $iefile, $sourcetype) or die "sql problem detected", $dbh->errstr;
foreach my $location (@{$refhash->{"files"}}) {
my $file = path($location)->basename(); # FIXME if multiple locations
$sth_file_plan->execute($ieid, $ieversion, $file) or die "sql problem detected", $dbh->errstr;
$sth_locat_plan->execute($ieid, $ieversion, $file, $location, $sourcetype) or die "sql problem detected", $dbh->errstr;
$plans->{file}->execute($ieid, $ieversion, $file) or die "sql problem detected", $dbh->errstr;
$plans->{locat}->execute($ieid, $ieversion, $file, $location, $sourcetype) or die "sql problem detected", $dbh->errstr;
}
foreach my $dcpair (@{$refhash->{"dcrecords"}}) {
my ($dckey,$dcvalue) = @{$dcpair};
# quote ' in dcvalue
$dcvalue=~tr/'/"/;
$sth_dc_plan->execute($ieid, $ieversion, $dckey, $dcvalue) or die "sql problem detected", $dbh->errstr;
$plans->{dc}->execute($ieid, $ieversion, $dckey, $dcvalue) or die "sql problem detected", $dbh->errstr;
}
$dbh->commit;
return 1;
......@@ -536,12 +545,14 @@ $dbh = DBI->connect("dbi:SQLite:dbname=$db_filename", "", "", {
RaiseError => 1,
sqlite_unicode => 1,
}) or die "could not connect to database (file '$db_filename')", $DBI::errstr;
my $plans = prepare_addsql( $dbh);
while (<$fh_unsorted_IEs>) {
chomp;
print $progressbar->report("parse IE files: %40b ETA: %E \r", $count++);
s/V0*(\d+-IE)/V$1/; # revert fake version
my $ret = parse_iexml($_, $flag_recovery);
write_addsql($dbh, $ret);
write_addsql($dbh, $plans, $ret);
}
$dbh->disconnect or warn("disconnecting problems, ", $dbh->errstr);
say "\rprocessed $count uniq IEs ";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment