Skip to content

Commit

Permalink
check software exists loading it with single query
Browse files Browse the repository at this point in the history
  • Loading branch information
alacn1 committed Jun 4, 2024
1 parent 68c6a11 commit 3a7a642
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions Apache/Ocsinventory/Server/Inventory/Software.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ our @EXPORT = qw /
_insert_software_categories_link
_del_category_soft
_trim_value
_verif_software_exists
_verif_software_already_in_cat
_clean_software_version
_get_info_software_version
Expand Down Expand Up @@ -206,29 +205,6 @@ sub _get_info_software_version {
return $valueResult;
}

sub _verif_software_exists {
my ($hardware_id, $name, $publisher, $version) = @_;
my $sql;
my @arg = ();
my $result;
my $id;

$sql = "SELECT ID FROM software WHERE HARDWARE_ID = ? AND NAME_ID = ? AND PUBLISHER_ID = ? AND VERSION_ID = ?";
push @arg, $hardware_id;
push @arg, $name;
push @arg, $publisher;
push @arg, $version;

$result = _prepare_sql($sql, @arg);
if(!defined $result) { return 1; }

while(my $row = $result->fetchrow_hashref()){
$id = $row->{ID};
}

return $id;
}

sub _verif_software_already_in_cat {
my ($name, $publisher, $version, $category) = @_;
my $sql;
Expand Down Expand Up @@ -316,7 +292,20 @@ sub _insert_software {
'FOLDER', 'COMMENTS', 'FILENAME',
'FILESIZE', 'SOURCE', 'GUID',
'LANGUAGE', 'INSTALLDATE', 'BITSWIDTH', 'ARCHITECTURE');
my @softIdAlreadyExists;

my @compareKeys = ('NAME_ID', 'PUBLISHER_ID', 'VERSION_ID');

# load at once software from db
my @softFromDb;
my @arg = ();
my $result;
$sql = 'SELECT ID, '.(join ', ', @compareKeys).' FROM software WHERE HARDWARE_ID = ?';
push @arg, $hardware_id;
$result = _prepare_sql($sql, @arg);
if(!defined $result) { return 1; }
while(my $row = $result->fetchrow_hashref()){
push @softFromDb, $row;
}

foreach my $software (@{$Apache::Ocsinventory::CURRENT_CONTEXT{'XML_ENTRY'}->{CONTENT}->{SOFTWARES}}) {

Expand Down Expand Up @@ -375,13 +364,29 @@ sub _insert_software {
}

# Verify if the software already exists on DB
my $softId = _verif_software_exists($arrayValue{HARDWARE_ID}, $arrayValue{NAME_ID}, $arrayValue{PUBLISHER_ID}, $arrayValue{VERSION_ID});
my $softId = undef;
for my $row (@softFromDb) {
next unless defined($row);
my $eq = undef;
for my $key (@compareKeys) {
my $valueXml = $arrayValue{$key};
my $valueDb = $row->{$key};
if($valueXml ne $valueDb) {
$eq = 0;
last;
}
$eq = 1 if !defined($eq);
}
if($eq) {
$softId = $row->{ID};
# remove from list, we will delete remaining items
$row = undef;
last;
}
}

# If return id : save the id in array
# If return undef : insert the software and save the id in array
if(defined $softId) {
push @softIdAlreadyExists, $softId;
} else {
# Doesn't exists on DB?
if(!defined $softId) {
my $arrayRefString = join ',', @arrayRef;
my @arg = ();

Expand All @@ -396,13 +401,6 @@ sub _insert_software {
my $result = _prepare_sql($sql, @arg);

if(!defined $result) { return 1; }

# Verify if the software already exists on DB
my $softIdInsert = _verif_software_exists($arrayValue{HARDWARE_ID}, $arrayValue{NAME_ID}, $arrayValue{PUBLISHER_ID}, $arrayValue{VERSION_ID});

if(!defined $softIdInsert) { return 1; }

push @softIdAlreadyExists, $softIdInsert;
}

# Check if software already in the correct software category
Expand All @@ -419,12 +417,15 @@ sub _insert_software {
}
}

# Delete all softwares who are not in softIdAlreadyExists with this hardware_id
if(@softIdAlreadyExists && defined $hardware_id) {
# Delete remaining elements that doesn't exist on xml
my @softIdDel;
for my $row (@softFromDb) {
next unless defined $row;
push @softIdDel, $row->{ID};
}
if(@softIdDel) {
my @arg = ();
$sql = "DELETE FROM software WHERE HARDWARE_ID = ? AND ID NOT IN (";
$sql .= (join ',', @softIdAlreadyExists).")";
push @arg, $hardware_id;
$sql = 'DELETE FROM software WHERE ID IN ('.(join ',', @softIdDel).')';

my $result = _prepare_sql($sql, @arg);

Expand Down

0 comments on commit 3a7a642

Please sign in to comment.