]>
Commit | Line | Data |
---|---|---|
f4fe6fc4 DM |
1 | package PMG::RuleDB::Counter; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
f4fe6fc4 DM |
5 | use DBI; |
6 | use Digest::SHA; | |
7 | ||
8 | use PVE::SafeSyslog; | |
9 | ||
10 | use PMG::Utils; | |
11 | use PMG::RuleDB::Object; | |
12 | ||
13 | use base qw(PMG::RuleDB::Object); | |
14 | ||
15 | sub otype { | |
16 | return 4999; | |
17 | } | |
18 | ||
19 | sub oclass { | |
20 | return 'action'; | |
21 | } | |
22 | ||
23 | sub otype_text { | |
24 | return 'Counter'; | |
25 | } | |
26 | ||
f4fe6fc4 DM |
27 | sub new { |
28 | my ($type, $count, $ogroup) = @_; | |
29 | ||
30 | my $class = ref($type) || $type; | |
31 | ||
7a2cf7e6 | 32 | my $self = $class->SUPER::new($class->otype(), $ogroup); |
f4fe6fc4 DM |
33 | |
34 | $count = 0 if !defined ($count); | |
35 | ||
36 | $self->{count} = $count; | |
37 | ||
38 | return $self; | |
39 | } | |
40 | ||
41 | sub load_attr { | |
42 | my ($type, $ruledb, $id, $ogroup, $value) = @_; | |
43 | ||
44 | my $class = ref($type) || $type; | |
45 | ||
9ef3f143 | 46 | defined($value) || die "undefined value: ERROR"; |
f4fe6fc4 DM |
47 | |
48 | my $obj = $class->new($value, $ogroup); | |
49 | $obj->{id} = $id; | |
50 | ||
51 | $obj->{digest} = Digest::SHA::sha1_hex($id, $value, $ogroup); | |
52 | ||
53 | return $obj; | |
54 | } | |
55 | ||
56 | sub save { | |
57 | my ($self, $ruledb) = @_; | |
58 | ||
59 | my $adr; | |
60 | ||
9ef3f143 DM |
61 | defined($self->{ogroup}) || die "undefined ogroup: ERROR"; |
62 | defined($self->{count}) || die "undefined count: ERROR"; | |
f4fe6fc4 DM |
63 | |
64 | if (defined ($self->{id})) { | |
65 | # update | |
66 | ||
67 | $ruledb->{dbh}->do( | |
68 | "UPDATE Object SET Value = ? WHERE ID = ?", | |
69 | undef, $self->{count}, $self->{id}); | |
70 | ||
71 | } else { | |
72 | # insert | |
73 | ||
74 | my $sth = $ruledb->{dbh}->prepare( | |
75 | "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " . | |
76 | "VALUES (?, ?, ?);"); | |
77 | ||
78 | $sth->execute($self->{ogroup}, $self->otype, $self->{count}); | |
79 | ||
80 | $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq'); | |
81 | } | |
82 | ||
83 | return $self->{id}; | |
84 | } | |
85 | ||
86 | sub execute { | |
87 | my ($self, $queue, $ruledb, $mod_group, $targets, | |
88 | $msginfo, $vars, $marks) = @_; | |
89 | ||
90 | eval { | |
91 | $ruledb->{dbh}->begin_work; | |
92 | ||
93 | $ruledb->{dbh}->do("LOCK TABLE Object IN SHARE MODE"); | |
94 | ||
95 | my $sth = $ruledb->{dbh}->prepare( | |
96 | "SELECT Value FROM Object where ID = ?"); | |
97 | $sth->execute($self->{id}); | |
98 | ||
99 | my $ref = $sth->fetchrow_hashref(); | |
100 | ||
101 | $sth->finish(); | |
102 | ||
9ef3f143 | 103 | defined($ref->{'value'}) || die "undefined value: ERROR"; |
f4fe6fc4 DM |
104 | |
105 | my $value = int($ref->{'value'}); | |
106 | ||
107 | $ruledb->{dbh}->do( | |
108 | "UPDATE Object SET Value = ? WHERE ID = ?", | |
109 | undef, $value + 1, $self->{id}); | |
110 | ||
111 | $ruledb->{dbh}->commit; | |
112 | ||
113 | if ($msginfo->{testmode}) { | |
114 | print ("counter increased\n"); | |
115 | } | |
116 | }; | |
117 | if (my $err = $@) { | |
118 | $ruledb->{dbh}->rollback; | |
119 | syslog('err', $err); | |
120 | return undef; | |
121 | } | |
122 | } | |
123 | ||
124 | sub count { | |
125 | my ($self, $count) = @_; | |
126 | ||
127 | if (defined ($count)) { | |
128 | $self->{count} = $count; | |
129 | } | |
130 | ||
131 | $self->{count}; | |
132 | } | |
133 | ||
134 | ||
135 | sub short_desc { | |
136 | my $self = shift; | |
137 | ||
138 | return "Increase Counter"; | |
139 | } | |
140 | ||
141 | 1; | |
142 | ||
143 | __END__ | |
144 | ||
145 | =head1 PMG::RuleDB::Counter | |
146 | ||
147 | Counter Object | |
148 | ||
149 | =head2 Attribues | |
150 | ||
151 | =head3 count | |
152 | ||
153 | Unique Name of the Counter | |
154 | ||
155 | =head2 Examples | |
156 | ||
157 | $obj = PMG::RuleDB::Counter->new (0); | |
158 |