]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/RuleDB/Counter.pm
1 package PMG
::RuleDB
::Counter
;
3 # FIXME: remove with PMG 8.0
13 use PMG
::RuleDB
::Object
;
15 use base
qw(PMG::RuleDB::Object);
30 my ($type, $count, $ogroup) = @_;
32 my $class = ref($type) || $type;
34 my $self = $class->SUPER::new
($class->otype(), $ogroup);
36 $count = 0 if !defined ($count);
38 $self->{count
} = $count;
44 my ($type, $ruledb, $id, $ogroup, $value) = @_;
46 my $class = ref($type) || $type;
48 defined($value) || die "undefined value: ERROR";
50 my $obj = $class->new($value, $ogroup);
53 $obj->{digest
} = Digest
::SHA
::sha1_hex
($id, $value, $ogroup);
59 my ($self, $ruledb) = @_;
63 defined($self->{ogroup
}) || die "undefined ogroup: ERROR";
64 defined($self->{count
}) || die "undefined count: ERROR";
66 if (defined ($self->{id
})) {
70 "UPDATE Object SET Value = ? WHERE ID = ?",
71 undef, $self->{count
}, $self->{id
});
76 my $sth = $ruledb->{dbh
}->prepare(
77 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
80 $sth->execute($self->{ogroup
}, $self->otype, $self->{count
});
82 $self->{id
} = PMG
::Utils
::lastid
($ruledb->{dbh
}, 'object_id_seq');
89 my ($self, $queue, $ruledb, $mod_group, $targets,
90 $msginfo, $vars, $marks) = @_;
92 syslog
('warning', "%s: deprecated action 'Counter' will be removed with PMG 8.0.",
96 $ruledb->{dbh
}->begin_work;
98 $ruledb->{dbh
}->do("LOCK TABLE Object IN SHARE MODE");
100 my $sth = $ruledb->{dbh
}->prepare(
101 "SELECT Value FROM Object where ID = ?");
102 $sth->execute($self->{id
});
104 my $ref = $sth->fetchrow_hashref();
108 defined($ref->{'value'}) || die "undefined value: ERROR";
110 my $value = int($ref->{'value'});
113 "UPDATE Object SET Value = ? WHERE ID = ?",
114 undef, $value + 1, $self->{id
});
116 $ruledb->{dbh
}->commit;
118 if ($msginfo->{testmode
}) {
119 print ("counter increased\n");
123 $ruledb->{dbh
}->rollback;
130 my ($self, $count) = @_;
132 if (defined ($count)) {
133 $self->{count
} = $count;
143 return "Increase Counter";
150 =head1 PMG::RuleDB::Counter
158 Unique Name of the Counter
162 $obj = PMG::RuleDB::Counter->new (0);