]>
git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Counter.pm
1 package PMG
::RuleDB
::Counter
;
11 use PMG
::RuleDB
::Object
;
13 use base
qw(PMG::RuleDB::Object);
28 my ($type, $count, $ogroup) = @_;
30 my $class = ref($type) || $type;
32 my $self = $class->SUPER::new
($class->otype(), $ogroup);
34 $count = 0 if !defined ($count);
36 $self->{count
} = $count;
42 my ($type, $ruledb, $id, $ogroup, $value) = @_;
44 my $class = ref($type) || $type;
46 defined($value) || die "undefined value: ERROR";
48 my $obj = $class->new($value, $ogroup);
51 $obj->{digest
} = Digest
::SHA
::sha1_hex
($id, $value, $ogroup);
57 my ($self, $ruledb) = @_;
61 defined($self->{ogroup
}) || die "undefined ogroup: ERROR";
62 defined($self->{count
}) || die "undefined count: ERROR";
64 if (defined ($self->{id
})) {
68 "UPDATE Object SET Value = ? WHERE ID = ?",
69 undef, $self->{count
}, $self->{id
});
74 my $sth = $ruledb->{dbh
}->prepare(
75 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
78 $sth->execute($self->{ogroup
}, $self->otype, $self->{count
});
80 $self->{id
} = PMG
::Utils
::lastid
($ruledb->{dbh
}, 'object_id_seq');
87 my ($self, $queue, $ruledb, $mod_group, $targets,
88 $msginfo, $vars, $marks) = @_;
91 $ruledb->{dbh
}->begin_work;
93 $ruledb->{dbh
}->do("LOCK TABLE Object IN SHARE MODE");
95 my $sth = $ruledb->{dbh
}->prepare(
96 "SELECT Value FROM Object where ID = ?");
97 $sth->execute($self->{id
});
99 my $ref = $sth->fetchrow_hashref();
103 defined($ref->{'value'}) || die "undefined value: ERROR";
105 my $value = int($ref->{'value'});
108 "UPDATE Object SET Value = ? WHERE ID = ?",
109 undef, $value + 1, $self->{id
});
111 $ruledb->{dbh
}->commit;
113 if ($msginfo->{testmode
}) {
114 print ("counter increased\n");
118 $ruledb->{dbh
}->rollback;
125 my ($self, $count) = @_;
127 if (defined ($count)) {
128 $self->{count
} = $count;
138 return "Increase Counter";
145 =head1 PMG::RuleDB::Counter
153 Unique Name of the Counter
157 $obj = PMG::RuleDB::Counter->new (0);