From 9fa18a22880f52192667c9df42d37bd75d11f1f4 Mon Sep 17 00:00:00 2001 From: Olivier Thauvin Date: Tue, 19 Oct 2010 11:42:39 +0000 Subject: - add initial function to have host changes validation --- lib/MGA/Mirrors/DB.pm | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'lib/MGA') diff --git a/lib/MGA/Mirrors/DB.pm b/lib/MGA/Mirrors/DB.pm index 1c79d8b..16070dc 100644 --- a/lib/MGA/Mirrors/DB.pm +++ b/lib/MGA/Mirrors/DB.pm @@ -422,6 +422,56 @@ sub find_host_ip_overlap { return keys %{ $res }; } +sub add_host_change_request { + my ($self, $hostname, %info) = @_; + + my $reqid = join("", map { sprintf("%x", int rand(256)) } (0..16)); + my $add = $self->db->prepare(sprintf(q{ + INSERT INTO hosts_ch_req( + hostname, country, city, readonly, infourl, hostby, sponsorurl, + "comment", contact, syncfrom, public, bandwidth, latitude, longitude, + added, reqid) + select *, '%s' from hosts where hostname = ? + }, $reqid) + ); + $add->execute( $hostname ); + my (@fields, @vals); + while (my ($field, $val) = each(%info)) { + push(@fields, $field); + push(@vals, $val || undef); + } + if (keys %info) { + my $upd = $self->db->prepare(sprintf(q{ + update hosts_ch_req set %s where reqid = ? + }, join(', ', map { "$_ = ?" } @fields))); + $upd->execute(@vals, $reqid); + } + + $self->db->commit; + + $reqid; +} + +sub apply_change_request { + my ($self, $reqid) = @_; + + my $findreq = $self->db->prepare(q{ + select * from hosts_ch_req where reqid = ? + }); + $findreq->execute($reqid); + my $res = $findreq->fetchrow_hashref; + $findreq->finish; + $res or return; + + my $hostname = $res->{hostname}; + delete $res->{hostname}; + delete $res->{reqid}; + + $self->add_or_update_host($hostname, %{$res}); + $self->db->commit; + 1 +} + sub add_or_update_host { my ($self, $hostname, %info) = @_; -- cgit v1.2.1