[pmg-devel] [PATCH pmg-api 05/12] add objectgroup attributes and/invert

Stoiko Ivanov s.ivanov at proxmox.com
Tue Feb 20 13:47:15 CET 2024


forgot one point in my last reply (or hid it behind the question of why
running transactions for selects): 
On Fri,  9 Feb 2024 13:54:29 +0100
Dominik Csapak <d.csapak at proxmox.com> wrote:

>..snip..
>  
> +sub load_group_attributes {
> +    my ($self, $og) = @_;
> +
> +    my $attribute_sth = $self->{dbh}->prepare("SELECT * FROM Objectgroup_Attributes WHERE Objectgroup_ID = ?");
> +    $attribute_sth->execute($og->{id});
> +
> +    while (my $ref = $attribute_sth->fetchrow_hashref()) {
> +	$og->{and} = $ref->{value} if $ref->{name} eq 'and';
> +	$og->{invert} = $ref->{value} if $ref->{name} eq 'invert';
> +    }
> +}
> +
>  sub load_objectgroups {
>      my ($self, $class, $id) = @_;
>  
> @@ -259,34 +319,47 @@ sub load_objectgroups {
>  
>      defined($class) || die "undefined object class";
>  
> -    if (!(defined($id))) {
> -        $sth = $self->{dbh}->prepare(
> -	    "SELECT * FROM Objectgroup where Class = ? ORDER BY name");
> -        $sth->execute($class);
> -
> -    } else {
> -        $sth = $self->{dbh}->prepare(
> -	    "SELECT * FROM Objectgroup where Class like ? and id = ? " .
> -	    "order by name");
> -        $sth->execute($class,$id);
> -    }
> +    $self->{dbh}->begin_work;
>  
>      my $arr_og = ();
> -    while (my $ref = $sth->fetchrow_hashref()) {
> -    	my $og = PMG::RuleDB::Group->new($ref->{name}, $ref->{info},
> -					 $ref->{class});
> -    	$og->{id} = $ref->{id};
>  
> -	if ($class eq 'action') {
> -	    my $objects = $self->load_group_objects($og->{id});
> -	    my $obj = @$objects[0];
> -	    defined($obj) || die "undefined action object: ERROR";
> -	    $og->{action} = $obj;
> +    eval {
> +	if (!(defined($id))) {
> +	    $sth = $self->{dbh}->prepare(
> +		"SELECT * FROM Objectgroup where Class = ? ORDER BY name");
> +	    $sth->execute($class);
> +
> +	} else {
> +	    $sth = $self->{dbh}->prepare(
> +		"SELECT * FROM Objectgroup where Class like ? and id = ? " .
> +		"order by name");
> +	    $sth->execute($class,$id);
>  	}
> -    	push @$arr_og, $og;
> -    }
>  
> -    $sth->finish();
> +	while (my $ref = $sth->fetchrow_hashref()) {
> +	    my $og = PMG::RuleDB::Group->new($ref->{name}, $ref->{info},
> +					     $ref->{class});
> +	    $og->{id} = $ref->{id};
> +
> +	    if ($class eq 'action') {
> +		my $objects = $self->load_group_objects($og->{id});
> +		my $obj = @$objects[0];
> +		defined($obj) || die "undefined action object: ERROR";
> +		$og->{action} = $obj;
> +	    } else {
> +		$self->load_group_attributes($og);
> +	    }
> +	    push @$arr_og, $og;
> +	}
> +
> +	$sth->finish();
> +    };
> +
> +    my $err = $@;
> +
> +    $self->{dbh}->rollback;
the rollback here is unconditional (not that I think it would ever do
something)
> +
> +    die $err if $err;
also can be dropped, if we drop the eval+transaction block
>  
>      return $arr_og;
>  }





More information about the pmg-devel mailing list