From: rettenbe Date: Thu, 28 Feb 2008 14:26:34 +0000 (+0000) Subject: add event module databases.pm X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=1e9aac8c6d6136391ca3238208022fa36b746da8;p=gosa.git add event module databases.pm git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@9191 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/server/events/databases.pm b/gosa-si/server/events/databases.pm new file mode 100644 index 000000000..054d5eab0 --- /dev/null +++ b/gosa-si/server/events/databases.pm @@ -0,0 +1,187 @@ +package databases; +use Exporter; +@ISA = qw(Exporter); +my @events = ( + "get_events", + "query_jobdb", + "query_packages_list", + "count_jobdb", + "count_packages_list", + "delete_jobdb_entry", + "clear_jobdb", + "update_status_jobdb_entry", + ); +@EXPORT = @events; + +use strict; +use warnings; +use GOSA::GosaSupportDaemon; +use Data::Dumper; +use utf8; + +BEGIN {} + +END {} + +### Start ###################################################################### + +#&main::read_configfile($main::cfg_file, %cfg_defaults); + +sub get_events { + return \@events; +} + + +sub query_packages_list { return &query_db( @_ ) ; } +sub query_jobdb { return &query_db( @_ ) ; } +sub query_db { + my ($msg, $msg_hash, $session_id) = @_ ; + my $header = @{$msg_hash->{'header'}}[0]; + my $target = @{$msg_hash->{'target'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + my $table; + my $db; + if( $header =~ /query_jobdb/ ) { + $table = $main::job_queue_tn; + $db = $main::job_db; + } elsif( $header =~ /query_packages_list/ ) { + $table = $main::packages_list_tn; + $db = $main::packages_list_db; + } + + # prepare sql statement and execute query + my $select= &get_select_statement($msg, $msg_hash); + my $where= &get_where_statement($msg, $msg_hash); + my $limit= &get_limit_statement($msg, $msg_hash); + my $orderby= &get_orderby_statement($msg, $msg_hash); + my $sql_statement= "SELECT $select FROM $table $where $orderby $limit"; + my $res_hash = $db->select_dbentry($sql_statement); + + my $out_xml = &db_res2si_msg($res_hash, $header, $target, $source); + my @out_msg_l = ( $out_xml ); + return @out_msg_l; +} + + +sub count_packages_list{ return &count_db( @_ ); } +sub count_jobdb{ return &count_db( @_ ); } +sub count_db { + my ($msg, $msg_hash, $session_id) = @_; + my $header = @{$msg_hash->{'header'}}[0]; + my $target = @{$msg_hash->{'target'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + my $table; + my $db; + if( $header =~ /count_jobdb/ ) { + $table = $main::job_queue_tn; + $db = $main::job_db; + } elsif( $header =~ /count_packages_list/ ) { + $table = $main::packages_list_tn; + $db = $main::packages_list_db; + } + + # prepare sql statement and execute query + my $res_hash = $db->select_dbentry("SELECT * FROM $table"); + + my $count = keys(%{$res_hash}); + my $out_xml= "
answer
$target$source$count
"; + my @out_msg_l = ( $out_xml ); + return @out_msg_l; +} + +sub delete_jobdb_entry { + my ($msg, $msg_hash, $session_id) = @_ ; + my $target = @{$msg_hash->{'target'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + + # prepare query sql statement + my $table= $main::job_queue_tn; + my $where= &get_where_statement($msg, $msg_hash); + my $sql_statement = "DELETE FROM $table $where"; + + # execute db query + my $db_res = $main::job_db->del_dbentry($sql_statement); + + my $res; + if( $db_res > 0 ) { + $res = 0 ; + } else { + $res = 1; + } + + # prepare xml answer + my $out_xml = "
answer
$target$source$res
"; + my @out_msg_l = ( $out_xml ); + return @out_msg_l; + +} + + +sub clear_jobdb { + my ($msg, $msg_hash, $session_id) = @_ ; + my $target = @{$msg_hash->{'target'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + + my $error= 0; + my $out_xml= "1"; + + my $table= $main::job_queue_tn; + + my $sql_statement = "DELETE FROM $table"; + my $db_res = $main::job_db->del_dbentry($sql_statement); + if( not $db_res > 0 ) { $error++; }; + + if( $error == 0 ) { + $out_xml = "
answer
$target$source0
"; + } + my @out_msg_l = ( $out_xml ); + return @out_msg_l; +} + + +sub update_status_jobdb_entry { + my ($msg, $msg_hash, $session_id) = @_ ; + my $target = @{$msg_hash->{'target'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + + my $error= 0; + my $out_xml= "
answer
$target$source1
"; + + my @len_hash = keys %{$msg_hash}; + if( 0 == @len_hash) { $error++; }; + + # prepare query sql statement + if( $error == 0) { + my $table= $main::job_queue_tn; + my $where= &get_where_statement($msg, $msg_hash); + my $update= &get_update_statement($msg, $msg_hash); + + # conditions + # no timestamp update if status eq waiting + my $res_hash = $main::job_db->select_dbentry("SELECT * FROM $table $where AND status='processing' "); + if( (0 != keys(%$res_hash)) && ($update =~ /timestamp/i) ) { + $error ++; + $out_xml = "1there is no timestamp update allowed while status is 'processing'"; + } + + if( $error == 0 ) { + my $sql_statement = "UPDATE $table $update $where"; + # execute db query + my $db_res = $main::job_db->update_dbentry($sql_statement); + + # check success of db update + if( not $db_res > 0 ) { $error++; }; + + } + } + + if( $error == 0) { + $out_xml = "0"; + } + + my $out_msg = sprintf("
answer
%s%s%s
", $target, $source, $out_xml); + my @out_msg_l = ( $out_msg ); + return @out_msg_l; +} + +1;